예제 #1
0
        public async Task ResourceStore_Identity_RemoveGetTest()
        {
            Stopwatch stopwatch = new Stopwatch();

            var storageContext = Services.BuildServiceProvider().GetService <ResourceStorageContext>();

            Assert.IsNotNull(storageContext);

            var store = new ResourceStore(storageContext, _logger);

            Assert.IsNotNull(store);

            var resource = new IdentityResources.Address();

            Console.WriteLine(JsonConvert.SerializeObject(resource));

            stopwatch.Start();
            await store.StoreAsync(resource);

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.StoreAsync({resource.Name})-identity: {stopwatch.ElapsedMilliseconds} ms");


            stopwatch.Reset();
            stopwatch.Start();
            var resources = await store.GetAllResourcesAsync();

            int count = resources.IdentityResources.Count();

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.GetAllResourcesAsync().IdentityResources.Count: {count} : {stopwatch.ElapsedMilliseconds} ms");
            Assert.IsTrue(count > 0);

            stopwatch.Reset();
            //Remove
            stopwatch.Start();

            await store.RemoveIdentityResourceAsync(resource.Name);

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.RemoveIdentityResourceAsync({resource.Name})-identity: {stopwatch.ElapsedMilliseconds} ms");

            stopwatch.Reset();
            stopwatch.Start();
            resources = await store.GetAllResourcesAsync();

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.GetAllResourcesAsync().IdentityResources.Count: {count} : {stopwatch.ElapsedMilliseconds} ms");
            Assert.IsNull(resources.IdentityResources.FirstOrDefault(f => f.Name == resource.Name));
        }
        public async Task GetAllResources_ShouldReturnAllResources()
        {
            var claims = A.ListOf <ApiResourceClaim>(2);
            var scopes = A.ListOf <ApiScope>();

            A.Configure <ApiResource>().Fill(x => x.UserClaims, claims);
            A.Configure <ApiResource>().Fill(x => x.Scopes, scopes);
            A.Configure <IdentityResource>().Fill(x => x.UserClaims, A.ListOf <IdentityClaim>(3));
            var resources    = A.ListOf <IdentityResource>(5);
            var apiResources = A.ListOf <ApiResource>(5);


            using (var session = db.Store.LightweightSession())
            {
                session.StoreObjects(resources);
                session.StoreObjects(apiResources);
                session.SaveChanges();
            }
            using (var session = db.Store.LightweightSession())
            {
                var store  = new ResourceStore(session);
                var result = await store.GetAllResourcesAsync();

                Assert.True(apiResources.Count <= result.ApiResources.Count);
                Assert.True(resources.Count <= result.IdentityResources.Count);
            }
        }
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden()
        {
            var storeHolder = GetConfigurationDocumentStoreHolder();

            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource
            {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            using (var session = storeHolder.OpenAsyncSession())
            {
                await session.StoreAsync(visibleIdentityResource.ToEntity());

                await session.StoreAsync(visibleApiResource.ToEntity());

                await session.StoreAsync(visibleApiScope.ToEntity());

                await session.StoreAsync(hiddenIdentityResource.ToEntity());

                await session.StoreAsync(hiddenApiResource.ToEntity());

                await session.StoreAsync(hiddenApiScope.ToEntity());

                await session.SaveChangesAsync();
            }

            WaitForIndexing(storeHolder.IntegrationTest_GetDocumentStore());
            WaitForUserToContinueTheTest(storeHolder.IntegrationTest_GetDocumentStore());

            var store     = new ResourceStore(storeHolder, FakeLogger <ResourceStore> .Create());
            var resources = await store.GetAllResourcesAsync();

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
예제 #4
0
        public async Task ResourceStore_Api_SaveGetTest()
        {
            Stopwatch stopwatch = new Stopwatch();

            var storageContext = Services.BuildServiceProvider().GetService <ResourceStorageContext>();

            Assert.IsNotNull(storageContext);

            var store = new ResourceStore(storageContext, _logger);

            Assert.IsNotNull(store);

            var resource = CreateApiTestObject();

            Console.WriteLine(JsonConvert.SerializeObject(resource));

            stopwatch.Start();
            await store.StoreAsync(resource);

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.StoreAsync({resource.Name})-api: {stopwatch.ElapsedMilliseconds} ms");

            stopwatch.Reset();
            stopwatch.Start();
            var findResource = (await store.FindApiResourcesByNameAsync(new string[] { resource.Name })).FirstOrDefault();

            stopwatch.Stop();
            Console.WriteLine($"{nameof(ResourceStore.FindApiResourcesByNameAsync)}({resource.Name})-api: {stopwatch.ElapsedMilliseconds} ms");
            Assert.AreEqual <string>(resource.Name, findResource.Name);

            stopwatch.Reset();
            stopwatch.Start();
            string[] findScopes          = new string[] { "api1Scope", Guid.NewGuid().ToString() };
            var      findScopesResources = await store.FindApiResourcesByScopeNameAsync(findScopes);

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.FindApiResourcesByScopeAsync({string.Join(",", findScopes)})-api: {stopwatch.ElapsedMilliseconds} ms");
            Assert.AreEqual <string>(resource.Name, findScopesResources.Single()?.Name);

            stopwatch.Reset();
            stopwatch.Start();
            var resources = await store.GetAllResourcesAsync();

            int count = resources.ApiResources.Count();

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.GetAllResourcesAsync().ApiResources.Count: {count} : {stopwatch.ElapsedMilliseconds} ms");
            Assert.IsTrue(count > 0);


            stopwatch.Reset();
            stopwatch.Start();
            string findScope = findScopes[0];
            var    apiScopes = await store.FindApiScopesByNameAsync(findScopes);

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.FindApiScopesByNameAsync({findScope})-api: {stopwatch.ElapsedMilliseconds} ms");
            Assert.AreEqual <int>(1, apiScopes.Where(w => w.Name == findScope).Count());
        }
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden(DbContextOptions <ConfigurationDbContext> options)
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.IdentityResources.Add(visibleIdentityResource.ToEntity());
                context.ApiResources.Add(visibleApiResource.ToEntity());
                context.ApiScopes.Add(visibleApiScope.ToEntity());

                context.IdentityResources.Add(hiddenIdentityResource.ToEntity());
                context.ApiResources.Add(hiddenApiResource.ToEntity());
                context.ApiScopes.Add(hiddenApiScope.ToEntity());

                context.SaveChanges();
            }

            Resources resources;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create());
                resources = await store.GetAllResourcesAsync();
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
예제 #6
0
        public async Task GetAllResourcesAsync_should_return_all_resources()
        {
            var store = new RavenDbTestDriverWrapper().GetDocumentStore();

            using var s1 = store.OpenAsyncSession();

            await s1.StoreAsync(new Entity.ProtectResource
            {
                Id        = "test",
                ApiScopes = new List <Entity.ApiApiScope>
                {
                    new Entity.ApiApiScope
                    {
                        Id = $"{nameof(Entity.ApiApiScope).ToLowerInvariant()}/test"
                    }
                }
            }, $"{nameof(Entity.ProtectResource).ToLowerInvariant()}/test");

            await s1.StoreAsync(new Entity.ApiApiScope
            {
                Id         = "test",
                ApiId      = "test",
                ApiScopeId = "test"
            }, $"{nameof(Entity.ApiApiScope).ToLowerInvariant()}/test");

            await s1.StoreAsync(new Entity.ApiScope
            {
                Id   = "test",
                Apis = new List <Entity.ApiApiScope>
                {
                    new Entity.ApiApiScope
                    {
                        Id = $"{nameof(Entity.ApiApiScope).ToLowerInvariant()}/test"
                    }
                }
            }, $"{nameof(Entity.ApiScope).ToLowerInvariant()}/test");

            await s1.StoreAsync(new Entity.IdentityResource
            {
                Id = "test",
            }, $"{nameof(Entity.IdentityResource).ToLowerInvariant()}/test");

            await s1.SaveChangesAsync();

            var sut = new ResourceStore(new ScopedAsynDocumentcSession(store.OpenAsyncSession()));

            var result = await sut.GetAllResourcesAsync();

            Assert.NotEmpty(result.IdentityResources);
            Assert.NotEmpty(result.ApiResources);
            Assert.NotEmpty(result.ApiScopes);
        }
        public void GetAllResourcesAsync_Success()
        {
            var resourceApiModel      = MockModels.ApiResource;
            var resourceIdentityModel = MockModels.IdentityResource;

            DatabaseHelper.AddEntryIntoDatabase(options, resourceApiModel);
            DatabaseHelper.AddEntryIntoDatabase(options, resourceIdentityModel);

            var response = store.GetAllResourcesAsync().Result;

            Assert.Single(response.ApiResources);
            Assert.Single(response.IdentityResources);
        }
예제 #8
0
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden()
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            await _context.IdentityResources.AddAsync(visibleIdentityResource.ToEntity());

            await _context.ApiResources.AddAsync(visibleApiResource.ToEntity());

            await _context.ApiScopes.AddAsync(visibleApiScope.ToEntity());

            await _context.IdentityResources.AddAsync(hiddenIdentityResource.ToEntity());

            await _context.ApiResources.AddAsync(hiddenApiResource.ToEntity());

            await _context.ApiScopes.AddAsync(hiddenApiScope.ToEntity());

            Resources resources;
            var       store = new ResourceStore(_context, FakeLogger <ResourceStore> .Create());

            resources = await store.GetAllResourcesAsync();

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
예제 #9
0
        public async Task ResourceStore_Api_RemoveGetTest()
        {
            Stopwatch stopwatch = new Stopwatch();

            var storageContext = Services.BuildServiceProvider().GetService <ResourceStorageContext>();

            Assert.IsNotNull(storageContext);

            var store = new ResourceStore(storageContext, _logger);

            Assert.IsNotNull(store);

            string name     = Guid.NewGuid().ToString("n");
            var    resource = CreateApiTestObject(name);

            Console.WriteLine(JsonConvert.SerializeObject(resource));

            stopwatch.Start();
            await store.StoreAsync(resource);

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.StoreAsync({resource.Name})-api: {stopwatch.ElapsedMilliseconds} ms");

            stopwatch.Reset();
            stopwatch.Start();
            var resources = await store.GetAllResourcesAsync();

            int count = resources.ApiResources.Count();

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.GetAllResourcesAsync().ApiResources.Count: {count} : {stopwatch.ElapsedMilliseconds} ms");
            Assert.IsNotNull(resources.ApiResources.FirstOrDefault(f => f.Name == name));

            //Remove
            stopwatch.Reset();
            stopwatch.Start();
            await store.RemoveApiResourceAsync(resource.Name);

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.StoreAsync({resource.Name})-api: {stopwatch.ElapsedMilliseconds} ms");


            stopwatch.Reset();
            stopwatch.Start();
            var findResource = (await store.FindApiResourcesByNameAsync(new string[] { resource.Name })).FirstOrDefault();

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.FindResourceByIdAsync({resource.Name})-api: {stopwatch.ElapsedMilliseconds} ms");
            Assert.IsNull(findResource);
        }
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden(ISessionFactory sessionFactory)
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = new List <Scope> {
                    new Scope {
                        Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
                    }
                }
            };

            using (var provider = new ConfigurationSessionProvider(sessionFactory.OpenSession))
            {
                await provider.Session.SaveAsync(_mapper.Map <Entities.IdentityResource>(visibleIdentityResource));

                await provider.Session.SaveAsync(_mapper.Map <Entities.ApiResource>(visibleApiResource));

                await provider.Session.SaveAsync(_mapper.Map <Entities.IdentityResource>(hiddenIdentityResource));

                await provider.Session.SaveAsync(_mapper.Map <Entities.ApiResource>(hiddenApiResource));
            }

            Resources resources;

            using (var provider = new ConfigurationSessionProvider(sessionFactory.OpenSession))
            {
                var store = new ResourceStore(provider);
                resources = await store.GetAllResourcesAsync();
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);

            Assert.Contains(resources.IdentityResources, x => !x.ShowInDiscoveryDocument);
            Assert.Contains(resources.ApiResources, x => !x.Scopes.Any(y => y.ShowInDiscoveryDocument));
        }
예제 #11
0
    public async Task Can_call_ResourceStore_GetAllResourcesAsync()
    => await ExecuteWithStrategyInTransactionAsync(
        async context =>
    {
        await SaveIdentityResources(context);
        await SaveApiScopes(context);
        await SaveApiResources(context);
    },
        async context =>
    {
        var store = new ResourceStore(context, new FakeLogger <ResourceStore>());

        var resources = await store.GetAllResourcesAsync();

        Assert.Equal(3, resources.ApiResources.Count);
        Assert.Equal(3, resources.ApiScopes.Count);
        Assert.Equal(3, resources.IdentityResources.Count);
    }
        );
예제 #12
0
        public async Task ResourceStore_Identity_SaveGetTest()
        {
            Stopwatch stopwatch = new Stopwatch();

            var storageContext = Services.BuildServiceProvider().GetService <ResourceStorageContext>();

            Assert.IsNotNull(storageContext);

            var store = new ResourceStore(storageContext, _logger);

            Assert.IsNotNull(store);

            foreach (Model.IdentityResource resource in GetIdentityResources())
            {
                Console.WriteLine(JsonConvert.SerializeObject(resource));

                stopwatch.Start();
                await store.StoreAsync(resource);

                stopwatch.Stop();
                Console.WriteLine($"ResourceStore.StoreAsync({resource.Name})-identity: {stopwatch.ElapsedMilliseconds} ms");

                stopwatch.Reset();
                stopwatch.Start();

                string[] findScopes          = new string[] { resource.Name, Guid.NewGuid().ToString() };
                var      findScopesResources = await store.FindIdentityResourcesByScopeNameAsync(findScopes);

                stopwatch.Stop();
                Console.WriteLine($"ResourceStore.FindIdentityResourcesByScopeAsync({resource.Name})-identity: {stopwatch.ElapsedMilliseconds} ms");
                Assert.AreEqual <string>(resource.Name, findScopesResources.Single()?.Name);
            }
            stopwatch.Reset();
            stopwatch.Start();
            var resources = await store.GetAllResourcesAsync();

            int count = resources.IdentityResources.Count();

            stopwatch.Stop();
            Console.WriteLine($"ResourceStore.GetAllResourcesAsync().IdentityResources.Count: {count} : {stopwatch.ElapsedMilliseconds} ms");
            Assert.AreEqual <int>(GetIdentityResources().Count(), count);
        }
예제 #13
0
        public void GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden(DbContextOptions <ConfigurationDbContext> options)
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = new List <Scope> {
                    new Scope {
                        Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
                    }
                }
            };

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.IdentityResources.Add(visibleIdentityResource.ToEntity());
                context.ApiResources.Add(visibleApiResource.ToEntity());
                context.IdentityResources.Add(hiddenIdentityResource.ToEntity());
                context.ApiResources.Add(hiddenApiResource.ToEntity());
                context.SaveChanges();
            }

            Resources resources;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create());
                resources = store.GetAllResourcesAsync().Result;
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);

            Assert.True(resources.IdentityResources.Any(x => !x.ShowInDiscoveryDocument));
            Assert.True(resources.ApiResources.Any(x => !x.Scopes.Any(y => y.ShowInDiscoveryDocument)));
        }
예제 #14
0
        public async Task Should_Retrieve_All_Resources_With_More_Than_One_Api_Scope(TestDatabase testDb)
        {
            var testIdentityResource = CreateTestIdentityResource("identity_resource_1");
            var testApiResource      = CreateTestApiResource("test_api_resource_1", new[] { "user_claim1", "user_claim2" });
            var testApiScope1        = CreateTestApiScopeForResource("user_scope1", new[] { "user_scope1_claim1", "user_scope1_claim2" });
            var testApiScope2        = CreateTestApiScopeForResource("user_scope2", new[] { "user_scope2_claim1" });

            testApiResource.Scopes.Add(testApiScope1.Name);
            testApiResource.Scopes.Add(testApiScope2.Name);

            using (var session = testDb.OpenSession())
            {
                await session.SaveAsync(testIdentityResource.ToEntity());

                await session.SaveAsync(testApiResource.ToEntity());

                await session.SaveAsync(testApiScope1.ToEntity());

                await session.SaveAsync(testApiScope2.ToEntity());

                await session.FlushAsync();
            }

            var       loggerMock = new Mock <ILogger <ResourceStore> >();
            Resources resources;

            using (var session = testDb.OpenStatelessSession())
            {
                var store = new ResourceStore(session, loggerMock.Object);
                resources = await store.GetAllResourcesAsync();
            }

            resources.Should().NotBeNull();
            resources.IdentityResources.Should().NotBeEmpty();
            resources.IdentityResources.Count.Should().Be(1);
            resources.ApiResources.Should().NotBeEmpty();
            resources.ApiResources.Count.Should().Be(1);
            resources.ApiResources.First().Scopes.Count.Should().Be(2);

            await CleanupTestDataAsync(testDb);
        }
예제 #15
0
        public async Task Should_Retrieve_All_Resources_Including_Hidden_Ones(TestDatabase testDb)
        {
            var visibleIdentityResource = CreateTestIdentityResource("identity_visible");
            var visibleApiResource      = CreateTestApiResource("api_visible", new[] { Guid.NewGuid().ToString() });
            var visibleApiScope         = CreateTestApiScopeForResource("api_visible_scope", new[] { Guid.NewGuid().ToString() });

            visibleApiResource.Scopes.Add(visibleApiScope.Name);
            var hiddenIdentityResource = new IdentityResource()
            {
                Name = "identity_hidden",
                ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource()
            {
                Name   = "api_hidden",
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope()
            {
                Name = "api_scope_hidden",
                ShowInDiscoveryDocument = false
            };

            using (var session = testDb.OpenSession())
            {
                await session.SaveAsync(visibleIdentityResource.ToEntity());

                await session.SaveAsync(visibleApiResource.ToEntity());

                await session.SaveAsync(visibleApiScope.ToEntity());

                await session.SaveAsync(hiddenIdentityResource.ToEntity());

                await session.SaveAsync(hiddenApiResource.ToEntity());

                await session.SaveAsync(hiddenApiScope.ToEntity());

                await session.FlushAsync();
            }

            var       loggerMock = new Mock <ILogger <ResourceStore> >();
            Resources resources;

            using (var session = testDb.OpenStatelessSession())
            {
                var store = new ResourceStore(session, loggerMock.Object);
                resources = await store.GetAllResourcesAsync();
            }

            resources.Should().NotBeNull();
            resources.IdentityResources.Should().NotBeEmpty();
            resources.IdentityResources.Count.Should().Be(2);
            resources.ApiResources.Should().NotBeEmpty();
            resources.ApiResources.Count.Should().Be(2);
            resources.ApiScopes.Should().NotBeEmpty();
            resources.ApiScopes.Count.Should().Be(2);

            resources.IdentityResources.Any(ir => ir.Name == visibleIdentityResource.Name).Should().BeTrue();
            resources.IdentityResources.Any(ir => ir.Name == hiddenIdentityResource.Name).Should().BeTrue();

            resources.ApiResources.Any(ar => ar.Name == visibleApiResource.Name).Should().BeTrue();
            resources.ApiResources.Any(ar => ar.Name == hiddenApiResource.Name).Should().BeTrue();

            resources.ApiScopes.Any(s => s.Name == visibleApiScope.Name).Should().BeTrue();
            resources.ApiScopes.Any(s => s.Name == hiddenApiScope.Name).Should().BeTrue();

            await CleanupTestDataAsync(testDb);
        }
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden()
        {
            using var ravenStore = GetDocumentStore();
            await new ApiResourceIndex().ExecuteAsync(ravenStore);

            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource
            {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            using (var session = ravenStore.OpenSession())
            {
                session.Store(visibleIdentityResource.ToEntity());
                session.Store(visibleApiResource.ToEntity());
                session.Store(visibleApiScope.ToEntity());

                session.Store(hiddenIdentityResource.ToEntity());
                session.Store(hiddenApiResource.ToEntity());
                session.Store(hiddenApiScope.ToEntity());

                session.SaveChanges();
            }

            WaitForIndexing(ravenStore);
            WaitForUserToContinueTheTest(ravenStore);

            Resources resources;

            using (var session = ravenStore.OpenAsyncSession())
            {
                var store = new ResourceStore(session, FakeLogger <ResourceStore> .Create());
                resources = await store.GetAllResourcesAsync();
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
예제 #17
0
        public async Task TestCreate()
        {
            var context = GetConfigContext();

            var clientCreate1 = Mapper.ToEntity(new IdentityServer4.Models.Client
            {
                ClientId          = "Client1",
                ClientName        = "Client 1",
                AllowedGrantTypes = new List <string> {
                    "client_credentials"
                }
            });

            var clientCreate2 = Mapper.ToEntity(new IdentityServer4.Models.Client
            {
                ClientId          = "Client2",
                ClientName        = "Client 2",
                Enabled           = true,
                AllowedGrantTypes = new List <string> {
                    "authorization_code"
                }
            });

            var apiCreate1 = Mapper.ToEntity(new IdentityServer4.Models.ApiResource
            {
                Name        = "123",
                Enabled     = true,
                DisplayName = "123",
                Scopes      = new List <IdentityServer4.Models.Scope> {
                    new IdentityServer4.Models.Scope {
                        Name = "Stuff"
                    }
                }
            });

            try
            {
                await context.Client.InsertOneAsync(clientCreate1);

                await context.Client.InsertOneAsync(clientCreate2);

                await context.ApiResource.InsertOneAsync(apiCreate1);

                var clientStore = new ClientStore(context);
                var client1     = await clientStore.FindClientByIdAsync("Client1");

                Assert.IsNotNull(client1?.ClientId);
                Assert.AreEqual("Client1", client1.ClientId);
                Assert.AreEqual("Client 1", client1.ClientName);
                var client2 = await clientStore.FindClientByIdAsync("Client2");

                Assert.IsNotNull(client2?.ClientId);
                Assert.AreEqual("Client2", client2.ClientId);
                Assert.AreEqual("Client 2", client2.ClientName);
                var resStore  = new ResourceStore(context);
                var resources = await resStore.GetAllResourcesAsync();

                Assert.AreEqual(1, resources.ApiResources.Count);
                Assert.AreEqual(0, resources.IdentityResources.Count);
                Assert.AreEqual("123", resources.ApiResources.FirstOrDefault().Name);
                var client3 = await clientStore.FindClientByIdAsync("NotPresent"); //Test non-existant client which previously caused a null reference exception

                Assert.IsNull(client3);
            }
            finally
            {
                try
                {
                    await context.Client.DeleteOneAsync(Builders <Client> .Filter.Eq(c => c.ClientId, "Client1"));

                    await context.Client.DeleteOneAsync(Builders <Client> .Filter.Eq(c => c.ClientId, "Client2"));

                    await context.ApiResource.DeleteOneAsync(Builders <ApiResource> .Filter.Eq(api => api.Name, "123"));
                }
                catch { }
            }
        }
예제 #18
0
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden()
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            var identityResourcesRepo = g.configurationDb.GetRepository <Storage.Entities.IdentityResource>();
            var apiResourcesRepo      = g.configurationDb.GetRepository <Storage.Entities.ApiResource>();
            var apiScopesRepo         = g.configurationDb.GetRepository <Storage.Entities.ApiScope>();

            var visibleIdentityResourceEntity = visibleIdentityResource.ToEntity();

            identityResourcesRepo.Insert(visibleIdentityResourceEntity);
            identityResourcesRepo.SaveMany(visibleIdentityResourceEntity, "UserClaims");
            identityResourcesRepo.SaveMany(visibleIdentityResourceEntity, "Properties");

            var visibleApiResourceEntity = visibleApiResource.ToEntity();

            apiResourcesRepo.Insert(visibleApiResourceEntity);
            apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Secrets");
            apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Scopes");
            apiResourcesRepo.SaveMany(visibleApiResourceEntity, "UserClaims");
            apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Properties");

            var visibleApiScopeEntity = visibleApiScope.ToEntity();

            apiScopesRepo.Insert(visibleApiScopeEntity);
            apiScopesRepo.SaveMany(visibleApiScopeEntity, "UserClaims");
            apiScopesRepo.SaveMany(visibleApiScopeEntity, "Properties");

            var hiddenIdentityResourceEntity = hiddenIdentityResource.ToEntity();

            identityResourcesRepo.Insert(hiddenIdentityResourceEntity);
            identityResourcesRepo.SaveMany(hiddenIdentityResourceEntity, "UserClaims");
            identityResourcesRepo.SaveMany(hiddenIdentityResourceEntity, "Properties");

            var hiddenApiResourceEntity = hiddenApiResource.ToEntity();

            apiResourcesRepo.Insert(hiddenApiResourceEntity);
            apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Secrets");
            apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Scopes");
            apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "UserClaims");
            apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Properties");

            var hiddenApiScopeEntity = hiddenApiScope.ToEntity();

            apiScopesRepo.Insert(hiddenApiScopeEntity);
            apiScopesRepo.SaveMany(hiddenApiScopeEntity, "UserClaims");
            apiScopesRepo.SaveMany(hiddenApiScopeEntity, "Properties");


            Resources resources;

            var store = new ResourceStore(g.configurationDb, FakeLogger <ResourceStore> .Create());

            resources = await store.GetAllResourcesAsync();


            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }