public async Task OnPostAsync_GivenUserDoesNotHaveCreateSpecificationPermissionForAnyFundingStream_ThenReturnsForbidResult() { // Arrange IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "Funding Period 1" }, new Reference { Id = "fp2", Name = "Funding Period 2" } }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { Id = "fp1", Name = "funding" } }; IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <IEnumerable <string> >(), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(false); IMapper mapper = MappingHelper.CreateFrontEndMapper(); CreateSpecificationViewModel createModel = new CreateSpecificationViewModel { Description = "Test spec", FundingPeriodId = "FY1819", FundingStreamIds = new List <string> { "fs1", "fs2" }, Name = "Test spec" }; CreateSpecificationPageModel pageModel = CreatePageModel(authorizationHelper: authorizationHelper, mapper: mapper); pageModel.CreateSpecificationViewModel = createModel; // Act IActionResult result = await pageModel.OnPostAsync(); // Assert result.Should().BeOfType <ForbidResult>(); pageModel .IsAuthorizedToCreate .Should().BeFalse(); }
public async Task OnPostAsync_GivenViewModelIsValid_ReturnsRedirect() { //Arrange const string specName = "spec name"; CreateSpecificationModel createModel = new CreateSpecificationModel { Name = specName, Description = "description", FundingStreamIds = new[] { "fs1" }, FundingPeriodId = "fp1" }; ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.NotFound); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); Specification createdSpecification = new Specification() { Id = "specId", }; apiClient .CreateSpecification(Arg.Any <CreateSpecificationModel>()) .Returns(new ValidatedApiResponse <Specification>(HttpStatusCode.OK, createdSpecification)); IMapper mapper = CreateMapper(); mapper .Map <CreateSpecificationModel>(Arg.Any <CreateSpecificationViewModel>()) .Returns(createModel); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <IEnumerable <string> >(), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(true); CreateSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper, authorizationHelper); pageModel.CreateSpecificationViewModel = new CreateSpecificationViewModel { Name = specName, Description = "description", FundingStreamIds = new[] { "fs1" }, FundingPeriodId = "fp1" }; //Act IActionResult result = await pageModel.OnPostAsync(); //Assert result .Should() .BeOfType <RedirectResult>(); RedirectResult redirectResult = result as RedirectResult; redirectResult .Url .Should() .Be("/specs/policies/specId?operationType=SpecificationCreated&operationId=specId"); }
public async Task OnPostAsync_GivenPagePopulatesButModelStateIsInvalid_ReturnsPage() { //Arrange const string specName = "spec name"; IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "funding" } }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { Id = "fp1", Name = "funding" } }; ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK); ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods); ApiResponse <IEnumerable <FundingStream> > fundingStreamsResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); apiClient .GetFundingStreams() .Returns(fundingStreamsResponse); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(fundingStreams); CreateSpecificationPageModel pageModel = CreatePageModel(specsClient: apiClient, authorizationHelper: authorizationHelper); pageModel.CreateSpecificationViewModel = new CreateSpecificationViewModel { Name = specName }; pageModel.PageContext.ModelState.AddModelError("test", "Invalid model"); //Act IActionResult result = await pageModel.OnPostAsync(); //Assert result .Should() .BeOfType <PageResult>(); pageModel .FundingStreams .Count() .Should() .Be(1); pageModel .FundingPeriods .Count() .Should() .Be(1); pageModel .IsAuthorizedToCreate .Should().BeTrue(); }