public static async Task AssertReleaseHandlerHandlesScenarioSuccessfully <TRequirement>( Func <ContentDbContext, IAuthorizationHandler> handlerSupplier, ReleaseHandlerTestScenario scenario) where TRequirement : IAuthorizationRequirement { var contextId = Guid.NewGuid().ToString(); using (var context = InMemoryApplicationDbContext(contextId)) { if (scenario.UserPublicationRoles != null) { await context.AddRangeAsync(scenario.UserPublicationRoles); } if (scenario.UserReleaseRoles != null) { await context.AddRangeAsync(scenario.UserReleaseRoles); } await context.SaveChangesAsync(); } using (var context = InMemoryApplicationDbContext(contextId)) { var handler = handlerSupplier(context); await AssertHandlerHandlesScenarioSuccessfully <TRequirement>(handler, scenario); } }
public async Task HasPreReleaseRoleWithinAccessWindowAuthorizationHandler_PreReleaseWindowNotOpen() { var release = new Release { Id = Guid.NewGuid() }; var preReleaseService = new Mock <IPreReleaseService>(); var userId = Guid.NewGuid(); var failureScenario = new ReleaseHandlerTestScenario { Entity = release, User = ClaimsPrincipalUtils.CreateClaimsPrincipal(userId), UserReleaseRoles = new List <UserReleaseRole> { new UserReleaseRole { ReleaseId = release.Id, UserId = userId, Role = PrereleaseViewer } }, ExpectedToPass = false, UnexpectedPassMessage = "Expected the test to fail because the Pre Release window is not open at the " + "current time" }; await GetEnumValues <PreReleaseAccess>() .Where(value => value != PreReleaseAccess.Within) .ToList() .ToAsyncEnumerable() .ForEachAwaitAsync(async access => { preReleaseService .Setup(s => s.GetPreReleaseWindowStatus(release, It.IsAny <DateTime>())) .Returns(new PreReleaseWindowStatus { Access = access }); // Assert that a User who specifically has the Pre Release role will cause this handler to fail // IF the Pre Release window is NOT open await AssertReleaseHandlerHandlesScenarioSuccessfully <ViewReleaseRequirement>( contentDbContext => new HasPreReleaseRoleWithinAccessWindowAuthorizationHandler( new UserReleaseRoleRepository(contentDbContext), preReleaseService.Object), failureScenario); }); }
private static ReleaseHandlerTestScenario CreateUserNotInProductionTeamScenario(Release release, ReleaseRole[] rolesExpectedToSucceed) { var userId = Guid.NewGuid(); var user = CreateClaimsPrincipal(userId); var rolesList = new List <UserReleaseRole>(); // add some roles to unrelated Users to ensure that only the current User is being // taken into consideration rolesExpectedToSucceed.ToList().ForEach(roleExpectedToSucceed => { rolesList.Add(new UserReleaseRole { ReleaseId = release.Id, UserId = Guid.NewGuid(), Role = roleExpectedToSucceed }); }); // add some roles to unrelated Releases to ensure that only the Release under test is being // taken into consideration rolesExpectedToSucceed.ToList().ForEach(roleExpectedToSucceed => { rolesList.Add(new UserReleaseRole { ReleaseId = Guid.NewGuid(), UserId = userId, Role = roleExpectedToSucceed }); }); var notInTeamScenario = new ReleaseHandlerTestScenario { User = user, Release = release, UserReleaseRoles = rolesList, ExpectedToPass = false, UnexpectedPassMessage = "Expected not having a role on the Release would have made the handler fail" }; return(notInTeamScenario); }
public static async void AssertReleaseHandlerHandlesScenarioSuccessfully <TRequirement>( IAuthorizationHandler handler, ReleaseHandlerTestScenario scenario) where TRequirement : IAuthorizationRequirement { var authContext = new AuthorizationHandlerContext( new IAuthorizationRequirement[] { Activator.CreateInstance <TRequirement>() }, scenario.User, scenario.Release); await handler.HandleAsync(authContext); if (scenario.ExpectedToPass) { Assert.True(authContext.HasSucceeded, scenario.UnexpectedFailMessage); } else { Assert.False(authContext.HasSucceeded, scenario.UnexpectedPassMessage); } }
public static void AssertReleaseHandlerHandlesScenarioSuccessfully <TRequirement>( Func <ContentDbContext, IAuthorizationHandler> handlerSupplier, ReleaseHandlerTestScenario scenario) where TRequirement : IAuthorizationRequirement { var contextId = Guid.NewGuid().ToString(); using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { if (scenario.UserReleaseRoles != null) { context.AddRange(scenario.UserReleaseRoles); context.SaveChanges(); } } using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { var handler = handlerSupplier(context); AssertReleaseHandlerHandlesScenarioSuccessfully <TRequirement>(handler, scenario); } }