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);
        }
        public void DeleteRefreshToken_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);
            var rt1      = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId");
            var rt2      = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId2");
            var rt3      = TokenCacheHelper.CreateRefreshTokenItem("userAssertion2", "homeAccountId");

            // Assert: Delete on empty collection doesn't throw
            accessor.DeleteRefreshToken(rt1);

            accessor.SaveRefreshToken(rt1);
            accessor.SaveRefreshToken(rt2);
            accessor.SaveRefreshToken(rt3);

            Assert.AreEqual(3, accessor.GetAllRefreshTokens().Count);

            // Assert: Delete on existing item
            accessor.DeleteRefreshToken(rt1);

            Assert.AreEqual(2, accessor.GetAllRefreshTokens().Count);
        }
        public void NoSupportedMethods_AppCache_Test()
        {
            var accessor = new InMemoryPartitionedAppTokenCacheAccessor(new NullLogger(), null);

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           accessor.SaveRefreshToken(TokenCacheHelper.CreateRefreshTokenItem())
                                                           );

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           accessor.SaveIdToken(TokenCacheHelper.CreateIdTokenCacheItem())
                                                           );

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           accessor.SaveAccount(TokenCacheHelper.CreateAccountItem())
                                                           );

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           accessor.GetIdToken(TokenCacheHelper.CreateAccessTokenItem())
                                                           );

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           accessor.GetAccount(TokenCacheHelper.CreateAccountItem().GetKey())
                                                           );

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           accessor.DeleteRefreshToken(TokenCacheHelper.CreateRefreshTokenItem())
                                                           );

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           accessor.DeleteIdToken(TokenCacheHelper.CreateIdTokenCacheItem())
                                                           );

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           accessor.DeleteAccount(TokenCacheHelper.CreateAccountItem())
                                                           );

            Assert.AreEqual(0, accessor.GetAllRefreshTokens().Count);
            Assert.AreEqual(0, accessor.GetAllIdTokens().Count);
            Assert.AreEqual(0, accessor.GetAllAccounts().Count);
        }
        public void GetAllRefreshTokens_Test()
        {
            var    accessor      = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);
            var    rt1           = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId");
            var    rt2           = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId2");
            var    rt3           = TokenCacheHelper.CreateRefreshTokenItem("userAssertion2", "homeAccountId");
            string partitionKey1 = CacheKeyFactory.GetKeyFromCachedItem(rt1);
            string partitionKey2 = CacheKeyFactory.GetKeyFromCachedItem(rt3);

            // Assert: Returns empty collection
            Assert.AreEqual(0, accessor.GetAllRefreshTokens().Count);
            Assert.AreEqual(0, accessor.GetAllRefreshTokens(partitionKey1).Count);

            accessor.SaveRefreshToken(rt1);
            accessor.SaveRefreshToken(rt2);
            accessor.SaveRefreshToken(rt3);

            // Assert: Get all tokens and get all tokens by partition key
            Assert.AreEqual(3, accessor.GetAllRefreshTokens().Count);
            Assert.AreEqual(2, accessor.GetAllRefreshTokens(partitionKey1).Count);
            Assert.AreEqual(1, accessor.GetAllRefreshTokens(partitionKey2).Count);
        }
        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 SaveRefreshToken_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);

            var rt1 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId");

            // Assert: Saves with new item
            accessor.SaveRefreshToken(rt1);

            Assert.AreEqual(1, accessor.GetAllRefreshTokens().Count);
            Assert.AreEqual(1, accessor.RefreshTokenCacheDictionary.Count);
            string partitionKey1 = CacheKeyFactory.GetKeyFromCachedItem(rt1);

            Assert.IsNotNull(accessor.RefreshTokenCacheDictionary[partitionKey1][rt1.GetKey().ToString()]);

            var rt2 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId2");

            // Assert: Save under the existing partition
            accessor.SaveRefreshToken(rt2);

            Assert.AreEqual(2, accessor.GetAllRefreshTokens().Count);
            Assert.AreEqual(1, accessor.RefreshTokenCacheDictionary.Count);
            Assert.IsNotNull(accessor.RefreshTokenCacheDictionary[partitionKey1][rt2.GetKey().ToString()]);

            var rt3 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion2", "homeAccountId");

            // Assert: Save under a new partition
            accessor.SaveRefreshToken(rt3);
            // Assert: Save overwrites the existing token
            accessor.SaveRefreshToken(rt3);

            Assert.AreEqual(3, accessor.GetAllRefreshTokens().Count);
            Assert.AreEqual(2, accessor.RefreshTokenCacheDictionary.Count);
            string partitionKey2 = CacheKeyFactory.GetKeyFromCachedItem(rt3);

            Assert.IsNotNull(accessor.RefreshTokenCacheDictionary[partitionKey2][rt3.GetKey().ToString()]);
        }