-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathAadInstanceDiscoveryTest.java
More file actions
292 lines (231 loc) · 12.1 KB
/
Copy pathAadInstanceDiscoveryTest.java
File metadata and controls
292 lines (231 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
@ExtendWith(MockitoExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class AadInstanceDiscoveryTest {
String instanceDiscoveryValidResponse;
AuthorizationCodeParameters parameters;
@BeforeAll
public void init() throws Exception {
instanceDiscoveryValidResponse = TestHelper.readResource(
this.getClass(),
"/instance_discovery_data/aad_instance_discovery_response_valid.json");
parameters = AuthorizationCodeParameters.builder(
"code", new URI("http://my.redirect.com")).build();
}
@BeforeEach
public void setup() {
AadInstanceDiscoveryProvider.cache.clear();
}
@Test
void aadInstanceDiscoveryTest_NotSetByDeveloper() throws Exception {
PublicClientApplication app = PublicClientApplication.builder("client_id")
.correlationId("correlation_id")
.authority("https://login.microsoftonline.com/my_tenant")
.build();
MsalRequest msalRequest = new AuthorizationCodeRequest(
parameters,
app,
new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
URL authority = new URL(app.authority());
AadInstanceDiscoveryResponse expectedResponse= JsonHelper.convertJsonStringToJsonSerializableObject(instanceDiscoveryValidResponse, AadInstanceDiscoveryResponse::fromJson);
try (MockedStatic<AadInstanceDiscoveryProvider> mockedInstanceDiscoveryProvider = mockStatic(AadInstanceDiscoveryProvider.class, CALLS_REAL_METHODS)) {
mockedInstanceDiscoveryProvider.when(() -> AadInstanceDiscoveryProvider.sendInstanceDiscoveryRequest(authority,
msalRequest,
app.serviceBundle())).thenReturn(expectedResponse);
InstanceDiscoveryMetadataEntry entry = AadInstanceDiscoveryProvider.getMetadataEntry(
authority,
false,
msalRequest,
app.serviceBundle());
assertValidResponse(entry);
}
}
@Test
void aadInstanceDiscoveryTest_responseSetByDeveloper_validResponse() throws Exception {
PublicClientApplication app = PublicClientApplication.builder("client_id")
.aadInstanceDiscoveryResponse(instanceDiscoveryValidResponse)
.build();
MsalRequest msalRequest = new AuthorizationCodeRequest(
parameters,
app,
new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
URL authority = new URL(app.authority());
InstanceDiscoveryMetadataEntry entry = AadInstanceDiscoveryProvider.getMetadataEntry(
authority,
false,
msalRequest,
app.serviceBundle());
assertValidResponse(entry);
}
@Test
void aadInstanceDiscoveryTest_responseSetByDeveloper_invalidJson() throws Exception {
String instanceDiscoveryResponse = TestHelper.readResource(
this.getClass(),
"/instance_discovery_data/aad_instance_discovery_response_invalid_json.json");
assertThrows(MsalClientException.class, () -> PublicClientApplication.builder("client_id")
.aadInstanceDiscoveryResponse(instanceDiscoveryResponse)
.build());
}
@Test
void aadInstanceDiscoveryTest_AutoDetectRegion_NoRegionDetected() throws Exception {
PublicClientApplication app = PublicClientApplication.builder("client_id")
.aadInstanceDiscoveryResponse(instanceDiscoveryValidResponse)
.autoDetectRegion(true)
.build();
MsalRequest msalRequest = new AuthorizationCodeRequest(
parameters,
app,
new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
URL authority = new URL(app.authority());
try (MockedStatic<AadInstanceDiscoveryProvider> mocked = mockStatic(AadInstanceDiscoveryProvider.class, CALLS_REAL_METHODS)) {
mocked.when(() -> AadInstanceDiscoveryProvider.discoverRegion(msalRequest,
app.serviceBundle())).thenReturn(null);
InstanceDiscoveryMetadataEntry entry = AadInstanceDiscoveryProvider.getMetadataEntry(
authority,
false,
msalRequest,
app.serviceBundle());
assertValidResponse(entry);
}
}
@Test
void discoveryEndpoint_routesToSovereignHost() throws Exception {
// Arrange
URL sovereignUrl = new URL("https://login.sovcloud-identity.fr/my_tenant");
Method method = AadInstanceDiscoveryProvider.class.getDeclaredMethod("getInstanceDiscoveryEndpoint", URL.class);
method.setAccessible(true);
// Act
String endpoint = (String) method.invoke(null, sovereignUrl);
// Assert
assertTrue(endpoint.contains("login.sovcloud-identity.fr"),
"Discovery endpoint should use the sovereign host, got: " + endpoint);
}
@Test
void regionalEndpoint_usesSovereignTemplate() throws Exception {
// Arrange
Method method = AadInstanceDiscoveryProvider.class.getDeclaredMethod("getRegionalizedHost", String.class, String.class);
method.setAccessible(true);
// Act
String result = (String) method.invoke(null, "login.sovcloud-identity.fr", "westeurope");
// Assert
assertEquals("westeurope.login.sovcloud-identity.fr", result);
}
@Test
void networkException_cachesFallbackAndDoesNotPropagate() throws Exception {
// Arrange
PublicClientApplication app = PublicClientApplication.builder("client_id")
.correlationId("correlation_id")
.authority("https://login.microsoftonline.com/my_tenant")
.build();
MsalRequest msalRequest = new AuthorizationCodeRequest(
parameters,
app,
new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
URL authority = new URL(app.authority());
try (MockedStatic<AadInstanceDiscoveryProvider> mocked = mockStatic(AadInstanceDiscoveryProvider.class, CALLS_REAL_METHODS)) {
mocked.when(() -> AadInstanceDiscoveryProvider.sendInstanceDiscoveryRequest(authority,
msalRequest,
app.serviceBundle())).thenThrow(new MsalClientException("Network timeout", AuthenticationErrorCode.UNKNOWN));
// Act — should not throw
InstanceDiscoveryMetadataEntry entry = assertDoesNotThrow(() ->
AadInstanceDiscoveryProvider.getMetadataEntry(
authority,
false,
msalRequest,
app.serviceBundle()));
// Assert — cache should contain a self-entry
assertNotNull(entry);
String host = authority.getHost();
InstanceDiscoveryMetadataEntry cached = AadInstanceDiscoveryProvider.cache.get(host);
assertNotNull(cached, "Fallback entry should be cached");
assertEquals(host, cached.preferredNetwork);
assertEquals(host, cached.preferredCache);
assertTrue(cached.aliases.contains(host));
}
}
@Test
void subsequentCallAfterNetworkFailure_usesCacheNoRetry() throws Exception {
// Arrange
PublicClientApplication app = PublicClientApplication.builder("client_id")
.correlationId("correlation_id")
.authority("https://login.microsoftonline.com/my_tenant")
.build();
MsalRequest msalRequest = new AuthorizationCodeRequest(
parameters,
app,
new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
URL authority = new URL(app.authority());
try (MockedStatic<AadInstanceDiscoveryProvider> mocked = mockStatic(AadInstanceDiscoveryProvider.class, CALLS_REAL_METHODS)) {
mocked.when(() -> AadInstanceDiscoveryProvider.sendInstanceDiscoveryRequest(any(URL.class),
any(MsalRequest.class),
any(ServiceBundle.class))).thenThrow(new MsalClientException("Network timeout", AuthenticationErrorCode.UNKNOWN));
// Act — first call triggers network failure + fallback cache
AadInstanceDiscoveryProvider.getMetadataEntry(authority, false, msalRequest, app.serviceBundle());
// Act — second call should hit cache, not retry network
InstanceDiscoveryMetadataEntry entry = AadInstanceDiscoveryProvider.getMetadataEntry(authority, false, msalRequest, app.serviceBundle());
// Assert — sendInstanceDiscoveryRequest should have been called only once
mocked.verify(() -> AadInstanceDiscoveryProvider.sendInstanceDiscoveryRequest(any(URL.class),
any(MsalRequest.class),
any(ServiceBundle.class)), times(1));
assertNotNull(entry);
}
}
@Test
void invalidInstanceException_stillPropagates() throws Exception {
// Arrange
PublicClientApplication app = PublicClientApplication.builder("client_id")
.correlationId("correlation_id")
.authority("https://login.microsoftonline.com/my_tenant")
.build();
MsalRequest msalRequest = new AuthorizationCodeRequest(
parameters,
app,
new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
URL authority = new URL(app.authority());
try (MockedStatic<AadInstanceDiscoveryProvider> mocked = mockStatic(AadInstanceDiscoveryProvider.class, CALLS_REAL_METHODS)) {
mocked.when(() -> AadInstanceDiscoveryProvider.sendInstanceDiscoveryRequest(authority,
msalRequest,
app.serviceBundle())).thenThrow(new MsalServiceException("invalid_instance", "invalid_instance"));
// Act / Assert — MsalServiceException should propagate
assertThrows(MsalServiceException.class, () ->
AadInstanceDiscoveryProvider.getMetadataEntry(
authority,
false,
msalRequest,
app.serviceBundle()));
// Assert — nothing should be cached
assertNull(AadInstanceDiscoveryProvider.cache.get(authority.getHost()));
}
}
void assertValidResponse(InstanceDiscoveryMetadataEntry entry) {
assertEquals(entry.preferredNetwork(), "login.microsoftonline.com");
assertEquals(entry.preferredCache(), "login.windows.net");
assertEquals(entry.aliases().size(), 4);
assertTrue(entry.aliases().contains("login.microsoftonline.com"));
assertTrue(entry.aliases().contains("login.windows.net"));
assertTrue(entry.aliases().contains("login.microsoft.com"));
assertTrue(entry.aliases().contains("sts.windows.net"));
}
}