Skip to content

Commit 0874c4c

Browse files
committed
Improve unit test
1 parent f042744 commit 0874c4c

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/OnBehalfOfTests.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
import org.junit.jupiter.api.extension.ExtendWith;
1313
import org.mockito.junit.jupiter.MockitoExtension;
1414
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1516
import static org.mockito.ArgumentMatchers.any;
1617
import static org.mockito.Mockito.*;
1718
import static org.mockito.Mockito.times;
1819

1920
@ExtendWith(MockitoExtension.class)
2021
class OnBehalfOfTests {
2122

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\":" +
2425
"\"Bearer\",\"client_id\":\"client_id\",\"Content-Type\":\"text/html; charset=utf-8\"}";
2526
}
2627

@@ -40,7 +41,7 @@ private HttpResponse expectedResponse(int statusCode, String response) {
4041
void OnBehalfOf_InternalCacheLookup_Success() throws Exception {
4142
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
4243

43-
when(httpClientMock.send(any(HttpRequest.class))).thenReturn(expectedResponse(200, getSuccessfulResponse()));
44+
when(httpClientMock.send(any(HttpRequest.class))).thenReturn(expectedResponse(200, getSuccessfulResponse("token")));
4445

4546
ConfidentialClientApplication cca =
4647
ConfidentialClientApplication.builder("clientId", ClientCredentialFactory.createFromSecret("password"))
@@ -64,8 +65,6 @@ void OnBehalfOf_InternalCacheLookup_Success() throws Exception {
6465
void OnBehalfOf_TenantOverride() throws Exception {
6566
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
6667

67-
when(httpClientMock.send(any(HttpRequest.class))).thenReturn(expectedResponse(200, getSuccessfulResponse()));
68-
6968
ConfidentialClientApplication cca =
7069
ConfidentialClientApplication.builder("clientId", ClientCredentialFactory.createFromSecret("password"))
7170
.authority("https://login.microsoftonline.com/tenant")
@@ -74,17 +73,23 @@ void OnBehalfOf_TenantOverride() throws Exception {
7473
.httpClient(httpClientMock)
7574
.build();
7675

76+
when(httpClientMock.send(any(HttpRequest.class))).thenReturn(expectedResponse(200, getSuccessfulResponse("appTenantToken")));
7777
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
7978

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();
8081
cca.acquireToken(parameters).get();
81-
cca.acquireToken(parameters).get();
82+
assertEquals(1, cca.tokenCache.accessTokens.size());
8283
verify(httpClientMock, times(1)).send(any());
8384

85+
when(httpClientMock.send(any(HttpRequest.class))).thenReturn(expectedResponse(200, getSuccessfulResponse("requestTenantToken")));
8486
parameters = OnBehalfOfParameters.builder(Collections.singleton("scopes"), new UserAssertion(TestHelper.signedToken)).tenant("otherTenant").build();
87+
8588
//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();
8690
cca.acquireToken(parameters).get();
87-
cca.acquireToken(parameters).get();
91+
assertEquals(2, cca.tokenCache.accessTokens.size());
8892
verify(httpClientMock, times(2)).send(any());
93+
assertNotEquals(resultAppLevelTenant.accessToken(), resultRequestLevelTenant.accessToken());
8994
}
9095
}

0 commit comments

Comments
 (0)