public void OnPostAsync_GivenNameAlreadyExistsAndPopulateFundingPeriodsReturnsOKButNullContent_ThrowsInvalidOperationException() { //Arrange ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK); ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); EditSpecificationPageModel pageModel = CreatePageModel(apiClient); pageModel.PageContext = new PageContext(); pageModel.EditSpecificationViewModel = new EditSpecificationViewModel { Name = specName }; //Act/Assert Func <Task> test = async() => await pageModel.OnPostAsync(); test .Should() .ThrowExactly <InvalidOperationException>(); }
public async Task OnPostAsync_GivenViewModelIsValidAndRedirectToSpecificationsActionProvided_ThenSpecificationIsEditedAndReturnsRedirectToSpecificationsPage() { //Arrange ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.NotFound); EditSpecificationModel editModel = new EditSpecificationModel(); EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel(); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .UpdateSpecification(Arg.Is(specificationId), Arg.Any <EditSpecificationModel>()) .Returns(HttpStatusCode.OK); IMapper mapper = CreateMapper(); mapper .Map <EditSpecificationModel>(Arg.Is(viewModel)) .Returns(editModel); EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper); pageModel.EditSpecificationViewModel = viewModel; pageModel.PageContext = new PageContext(); //Act IActionResult result = await pageModel.OnPostAsync(specificationId, EditSpecificationRedirectAction.Specifications); //Assert result .Should() .BeOfType <RedirectResult>(); RedirectResult redirectResult = result as RedirectResult; redirectResult .Url .Should() .Be("/specs?operationType=SpecificationUpdated&operationId=spec-id"); await apiClient .Received(1) .UpdateSpecification(Arg.Is(specificationId), Arg.Is(editModel)); }
public async Task OnPostAsync_GivenViewModelIsValidAndUpdateSpecificationCallFails_ThenInternalServerErrorReturned() { // Arrange ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.NotFound); EditSpecificationModel editModel = new EditSpecificationModel(); EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel(); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .UpdateSpecification(Arg.Is(specificationId), Arg.Any <EditSpecificationModel>()) .Returns(HttpStatusCode.InternalServerError); IMapper mapper = CreateMapper(); mapper .Map <EditSpecificationModel>(Arg.Is(viewModel)) .Returns(editModel); EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper); pageModel.EditSpecificationViewModel = viewModel; pageModel.PageContext = new PageContext(); // Act IActionResult result = await pageModel.OnPostAsync(specificationId, EditSpecificationRedirectAction.Specifications); //Assert result .Should() .BeOfType <InternalServerErrorResult>() .Which .Value .Should() .Be("Unable to update specification. API returned 'InternalServerError'"); await apiClient .Received(1) .UpdateSpecification(Arg.Is(specificationId), Arg.Is(editModel)); }
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(); }
public async Task OnPostAsync_GivenPagePopulatesButModelStateIsInvalid_ReturnsPage() { //Arrange IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "funding" } }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { Id = "fs1", Name = "funding stream" } }; 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 .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification)) .Returns(true); authorizationHelper .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(fundingStreams); EditSpecificationPageModel pageModel = CreatePageModel(specsClient: apiClient, authorizationHelper: authorizationHelper); pageModel.EditSpecificationViewModel = new EditSpecificationViewModel { Name = specName, FundingStreamIds = new List <string> { "fs1" } }; pageModel.PageContext = new PageContext(); //Act IActionResult result = await pageModel.OnPostAsync(); //Assert result .Should() .BeOfType <PageResult>(); pageModel .FundingStreams .Count() .Should() .Be(1); pageModel .FundingPeriods .Count() .Should() .Be(1); pageModel .IsAuthorizedToEdit .Should().BeTrue(); }