2020@ ExtendWith (MockitoExtension .class )
2121class OnBehalfOfTests {
2222
23- private String getSuccessfulResponse (String accessToken ) {
24- return "{\" access_token\" :\" " +accessToken +"\" ,\" expires_in\" : \" " + 60 *60 *1000 +"\" ,\" token_type\" :" +
25- "\" Bearer\" ,\" client_id\" :\" client_id\" ,\" Content-Type\" :\" text/html; charset=utf-8\" }" ;
26- }
27-
28- private HttpResponse expectedResponse (int statusCode , String response ) {
29- Map <String , List <String >> headers = new HashMap <String , List <String >>();
30- headers .put ("Content-Type" , Collections .singletonList ("application/json" ));
31-
32- HttpResponse httpResponse = new HttpResponse ();
33- httpResponse .statusCode (statusCode );
34- httpResponse .body (response );
35- httpResponse .addHeaders (headers );
36-
37- return httpResponse ;
38- }
39-
4023 @ Test
4124 void OnBehalfOf_InternalCacheLookup_Success () throws Exception {
4225 DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
4326
44- when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ( "token" )));
27+ when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (TestHelper . expectedResponse (200 , TestHelper . getSuccessfulTokenResponse ( new HashMap <>() )));
4528
4629 ConfidentialClientApplication cca =
4730 ConfidentialClientApplication .builder ("clientId" , ClientCredentialFactory .createFromSecret ("password" ))
@@ -51,7 +34,7 @@ void OnBehalfOf_InternalCacheLookup_Success() throws Exception {
5134 .httpClient (httpClientMock )
5235 .build ();
5336
54- OnBehalfOfParameters parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedToken )).build ();
37+ OnBehalfOfParameters parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedAssertion )).build ();
5538
5639 IAuthenticationResult result = cca .acquireToken (parameters ).get ();
5740 IAuthenticationResult result2 = cca .acquireToken (parameters ).get ();
@@ -73,23 +56,32 @@ void OnBehalfOf_TenantOverride() throws Exception {
7356 .httpClient (httpClientMock )
7457 .build ();
7558
76- when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ("appTenantToken" )));
77- OnBehalfOfParameters parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedToken )).build ();
59+ HashMap <String , String > tokenResponseValues = new HashMap <>();
60+ tokenResponseValues .put ("access_token" , "accessTokenFirstCall" );
61+
62+ when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (TestHelper .expectedResponse (200 , TestHelper .getSuccessfulTokenResponse (tokenResponseValues )));
63+ OnBehalfOfParameters parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedAssertion )).build ();
7864
79- //The two acquireToken calls have the same parameters and should only cause one call from the HTTP client
65+ //The two acquireToken calls have the same parameters...
8066 IAuthenticationResult resultAppLevelTenant = cca .acquireToken (parameters ).get ();
81- cca .acquireToken (parameters ).get ();
67+ IAuthenticationResult resultAppLevelTenantCached = cca .acquireToken (parameters ).get ();
68+ //...so only one token should be added to the cache, and the mocked HTTP client's "send" method should only have been called once
8269 assertEquals (1 , cca .tokenCache .accessTokens .size ());
70+ assertEquals (resultAppLevelTenant .accessToken (), resultAppLevelTenantCached .accessToken ());
8371 verify (httpClientMock , times (1 )).send (any ());
8472
85- when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ("requestTenantToken" )));
86- parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedToken )).tenant ("otherTenant" ).build ();
73+ tokenResponseValues .put ("access_token" , "accessTokenSecondCall" );
74+
75+ when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (TestHelper .expectedResponse (200 , TestHelper .getSuccessfulTokenResponse (tokenResponseValues )));
76+ parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedAssertion )).tenant ("otherTenant" ).build ();
8777
88- //Overriding the tenant parameter in the request should lead to a new token call being made, but followup calls should not
78+ //Overriding the tenant parameter in the request should lead to a new token call being made...
8979 IAuthenticationResult resultRequestLevelTenant = cca .acquireToken (parameters ).get ();
90- cca .acquireToken (parameters ).get ();
80+ IAuthenticationResult resultRequestLevelTenantCached = cca .acquireToken (parameters ).get ();
81+ //...which should be different from the original token, and thus the cache should have two tokens created from two HTTP calls
9182 assertEquals (2 , cca .tokenCache .accessTokens .size ());
92- verify ( httpClientMock , times ( 2 )). send ( any ());
83+ assertEquals ( resultRequestLevelTenant . accessToken (), resultRequestLevelTenantCached . accessToken ());
9384 assertNotEquals (resultAppLevelTenant .accessToken (), resultRequestLevelTenant .accessToken ());
85+ verify (httpClientMock , times (2 )).send (any ());
9486 }
9587}
0 commit comments