Skip to content

Commit

Permalink
modify a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyf19 committed Jun 27, 2020
1 parent d56111c commit 5f751ba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private async Task RefreshCacheForReadOperationsAsync(CacheEvent.TokenTypes cach
_requestParams.Account,
hasStateChanged: false,
TokenCacheInternal.IsApplicationCache,
_requestParams.Account?.HomeAccountId?.Identifier);
_requestParams.SuggestedCacheKey ?? _requestParams.Account?.HomeAccountId?.Identifier);

try
{
Expand Down
10 changes: 7 additions & 3 deletions src/client/Microsoft.Identity.Client/ClientApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ internal ClientApplicationBase(ApplicationConfiguration config)
/// </summary>
public async Task<IEnumerable<IAccount>> GetAccountsAsync()
{
return await GetAccountsWithHomeAccountIdAsync(null).ConfigureAwait(false);
return await GetAccountsAndSetCacheKeyAsync(null).ConfigureAwait(false);
}

/// <summary>
/// Returns all the available <see cref="IAccount">accounts</see> in the user token cache for the application.
/// Also sets the cache key based on a given home account id, which is the account id of the home account for the user.
/// This uniquely identifies the user across AAD tenants.
/// </summary>
public async Task<IEnumerable<IAccount>> GetAccountsWithHomeAccountIdAsync(string homeAccountId)
/// <param name="homeAccountId">The identifier is the home account id of the account being targetted in the cache./>
/// </param>
private async Task<IEnumerable<IAccount>> GetAccountsAndSetCacheKeyAsync(string homeAccountId)
{
RequestContext requestContext = CreateRequestContext(Guid.NewGuid());
IEnumerable<IAccount> localAccounts = Enumerable.Empty<IAccount>();
Expand Down Expand Up @@ -161,7 +165,7 @@ public async Task<IEnumerable<IAccount>> GetAccountsAsync(string userFlow)
/// </param>
public async Task<IAccount> GetAccountAsync(string accountId)
{
var accounts = await GetAccountsWithHomeAccountIdAsync(accountId).ConfigureAwait(false);
var accounts = await GetAccountsAndSetCacheKeyAsync(accountId).ConfigureAwait(false);
return accounts.FirstOrDefault(account => account.HomeAccountId.Identifier.Equals(accountId, StringComparison.OrdinalIgnoreCase));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public async Task ConfidentialClientWithCertificateTestAsync()
MsalAssert.AssertAuthResult(authResult);
appCacheRecorder.AssertAccessCounts(1, 1);
Assert.IsTrue(appCacheRecorder.LastNotificationArgs.IsApplicationCache);
Assert.AreEqual("16dab2ba-145d-4b1b-8569-bf4b9aed4dc8_AppTokenCache", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);

// Call again to ensure token cache is hit
authResult = await confidentialApp
Expand All @@ -141,6 +142,7 @@ public async Task ConfidentialClientWithCertificateTestAsync()
MsalAssert.AssertAuthResult(authResult);
appCacheRecorder.AssertAccessCounts(2, 1);
Assert.IsTrue(appCacheRecorder.LastNotificationArgs.IsApplicationCache);
Assert.AreEqual("16dab2ba-145d-4b1b-8569-bf4b9aed4dc8_AppTokenCache", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);
}

[TestMethod]
Expand All @@ -166,6 +168,7 @@ public async Task ConfidentialClientWithRSACertificateTestAsync()
MsalAssert.AssertAuthResult(authResult);
appCacheRecorder.AssertAccessCounts(1, 1);
Assert.IsTrue(appCacheRecorder.LastNotificationArgs.IsApplicationCache);
Assert.AreEqual("16dab2ba-145d-4b1b-8569-bf4b9aed4dc8_AppTokenCache", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);

// Call again to ensure token cache is hit
authResult = await confidentialApp
Expand All @@ -176,6 +179,7 @@ public async Task ConfidentialClientWithRSACertificateTestAsync()
MsalAssert.AssertAuthResult(authResult);
appCacheRecorder.AssertAccessCounts(2, 1);
Assert.IsTrue(appCacheRecorder.LastNotificationArgs.IsApplicationCache);
Assert.AreEqual("16dab2ba-145d-4b1b-8569-bf4b9aed4dc8_AppTokenCache", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);
}

[TestMethod]
Expand Down Expand Up @@ -214,6 +218,7 @@ public async Task RunTestWithClientSecretAsync(string clientID, string authority
MsalAssert.AssertAuthResult(authResult);
appCacheRecorder.AssertAccessCounts(1, 1);
Assert.IsTrue(appCacheRecorder.LastNotificationArgs.IsApplicationCache);
Assert.AreEqual("16dab2ba-145d-4b1b-8569-bf4b9aed4dc8_AppTokenCache", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);

// Call again to ensure token cache is hit
authResult = await confidentialApp.AcquireTokenForClient(s_keyvaultScope)
Expand All @@ -223,6 +228,7 @@ public async Task RunTestWithClientSecretAsync(string clientID, string authority
MsalAssert.AssertAuthResult(authResult);
appCacheRecorder.AssertAccessCounts(2, 1);
Assert.IsTrue(appCacheRecorder.LastNotificationArgs.IsApplicationCache);
Assert.AreEqual("16dab2ba-145d-4b1b-8569-bf4b9aed4dc8_AppTokenCache", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);
}

[TestMethod]
Expand Down Expand Up @@ -299,6 +305,7 @@ public async Task ConfidentialClientWithSignedAssertionTestAsync()

appCacheRecorder.AssertAccessCounts(1, 1);
Assert.IsTrue(appCacheRecorder.LastNotificationArgs.IsApplicationCache);
Assert.AreEqual("16dab2ba-145d-4b1b-8569-bf4b9aed4dc8_AppTokenCache", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);
ValidateClaimsInAssertion(claims, ((ConfidentialClientApplication)confidentialApp).ClientCredential.SignedAssertion);
MsalAssert.AssertAuthResult(authResult);

Expand All @@ -309,6 +316,7 @@ public async Task ConfidentialClientWithSignedAssertionTestAsync()

appCacheRecorder.AssertAccessCounts(2, 1);
Assert.IsTrue(appCacheRecorder.LastNotificationArgs.IsApplicationCache);
Assert.AreEqual("16dab2ba-145d-4b1b-8569-bf4b9aed4dc8_AppTokenCache", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);
}

private void ValidateClaimsInAssertion(IDictionary<string, string> claims, string assertion)
Expand Down Expand Up @@ -566,11 +574,14 @@ private async Task RunOnBehalfOfTestAsync(LabResponse labResponse)
.WithTestLogging()
.Build();

var appCacheRecorder = confidentialApp.AppTokenCache.RecordAccess();

authResult = await confidentialApp.AcquireTokenOnBehalfOf(s_scopes, new UserAssertion(authResult.AccessToken))
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);

MsalAssert.AssertAuthResult(authResult, user);
//Assert.AreEqual("YKaBLu1qpbkeiYa3QRiHRD066w8xp31kaWKrDZdxCfo", appCacheRecorder.LastNotificationArgs.SuggestedCacheKey);
}
}
}

0 comments on commit 5f751ba

Please sign in to comment.