예제 #1
0
        public async Task RemoveAsync()
        {
            //Arrange
            var sut      = new TokenHandleStore(NhibernateSession, ScopeStoreMock.Object, ClientStoreMock.Object);
            var testKey  = Guid.NewGuid().ToString();
            var testCode = ObjectCreator.GetTokenHandle();

            var tokenHandle = new Token
            {
                Key       = testKey,
                SubjectId = testCode.SubjectId,
                ClientId  = testCode.ClientId,
                JsonCode  = ConvertToJson(testCode),
                Expiry    = DateTime.UtcNow.AddSeconds(testCode.Client.AuthorizationCodeLifetime),
                TokenType = TokenType.TokenHandle
            };

            ExecuteInTransaction(session =>
            {
                session.Save(tokenHandle);
            });

            //Act
            await sut.RemoveAsync(testKey);

            ExecuteInTransaction(session =>
            {
                //Assert
                var token = session.Query <Token>()
                            .SingleOrDefault(t => t.TokenType == TokenType.TokenHandle &&
                                             t.Key == testKey);

                Assert.Null(token);
            });
        }
        public async Task TestCreateTokenHandleAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            int i      = 0;
            var claims = new List <Claim>()
            {
                new Claim("Type 0:" + i, "Value 0:" + i),
                new Claim("Type 1:" + i, "Value 1:" + i)
            };
            var json = JsonConvert.SerializeObject(claims);

            var insert = await CassandraTestHelper.InsertTestData_Clients(1);

            var flat = new FlattenedTokenHandle
            {
                Key          = Guid.NewGuid().ToString(),
                Audience     = "Audience:" + i,
                Claims       = JsonConvert.SerializeObject(claims),
                ClientId     = insert[0].ClientId,
                CreationTime = DateTimeOffset.UtcNow,
                Expires      = DateTimeOffset.UtcNow,
                Issuer       = "Issuer:" + i,
                Lifetime     = 1,
                SubjectId    = "SubjectId:" + i,
                Type         = "Type:" + i,
                Version      = 1
            };
            IClientStore      cs  = new ClientStore();
            ITokenHandleStore ths = new TokenHandleStore();
            var result            = await dao.CreateTokenHandleAsync(flat);

            var result_of_find = await dao.FindTokenByKey(flat.Key, cs);

            Token tt = result_of_find;

            Assert.AreEqual(flat.ClientId, result_of_find.ClientId);

            var newKey = Guid.NewGuid().ToString();
            await ths.StoreAsync(newKey, tt);

            result_of_find = await ths.GetAsync(newKey);

            Assert.AreEqual(flat.ClientId, result_of_find.ClientId);

            await ths.RemoveAsync(newKey);

            result_of_find = await ths.GetAsync(newKey);

            Assert.AreEqual(result_of_find, null);
        }
        public void TestRemoveAsync()
        {
            var  insert = InsertTestData(_clientStore, _scopeStore, _tokenHandleStore, 1);
            Guid id     = insert[0].Id;
            var  key    = insert[0].Record.Key;
            var  result = _tokenHandleStore.GetAsync(key);

            Assert.IsNotNull(result.Result);
            Assert.AreEqual(result.Result.ClientId, insert[0].Record.ClientId);

            _tokenHandleStore.RemoveAsync(key);
            result = _tokenHandleStore.GetAsync(key);

            Assert.IsNull(result.Result);
        }
        public async Task TestRemoveAsync()
        {
            var insert = await CassandraTestHelper.InsertTestData_Tokens(1);

            ITokenHandleStore ths = new TokenHandleStore();
            var subjectId         = insert[0].SubjectId;
            var clientId          = insert[0].ClientId;
            var key           = insert[0].Key;
            var find_metadata = await ths.GetAllAsync(subjectId);

            Assert.AreEqual(find_metadata.Count(), insert.Count);

            await ths.RemoveAsync(key);

            find_metadata = await ths.GetAllAsync(subjectId);

            Assert.AreEqual(find_metadata.Count(), 0);
        }
        public void RemoveAsync_WhenCalled_ExpectAction()
        {
            // Arrange
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();
            mockCacheConfiguration.Setup(r => r.Get).Returns(
                new RedisCacheConfigurationEntity
                {
                    CacheDuration = 10,
                    RedisCacheDefaultPrefix = "DEFAULT",
                    UseObjectCompression = false
                });

            var jsonSettingsFactory = new JsonSettingsFactory(new CustomMappersConfiguration());

            var cacheManager = new RedisCacheManager<Token>(
                RedisHelpers.ConnectionMultiplexer,
                mockCacheConfiguration.Object,
                jsonSettingsFactory.Create());

            var tokenStore = new TokenHandleStore(
                cacheManager,
                mockCacheConfiguration.Object);

            // Act
            var stopwatch = Stopwatch.StartNew();

            tokenStore.RemoveAsync("Delete").Wait();

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var redisValue = RedisHelpers.ConnectionMultiplexer.GetDatabase().StringGet("DEFAULT_RTS_Delete");

            Assert.That(redisValue.HasValue, Is.False);
        }
        public void RemoveAsync_WhenCalled_ExpectAction()
        {
            // Arrange
            var mockCacheManager = new Mock<ICacheManager<Token>>();
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();

            mockCacheConfiguration.Setup(r => r.Get).Returns(new RedisCacheConfigurationEntity { CacheDuration = 1 });

            var keyCallback = default(string);
            mockCacheManager.Setup(r => r.DeleteAsync(It.IsAny<string>()))
                .Callback((string s) => keyCallback = s)
                .Returns(Task.FromResult(0));

            var tokenHandleStore = new TokenHandleStore(
                mockCacheManager.Object,
                mockCacheConfiguration.Object);

            // Act
            var stopwatch = Stopwatch.StartNew();

            tokenHandleStore.RemoveAsync("string").Wait();

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(keyCallback, Is.EqualTo("THS_string"));
            mockCacheManager.Verify(r => r.DeleteAsync(It.IsAny<string>()), Times.Once());
        }