public async Task FindApiResourcesByScopeNameAsync_WhenResourcesExist_ExpectResourcesReturned()
        {
            using var ravenStore = GetDocumentStore();
            await new ApiResourceIndex().ExecuteAsync(ravenStore);

            var testApiResource = CreateApiResourceTestResource();
            var testApiScope    = CreateApiScopeTestResource();

            testApiResource.Scopes.Add(testApiScope.Name);

            using (var session = ravenStore.OpenSession())
            {
                session.Store(testApiResource.ToEntity());
                //session.Store(testApiScope.ToEntity());
                session.SaveChanges();
            }

            WaitForIndexing(ravenStore);

            IEnumerable <ApiResource> resources;

            using (var session = ravenStore.OpenAsyncSession())
            {
                var store = new ResourceStore(session, FakeLogger <ResourceStore> .Create());
                resources = await store.FindApiResourcesByScopeNameAsync(new List <string>
                {
                    testApiScope.Name
                });
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources);
            Assert.NotNull(resources.Single(x => x.Name == testApiResource.Name));
        }
        public async Task FindApiResourcesByScopeNameAsync_WhenResourcesExist_ExpectOnlyResourcesRequestedReturned(DbContextOptions <ConfigurationDbContext> options)
        {
            var testIdentityResource = CreateIdentityTestResource();
            var testApiResource      = CreateApiResourceTestResource();
            var testApiScope         = CreateApiScopeTestResource();

            testApiResource.Scopes.Add(testApiScope.Name);

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.IdentityResources.Add(testIdentityResource.ToEntity());
                context.ApiResources.Add(testApiResource.ToEntity());
                context.ApiScopes.Add(testApiScope.ToEntity());
                context.IdentityResources.Add(CreateIdentityTestResource().ToEntity());
                context.ApiResources.Add(CreateApiResourceTestResource().ToEntity());
                context.ApiScopes.Add(CreateApiScopeTestResource().ToEntity());
                context.SaveChanges();
            }

            IEnumerable <ApiResource> resources;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create());
                resources = await store.FindApiResourcesByScopeNameAsync(new[] { testApiScope.Name });
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources);
            Assert.NotNull(resources.Single(x => x.Name == testApiResource.Name));
        }
        public async Task FindApiResourcesByScopeNameAsync_WhenResourcesExist_ExpectOnlyResourcesRequestedReturned()
        {
            var storeHolder = GetConfigurationDocumentStoreHolder();

            var testIdentityResource = CreateIdentityTestResource();
            var testApiResource      = CreateApiResourceTestResource();
            var testApiScope         = CreateApiScopeTestResource();

            testApiResource.Scopes.Add(testApiScope.Name);

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

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

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

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

                await session.SaveChangesAsync();
            }

            WaitForIndexing(storeHolder.IntegrationTest_GetDocumentStore());

            var store     = new ResourceStore(storeHolder, FakeLogger <ResourceStore> .Create());
            var resources = await store.FindApiResourcesByScopeNameAsync(new[] { testApiScope.Name });

            Assert.NotNull(resources);
            Assert.NotEmpty(resources);
            Assert.NotNull(resources.Single(x => x.Name == testApiResource.Name));
        }
Ejemplo n.º 4
0
        public async Task FindApiResourcesByScopeNameAsync_WhenResourcesExist_ExpectOnlyResourcesRequestedReturned()
        {
            var testIdentityResource = CreateIdentityTestResource();
            var testApiResource      = CreateApiResourceTestResource();
            var testApiScope         = CreateApiScopeTestResource();

            testApiResource.Scopes.Add(testApiScope.Name);

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

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

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

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

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

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

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

            resources = await store.FindApiResourcesByScopeNameAsync(new[] { testApiScope.Name });

            Assert.NotNull(resources);
            Assert.NotEmpty(resources);
            Assert.NotNull(resources.Single(x => x.Name == testApiResource.Name));
        }
    public async Task FindApiResourcesByScopeNameAsync_WhenResourcesExist_ExpectResourcesReturned(DbContextOptions <ConfigurationDbContext> options)
    {
        var testApiResource = CreateApiResourceTestResource();
        var testApiScope    = CreateApiScopeTestResource();

        testApiResource.Scopes.Add(testApiScope.Name);

        using (var context = new ConfigurationDbContext(options))
        {
            context.ApiResources.Add(testApiResource.ToEntity());
            context.ApiScopes.Add(testApiScope.ToEntity());
            context.SaveChanges();
        }

        IEnumerable <ApiResource> resources;

        using (var context = new ConfigurationDbContext(options))
        {
            var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create(), new NoneCancellationTokenProvider());
            resources = await store.FindApiResourcesByScopeNameAsync(new List <string>
            {
                testApiScope.Name
            });
        }

        Assert.NotNull(resources);
        Assert.NotEmpty(resources);
        Assert.NotNull(resources.Single(x => x.Name == testApiResource.Name));
    }
Ejemplo n.º 6
0
        public async Task FindApiResourcesByScopeNameAsync_WhenResourcesExist_ExpectResourcesReturned()
        {
            var testApiResource = CreateApiResourceTestResource();
            var testApiScope    = CreateApiScopeTestResource();

            testApiResource.Scopes.Add(testApiScope.Name);

            var repo = g.configurationDb.GetRepository <Storage.Entities.ApiResource>();

            var entity = testApiResource.ToEntity();

            repo.Insert(entity);
            repo.SaveMany(entity, "Scopes");

            g.configurationDb.Insert(testApiScope.ToEntity()).ExecuteAffrows();

            IEnumerable <ApiResource> resources;

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

            resources = await store.FindApiResourcesByScopeNameAsync(new List <string>
            {
                testApiScope.Name
            });


            Assert.NotNull(resources);
            Assert.NotEmpty(resources);
            Assert.NotNull(resources.Single(x => x.Name == testApiResource.Name));
        }
Ejemplo n.º 7
0
        public async Task Should_Retrieve_Existing_Api_Resources_By_Scope_Names(TestDatabase testDb)
        {
            var testApiResource1 = CreateTestApiResource("test_api_resource1", new [] { "ar1_userclaim1", "ar1_userclaim2" });
            var testApiScope1    = CreateTestApiScopeForResource("test_api_scope1", new[] { "as_user_scope1" });

            testApiResource1.Scopes.Add(testApiScope1.Name);

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

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

                await session.FlushAsync();
            }

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

            using (var session = testDb.OpenStatelessSession())
            {
                var store = new ResourceStore(session, loggerMock.Object);
                resources = (await store.FindApiResourcesByScopeNameAsync(new[] { testApiScope1.Name })).ToList();
            }

            resources.Count().Should().Be(1);
            resources.First().Name.Should().Be("test_api_resource1");
            resources.First().Scopes.Count.Should().Be(1);
            resources.First().Scopes.First().Should().Be("test_api_scope1");

            await CleanupTestDataAsync(testDb);
        }
Ejemplo n.º 8
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());
        }
Ejemplo n.º 9
0
    public async Task Can_call_ResourceStore_FindApiResourcesByScopeNameAsync()
    => await ExecuteWithStrategyInTransactionAsync(
        async context =>
    {
        await SaveApiResources(context);
    },
        async context =>
    {
        var store = new ResourceStore(context, new FakeLogger <ResourceStore>());

        Assert.Equal(2, (await store.FindApiResourcesByScopeNameAsync(new[] { "S1", "S4" })).Count());
    }
        );
Ejemplo n.º 10
0
        public async Task Should_Retrieve_Requested_Api_Resources_By_Scope_Names(TestDatabase testDb)
        {
            const string testScope1 = "ar2_scope1";
            const string testScope2 = "ar2_scope2";
            const string testScope3 = "ar2_scope3";

            var testApiResource1 = CreateTestApiResource("test_api_resource2", new[] { testScope1, testScope2 });
            var testApiScope1    = CreateTestApiScopeForResource(testScope1, new[] { "u_scope3", "u_scope4" });
            var testApiScope2    = CreateTestApiScopeForResource(testScope2, new[] { "u_scope5", "u_scope6" });

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

            var testApiResource2 = CreateTestApiResource("test_api_resource3", new[] { testScope3 });
            var testApiScope3    = CreateTestApiScopeForResource(testScope3, new[] { "u_scope7", "u_scope8" });

            testApiResource2.Scopes.Add(testApiScope3.Name);

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

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

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

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

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

                await session.FlushAsync();
            }

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

            using (var session = testDb.OpenStatelessSession())
            {
                var store = new ResourceStore(session, loggerMock.Object);
                resources = (await store.FindApiResourcesByScopeNameAsync(new[] { testScope1, testScope2 })).ToList();
            }

            resources.Count().Should().Be(1);
            resources.First().Scopes.Count.Should().Be(2);

            await CleanupTestDataAsync(testDb);
        }
Ejemplo n.º 11
0
        public async Task Should_Retrieve_All_Api_Resources_By_Scope_Names(TestDatabase testDb)
        {
            var testScope1 = Guid.NewGuid().ToString();
            var testScope2 = Guid.NewGuid().ToString();
            var testScope3 = Guid.NewGuid().ToString();

            var testApiResource1 = CreateTestApiResource(Guid.NewGuid().ToString(), new[] { "user_claim1", "user_claim2" });
            var testApiScope1    = CreateTestApiScopeForResource(testScope1, new[] { "as1_user_claim1" });
            var testApiScope2    = CreateTestApiScopeForResource(testScope2, new[] { "as2_user_claim1" });

            testApiResource1.Scopes.Add(testApiScope1.Name);
            testApiResource1.Scopes.Add(testApiScope2.Name);
            var testApiResource2 = CreateTestApiResource(Guid.NewGuid().ToString(), new[] { "user_claim3" });
            var testApiScope3    = CreateTestApiScopeForResource(testScope3, new[] { "as3_user_claim1" });

            testApiResource2.Scopes.Add(testApiScope3.Name);

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

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

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

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

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

                await session.FlushAsync();
            }

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

            using (var session = testDb.OpenStatelessSession())
            {
                var store = new ResourceStore(session, loggerMock.Object);
                resources = await store.FindApiResourcesByScopeNameAsync(new[] { testScope1, testScope2, testScope3 });
            }

            resources.ToList().Count.Should().Be(2);

            await CleanupTestDataAsync(testDb);
        }
Ejemplo n.º 12
0
        public async Task FindApiResourcesByScopeNameAsync_should_find_api_by_scope_name()
        {
            using 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.SaveChangesAsync();

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

            var result = await sut.FindApiResourcesByScopeNameAsync(new[] { "test" });

            Assert.NotEmpty(result);
        }
Ejemplo n.º 13
0
        public async Task Should_Not_Retrieve_Api_Resources_With_Unexisting_Scope_Name(TestDatabase testDb)
        {
            var testApiResource = CreateTestApiResource("test_api_resource", new[] { "ar_userclaim" });

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

                await session.FlushAsync();
            }

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

            using (var session = testDb.OpenStatelessSession())
            {
                var store = new ResourceStore(session, loggerMock.Object);
                resources = await store.FindApiResourcesByScopeNameAsync(new[] { "non_existing_scope" });
            }

            resources.ToList().Should().BeEmpty();

            await CleanupTestDataAsync(testDb);
        }
Ejemplo n.º 14
0
        public async Task FindApiResourcesByScopeNameAsync_WhenResourcesExist_ExpectOnlyResourcesRequestedReturned()
        {
            var testIdentityResource = CreateIdentityTestResource();
            var testApiResource      = CreateApiResourceTestResource();
            var testApiScope         = CreateApiScopeTestResource();

            testApiResource.Scopes.Add(testApiScope.Name);

            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 testIdentityResourceEntity = testIdentityResource.ToEntity();

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

            var testApiResourceEntity = testApiResource.ToEntity();

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

            var testApiScopeEntity = testApiScope.ToEntity();

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

            var testIdentityResourceEntity2 = CreateIdentityTestResource().ToEntity();

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

            var testApiResourceEntity2 = CreateApiResourceTestResource().ToEntity();

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

            var testApiScopeEntity2 = CreateApiScopeTestResource().ToEntity();

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

            IEnumerable <ApiResource> resources;

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

            resources = await store.FindApiResourcesByScopeNameAsync(new[] { testApiScope.Name });


            Assert.NotNull(resources);
            Assert.NotEmpty(resources);
            Assert.NotNull(resources.Single(x => x.Name == testApiResource.Name));
        }