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,36 @@ protected void verifySnykTokenRequest(String token) {
213215 }
214216 }
215217
218+ protected void verifyTpaTokenRequest (String token ) {
219+ if (token == null ) {
220+ server .verify (
221+ 1 ,
222+ getRequestedFor (urlPathEqualTo (Constants .TPA_TOKEN_PATH ))
223+ .withQueryParam ("limit" , equalTo ("0" )));
224+ } else {
225+ server .verify (
226+ 1 ,
227+ getRequestedFor (urlPathEqualTo (Constants .TPA_TOKEN_PATH ))
228+ .withQueryParam ("limit" , equalTo ("0" ))
229+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + token )));
230+ }
231+ }
232+
233+ protected void verifyTpaRequest (String token ) {
234+ verifyTpaRequest (token , 1 );
235+ }
236+
237+ protected void verifyTpaRequest (String token , int count ) {
238+ if (token == null ) {
239+ server .verify (count , postRequestedFor (urlEqualTo (Constants .TPA_ANALYZE_PATH )));
240+ } else {
241+ server .verify (
242+ count ,
243+ postRequestedFor (urlEqualTo (Constants .TPA_ANALYZE_PATH ))
244+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + token )));
245+ }
246+ }
247+
216248 protected void stubAllProviders () {
217249 stubSnykRequests ();
218250 stubOssToken ();
@@ -232,7 +264,8 @@ protected void verifyProviders(Collection<String> providers, Map<String, String>
232264 credentials .get (Constants .OSS_INDEX_USER_HEADER ),
233265 credentials .get (Constants .OSS_INDEX_TOKEN_HEADER ));
234266 case Constants .OSV_PROVIDER -> verifyOsvNvdRequest ();
235- case Constants .TPA_PROVIDER -> verifyTpaRequest ();
267+ case Constants .TPA_PROVIDER -> verifyTpaRequest (
268+ credentials .get (Constants .TPA_TOKEN_HEADER ));
236269 }
237270 });
238271 verifyTrustedContentRequest ();
@@ -352,8 +385,25 @@ protected void stubOsvRequests() {
352385 }
353386
354387 protected void stubTpaRequests () {
388+ // Missing token
389+ server .stubFor (post (Constants .TPA_ANALYZE_PATH ).willReturn (aResponse ().withStatus (401 )));
390+
391+ // Invalid token
355392 server .stubFor (
356393 post (Constants .TPA_ANALYZE_PATH )
394+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + INVALID_TOKEN ))
395+ .withHeader (Exchange .CONTENT_TYPE , containing (MediaType .APPLICATION_JSON ))
396+ .willReturn (
397+ aResponse ()
398+ .withStatus (401 )
399+ .withBody (
400+ "{\" error\" : \" Unauthorized\" , \" message\" : \" Authentication failed\" }" )));
401+
402+ server .stubFor (
403+ post (Constants .TPA_ANALYZE_PATH )
404+ .withHeader (
405+ Constants .AUTHORIZATION_HEADER ,
406+ equalTo ("Bearer " + TPA_TOKEN ).or (equalTo ("Bearer " + OK_TOKEN )))
357407 .withHeader (Exchange .CONTENT_TYPE , containing (MediaType .APPLICATION_JSON ))
358408 .willReturn (
359409 aResponse ()
@@ -363,6 +413,9 @@ protected void stubTpaRequests() {
363413
364414 server .stubFor (
365415 post (Constants .TPA_ANALYZE_PATH )
416+ .withHeader (
417+ Constants .AUTHORIZATION_HEADER ,
418+ equalTo ("Bearer " + TPA_TOKEN ).or (equalTo ("Bearer " + OK_TOKEN )))
366419 .withHeader (Exchange .CONTENT_TYPE , containing (MediaType .APPLICATION_JSON ))
367420 .withRequestBody (
368421 equalToJson (loadFileAsString ("__files/tpa/maven_request.json" ), true , false ))
@@ -373,6 +426,9 @@ protected void stubTpaRequests() {
373426 .withBodyFile ("tpa/maven_report.json" )));
374427 server .stubFor (
375428 post (Constants .TPA_ANALYZE_PATH )
429+ .withHeader (
430+ Constants .AUTHORIZATION_HEADER ,
431+ equalTo ("Bearer " + TPA_TOKEN ).or (equalTo ("Bearer " + OK_TOKEN )))
376432 .withHeader (Exchange .CONTENT_TYPE , containing (MediaType .APPLICATION_JSON ))
377433 .withRequestBody (
378434 equalToJson (loadFileAsString ("__files/tpa/batch_request.json" ), true , false ))
@@ -383,6 +439,42 @@ protected void stubTpaRequests() {
383439 .withBodyFile ("tpa/maven_report.json" )));
384440 }
385441
442+ protected void stubTpaTokenRequests () {
443+ // Missing token
444+ server .stubFor (
445+ get (urlPathEqualTo (Constants .TPA_TOKEN_PATH ))
446+ .withQueryParam ("limit" , equalTo ("0" ))
447+ .willReturn (aResponse ().withStatus (401 )));
448+ // Default request
449+ server .stubFor (
450+ get (urlPathEqualTo (Constants .TPA_TOKEN_PATH ))
451+ .withHeader (
452+ Constants .AUTHORIZATION_HEADER ,
453+ equalTo ("Bearer " + TPA_TOKEN ).or (equalTo ("Bearer " + OK_TOKEN )))
454+ .withQueryParam ("limit" , equalTo ("0" ))
455+ .willReturn (
456+ aResponse ()
457+ .withStatus (200 )
458+ .withHeader (Exchange .CONTENT_TYPE , MediaType .APPLICATION_JSON )
459+ .withBodyFile ("tpa/empty_report.json" )));
460+ // Internal Error
461+ server .stubFor (
462+ get (urlPathEqualTo (Constants .TPA_TOKEN_PATH ))
463+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + ERROR_TOKEN ))
464+ .withQueryParam ("limit" , equalTo ("0" ))
465+ .willReturn (aResponse ().withStatus (500 ).withBody ("This is an example error" )));
466+ // Invalid token
467+ server .stubFor (
468+ get (urlPathEqualTo (Constants .TPA_TOKEN_PATH ))
469+ .withHeader (Constants .AUTHORIZATION_HEADER , equalTo ("Bearer " + INVALID_TOKEN ))
470+ .withQueryParam ("limit" , equalTo ("0" ))
471+ .willReturn (
472+ aResponse ()
473+ .withStatus (401 )
474+ .withBody (
475+ "{\" error\" : \" Unauthorized\" , \" message\" : \" Authentication failed\" }" )));
476+ }
477+
386478 protected void verifyTrustedContentRequest () {
387479 server .verify (1 , postRequestedFor (urlEqualTo (Constants .TRUSTED_CONTENT_PATH )));
388480 }
@@ -612,14 +704,6 @@ protected void verifyOsvNvdRequest(int count) {
612704 server .verify (count , postRequestedFor (urlEqualTo (Constants .OSV_NVD_PURLS_PATH )));
613705 }
614706
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-
623707 protected void verifyNoInteractions () {
624708 verifyNoInteractionsWithSnyk ();
625709 verifyNoInteractionsWithOSS ();
0 commit comments