public async Task Update_Api_Resource_Server_validator(UpdateApiResourceDto model, bool isvalid)
        {
            using (var ctx = await TestDbContext.Create(nameof(Update_Api_Resource_Server_validator)))
            {
                ApiResource entity = new ApiResource
                {
                    Address    = "z",
                    HttpMethod = HttpMethods.GET,
                    Title      = "z"
                };
                ApiResource entity2 = new ApiResource
                {
                    Address    = "zx",
                    HttpMethod = HttpMethods.GET,
                    Title      = "zx"
                };
                entity.Id  = Update_Api_Resource_validatorTestData.validGuid;
                entity2.Id = Update_Api_Resource_validatorTestData.validGuid2;

                ctx.ApiResources.Add(entity);
                ctx.ApiResources.Add(entity2);

                await ctx.SaveChangesAsync();

                var vaidator   = new ServerSideValidator(ctx);
                var validation = vaidator.Validate(new UpdateApiResource(model));
                _output.WriteLine(string.Join(",\n", validation.Errors.Select(x => x.ErrorMessage)));
                Assert.Equal(isvalid, validation.IsValid);
            }
        }
예제 #2
0
        public async Task Can_Update_Api_Resource()
        {
            using (var ctx = await TestDbContext.Create(nameof(Can_Update_Api_Resource)))
            {
                var entity = new ApiResource()
                {
                    Address    = "http://abc",
                    HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET,
                    Title      = "Some Title"
                };
                ctx.ApiResources.Add(entity);
                await ctx.SaveChangesAsync();

                var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile()));
                var model  = new UpdateApiResourceDto(
                    entity.Id,
                    "http://Test",
                    DotNetAccessControl.domain.enums.HttpMethods.POST,
                    "Test Title"
                    );
                var request = new UpdateApiResource(model);

                var response = await new UpdateApiResourceHandler(ctx, mapper.CreateMapper()).Handle(request, CancellationToken.None);
                var toCheck  = await ctx.ApiResources.AsNoTracking().SingleAsync(x => x.Id == entity.Id);

                Assert.Equal(model.Title, toCheck.Title);
                Assert.Equal(model.Address, toCheck.Address);
                Assert.Equal(model.HttpMethod, toCheck.HttpMethod);
            }
        }
예제 #3
0
        public async Task Can_RoleAccess_To_Api()
        {
            using (var ctx = await TestDbContext.Create(nameof(Can_RoleAccess_To_Api)))
            {
                var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile()));

                var api = new ApiResource
                {
                    Address    = "some1",
                    HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET,
                    Title      = "some1"
                };
                ctx.ApiResources.Add(api);
                await ctx.SaveChangesAsync();

                var handler = new RoleAccessToApiHandler(new GenericRoleToResourceHandler(ctx, mapper.CreateMapper()));
                var model   = new AssignRoleToResourceDto("Role1", api.Id, DotNetAccessControl.domain.enums.Permission.Allow);
                var result  = await handler.Handle(new RoleAccessToApi(model), CancellationToken.None);

                Assert.True(result);
                var saved = await ctx.AccessControls.OfType <RoleAccessControl>().SingleAsync(x => x.ResourceId == api.Id);

                Assert.Equal(model.RoleName, saved.RoleName);
            }
        }
        public IQueryableExtensionsTests(ITestOutputHelper output)
        {
            _output = output;

            using (var context = TestDbContext.Create())
            {
                Database.SetInitializer(new MigrateDatabaseToLatestVersion <TestDbContext, TestDbConfiguration>());
                context.Database.Initialize(true);
            }
        }
예제 #5
0
        public void NoMapping_WithSqlite_ReturnsIncorrectPrecision()
        {
            using var dbContext = TestDbContext.Create();

            var entity       = new TestEntity();
            var loadedEntity = this.SaveAndReload(entity, dbContext);

            Assert.Equal(entity.Id, loadedEntity.Id);
            Assert.Equal(65536, GetSignAndScale(loadedEntity.Id));
        }
예제 #6
0
        public void CreateExecutionStrategy_WithoutRetryOnOptimisticConcurrencyFailure_ShouldReturnExpectedResult()
        {
            this.OptionsBuilder.ExecutionStrategyOptions = ExecutionStrategyOptions.None;

            var dbContext = TestDbContext.Create();

            var result = this.Provider.CreateExecutionStrategy(dbContext);

            Assert.IsType(dbContext.Database.CreateExecutionStrategy().GetType(), result);
        }
예제 #7
0
        public void StoreDecimalIdsWithCorrectPrecision_WithSqlite_DoesNotAffectNonIdDecimals()
        {
            using var dbContext = TestDbContext.Create((modelBuilder, dbContext) =>
                                                       modelBuilder.StoreDecimalIdsWithCorrectPrecision(dbContext));

            var entity       = new TestEntity();
            var loadedEntity = this.SaveAndReload(entity, dbContext);

            Assert.Equal(entity.Number, loadedEntity.Number);
            Assert.NotEqual(0, GetSignAndScale(loadedEntity.Number));
        }
예제 #8
0
        public void StoreWithDecimalIdPrecision_WithNonIdProperty_ReturnsExpectedPrecision()
        {
            using var dbContext = TestDbContext.Create((modelBuilder, dbContext) =>
                                                       modelBuilder.Entity <TestEntity>(entity => entity.Property(e => e.DoesNotHaveIdSuffix).StoreWithDecimalIdPrecision(dbContext)));

            var entity       = new TestEntity();
            var loadedEntity = this.SaveAndReload(entity, dbContext);

            Assert.Equal(entity.DoesNotHaveIdSuffix, loadedEntity.DoesNotHaveIdSuffix);
            Assert.Equal(0, GetSignAndScale(loadedEntity.DoesNotHaveIdSuffix));
        }
예제 #9
0
        public void StoreDecimalIdsWithCorrectPrecision_WithSqlite_ReturnsExpectedPrecision()
        {
            using var dbContext = TestDbContext.Create((modelBuilder, dbContext) =>
                                                       modelBuilder.StoreDecimalIdsWithCorrectPrecision(dbContext));

            var entity       = new TestEntity();
            var loadedEntity = this.SaveAndReload(entity, dbContext);

            Assert.Equal(entity.Id, loadedEntity.Id);
            Assert.Equal(0, GetSignAndScale(loadedEntity.Id));
        }
        public async Task TestDBPagedListAsync()
        {
            using (var context = TestDbContext.Create())
            {
                var paged = await context.DistributedLocks
                            .OrderBy(l => l.Id)
                            .ToPagedListAsync(page: 1, pageSize: 100);

                Assert.True(paged.CurrentPage == 1);
                Assert.True(paged.PageSize <= 100);
            }
        }
예제 #11
0
        public async Task <int> InsertAsync(ICollection <Product> sources, CancellationToken cancelToken)
        {
            var result = 0;

            using (var dbContext = TestDbContext.Create())
            {
                dbContext.Products.AddRange(sources);
                result = await dbContext.SaveChangesAsync(cancelToken);
            }

            return(result);
        }
예제 #12
0
        public void StoreWithDecimalIdPrecision_WithInMemoryAndDifferentColumnType_ReturnsExpectedPrecision()
        {
            using var dbContext = TestDbContext.Create(useInMemoryInsteadOfSqlite: true, onModelCreating: (modelBuilder, dbContext) =>
                                                       modelBuilder.Entity <TestEntity>(entity => entity.Property(e => e.Id).StoreWithDecimalIdPrecision(dbContext, columnType: "DECIMAL(29,1)")));

            var entity       = new TestEntity();
            var loadedEntity = this.SaveAndReload(entity, dbContext);

            Assert.Equal(entity.Id, loadedEntity.Id);
            //Assert.Equal(1, GetSignAndScale(loadedEntity.Id)); // Unfortunately, the in-memory provider does not honor this, but at least we know that the flow did not throw
            //Assert.Equal("DECIMAL(29,1)", dbContext.Model.FindEntityType(typeof(TestEntity)).FindProperty(nameof(TestEntity.Id)).GetColumnType()); // Does not work with in-memory provider
        }
예제 #13
0
        public void StoreWithDecimalIdPrecision_WithInMemoryAndExplicitColumnType_ReturnsExpectedPrecision()
        {
            using var dbContext = TestDbContext.Create(useInMemoryInsteadOfSqlite: true, onModelCreating: (modelBuilder, dbContext) =>
                                                       modelBuilder.Entity <TestEntity>(entity => entity.Property(e => e.Id).StoreWithDecimalIdPrecision(dbContext, columnType: "DECIMAL(28,0)")));

            var entity       = new TestEntity();
            var loadedEntity = this.SaveAndReload(entity, dbContext);

            Assert.Equal(entity.Id, loadedEntity.Id);
            Assert.Equal(0, GetSignAndScale(loadedEntity.Id));
            //Assert.Equal("DECIMAL(28,0)", dbContext.Model.FindEntityType(typeof(TestEntity)).FindProperty(nameof(TestEntity.Id)).GetColumnType()); // Does not work with in-memory provider
        }
예제 #14
0
        public static void AssemblyInitialize(TestContext context)
        {
            using (var dbDbContext = TestDbContext.Create())
            {
                if (dbDbContext.Database.Exists() == false)
                {
                    dbDbContext.Database.Initialize(true);
                }
            }

            s_client             = new HttpClient();
            s_client.BaseAddress = new Uri(HOST_ADDRESS);
        }
예제 #15
0
        public void StoreDecimalIdsWithCorrectPrecision_WithSqlite_AffectsOtherIdProperties()
        {
            using var dbContext = TestDbContext.Create((modelBuilder, dbContext) =>
                                                       modelBuilder.StoreDecimalIdsWithCorrectPrecision(dbContext));

            var entity       = new TestEntity();
            var loadedEntity = this.SaveAndReload(entity, dbContext);

            Assert.Equal(entity.ForeignId, loadedEntity.ForeignId);
            Assert.Equal(entity.ForeignID, loadedEntity.ForeignID);

            Assert.Equal(0, GetSignAndScale(loadedEntity.ForeignId));
            Assert.Equal(0, GetSignAndScale(loadedEntity.ForeignID));
        }
예제 #16
0
        public async Task Can_Add_ApiResource()
        {
            using (var ctx = await TestDbContext.Create(nameof(Can_Add_ApiResource)))
            {
                var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile()));
                var model  = new AddApiResourceDto(
                    "http://abc",
                    DotNetAccessControl.domain.enums.HttpMethods.GET,
                    "Some Title"
                    );
                var request = new AddApiResource(model);

                var response = await new  AddApiResourceHandler(ctx, mapper.CreateMapper()).Handle(request, CancellationToken.None);
                Assert.True(await ctx.ApiResources.AnyAsync(x => x.Id == response));
            }
        }
예제 #17
0
        public async Task Add_Api_Resource_Server_validator(AddApiResourceDto model, bool isvalid)
        {
            using (var ctx = await TestDbContext.Create(nameof(Add_Api_Resource_Server_validator)))
            {
                ApiResource entity = new ApiResource
                {
                    Address    = "z",
                    HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET,
                    Title      = "z"
                };
                ctx.ApiResources.Add(entity);
                await ctx.SaveChangesAsync();

                var vaidator = new ServerSideValidator(ctx);
                Assert.Equal(isvalid, vaidator.Validate(new AddApiResource(model)).IsValid);
            }
        }
예제 #18
0
        public void CreateExecutionStrategy_WithRetryOnOptimisticConcurrencyFailure_ShouldReturnExpectedResult()
        {
            this.OptionsBuilder.ExecutionStrategyOptions = ExecutionStrategyOptions.RetryOnOptimisticConcurrencyFailure;

            var dbContext = TestDbContext.Create();

            var result = this.Provider.CreateExecutionStrategy(dbContext);

            Assert.IsType <RetryOnOptimisticConcurrencyFailureExecutionStrategy>(result);

            var wrappedStrategyProperty = typeof(RetryOnOptimisticConcurrencyFailureExecutionStrategy).GetProperty("WrappedStrategy", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ??
                                          throw new Exception("The WrappedStrategy property could not be found.");

            var wrappedStrategy = wrappedStrategyProperty.GetValue(result);

            Assert.IsType(dbContext.Database.CreateExecutionStrategy().GetType(), wrappedStrategy);
        }
예제 #19
0
        public async Task Can_RoleAccess_To_Menu()
        {
            using (var ctx = await TestDbContext.Create(nameof(Can_RoleAccess_To_Menu)))
            {
                var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile()));

                var menu = new MenuResource
                {
                    Title = "someMenu",
                    Icon  = "",
                    Name  = "Mn1",
                    Order = 1,
                };
                var page = new PageResource
                {
                    Link  = "http://abc",
                    Name  = "p1",
                    Title = "Some",
                };
                menu.Page = page;
                ctx.MenuResources.Add(menu);
                ctx.PageResources.Add(page);
                await ctx.SaveChangesAsync();

                Application.Access.GenericRoleToResourceHandler genericRoleToResourceHandler = new Application.Access.GenericRoleToResourceHandler(ctx, mapper.CreateMapper());


                var mediator = new Mock <IMediator>();
                mediator
                .Setup(m => m.Send(It.IsAny <RoleAccessToPage>(), It.IsAny <CancellationToken>()))
                .ReturnsAsync(true)
                .Verifiable();

                var model = new AssignRoleToResourceDto("Role1", menu.Id, DotNetAccessControl.domain.enums.Permission.Allow);

                var handler = new AssignRoleToMenuHandler(genericRoleToResourceHandler, ctx, mediator.Object);
                var result  = await handler.Handle(new RoleAccessToMenu(model, true), CancellationToken.None);

                var saved = await ctx.AccessControls.OfType <RoleAccessControl>().SingleAsync(x => x.ResourceId == menu.Id);

                Assert.Equal(model.RoleName, saved.RoleName);
                mediator.Verify(x => x.Send(It.IsAny <RoleAccessToPage>(), It.IsAny <CancellationToken>()), Times.Once);
            }
        }
        public async Task Can_Get_Menu_Resource_By_Id()
        {
            using (var ctx = await TestDbContext.Create(nameof(Can_Get_Menu_Resource_By_Id)))
            {
                var entity = new ApiResource()
                {
                    Address    = "http://abc",
                    HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET,
                    Title      = "Some Title"
                };
                ctx.ApiResources.Add(entity);
                await ctx.SaveChangesAsync();

                var mapper  = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile()));
                var handler = new GetMenuResourceByIdHandler(ctx, mapper.CreateMapper());
                var result  = await handler.Handle(new GetMenuResourceById(entity.Id), CancellationToken.None);

                Assert.True(result != null);
                Assert.Equal(entity.Title, result.Title);
                Assert.Equal(entity.Address, result.Address);
                Assert.Equal(entity.HttpMethod, result.HttpMethod);
            }
        }