public async Task InvokeAsync_ThrowsExceptionWhenNextIsNotSet() { ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware(); var context = this.GetContext(); var exception = await ShouldThrowAsyncExtensions.ShouldThrowAsync <MiddlewarePipelineException>(() => middleware.InvokeAsync(context)); exception.Message.ShouldContain("must have a Next middleware"); }
public async Task Handle_GivenInvalidTopicId_ThrowsBadRequestException() { var command = new UpsertContentCommand() { TopicId = 10 }; await ShouldThrowAsyncExtensions.ShouldThrowAsync <BadRequestException>(() => _handler.Handle(command, CancellationToken.None)); }
public async Task InvalidTransitionShouldThrowUnhandledTriggerException() { List <string> logs = new List <string>(); var machine = this.CreateInstance(logs); var exception = await ShouldThrowAsyncExtensions.ShouldThrowAsync <UnhandledTriggerException>(() => machine.FireAsync(Trigger.StartDispensing)); exception.State.ShouldBe(State.Stopped); exception.Trigger.ShouldBe(Trigger.StartDispensing); }
public async Task Handle_GivenIdAndDoesntExist_ThrowsNotFoundException() { var id = 10; var command = new UpsertContentCommand() { Id = id, TopicId = validTopicId }; await ShouldThrowAsyncExtensions.ShouldThrowAsync <NotFoundException>(() => _handler.Handle(command, CancellationToken.None)); }
public async Task Handle_GivenInvalidId_ThrowsNotFoundException() { int invalidId = 10; var command = new DeleteContentCommand() { Id = invalidId }; await ShouldThrowAsyncExtensions.ShouldThrowAsync <NotFoundException>(() => _handler.Handle(command, CancellationToken.None)); }
public async Task Handle_GivenIdAndDoesntExist_ThrowsNotFoundException() { var id = 10; var command = new UpsertCategoryCommand() { Id = id, Title = "New Category", Description = "New category description" }; await ShouldThrowAsyncExtensions.ShouldThrowAsync <NotFoundException>(() => _handler.Handle(command, CancellationToken.None)); }
public async Task Handle_GivenValidIdAndCategoryNotEmpty_ThrowsDeleteFailureException() { var validId = 1; var command = new DeleteCategoryCommand() { Id = validId }; await ShouldThrowAsyncExtensions.ShouldThrowAsync <DeleteFailureException>(() => _handler.Handle(command, CancellationToken.None)); }
public async Task UpdateAsyncTest() { IServiceCollection services = new ServiceCollection(); services.AddAuthorizationExtension(); services.AddScoped <ISystemResourceStore <SystemResource> >(sp => { var mockResourceStore = new Mock <ISystemResourceStore <SystemResource> >(); mockResourceStore.Setup(s => s.UpdateAsync(It.IsAny <SystemResource>(), It.IsAny <CancellationToken>())) .Returns((SystemResource resource, CancellationToken _) => Task.FromResult(resource)); SystemResource[] resources = new SystemResource[] { new SystemResource() { Id = "/Account/Index", Name = "Index", ResourceType = ResourceType.Api, PermissionId = "permission0" }, new SystemResource() { Id = "/Account/Create", Name = "Create", ResourceType = ResourceType.Api, PermissionId = "permission1" }, }; mockResourceStore.Setup(s => s.FindByIdAsync(It.IsIn("/Account/Index", "/Account/Create", "null"), It.IsAny <CancellationToken>())) .Returns((string id, CancellationToken _) => Task.FromResult(resources.FirstOrDefault(r => r.Id == id))); return(mockResourceStore.Object); }); services.AddScoped <ISystemPermissionStore <SystemPermission> >(sp => new Mock <ISystemPermissionStore <SystemPermission> >().Object); var mockPermissionMonitor = new Mock <IPermissionMonitor>(); services.AddScoped <IPermissionMonitor>(sp => { mockPermissionMonitor.Setup(p => p.OnResourceChangedAsync(It.IsAny <string>())); return(mockPermissionMonitor.Object); }); IServiceProvider serviceProvider = services.BuildServiceProvider(); using IServiceScope serviceScope = serviceProvider.CreateScope(); ISystemResourceService <SystemResource> systemResourceService = serviceScope.ServiceProvider.GetRequiredService <ISystemResourceService <SystemResource> >(); await systemResourceService.UpdateAsync(new SystemResource() { Id = "/Account/Index", Name = "首页", ResourceType = ResourceType.Api, PermissionId = "permission0" }, CancellationToken.None); mockPermissionMonitor.Verify(p => p.OnResourceChangedAsync("/Account/Index"), Times.Never(), "未改动相关属性,触发调用了OnResourceChangedAsync(\"/Account/Index\")"); await systemResourceService.UpdateAsync(new SystemResource() { Id = "/Account/Create", Name = "首页", ResourceType = ResourceType.Api, PermissionId = "permission3" }, CancellationToken.None); mockPermissionMonitor.Verify(p => p.OnResourceChangedAsync("/Account/Create"), Times.AtLeastOnce(), "改动相关属性,未触发调用了OnResourceChangedAsync(\"/Account/Create\")"); Task task = systemResourceService.UpdateAsync(new SystemResource() { Id = "null", Name = "首页", ResourceType = ResourceType.Api, PermissionId = "permission1" }, CancellationToken.None); await ShouldThrowAsyncExtensions.ShouldThrowAsync <Exception>(task); }
public async Task UpdateAsyncTest() { IServiceCollection services = new ServiceCollection(); services.AddAuthorizationExtension(); services.AddScoped <ISystemPermissionRoleStore <SystemPermissionRole> >(sp => new Mock <ISystemPermissionRoleStore <SystemPermissionRole> >().Object); services.AddScoped <ISystemPermissionUserStore <SystemPermissionUser> >(sp => new Mock <ISystemPermissionUserStore <SystemPermissionUser> >().Object); services.AddScoped <ISystemPermissionStore <SystemPermission> >(sp => { var mockSystemPermissionStore = new Mock <ISystemPermissionStore <SystemPermission> >(); mockSystemPermissionStore.Setup(s => s.UpdateAsync(It.IsAny <SystemPermission>(), It.IsAny <CancellationToken>())) .Returns((SystemPermission p, CancellationToken _) => Task.FromResult(p)); SystemPermission[] permissions = new SystemPermission[] { new SystemPermission() { Id = "permission0", Name = "permission0", AllowedAllRoles = true }, new SystemPermission() { Id = "permission1", Name = "permission1", AllowedAnonymous = true }, new SystemPermission() { Id = "permission2", Name = "permission2", DeniedAll = true }, new SystemPermission() { Id = "permission3", Name = "permission3" }, }; mockSystemPermissionStore.Setup(s => s.FindByIdAsync(It.IsIn("permission0", "permission1", "permission2", "permission3", "permission4"), It.IsAny <CancellationToken>())) .Returns((string id, CancellationToken _) => Task.FromResult(permissions.FirstOrDefault(p => p.Id == id))); return(mockSystemPermissionStore.Object); }); var mockPermissionMonitor = new Mock <IPermissionMonitor>(); services.AddScoped <IPermissionMonitor>(sp => { mockPermissionMonitor.Setup(p => p.OnPermissionChangedAsync(It.IsAny <string>())); return(mockPermissionMonitor.Object); }); IServiceProvider serviceProvider = services.BuildServiceProvider(); using IServiceScope serviceScope = serviceProvider.CreateScope(); ISystemPermissionService <SystemPermission> systemPermissionService = serviceScope.ServiceProvider.GetRequiredService <ISystemPermissionService <SystemPermission> >(); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); await systemPermissionService.UpdateAsync(new SystemPermission() { Id = "permission0", Name = "permission00", AllowedAllRoles = true }, cancellationTokenSource.Token); mockPermissionMonitor.Verify(p => p.OnPermissionChangedAsync("permission0"), Times.Never(), "未改动相关属性,触发调用了OnPermissionChangedAsync(\"permission0\")"); await systemPermissionService.UpdateAsync(new SystemPermission() { Id = "permission0", Name = "permission00", AllowedAllRoles = false }, cancellationTokenSource.Token); mockPermissionMonitor.Verify(p => p.OnPermissionChangedAsync("permission0"), Times.AtLeastOnce(), "改动相关属性AllowedAllRoles,未调用OnPermissionChangedAsync(\"permission0\")"); await systemPermissionService.UpdateAsync(new SystemPermission() { Id = "permission1", Name = "permission1", AllowedAnonymous = false }, cancellationTokenSource.Token); mockPermissionMonitor.Verify(p => p.OnPermissionChangedAsync("permission1"), Times.AtLeastOnce(), "改动相关属性AllowedAnonymous,未调用OnPermissionChangedAsync(\"permission1\")"); await systemPermissionService.UpdateAsync(new SystemPermission() { Id = "permission2", Name = "permission2", DeniedAll = false }, cancellationTokenSource.Token); mockPermissionMonitor.Verify(p => p.OnPermissionChangedAsync("permission2"), Times.AtLeastOnce(), "改动相关属性DeniedAll,未调用OnPermissionChangedAsync(\"permission2\")"); await systemPermissionService.UpdateAsync(new SystemPermission() { Id = "permission3", Name = "permission3", AllowedAllRoles = true, AllowedAnonymous = true, DeniedAll = true }, cancellationTokenSource.Token); mockPermissionMonitor.Verify(p => p.OnPermissionChangedAsync("permission3"), Times.AtLeastOnce(), "改动相关属性,未调用OnPermissionChangedAsync(\"permission3\")"); Task task = systemPermissionService.UpdateAsync(new SystemPermission() { Id = "permission4", Name = "permission3", AllowedAllRoles = true, AllowedAnonymous = true, DeniedAll = true }, cancellationTokenSource.Token); await ShouldThrowAsyncExtensions.ShouldThrowAsync <Exception>(task); // cacheManager.Set() //await systemPermissionRoleService.AddToRoleAsync(permissionId,roleId,CancellationToken.None); }