public async Task Store_should_update_record_if_key_already_exists(ISessionFactory sessionFactory)
        {
            var persistedGrant = CreateTestObject();

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                await provider.Session.SaveAsync(_mapper.Map <Entities.PersistedGrant>(persistedGrant));
            }

            var newDate = persistedGrant.Expiration.Value.AddHours(1);

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var store = new PersistedGrantStore(provider);
                persistedGrant.Expiration = newDate;
                await store.StoreAsync(persistedGrant);
            }

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var foundGrant = await provider.Session.GetAsync <Entities.PersistedGrant>(persistedGrant.Key);

                Assert.NotNull(foundGrant);
                Assert.Equal(newDate, persistedGrant.Expiration);
            }
        }
        public async Task StoreAsync_WhenPersistedGrantStored_ExpectSuccess(ISessionFactory sessionFactory)
        {
            var persistedGrant = CreateTestObject();

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var store = new PersistedGrantStore(provider);
                await store.StoreAsync(persistedGrant);
            }

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var foundGrant = await provider.Session.GetAsync <Entities.PersistedGrant>(persistedGrant.Key);

                Assert.NotNull(foundGrant);
            }
        }
        public async Task GetAsync_WithKeyAndPersistedGrantExists_ExpectPersistedGrantReturned(ISessionFactory sessionFactory)
        {
            var persistedGrant = CreateTestObject();

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                await provider.Session.SaveAsync(_mapper.Map <Entities.PersistedGrant>(persistedGrant));
            }

            PersistedGrant foundPersistedGrant;

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var store = new PersistedGrantStore(provider);
                foundPersistedGrant = await store.GetAsync(persistedGrant.Key);
            }

            Assert.NotNull(foundPersistedGrant);
        }
        public async Task GetAsync_WithSubAndTypeAndPersistedGrantExists_ExpectPersistedGrantReturned(ISessionFactory sessionFactory)
        {
            var persistedGrant = CreateTestObject();

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                await provider.Session.SaveAsync(_mapper.Map <Entities.PersistedGrant>(persistedGrant));
            }

            IEnumerable <PersistedGrant> foundPersistedGrants;

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var store = new PersistedGrantStore(provider);
                foundPersistedGrants = await store.GetAllAsync(persistedGrant.SubjectId);
            }

            Assert.NotNull(foundPersistedGrants);
            Assert.NotEmpty(foundPersistedGrants);
        }
Пример #5
0
        public async Task ClearTokens_OnNonExpiredTokens_ExpectSome(ISessionFactory sessionFactory)
        {
            var svcs = new ServiceCollection();

            svcs.AddScoped(_ => new OperationalSessionProvider(sessionFactory.OpenSession));

            var tokenCleanup = new TokenCleanup(svcs.BuildServiceProvider());

            var baseDate = DateTime.UtcNow;

            // create test data
            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var grant = new PersistedGrant
                {
                    ClientId     = Guid.NewGuid().ToString(),
                    Key          = Guid.NewGuid().ToString(),
                    Type         = Guid.NewGuid().ToString(),
                    Data         = Guid.NewGuid().ToString(),
                    SubjectId    = Guid.NewGuid().ToString(),
                    CreationTime = baseDate.AddDays(2),
                    Expiration   = baseDate.AddMinutes(1)
                };
                await provider.Session.SaveAsync(grant);
            }

            // Clear tokens
            await tokenCleanup.ClearTokens(baseDate);

            // assert no data exists
            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var exists = provider.Session
                             .Query <PersistedGrant>()
                             .Any(x => x.Expiration >= baseDate);

                Assert.True(exists);
            }
        }
        public async Task RemoveAsync_WhenKeyOfExistingReceived_ExpectGrantDeleted(ISessionFactory sessionFactory)
        {
            var persistedGrant = CreateTestObject();

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                await provider.Session.SaveAsync(_mapper.Map <Entities.PersistedGrant>(persistedGrant));
            }

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var store = new PersistedGrantStore(provider);
                await store.RemoveAsync(persistedGrant.Key);
            }

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var foundGrant = await provider.Session.GetAsync <Entities.PersistedGrant>(persistedGrant.Key);

                Assert.Null(foundGrant);
            }
        }
        public async Task Store_should_create_new_record_if_key_does_not_exist(ISessionFactory sessionFactory)
        {
            var persistedGrant = CreateTestObject();

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var foundGrant = await provider.Session.GetAsync <Entities.PersistedGrant>(persistedGrant.Key);

                Assert.Null(foundGrant);
            }

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var store = new PersistedGrantStore(provider);
                await store.StoreAsync(persistedGrant);
            }

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var foundGrant = await provider.Session.GetAsync <Entities.PersistedGrant>(persistedGrant.Key);

                Assert.NotNull(foundGrant);
            }
        }