public void CanUseEntityBuilders()
        {
            using var serviceProvider = DbDependentTestApplicationServiceProviderFactory.CreateTestHostProvider();
            var allPermissions = GetPermissions(serviceProvider);
            var builder        = CreateBuilder(serviceProvider, allPermissions);

            var result = builder
                         .IncludePage()
                         .ExcludePage(c => c.Special().AdminModule())
                         .IncludeImageAsset(c => c.Read().Delete())
                         .IncludeCustomEntity <TestCustomEntityDefinition>(c => c.Read())
                         .Build();

            using (new AssertionScope())
            {
                result.Should().HaveCount(7);
                result.Should().Contain(p => p is PageReadPermission);
                result.Should().Contain(p => p is PageCreatePermission);
                result.Should().Contain(p => p is PageUpdatePermission);
                result.Should().Contain(p => p is PageDeletePermission);
                result.Should().Contain(p => p is ImageAssetReadPermission);
                result.Should().Contain(p => p is ImageAssetDeletePermission);
                result.Should().Contain(p => p.GetUniqueIdentifier() == TestCustomEntityDefinition.Code + CommonPermissionTypes.ReadPermissionCode);
            }
        }
        public void IncludeAll_AddAll()
        {
            using var serviceProvider = DbDependentTestApplicationServiceProviderFactory.CreateTestHostProvider();
            var allPermissions = GetPermissions(serviceProvider);
            var builder        = CreateBuilder(serviceProvider, allPermissions);

            var result = builder
                         .IncludeAll()
                         .Build();

            result.Should().HaveCount(allPermissions.Count());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// <para>
        /// Creates a new <see cref="DbDependentTestApplication"/> instance
        /// which can be used to create and work with test entities directly
        /// through the domain layer with the same API used in the domain
        /// integration tests project.
        /// </para>
        /// <para>
        /// Note that although the test app and client instances share the same base
        /// service configuration, they don't seem to share the service collection and
        /// therefore the same singleton instances, so be aware that features like in-memory
        /// caching are not shared. To get around this, the caching services in the client
        /// app will be reset when a new client is created.
        /// </para>
        /// <para>
        /// The application should be disposed of
        /// when you are done with it.
        /// </para>
        /// </summary>
        public static DbDependentTestApplication CreateApp <TEntryPoint>(this WebApplicationFactory <TEntryPoint> factory)
            where TEntryPoint : class
        {
            var seededEntities = factory.Services.GetRequiredService <SeededEntities>();

            var factoryWithAppDependencies = factory.WithServices(services =>
            {
                DbDependentTestApplicationServiceProviderFactory.ConfigureTestServices(services);
            });

            var app = new DbDependentTestApplication(factoryWithAppDependencies.Services, seededEntities);

            return(app);
        }
        public void Build_WhenNoMethodCalled_ReturnsEmpty()
        {
            using var serviceProvider = DbDependentTestApplicationServiceProviderFactory.CreateTestHostProvider();
            var allPermissions = GetPermissions(serviceProvider);
            var builder        = CreateBuilder(serviceProvider, allPermissions);

            var result = builder.Build();

            using (new AssertionScope())
            {
                allPermissions.Should().NotBeNullOrEmpty();
                result.Should().NotBeNull();
                result.Should().BeEmpty();
            }
        }
        public void CanIncludeSinglePermission()
        {
            using var serviceProvider = DbDependentTestApplicationServiceProviderFactory.CreateTestHostProvider();
            var allPermissions       = GetPermissions(serviceProvider);
            var builder              = CreateBuilder(serviceProvider, allPermissions);
            var numCreatePermissions = allPermissions.Count(p => p.PermissionType.Code == CommonPermissionTypes.CreatePermissionCode);

            var result = builder
                         .Include <CofoundryUserReadPermission>()
                         .Build();

            using (new AssertionScope())
            {
                numCreatePermissions.Should().BePositive();
                result.Should().HaveCount(1);
                result.First().Should().BeOfType <CofoundryUserReadPermission>();
            }
        }
        public void CanApplyRoleConfiguration()
        {
            using var serviceProvider = DbDependentTestApplicationServiceProviderFactory.CreateTestHostProvider();
            var allPermissions           = GetPermissions(serviceProvider);
            var builder                  = CreateBuilder(serviceProvider, allPermissions);
            var anonymousRolePermissions = allPermissions.FilterToAnonymousRoleDefaults();

            var result = builder
                         .ApplyAnonymousRoleConfiguration()
                         .Build();

            using (new AssertionScope())
            {
                result.Should().HaveSameCount(anonymousRolePermissions);
                result.Should().Contain(anonymousRolePermissions);
                result.Should().Contain(p => p is PageReadPermission);
            }
        }
        public void CanExludeByPermissionType()
        {
            using var serviceProvider = DbDependentTestApplicationServiceProviderFactory.CreateTestHostProvider();
            var allPermissions       = GetPermissions(serviceProvider);
            var builder              = CreateBuilder(serviceProvider, allPermissions);
            var numCreatePermissions = allPermissions.Count(p => p.PermissionType.Code == CommonPermissionTypes.CreatePermissionCode);

            var result = builder
                         .IncludeAll()
                         .ExcludeAllCreate()
                         .Build();

            var numCreatesInResult = result.Count(p => p.PermissionType.Code == CommonPermissionTypes.CreatePermissionCode);

            using (new AssertionScope())
            {
                numCreatePermissions.Should().BePositive();
                result.Should().HaveCount(allPermissions.Count() - numCreatePermissions);
            }
        }
        public void IncludeAll_WhenInvokedTwice_ReturnsNoDuplicates()
        {
            using var serviceProvider = DbDependentTestApplicationServiceProviderFactory.CreateTestHostProvider();
            var allPermissions = GetPermissions(serviceProvider);
            var builder        = CreateBuilder(serviceProvider, allPermissions);

            var result = builder
                         .IncludeAll()
                         .IncludeAll()
                         .Build();

            var duplicates = result
                             .GroupBy(p => p.GetUniqueIdentifier())
                             .Where(g => g.Count() > 1);

            using (new AssertionScope())
            {
                result.Should().HaveSameCount(allPermissions);
                duplicates.Should().BeEmpty();
            }
        }
        public void WhenNotInAvailablePermissions_DoesNotAdd()
        {
            using var serviceProvider = DbDependentTestApplicationServiceProviderFactory.CreateTestHostProvider();
            var allPermissions = new IPermission[]
            {
                new PageDirectoryReadPermission(),
                new PageDirectoryDeletePermission()
            };
            var builder = CreateBuilder(serviceProvider, allPermissions);

            var result = builder
                         .IncludePageDirectory()
                         .IncludeRole()
                         .IncludeAllUpdate()
                         .Build();

            using (new AssertionScope())
            {
                result.Should().HaveCount(2);
                result.Should().Contain(p => p is PageDirectoryReadPermission);
                result.Should().Contain(p => p is PageDirectoryDeletePermission);
            }
        }