Esempio n. 1
0
        private ITokenCacheAccessor CreateTokenCacheAccessorWithKeyPrefix(
            string keyPrefix,
            int numAccessTokens,
            int numRefreshTokens,
            int numIdTokens,
            int numAccounts)
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(Substitute.For <ICoreLogger>(), null);

            for (int i = 1; i <= numAccessTokens; i++)
            {
                var item = CreateAccessTokenItem(
                    cachedAt: DateTimeHelpers.UnixTimestampToDateTime(34567),
                    expiresOn: DateTimeHelpers.UnixTimestampToDateTime(12345),
                    extendedExpiresOn: DateTimeHelpers.UnixTimestampToDateTime(23456));

                item.Environment = item.Environment + $"_{keyPrefix}{i}"; // ensure we get unique cache keys
                accessor.SaveAccessToken(item);
            }

            for (int i = 1; i <= numRefreshTokens; i++)
            {
                var item = CreateRefreshTokenItem();
                item.Environment = item.Environment + $"_{keyPrefix}{i}"; // ensure we get unique cache keys
                accessor.SaveRefreshToken(item);
            }

            // Create an FRT
            var frt = CreateRefreshTokenItem(true);

            frt.FamilyId = "1";
            accessor.SaveRefreshToken(frt);

            for (int i = 1; i <= numIdTokens; i++)
            {
                var item = CreateIdTokenItem();
                item.Environment = item.Environment + $"_{keyPrefix}{i}"; // ensure we get unique cache keys
                accessor.SaveIdToken(item);
            }

            for (int i = 1; i <= numAccounts; i++)
            {
                var item = CreateAccountItem();
                item.Environment = item.Environment + $"_{keyPrefix}{i}"; // ensure we get unique cache keys
                accessor.SaveAccount(item);
            }

            accessor.SaveAppMetadata(new MsalAppMetadataCacheItem(TestConstants.ClientId, "env_1", "1"));
            accessor.SaveAppMetadata(new MsalAppMetadataCacheItem(TestConstants.ClientId, "env_2", ""));
            accessor.SaveAppMetadata(new MsalAppMetadataCacheItem(TestConstants.ClientId2, "env_1", "another_family"));

            return(accessor);
        }
        public void HasAccessOrRefreshTokens_UserCache_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);

            // Assert: false with empty collection
            Assert.IsFalse(accessor.HasAccessOrRefreshTokens());

            accessor.SaveAccessToken(TokenCacheHelper.CreateAccessTokenItem(isExpired: true));

            // Assert: false with expired access token
            Assert.IsFalse(accessor.HasAccessOrRefreshTokens());

            accessor.SaveAccessToken(TokenCacheHelper.CreateAccessTokenItem());

            // Assert: true with access token
            Assert.IsTrue(accessor.HasAccessOrRefreshTokens());

            accessor.Clear();
            accessor.SaveRefreshToken(TokenCacheHelper.CreateRefreshTokenItem());

            // Assert: true with valid refresh token
            Assert.IsTrue(accessor.HasAccessOrRefreshTokens());
        }
        public void ClearCache_UserCache_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);

            accessor.SaveAccessToken(TokenCacheHelper.CreateAccessTokenItem());
            accessor.SaveRefreshToken(TokenCacheHelper.CreateRefreshTokenItem());
            accessor.SaveIdToken(TokenCacheHelper.CreateIdTokenCacheItem());
            accessor.SaveAccount(TokenCacheHelper.CreateAccountItem());

            Assert.AreEqual(1, accessor.AccessTokenCacheDictionary.Count);
            Assert.AreEqual(1, accessor.RefreshTokenCacheDictionary.Count);
            Assert.AreEqual(1, accessor.IdTokenCacheDictionary.Count);
            Assert.AreEqual(1, accessor.AccountCacheDictionary.Count);

            accessor.Clear();

            Assert.AreEqual(0, accessor.AccessTokenCacheDictionary.Count);
            Assert.AreEqual(0, accessor.RefreshTokenCacheDictionary.Count);
            Assert.AreEqual(0, accessor.IdTokenCacheDictionary.Count);
            Assert.AreEqual(0, accessor.AccountCacheDictionary.Count);
        }