public void GetIdToken_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);
            var idt1     = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId");
            var idt2     = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId2");
            var idt3     = TokenCacheHelper.CreateIdTokenCacheItem("tenant2", "homeAccountId");
            var at2      = TokenCacheHelper.CreateAccessTokenItem("scope1", "tenant1", "homeAccountId2");
            var at3      = TokenCacheHelper.CreateAccessTokenItem("scope1", "tenant2", "homeAccountId");

            // Assert: Null non-existing item
            Assert.IsNull(accessor.GetIdToken(at2));

            accessor.SaveIdToken(idt1);
            accessor.SaveIdToken(idt2);
            accessor.SaveIdToken(idt3);

            // Assert: Get token by key
            Assert.AreEqual(idt2.GetKey(), accessor.GetIdToken(at2).GetKey());
            Assert.AreEqual(idt3.GetKey(), accessor.GetIdToken(at3).GetKey());
        }
        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 DeleteIdToken_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);
            var idt1     = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId");
            var idt2     = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId2");
            var idt3     = TokenCacheHelper.CreateIdTokenCacheItem("tenant2", "homeAccountId");

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

            accessor.SaveIdToken(idt1);
            accessor.SaveIdToken(idt2);
            accessor.SaveIdToken(idt3);

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

            // Assert: Delete on existing item
            accessor.DeleteIdToken(idt1);

            Assert.AreEqual(2, accessor.GetAllIdTokens().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 GetAllIdTokens_Test()
        {
            var    accessor      = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);
            var    idt1          = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId");
            var    idt2          = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId2");
            var    idt3          = TokenCacheHelper.CreateIdTokenCacheItem("tenant2", "homeAccountId");
            string partitionKey1 = CacheKeyFactory.GetKeyFromCachedItem(idt1);
            string partitionKey2 = CacheKeyFactory.GetKeyFromCachedItem(idt2);

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

            accessor.SaveIdToken(idt1);
            accessor.SaveIdToken(idt2);
            accessor.SaveIdToken(idt3);

            // Assert: Get all tokens and get all tokens by partition key
            Assert.AreEqual(3, accessor.GetAllIdTokens().Count);
            Assert.AreEqual(2, accessor.GetAllIdTokens(partitionKey1).Count);
            Assert.AreEqual(1, accessor.GetAllIdTokens(partitionKey2).Count);
        }
        public void SaveIdToken_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);

            var idt1 = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId");

            // Assert: Saves with new item
            accessor.SaveIdToken(idt1);

            Assert.AreEqual(1, accessor.GetAllIdTokens().Count);
            Assert.AreEqual(1, accessor.IdTokenCacheDictionary.Count);
            string partitionKey1 = CacheKeyFactory.GetKeyFromCachedItem(idt1);

            Assert.IsNotNull(accessor.IdTokenCacheDictionary[partitionKey1][idt1.GetKey().ToString()]);

            var idt2 = TokenCacheHelper.CreateIdTokenCacheItem("tenant2", "homeAccountId");

            // Assert: Save under the existing partition
            accessor.SaveIdToken(idt2);

            Assert.AreEqual(2, accessor.GetAllIdTokens().Count);
            Assert.AreEqual(1, accessor.IdTokenCacheDictionary.Count);
            Assert.IsNotNull(accessor.IdTokenCacheDictionary[partitionKey1][idt2.GetKey().ToString()]);

            var idt3 = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId2");

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

            Assert.AreEqual(3, accessor.GetAllIdTokens().Count);
            Assert.AreEqual(2, accessor.IdTokenCacheDictionary.Count);
            string partitionKey2 = CacheKeyFactory.GetKeyFromCachedItem(idt3);

            Assert.IsNotNull(accessor.IdTokenCacheDictionary[partitionKey2][idt3.GetKey().ToString()]);
        }