3030import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
3131import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
3232import static com .redhat .exhort .extensions .WiremockExtension .SNYK_TOKEN ;
33+ import static com .redhat .exhort .extensions .WiremockExtension .TPA_TOKEN ;
3334import static org .junit .jupiter .api .Assertions .assertEquals ;
3435import static org .junit .jupiter .api .Assertions .assertFalse ;
3536import static org .junit .jupiter .api .Assertions .assertTrue ;
@@ -198,6 +199,7 @@ protected void verifyTokenRequest(String provider, Map<String, String> headers)
198199 case Constants .OSS_INDEX_PROVIDER -> verifyOssRequest (
199200 headers .get (Constants .OSS_INDEX_USER_HEADER ),
200201 headers .get (Constants .OSS_INDEX_TOKEN_HEADER ));
202+ case Constants .TPA_PROVIDER -> verifyTpaTokenRequest (headers .get (Constants .TPA_TOKEN_HEADER ));
201203 }
202204 }
203205
@@ -213,6 +215,32 @@ protected void verifySnykTokenRequest(String token) {
213215 }
214216 }
215217
218+ protected void verifyTpaTokenRequest (String token ) {
219+ if (token == null ) {
220+ server .verify (1 , getRequestedFor (urlEqualTo (Constants .TPA_TOKEN_PATH )));
221+ } else {
222+ server .verify (
223+ 1 ,
224+ getRequestedFor (urlEqualTo (Constants .TPA_TOKEN_PATH ))
225+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + token )));
226+ }
227+ }
228+
229+ protected void verifyTpaRequest (String token ) {
230+ verifyTpaRequest (token , 1 );
231+ }
232+
233+ protected void verifyTpaRequest (String token , int count ) {
234+ if (token == null ) {
235+ server .verify (count , postRequestedFor (urlEqualTo (Constants .TPA_ANALYZE_PATH )));
236+ } else {
237+ server .verify (
238+ count ,
239+ postRequestedFor (urlEqualTo (Constants .TPA_ANALYZE_PATH ))
240+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + token )));
241+ }
242+ }
243+
216244 protected void stubAllProviders () {
217245 stubSnykRequests ();
218246 stubOssToken ();
@@ -232,7 +260,8 @@ protected void verifyProviders(Collection<String> providers, Map<String, String>
232260 credentials .get (Constants .OSS_INDEX_USER_HEADER ),
233261 credentials .get (Constants .OSS_INDEX_TOKEN_HEADER ));
234262 case Constants .OSV_PROVIDER -> verifyOsvNvdRequest ();
235- case Constants .TPA_PROVIDER -> verifyTpaRequest ();
263+ case Constants .TPA_PROVIDER -> verifyTpaRequest (
264+ credentials .get (Constants .TPA_TOKEN_HEADER ));
236265 }
237266 });
238267 verifyTrustedContentRequest ();
@@ -352,8 +381,25 @@ protected void stubOsvRequests() {
352381 }
353382
354383 protected void stubTpaRequests () {
384+ // Missing token
385+ server .stubFor (post (Constants .TPA_ANALYZE_PATH ).willReturn (aResponse ().withStatus (401 )));
386+
387+ // Invalid token
388+ server .stubFor (
389+ post (Constants .TPA_ANALYZE_PATH )
390+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + INVALID_TOKEN ))
391+ .withHeader (Exchange .CONTENT_TYPE , containing (MediaType .APPLICATION_JSON ))
392+ .willReturn (
393+ aResponse ()
394+ .withStatus (401 )
395+ .withBody (
396+ "{\" error\" : \" Unauthorized\" , \" message\" : \" Authentication failed\" }" )));
397+
355398 server .stubFor (
356399 post (Constants .TPA_ANALYZE_PATH )
400+ .withHeader (
401+ Constants .AUTHORIZATION_HEADER ,
402+ equalTo ("Bearer " + TPA_TOKEN ).or (equalTo ("Bearer " + OK_TOKEN )))
357403 .withHeader (Exchange .CONTENT_TYPE , containing (MediaType .APPLICATION_JSON ))
358404 .willReturn (
359405 aResponse ()
@@ -363,6 +409,9 @@ protected void stubTpaRequests() {
363409
364410 server .stubFor (
365411 post (Constants .TPA_ANALYZE_PATH )
412+ .withHeader (
413+ Constants .AUTHORIZATION_HEADER ,
414+ equalTo ("Bearer " + TPA_TOKEN ).or (equalTo ("Bearer " + OK_TOKEN )))
366415 .withHeader (Exchange .CONTENT_TYPE , containing (MediaType .APPLICATION_JSON ))
367416 .withRequestBody (
368417 equalToJson (loadFileAsString ("__files/tpa/maven_request.json" ), true , false ))
@@ -373,6 +422,9 @@ protected void stubTpaRequests() {
373422 .withBodyFile ("tpa/maven_report.json" )));
374423 server .stubFor (
375424 post (Constants .TPA_ANALYZE_PATH )
425+ .withHeader (
426+ Constants .AUTHORIZATION_HEADER ,
427+ equalTo ("Bearer " + TPA_TOKEN ).or (equalTo ("Bearer " + OK_TOKEN )))
376428 .withHeader (Exchange .CONTENT_TYPE , containing (MediaType .APPLICATION_JSON ))
377429 .withRequestBody (
378430 equalToJson (loadFileAsString ("__files/tpa/batch_request.json" ), true , false ))
@@ -383,6 +435,36 @@ protected void stubTpaRequests() {
383435 .withBodyFile ("tpa/maven_report.json" )));
384436 }
385437
438+ protected void stubTpaTokenRequests () {
439+ // Missing token
440+ server .stubFor (get (Constants .TPA_TOKEN_PATH ).willReturn (aResponse ().withStatus (401 )));
441+ // Default request
442+ server .stubFor (
443+ get (Constants .TPA_TOKEN_PATH )
444+ .withHeader (
445+ Constants .AUTHORIZATION_HEADER ,
446+ equalTo ("Bearer " + TPA_TOKEN ).or (equalTo ("Bearer " + OK_TOKEN )))
447+ .willReturn (
448+ aResponse ()
449+ .withStatus (200 )
450+ .withHeader (Exchange .CONTENT_TYPE , MediaType .APPLICATION_JSON )
451+ .withBodyFile ("tpa/empty_report.json" )));
452+ // Internal Error
453+ server .stubFor (
454+ get (Constants .TPA_TOKEN_PATH )
455+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + ERROR_TOKEN ))
456+ .willReturn (aResponse ().withStatus (500 ).withBody ("This is an example error" )));
457+ // Invalid token
458+ server .stubFor (
459+ get (Constants .TPA_TOKEN_PATH )
460+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + INVALID_TOKEN ))
461+ .willReturn (
462+ aResponse ()
463+ .withStatus (401 )
464+ .withBody (
465+ "{\" error\" : \" Unauthorized\" , \" message\" : \" Authentication failed\" }" )));
466+ }
467+
386468 protected void verifyTrustedContentRequest () {
387469 server .verify (1 , postRequestedFor (urlEqualTo (Constants .TRUSTED_CONTENT_PATH )));
388470 }
@@ -612,14 +694,6 @@ protected void verifyOsvNvdRequest(int count) {
612694 server .verify (count , postRequestedFor (urlEqualTo (Constants .OSV_NVD_PURLS_PATH )));
613695 }
614696
615- protected void verifyTpaRequest () {
616- verifyTpadRequest (1 );
617- }
618-
619- protected void verifyTpadRequest (int count ) {
620- server .verify (count , postRequestedFor (urlEqualTo (Constants .TPA_ANALYZE_PATH )));
621- }
622-
623697 protected void verifyNoInteractions () {
624698 verifyNoInteractionsWithSnyk ();
625699 verifyNoInteractionsWithOSS ();
0 commit comments