Skip to content

Commit e274837

Browse files
committed
Refactor and clean up new test infrastructure and existing Selenium helpers
1 parent 4583a7d commit e274837

24 files changed

Lines changed: 707 additions & 692 deletions

msal4j-sdk/src/integrationtest/java/com.microsoft.aad.msal4j/AcquireTokenInteractiveIT.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
class AcquireTokenInteractiveIT extends SeleniumTest {
2727
private static final Logger LOG = LoggerFactory.getLogger(AcquireTokenInteractiveIT.class);
2828

29-
private Config cfg;
30-
3129
@AfterEach
3230
public void stopBrowser() {
3331
cleanUp();
@@ -40,25 +38,20 @@ public void startBrowser() {
4038

4139
@Test
4240
void acquireTokenInteractive_ManagedUser() {
43-
cfg = new Config();
44-
45-
LabResponse labResponse = LabUserHelper.getDefaultUserMultiTenantApp();
41+
LabResponse labResponse = LabUserHelper.getDefaultUserMultiTenantAppPublicClient();
4642
LabUser user = labResponse.getUser();
4743

48-
assertAcquireTokenCommon(user, labResponse.getApp().getAppId(), labResponse.getApp().getAuthority()+ "common", cfg.graphDefaultScope());
44+
assertAcquireTokenCommon(user, labResponse.getApp().getAppId(), labResponse.getApp().getAuthority()+ "common", TestConstants.GRAPH_DEFAULT_SCOPE);
4945
}
5046

5147
@Test
5248
void acquireTokenInteractive_Arlington() {
53-
cfg = new Config();
54-
5549
LabResponse labResponse = LabUserHelper.getArlingtonUser();
5650
LabUser user = labResponse.getUser();
5751

58-
assertAcquireTokenCommon(user, labResponse.getApp().getAppId(), labResponse.getApp().getAuthority()+ "common", cfg.graphDefaultScope());
52+
assertAcquireTokenCommon(user, labResponse.getApp().getAppId(), labResponse.getApp().getAuthority()+ "common", TestConstants.GRAPH_DEFAULT_SCOPE);
5953
}
6054

61-
//TODO: need to sort out ADFS 2022 configuration
6255
@Test()
6356
@DisabledIfSystemProperty(named = "adfs.disabled", matches = "true")
6457
void acquireTokenInteractive_ADFSv2022() {
@@ -70,26 +63,20 @@ void acquireTokenInteractive_ADFSv2022() {
7063

7164
@Test
7265
void acquireTokenWithAuthorizationCode_B2C_Local() {
73-
cfg = new Config();
74-
7566
LabResponse labResponse = LabUserHelper.getB2CLocalAccount();
7667
LabUser user = labResponse.getUser();
7768
assertAcquireTokenB2C(user, TestConstants.B2C_AUTHORITY);
7869
}
7970

8071
@Test
8172
void acquireTokenWithAuthorizationCode_B2C_LegacyFormat() {
82-
cfg = new Config();
83-
8473
LabResponse labResponse = LabUserHelper.getB2CLocalAccount();
8574
LabUser user = labResponse.getUser();
8675
assertAcquireTokenB2C(user, TestConstants.B2C_AUTHORITY_LEGACY_FORMAT);
8776
}
8877

8978
@Test
9079
void acquireTokenInteractive_ManagedUser_InstanceAware() {
91-
cfg = new Config();
92-
9380
LabResponse labResponse = LabUserHelper.getArlingtonUser();
9481
LabUser user = labResponse.getUser();
9582
assertAcquireTokenInstanceAware(user, labResponse.getApp().getAppId(), labResponse.getLab().getTenantId());
@@ -172,7 +159,7 @@ private void assertAcquireTokenB2C(LabUser user, String authority) {
172159
private void assertAcquireTokenInstanceAware(LabUser user, String appId, String tenantId) {
173160
PublicClientApplication pca = IntegrationTestHelper.createPublicApp(appId, TestConstants.MICROSOFT_AUTHORITY_HOST + tenantId);
174161

175-
IAuthenticationResult result = acquireTokenInteractive_instanceAware(user, pca, cfg.graphDefaultScope());
162+
IAuthenticationResult result = acquireTokenInteractive_instanceAware(user, pca, TestConstants.GRAPH_DEFAULT_SCOPE);
176163

177164
IntegrationTestHelper.assertAccessAndIdTokensNotNull(result);
178165
assertEquals(user.getUpn(), result.account().username());
@@ -185,7 +172,7 @@ private void assertAcquireTokenInstanceAware(LabUser user, String appId, String
185172

186173
IAuthenticationResult cachedResult;
187174
try {
188-
cachedResult = acquireTokenSilently(pca, result.account(), cfg.graphDefaultScope());
175+
cachedResult = acquireTokenSilently(pca, result.account(), TestConstants.GRAPH_DEFAULT_SCOPE);
189176
} catch (Exception ex) {
190177
throw new RuntimeException(ex.getMessage());
191178
}

msal4j-sdk/src/integrationtest/java/com.microsoft.aad.msal4j/AcquireTokenSilentIT.java

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,32 @@
2020

2121
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2222
class AcquireTokenSilentIT {
23-
private Config cfg;
24-
2523
@Test
2624
void acquireTokenSilent_OrganizationAuthority_TokenRefreshed() throws Exception {
27-
cfg = new Config();
28-
2925
// When using common, organization, or consumer tenants, cache has no way
3026
// of determining which access token to return therefore token is always refreshed
3127
IPublicClientApplication pca = getPublicClientApplicationWithTokensInCache();
3228

3329
IAccount account = pca.getAccounts().join().iterator().next();
34-
IAuthenticationResult result = acquireTokenSilently(pca, account, cfg.graphDefaultScope(), false);
30+
IAuthenticationResult result = acquireTokenSilently(pca, account, TestConstants.GRAPH_DEFAULT_SCOPE, false);
3531
assertResultNotNull(result);
3632
}
3733

3834
@Test
3935
void acquireTokenSilent_LabAuthority_TokenNotRefreshed() throws Exception {
40-
cfg = new Config();
41-
4236
// Access token should be returned from cache, and not using refresh token
4337
LabResponse labResponse = LabUserHelper.getDefaultUser();
4438
LabUser user = labResponse.getUser();
4539

4640
PublicClientApplication pca = PublicClientApplication.builder(
4741
labResponse.getApp().getAppId()).
48-
authority(cfg.organizationsAuthority()).
42+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
4943
build();
5044

51-
IAuthenticationResult result = acquireTokenUsernamePassword(user, pca, cfg.graphDefaultScope());
45+
IAuthenticationResult result = acquireTokenUsernamePassword(user, pca, TestConstants.GRAPH_DEFAULT_SCOPE);
5246

5347
IAccount account = pca.getAccounts().join().iterator().next();
54-
IAuthenticationResult acquireSilentResult = acquireTokenSilently(pca, account, cfg.graphDefaultScope(), false);
48+
IAuthenticationResult acquireSilentResult = acquireTokenSilently(pca, account, TestConstants.GRAPH_DEFAULT_SCOPE, false);
5549
assertResultNotNull(result);
5650

5751
// Check that access and id tokens are coming from cache
@@ -63,21 +57,19 @@ void acquireTokenSilent_LabAuthority_TokenNotRefreshed() throws Exception {
6357

6458
@Test
6559
void acquireTokenSilent_ForceRefresh() throws Exception {
66-
cfg = new Config();
67-
6860
LabResponse labResponse = LabUserHelper.getDefaultUser();
6961
LabUser user = labResponse.getUser();
7062

7163
PublicClientApplication pca = PublicClientApplication.builder(
7264
labResponse.getApp().getAppId()).
73-
authority(cfg.organizationsAuthority()).
65+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
7466
build();
7567

76-
IAuthenticationResult result = acquireTokenUsernamePassword(user, pca, cfg.graphDefaultScope());
68+
IAuthenticationResult result = acquireTokenUsernamePassword(user, pca, TestConstants.GRAPH_DEFAULT_SCOPE);
7769
assertResultNotNull(result);
7870

7971
IAccount account = pca.getAccounts().join().iterator().next();
80-
IAuthenticationResult resultAfterRefresh = acquireTokenSilently(pca, account, cfg.graphDefaultScope(), true);
72+
IAuthenticationResult resultAfterRefresh = acquireTokenSilently(pca, account, TestConstants.GRAPH_DEFAULT_SCOPE, true);
8173
assertResultNotNull(resultAfterRefresh);
8274

8375
// Check that new refresh and id tokens are being returned
@@ -88,34 +80,28 @@ void acquireTokenSilent_ForceRefresh() throws Exception {
8880

8981
@Test
9082
void acquireTokenSilent_usingOrganizationsAuthority_returnCachedAt() throws Exception {
91-
cfg = new Config();
92-
9383
LabResponse labResponse = LabUserHelper.getDefaultUser();
9484
LabUser user = labResponse.getUser();
9585

96-
acquireTokenSilent_returnCachedTokens(labResponse.getApp().getAppId(), cfg.organizationsAuthority(), user);
86+
acquireTokenSilent_returnCachedTokens(labResponse.getApp().getAppId(), TestConstants.ORGANIZATIONS_AUTHORITY, user);
9787
}
9888

9989
@Test
10090
void acquireTokenSilent_usingTenantSpecificAuthority_returnCachedAt() throws Exception {
101-
cfg = new Config();
102-
10391
LabResponse labResponse = LabUserHelper.getDefaultUser();
10492
LabUser user = labResponse.getUser();
10593

106-
acquireTokenSilent_returnCachedTokens(labResponse.getApp().getAppId(), cfg.tenantSpecificAuthority(labResponse.getUser().getTenantId()), user);
94+
acquireTokenSilent_returnCachedTokens(labResponse.getApp().getAppId(), TestConstants.MICROSOFT_AUTHORITY_HOST + labResponse.getUser().getTenantId(), user);
10795
}
10896

10997
@Test
11098
void acquireTokenSilent_ConfidentialClient_acquireTokenSilent() throws Exception {
111-
cfg = new Config();
112-
11399
IConfidentialClientApplication cca = getConfidentialClientApplications();
114100
//test that adding extra query parameters does not break the flow
115101
Map<String, String> extraParameters = new HashMap<>();
116102
extraParameters.put("test","test");
117103
IAuthenticationResult result = cca.acquireToken(ClientCredentialParameters
118-
.builder(Collections.singleton(cfg.graphDefaultScope()))
104+
.builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE))
119105
.extraQueryParameters(extraParameters)
120106
.build())
121107
.get();
@@ -126,7 +112,7 @@ void acquireTokenSilent_ConfidentialClient_acquireTokenSilent() throws Exception
126112
String cachedAt = result.accessToken();
127113

128114
result = cca.acquireTokenSilently(SilentParameters
129-
.builder(Collections.singleton(cfg.graphDefaultScope()))
115+
.builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE))
130116
.extraQueryParameters(extraParameters)
131117
.build())
132118
.get();
@@ -138,7 +124,6 @@ void acquireTokenSilent_ConfidentialClient_acquireTokenSilent() throws Exception
138124
@Test
139125
void acquireTokenSilent_ConfidentialClient_acquireTokenSilentDifferentScopeThrowsException()
140126
throws Exception {
141-
cfg = new Config();
142127

143128
IConfidentialClientApplication cca = getConfidentialClientApplications();
144129

@@ -152,27 +137,25 @@ void acquireTokenSilent_ConfidentialClient_acquireTokenSilentDifferentScopeThrow
152137

153138
//Acquiring token for different scope, expect exception to be thrown
154139
assertThrows(ExecutionException.class, () -> cca.acquireTokenSilently(SilentParameters
155-
.builder(Collections.singleton(cfg.graphDefaultScope()))
140+
.builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE))
156141
.build())
157142
.get());
158143
}
159144

160145
@Test
161146
void acquireTokenSilent_WithRefreshOn() throws Exception {
162-
cfg = new Config();
163-
164147
LabResponse labResponse = LabUserHelper.getDefaultUser();
165148
LabUser user = labResponse.getUser();
166149

167150
PublicClientApplication pca = PublicClientApplication.builder(
168151
labResponse.getApp().getAppId()).
169-
authority(cfg.organizationsAuthority()).
152+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
170153
build();
171154

172-
IAuthenticationResult resultOriginal = acquireTokenUsernamePassword(user, pca, cfg.graphDefaultScope());
155+
IAuthenticationResult resultOriginal = acquireTokenUsernamePassword(user, pca, TestConstants.GRAPH_DEFAULT_SCOPE);
173156
assertResultNotNull(resultOriginal);
174157

175-
IAuthenticationResult resultSilent = acquireTokenSilently(pca, resultOriginal.account(), cfg.graphDefaultScope(), false);
158+
IAuthenticationResult resultSilent = acquireTokenSilently(pca, resultOriginal.account(), TestConstants.GRAPH_DEFAULT_SCOPE, false);
176159
assertNotNull(resultSilent);
177160
assertTokensAreEqual(resultOriginal, resultSilent);
178161

@@ -186,7 +169,7 @@ void acquireTokenSilent_WithRefreshOn() throws Exception {
186169
token.refreshOn(Long.toString(currTimestampSec + 60));
187170
pca.tokenCache.accessTokens.put(key, token);
188171

189-
IAuthenticationResult resultSilentWithRefreshOn = acquireTokenSilently(pca, resultOriginal.account(), cfg.graphDefaultScope(), false);
172+
IAuthenticationResult resultSilentWithRefreshOn = acquireTokenSilently(pca, resultOriginal.account(), TestConstants.GRAPH_DEFAULT_SCOPE, false);
190173
//Current time is before refreshOn, so token should not have been refreshed
191174
assertNotNull(resultSilentWithRefreshOn);
192175
assertEquals(pca.tokenCache.accessTokens.get(key).refreshOn(), Long.toString(currTimestampSec + 60));
@@ -196,7 +179,7 @@ void acquireTokenSilent_WithRefreshOn() throws Exception {
196179
token.refreshOn(Long.toString(currTimestampSec - 60));
197180
pca.tokenCache.accessTokens.put(key, token);
198181

199-
resultSilentWithRefreshOn = acquireTokenSilently(pca, resultOriginal.account(), cfg.graphDefaultScope(), false);
182+
resultSilentWithRefreshOn = acquireTokenSilently(pca, resultOriginal.account(), TestConstants.GRAPH_DEFAULT_SCOPE, false);
200183
//Current time is after refreshOn, so token should be refreshed
201184
assertNotNull(resultSilentWithRefreshOn);
202185
assertTokensAreNotEqual(resultSilent, resultSilentWithRefreshOn);
@@ -206,30 +189,28 @@ void acquireTokenSilent_WithRefreshOn() throws Exception {
206189

207190
@Test
208191
void acquireTokenSilent_TenantAsParameter() throws Exception {
209-
cfg = new Config();
210-
211192
LabResponse labResponse = LabUserHelper.getDefaultUser();
212193
LabUser user = labResponse.getUser();
213194

214195
PublicClientApplication pca = PublicClientApplication.builder(
215196
labResponse.getApp().getAppId()).
216-
authority(cfg.organizationsAuthority()).
197+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
217198
build();
218199

219200
IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters.
220-
builder(Collections.singleton(cfg.graphDefaultScope()),
201+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
221202
user.getUpn(),
222203
user.getPassword().toCharArray())
223204
.build()).get();
224205
assertResultNotNull(result);
225206

226207
IAccount account = pca.getAccounts().join().iterator().next();
227-
IAuthenticationResult silentResult = acquireTokenSilently(pca, account, cfg.graphDefaultScope(), false);
208+
IAuthenticationResult silentResult = acquireTokenSilently(pca, account, TestConstants.GRAPH_DEFAULT_SCOPE, false);
228209
assertResultNotNull(silentResult);
229210
assertTokensAreEqual(result, silentResult);
230211

231212
IAuthenticationResult resultWithTenantParam = pca.acquireTokenSilently(SilentParameters.
232-
builder(Collections.singleton(cfg.graphDefaultScope()), account).
213+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account).
233214
tenant(labResponse.getUser().getTenantId()).
234215
build()).get();
235216
assertResultNotNull(resultWithTenantParam);
@@ -238,13 +219,12 @@ void acquireTokenSilent_TenantAsParameter() throws Exception {
238219

239220
@Test
240221
void acquireTokenSilent_emptyStringScope() throws Exception {
241-
cfg = new Config();
242222
LabResponse labResponse = LabUserHelper.getDefaultUser();
243223
LabUser user = labResponse.getUser();
244224

245225
PublicClientApplication pca = PublicClientApplication.builder(
246226
labResponse.getApp().getAppId()).
247-
authority(cfg.organizationsAuthority()).
227+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
248228
build();
249229

250230
String emptyScope = StringHelper.EMPTY_STRING;
@@ -259,14 +239,13 @@ void acquireTokenSilent_emptyStringScope() throws Exception {
259239

260240
@Test
261241
void acquireTokenSilent_emptyScopeSet() throws Exception {
262-
cfg = new Config();
263242
LabResponse labResponse = LabUserHelper.getDefaultUser();
264243
LabUser user = labResponse.getUser();
265244

266245
Set<String> scopes = new HashSet<>();
267246
PublicClientApplication pca = PublicClientApplication.builder(
268247
labResponse.getApp().getAppId()).
269-
authority(cfg.organizationsAuthority()).
248+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
270249
build();
271250

272251
IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters.
@@ -289,14 +268,13 @@ void acquireTokenSilent_emptyScopeSet() throws Exception {
289268

290269
@Test
291270
public void acquireTokenSilent_ClaimsForceRefresh() throws Exception {
292-
cfg = new Config();
293271
LabResponse labResponse = LabUserHelper.getDefaultUser();
294272
LabUser user = labResponse.getUser();
295273

296274
Set<String> scopes = new HashSet<>();
297275
PublicClientApplication pca = PublicClientApplication.builder(
298276
labResponse.getApp().getAppId()).
299-
authority(cfg.organizationsAuthority()).
277+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
300278
build();
301279

302280
IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters.
@@ -331,33 +309,31 @@ public void acquireTokenSilent_ClaimsForceRefresh() throws Exception {
331309
}
332310

333311
private IConfidentialClientApplication getConfidentialClientApplications() throws Exception {
334-
String clientId = cfg.appProvider().getOboAppId();
335-
String password = cfg.appProvider().getOboAppPassword();
312+
String clientId = TestConstants.OBO_CLIENT_ID;
313+
String password = KeyVaultRegistry.getMsalTeamProvider().getSecretByName("IdentityDivisionDotNetOBOServiceSecret").getValue();
336314

337315
IClientCredential credential = ClientCredentialFactory.createFromSecret(password);
338316

339317
return ConfidentialClientApplication.builder(
340318
clientId, credential).
341-
//authority(MICROSOFT_AUTHORITY)
342-
authority(cfg.tenantSpecificAuthority()).
319+
authority(TestConstants.AUTHORITY_PUBLIC_TENANT_SPECIFIC).
343320
build();
344321
}
345322

346323
private void acquireTokenSilent_returnCachedTokens(String appId, String authority, LabUser user) throws Exception {
347324

348-
349325
PublicClientApplication pca = PublicClientApplication.builder(
350326
appId).
351327
authority(authority).
352328
build();
353329

354-
IAuthenticationResult interactiveAuthResult = acquireTokenUsernamePassword(user, pca, cfg.graphDefaultScope());
330+
IAuthenticationResult interactiveAuthResult = acquireTokenUsernamePassword(user, pca, TestConstants.GRAPH_DEFAULT_SCOPE);
355331

356332
assertNotNull(interactiveAuthResult);
357333

358334
IAuthenticationResult silentAuthResult = pca.acquireTokenSilently(
359335
SilentParameters.builder(
360-
Collections.singleton(cfg.graphDefaultScope()), interactiveAuthResult.account())
336+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), interactiveAuthResult.account())
361337
.build())
362338
.get();
363339

@@ -372,10 +348,10 @@ private IPublicClientApplication getPublicClientApplicationWithTokensInCache()
372348

373349
PublicClientApplication pca = PublicClientApplication.builder(
374350
labResponse.getApp().getAppId()).
375-
authority(cfg.organizationsAuthority()).
351+
authority(TestConstants.ORGANIZATIONS_AUTHORITY).
376352
build();
377353

378-
acquireTokenUsernamePassword(user, pca, cfg.graphDefaultScope());
354+
acquireTokenUsernamePassword(user, pca, TestConstants.GRAPH_DEFAULT_SCOPE);
379355
return pca;
380356
}
381357

0 commit comments

Comments
 (0)