12
12
import org .junit .jupiter .api .extension .ExtendWith ;
13
13
import org .mockito .junit .jupiter .MockitoExtension ;
14
14
import static org .junit .jupiter .api .Assertions .assertEquals ;
15
+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
15
16
import static org .mockito .ArgumentMatchers .any ;
16
17
import static org .mockito .Mockito .*;
17
18
import static org .mockito .Mockito .times ;
18
19
19
20
@ ExtendWith (MockitoExtension .class )
20
21
class OnBehalfOfTests {
21
22
22
- private String getSuccessfulResponse () {
23
- return "{\" access_token\" :\" accessToken\" ,\" expires_in\" : \" " + 60 *60 *1000 +"\" ,\" token_type\" :" +
23
+ private String getSuccessfulResponse (String accessToken ) {
24
+ return "{\" access_token\" :\" " + accessToken + " \" ,\" expires_in\" : \" " + 60 *60 *1000 +"\" ,\" token_type\" :" +
24
25
"\" Bearer\" ,\" client_id\" :\" client_id\" ,\" Content-Type\" :\" text/html; charset=utf-8\" }" ;
25
26
}
26
27
@@ -40,7 +41,7 @@ private HttpResponse expectedResponse(int statusCode, String response) {
40
41
void OnBehalfOf_InternalCacheLookup_Success () throws Exception {
41
42
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
42
43
43
- when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ()));
44
+ when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ("token" )));
44
45
45
46
ConfidentialClientApplication cca =
46
47
ConfidentialClientApplication .builder ("clientId" , ClientCredentialFactory .createFromSecret ("password" ))
@@ -64,8 +65,6 @@ void OnBehalfOf_InternalCacheLookup_Success() throws Exception {
64
65
void OnBehalfOf_TenantOverride () throws Exception {
65
66
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
66
67
67
- when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ()));
68
-
69
68
ConfidentialClientApplication cca =
70
69
ConfidentialClientApplication .builder ("clientId" , ClientCredentialFactory .createFromSecret ("password" ))
71
70
.authority ("https://login.microsoftonline.com/tenant" )
@@ -74,17 +73,23 @@ void OnBehalfOf_TenantOverride() throws Exception {
74
73
.httpClient (httpClientMock )
75
74
.build ();
76
75
76
+ when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ("appTenantToken" )));
77
77
OnBehalfOfParameters parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedToken )).build ();
78
- //The two acquireToken calls have the same parameters and should only cause one call from the HTTP client
79
78
79
+ //The two acquireToken calls have the same parameters and should only cause one call from the HTTP client
80
+ IAuthenticationResult resultAppLevelTenant = cca .acquireToken (parameters ).get ();
80
81
cca .acquireToken (parameters ).get ();
81
- cca .acquireToken ( parameters ). get ( );
82
+ assertEquals ( 1 , cca .tokenCache . accessTokens . size () );
82
83
verify (httpClientMock , times (1 )).send (any ());
83
84
85
+ when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ("requestTenantToken" )));
84
86
parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedToken )).tenant ("otherTenant" ).build ();
87
+
85
88
//Overriding the tenant parameter in the request should lead to a new token call being made, but followup calls should not
89
+ IAuthenticationResult resultRequestLevelTenant = cca .acquireToken (parameters ).get ();
86
90
cca .acquireToken (parameters ).get ();
87
- cca .acquireToken ( parameters ). get ( );
91
+ assertEquals ( 2 , cca .tokenCache . accessTokens . size () );
88
92
verify (httpClientMock , times (2 )).send (any ());
93
+ assertNotEquals (resultAppLevelTenant .accessToken (), resultRequestLevelTenant .accessToken ());
89
94
}
90
95
}
0 commit comments