From 7883d6b2627a8b4152c810c95bf8dd06ad5b4e91 Mon Sep 17 00:00:00 2001 From: pmaytak <34331512+pmaytak@users.noreply.github.com> Date: Tue, 5 Jan 2021 02:12:14 -0800 Subject: [PATCH] Add unit tests. --- .../Microsoft.Identity.Client/TokenCache.cs | 2 +- ...nfidentialClientApplicationBuilderTests.cs | 12 ++++++ .../CacheTests/TokenCacheTests.cs | 39 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/client/Microsoft.Identity.Client/TokenCache.cs b/src/client/Microsoft.Identity.Client/TokenCache.cs index be3413f815..8321e4a700 100644 --- a/src/client/Microsoft.Identity.Client/TokenCache.cs +++ b/src/client/Microsoft.Identity.Client/TokenCache.cs @@ -42,7 +42,7 @@ public sealed partial class TokenCache : ITokenCacheInternal private ICoreLogger Logger => ServiceBundle.DefaultLogger; internal IServiceBundle ServiceBundle { get; } - internal ILegacyCachePersistence LegacyCachePersistence { get; } + internal ILegacyCachePersistence LegacyCachePersistence { get; set; } internal string ClientId => ServiceBundle.Config.ClientId; ITokenCacheAccessor ITokenCacheInternal.Accessor => _accessor; diff --git a/tests/Microsoft.Identity.Test.Unit/AppConfigTests/ConfidentialClientApplicationBuilderTests.cs b/tests/Microsoft.Identity.Test.Unit/AppConfigTests/ConfidentialClientApplicationBuilderTests.cs index 5282eeb865..25e89dcfb0 100644 --- a/tests/Microsoft.Identity.Test.Unit/AppConfigTests/ConfidentialClientApplicationBuilderTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/AppConfigTests/ConfidentialClientApplicationBuilderTests.cs @@ -338,5 +338,17 @@ public void TestConstructor_BadInstanceMetadata() Assert.AreEqual(ex.ErrorCode, MsalError.InvalidUserInstanceMetadata); } + + [TestMethod] + public void TestConstructor_WithAdalCacheCompatibility() + { + var cca = ConfidentialClientApplicationBuilder + .Create(TestConstants.ClientId) + .WithClientSecret(TestConstants.ClientSecret) + .WithAdalCacheCompatibility(true) + .Build(); + + Assert.AreEqual(true, cca.AppConfig.AdalCacheCompatibilityEnabled); + } } } diff --git a/tests/Microsoft.Identity.Test.Unit/CacheTests/TokenCacheTests.cs b/tests/Microsoft.Identity.Test.Unit/CacheTests/TokenCacheTests.cs index d69e2f93da..3a8d699723 100644 --- a/tests/Microsoft.Identity.Test.Unit/CacheTests/TokenCacheTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/CacheTests/TokenCacheTests.cs @@ -44,6 +44,45 @@ public override void TestInitialize() base.TestInitialize(); } + [DataTestMethod] + [DataRow(true)] + [DataRow(false)] + public async Task WithAdalCacheCompatibilityTest_Async(bool enableAdalCacheCompatibility) + { + // Arrange + var legacyCachePersistence = Substitute.For(); + var serviceBundle = TestCommon.CreateServiceBundleWithCustomHttpManager(null, isAdalCacheEnabled: enableAdalCacheCompatibility); + var requestContext = new RequestContext(serviceBundle, Guid.NewGuid()); + var response = TestConstants.CreateMsalTokenResponse(); + + ITokenCacheInternal cache = new TokenCache(serviceBundle, false); + ((TokenCache)cache).LegacyCachePersistence = legacyCachePersistence; + + var requestParams = TestCommon.CreateAuthenticationRequestParameters(serviceBundle); + requestParams.TenantUpdatedCanonicalAuthority = Authority.CreateAuthorityWithTenant( + requestParams.AuthorityInfo, + TestConstants.Utid); + requestParams.Account = new Account(TestConstants.s_userIdentifier, $"1{TestConstants.DisplayableId}", TestConstants.ProductionPrefNetworkEnvironment); + + // Act + await cache.FindRefreshTokenAsync(requestParams).ConfigureAwait(true); + await cache.SaveTokenResponseAsync(requestParams, response).ConfigureAwait(true); + await cache.GetAccountsAsync(requestParams).ConfigureAwait(true); + await cache.RemoveAccountAsync(requestParams.Account, requestContext).ConfigureAwait(true); + + // Assert + if (enableAdalCacheCompatibility) + { + legacyCachePersistence.ReceivedWithAnyArgs().LoadCache(); + legacyCachePersistence.ReceivedWithAnyArgs().WriteCache(Arg.Any()); + } + else + { + legacyCachePersistence.DidNotReceiveWithAnyArgs().LoadCache(); + legacyCachePersistence.DidNotReceiveWithAnyArgs().WriteCache(Arg.Any()); + } + } + [TestMethod] public void GetExactScopesMatchedAccessTokenTest() {