public async Task CanAddViolationAction(AccessRuleViolationAction action) { var uniqueData = UNIQUE_PREFIX + nameof(CanAddViolationAction) + action.ToString().Substring(0, 2); using var app = _appFactory.Create(); var contentRepository = app.Services.GetContentRepositoryWithElevatedPermissions(); var dbContext = app.Services.GetRequiredService <CofoundryDbContext>(); var directoryId = await app.TestData.PageDirectories().AddAsync(uniqueData); var command = new UpdatePageDirectoryAccessRuleSetCommand() { PageDirectoryId = directoryId, ViolationAction = action }; command.AccessRules.AddNew(app.SeededEntities.TestUserArea2.UserAreaCode, app.SeededEntities.TestUserArea2.RoleA.RoleId); await contentRepository .PageDirectories() .AccessRules() .UpdateAsync(command); var directory = await dbContext .PageDirectories .AsNoTracking() .FilterById(directoryId) .SingleOrDefaultAsync(); using (new AssertionScope()) { directory.Should().NotBeNull(); directory.AccessRuleViolationActionId.Should().Be((int)action); } }
private async Task AssertAccessRuleResponseAsync( HttpResponseMessage result, AccessRuleViolationAction routeAccessRuleViolationAction ) { switch (routeAccessRuleViolationAction) { case AccessRuleViolationAction.Error: await result.Should().BeDeveloperPageExceptionAsync <AccessRuleViolationException>(); break; case AccessRuleViolationAction.NotFound: result.StatusCode.Should().Be(HttpStatusCode.NotFound); break; default: throw new NotImplementedException($"Known {nameof(AccessRuleViolationAction)} '{routeAccessRuleViolationAction}'"); } }
public async Task WhenDirectoryAccessRuleForRoleAndUserInvalid_ReturnsCorrectAction(AccessRuleViolationAction routeAccessRuleViolationAction) { var uniqueData = UNIQUE_PREFIX + "DAR4RoleAndUserInv" + routeAccessRuleViolationAction.ToString().Substring(0, 2); var sluggedUniqueData = SlugFormatter.ToSlug(uniqueData); using var app = _webApplicationFactory.CreateApp(); var directoryId = await app.TestData.PageDirectories().AddAsync(uniqueData); var pageId = await app.TestData.Pages().AddAsync(uniqueData, directoryId, c => c.Publish = true); await app.TestData.PageDirectories().AddAccessRuleAsync( directoryId, app.SeededEntities.TestUserArea1.UserAreaCode, app.SeededEntities.TestUserArea1.RoleA.RoleId, c => c.ViolationAction = routeAccessRuleViolationAction ); using var client = _webApplicationFactory.CreateClientWithServices(s => s.TurnOnDeveloperExceptionPage()); await client.ImpersonateUserAsync(app.SeededEntities.TestUserArea2.RoleA.User); var result = await client.GetAsync($"/{sluggedUniqueData}/{sluggedUniqueData}"); await AssertAccessRuleResponseAsync(result, routeAccessRuleViolationAction); }