20
20
@ ExtendWith (MockitoExtension .class )
21
21
class OnBehalfOfTests {
22
22
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
-
40
23
@ Test
41
24
void OnBehalfOf_InternalCacheLookup_Success () throws Exception {
42
25
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
43
26
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 <>() )));
45
28
46
29
ConfidentialClientApplication cca =
47
30
ConfidentialClientApplication .builder ("clientId" , ClientCredentialFactory .createFromSecret ("password" ))
@@ -51,7 +34,7 @@ void OnBehalfOf_InternalCacheLookup_Success() throws Exception {
51
34
.httpClient (httpClientMock )
52
35
.build ();
53
36
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 ();
55
38
56
39
IAuthenticationResult result = cca .acquireToken (parameters ).get ();
57
40
IAuthenticationResult result2 = cca .acquireToken (parameters ).get ();
@@ -73,23 +56,32 @@ void OnBehalfOf_TenantOverride() throws Exception {
73
56
.httpClient (httpClientMock )
74
57
.build ();
75
58
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 ();
78
64
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...
80
66
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
82
69
assertEquals (1 , cca .tokenCache .accessTokens .size ());
70
+ assertEquals (resultAppLevelTenant .accessToken (), resultAppLevelTenantCached .accessToken ());
83
71
verify (httpClientMock , times (1 )).send (any ());
84
72
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 ();
87
77
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...
89
79
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
91
82
assertEquals (2 , cca .tokenCache .accessTokens .size ());
92
- verify ( httpClientMock , times ( 2 )). send ( any ());
83
+ assertEquals ( resultRequestLevelTenant . accessToken (), resultRequestLevelTenantCached . accessToken ());
93
84
assertNotEquals (resultAppLevelTenant .accessToken (), resultRequestLevelTenant .accessToken ());
85
+ verify (httpClientMock , times (2 )).send (any ());
94
86
}
95
87
}
0 commit comments