public void OnPostAsync_GivenCalculationDoesNotAlreadyExistButSavingIsNotOK_InvalidOperationException() { //Arrange CreateCalculationViewModel viewModel = new CreateCalculationViewModel { Name = "any name" }; Specification specification = CreateSpecification(); IEnumerable <Reference> allocationLines = new[] { new Reference { Id = "alloc-id", Name = "alloc-name" } }; CalculationCreateModel createModel = new CalculationCreateModel { SpecificationId = specificationId }; IMapper mapper = CreateMapper(); mapper .Map <CalculationCreateModel>(Arg.Is(viewModel)) .Returns(createModel); ApiResponse <Calculation> calcApiRespnse = new ApiResponse <Calculation>(HttpStatusCode.NotFound); ValidatedApiResponse <Calculation> newCalcApiResponse = new ValidatedApiResponse <Calculation>(HttpStatusCode.InternalServerError, new Calculation { Id = "new-calc-id", }); newCalcApiResponse.ModelState = new Dictionary <string, IEnumerable <string> >(); ISpecsApiClient specsClient = CreateSpecsApiClient(); specsClient .GetCalculationBySpecificationIdAndCalculationName(Arg.Is(specificationId), Arg.Is(viewModel.Name)) .Returns(calcApiRespnse); specsClient .CreateCalculation(Arg.Is(createModel)) .Returns(newCalcApiResponse); List <FundingStream> fundingStreams = new List <FundingStream>(); fundingStreams.Add(new FundingStream { Id = fundingStreamId, AllocationLines = new List <AllocationLine>() { new AllocationLine() { Id = "al1", Name = "Allocation Line 1", } } }); ApiResponse <IEnumerable <FundingStream> > fundingStreamResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); specsClient .GetFundingStreamsForSpecification(Arg.Is(specification.Id)) .Returns(fundingStreamResponse); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, Enumerable.Empty <CalculationCurrentVersion>())); specsClient.GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification)); CreateCalculationPageModel pageModel = CreatePageModel(specsClient, mapper); pageModel.PageContext = new PageContext(); pageModel.CreateCalculationViewModel = viewModel; //Act Func <Task> test = async() => await pageModel.OnPostAsync(specificationId); //Assert test .Should() .ThrowExactly <InvalidOperationException>(); }
private async Task <IActionResult> PopulateForm(Specification specification) { IsAuthorizedtoEdit = await _authorizationHelper.DoesUserHavePermission(User, specification, SpecificationActionTypes.CanEditSpecification); IActionResult populateResult = await PopulateAllocationLines(specification.Id); if (populateResult != null) { return(populateResult); } if (AllocationLines.IsNullOrEmpty()) { return(new InternalServerErrorResult($"Failed to load allocation lines for specification id: {specification.Id}")); } PopulatePolicies(specification); PopulateCalculationTypes(); ApiResponse <IEnumerable <CalculationCurrentVersion> > baselinesQuery = await _specsClient.GetBaselineCalculationsBySpecificationId(specification.Id); IActionResult baselinesError = baselinesQuery.IsSuccessOrReturnFailureResult("Calculation baselines"); if (baselinesError != null) { return(baselinesError); } IEnumerable <string> hiddenAllocationLineIds; if (baselinesQuery.Content.AnyWithNullCheck()) { hiddenAllocationLineIds = baselinesQuery.Content.Select(s => s.AllocationLine?.Id).Where(s => !string.IsNullOrWhiteSpace(s)); } else { hiddenAllocationLineIds = Enumerable.Empty <string>(); } HideAllocationLinesForBaselinesJson = JsonConvert.SerializeObject(hiddenAllocationLineIds); AvailableBaselineAllocationLineIds = AllocationLines.Count() - hiddenAllocationLineIds.Count(); return(Page()); }
public async Task OnGet_WhenUserDoesNotHaveEditSpecificationPermission_ThenReturnPageResultWithAuthorizedToEditFlagSetToFalse() { // Arrange IAuthorizationHelper mockAuthHelper = Substitute.For <IAuthorizationHelper>(); mockAuthHelper .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification)) .Returns(false); ISpecsApiClient specsClient = CreateSpecsApiClient(); Specification specification = new Specification() { Id = specificationId, FundingPeriod = new Reference("fp1", "FP 2"), }; specsClient.GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification)); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, Enumerable.Empty <CalculationCurrentVersion>())); List <FundingStream> fundingStreams = new List <FundingStream>(); fundingStreams.Add(new FundingStream { Id = fundingStreamId, AllocationLines = new List <AllocationLine>() { new AllocationLine() { Id = "al1", Name = "Allocation Line 1", } } }); ApiResponse <IEnumerable <FundingStream> > fundingStreamResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); specsClient .GetFundingStreamsForSpecification(Arg.Is(specification.Id)) .Returns(fundingStreamResponse); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, Enumerable.Empty <CalculationCurrentVersion>())); CreateCalculationPageModel pageModel = CreatePageModel(specsClient: specsClient, authorizationHelper: mockAuthHelper); // Act IActionResult result = await pageModel.OnGetAsync(specificationId); // Assert result .Should() .BeOfType <PageResult>(); pageModel .IsAuthorizedtoEdit .Should() .BeFalse(); }
public async Task OnPostAsync_GivenCalculationDoesNotAlreadyExistAndCalcTypeIsFundingButNoAllocationLineSelected_ReturnsPage() { //Arrange CreateCalculationViewModel viewModel = new CreateCalculationViewModel { Name = "any name", CalculationType = "Funding" }; Specification specification = CreateSpecification(); IEnumerable <AllocationLine> allocationLines = new[] { new AllocationLine { Id = "alloc-id", Name = "alloc-name" } }; List <FundingStream> fundingStreams = new List <FundingStream>(); FundingStream fundingStream = new FundingStream { Id = fundingStreamId, AllocationLines = allocationLines }; fundingStreams.Add(fundingStream); List <CalculationCurrentVersion> baselineCalculations = new List <CalculationCurrentVersion>(); ApiResponse <IEnumerable <FundingStream> > fundingStreamResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); CalculationCreateModel createModel = new CalculationCreateModel { SpecificationId = specificationId }; ApiResponse <Calculation> calcApiResponse = new ApiResponse <Calculation>(HttpStatusCode.NotFound); ApiResponse <Specification> apiResponse = new ApiResponse <Specification>(HttpStatusCode.OK, specification); ApiResponse <IEnumerable <CalculationCurrentVersion> > baselineCalculationsResponse = new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, baselineCalculations); ISpecsApiClient specsClient = CreateSpecsApiClient(); specsClient .GetCalculationBySpecificationIdAndCalculationName(Arg.Is(specificationId), Arg.Is(viewModel.Name)) .Returns(calcApiResponse); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(apiResponse); specsClient .GetFundingStreamsForSpecification(Arg.Is(specification.Id)) .Returns(fundingStreamResponse); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(baselineCalculationsResponse); CreateCalculationPageModel pageModel = CreatePageModel(specsClient); pageModel.PageContext = new PageContext(); pageModel.CreateCalculationViewModel = viewModel; //Act IActionResult result = await pageModel.OnPostAsync(specificationId); //Assert result .Should() .NotBeNull(); result .Should() .BeAssignableTo <PageResult>(); pageModel .ModelState .ErrorCount .Should() .Be(1); }
public async Task OnGetAsync_GivenSpecificationFound_PopulatesFormReturnsPage() { //Arrange Specification specification = CreateSpecification(); IEnumerable <AllocationLine> allocationLines = new[] { new AllocationLine { Id = "alloc-id", Name = "alloc-name" } }; List <FundingStream> fundingStreams = new List <FundingStream>(); FundingStream fundingStream = new FundingStream { Id = fundingStreamId, AllocationLines = allocationLines }; fundingStreams.Add(fundingStream); List <CalculationCurrentVersion> baselineCalculations = new List <CalculationCurrentVersion>(); baselineCalculations.Add(new CalculationCurrentVersion() { AllocationLine = new Reference("AL1", "Allocation Line Name"), }); ApiResponse <IEnumerable <FundingStream> > fundingStreamResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); ApiResponse <Specification> apiResponse = new ApiResponse <Specification>(HttpStatusCode.OK, specification); ApiResponse <IEnumerable <CalculationCurrentVersion> > baselineCalculationsResult = new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, baselineCalculations); ISpecsApiClient specsClient = CreateSpecsApiClient(); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(apiResponse); specsClient .GetFundingStreamsForSpecification(Arg.Is(specification.Id)) .Returns(fundingStreamResponse); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(baselineCalculationsResult); CreateCalculationPageModel pageModel = CreatePageModel(specsClient); //Act IActionResult result = await pageModel.OnGetAsync(specificationId); //Assert result .Should() .NotBeNull(); result .Should() .BeAssignableTo <PageResult>(); pageModel .FundingPeriodName .Should() .Be("2018 - 19"); pageModel .FundingPeriodId .Should() .Be("2018/19"); pageModel .SpecificationName .Should() .Be("spec-name"); pageModel .Policies .Count .Should() .Be(2); pageModel .AllocationLines .Count() .Should() .Be(1); pageModel .CalculationTypes .Any() .Should() .BeTrue(); pageModel .IsAuthorizedtoEdit .Should().BeTrue(); pageModel .HideAllocationLinesForBaselinesJson .Should() .Be("[\"AL1\"]"); }
private async Task <IActionResult> PopulateForm(Specification specification, string calculationId) { Specification = _mapper.Map <SpecificationViewModel>(specification); PopulateAllocationLines(specification); if (AllocationLines.IsNullOrEmpty()) { return(new InternalServerErrorResult($"Failed to load allocation lines for specification id: {specification.Id}")); } PopulatePolicies(specification); PopulateCalculationTypes(); ApiResponse <IEnumerable <CalculationCurrentVersion> > baselinesQuery = await _specsClient.GetBaselineCalculationsBySpecificationId(specification.Id); IActionResult baselinesError = baselinesQuery.IsSuccessOrReturnFailureResult("Calculation baselines"); if (baselinesError != null) { return(baselinesError); } IEnumerable <string> hiddenAllocationLineIds; if (baselinesQuery.Content.AnyWithNullCheck()) { hiddenAllocationLineIds = baselinesQuery.Content.Select(s => s.AllocationLine?.Id).Where(s => !string.IsNullOrWhiteSpace(s)); } else { hiddenAllocationLineIds = Enumerable.Empty <string>(); } HideAllocationLinesForBaselinesJson = JsonConvert.SerializeObject(hiddenAllocationLineIds); ExistingAllocationLineId = JsonConvert.SerializeObject(specification.GetCalculationById(calculationId)?.AllocationLine?.Id); AvailableBaselineAllocationLineIds = AllocationLines.Count() - hiddenAllocationLineIds.Count(); return(Page()); }
public async Task EditCalculationPageModel_OnPostAsync_WhenInvalidCalculationDetailsProvided_ThenCalculationPageIsReturnedWithModelStateErrors() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; EditCalculationViewModel viewModel = new EditCalculationViewModel() { Name = null, AllocationLineId = "al2", CalculationType = "Number", Description = "Updated description", IsPublic = true, PolicyId = "pol2", }; Calculation resultCalculation = new Calculation() { Id = calculationId, Name = "Calculation Name", AllocationLine = new Reference("al1", "Allocation Line"), CalculationType = CalculationSpecificationType.Funding, Description = "Calculation Description", IsPublic = false, }; List <CalculationCurrentVersion> baselineCalculations = new List <CalculationCurrentVersion>(); ISpecsApiClient specsClient = CreateSpecsClient(); specsClient .UpdateCalculation(Arg.Is(specificationId), Arg.Is(calculationId), Arg.Any <CalculationUpdateModel>()) .Returns(new ValidatedApiResponse <Calculation>(HttpStatusCode.OK, resultCalculation)); Specification specification = CreateSpecification(specificationId); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification)); ApiResponse <IEnumerable <CalculationCurrentVersion> > baselineCalculationsResponse = new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, baselineCalculations); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(baselineCalculationsResponse); EditCalculationPageModel pageModel = CreatePageModel(specsClient); pageModel.PageContext = new PageContext(); pageModel.EditCalculationViewModel = viewModel; pageModel.ModelState.AddModelError(nameof(EditCalculationViewModel.Name), "Invalid Name"); // Act IActionResult result = await pageModel.OnPostAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <PageResult>() .Which .Should() .NotBeNull(); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); pageModel .EditCalculationViewModel .Should() .BeEquivalentTo <EditCalculationViewModel>(new EditCalculationViewModel() { AllocationLineId = "al2", CalculationType = "Number", Description = "Updated description", IsPublic = true, PolicyId = "pol2", Name = null, }); pageModel .Specification .Should() .BeEquivalentTo(new SpecificationViewModel() { Id = specificationId, Name = "Specification Name", FundingStreams = new List <ReferenceViewModel>() { new ReferenceViewModel("fsId", "Funding Stream 1"), new ReferenceViewModel("fs2Id", "Funding Stream 2"), }, }, c => c.Excluding(m => m.Policies)); pageModel .Policies .Should() .BeEquivalentTo(new List <SelectListItem>() { new SelectListItem() { Disabled = false, Selected = false, Text = "Policy 1", Value = "p1", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 1", Value = "sub1", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 2", Value = "sub2", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 2", Value = "p2", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 3", Value = "p3", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 3", Value = "sub3", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 4", Value = "p4", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, } }); pageModel .AllocationLines .Should() .BeEquivalentTo(new List <SelectListItem>() { new SelectListItem() { Disabled = false, Selected = false, Text = "Funding Stream - Allocation Line 1", Value = "al1", }, new SelectListItem { Disabled = false, Selected = true, Text = "Funding Stream - Allocation Line 2", Value = "al2", }, new SelectListItem { Disabled = false, Selected = false, Text = "Funding Stream 2 - Allocation Line 1", Value = "al3", }, new SelectListItem { Disabled = false, Selected = false, Text = "Funding Stream 2 - Allocation Line 2", Value = "al4", } }); pageModel .ModelState .IsValid .Should() .BeFalse(); pageModel .ModelState .Should() .HaveCount(1); pageModel .IsAuthorizedToEdit .Should().BeTrue(); }
public async Task EditCalculationPageModel_OnGetAsync_WhenUserDoesNotHaveEditSpecificationPermission_ThenReturnOkWithAuthorizedToEditFlagSetToFalse() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; CalculationCurrentVersion calculation = new CalculationCurrentVersion() { Id = calculationId, Name = "Calculation Name", AllocationLine = new Reference("al1", "Allocation Line"), CalculationType = CalculationSpecificationType.Funding, Description = "Calculation Description", IsPublic = false, PolicyId = "policyId", PolicyName = "Policy Name" }; Specification specification = CreateSpecification(specificationId); ISpecsApiClient specsClient = CreateSpecsClient(); specsClient .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)) .Returns(new ApiResponse <CalculationCurrentVersion>(HttpStatusCode.OK, calculation)); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification)); List <FundingStream> fundingStreams = new List <FundingStream>(); fundingStreams.Add(new FundingStream { Id = "fs1", AllocationLines = new List <AllocationLine>() { new AllocationLine() { Id = "al1", Name = "Allocation Line 1", } } }); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specification.Id)) .Returns(new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, Enumerable.Empty <CalculationCurrentVersion>())); ApiResponse <IEnumerable <FundingStream> > fundingStreamResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); specsClient .GetFundingStreamsForSpecification(Arg.Is(specification.Id)) .Returns(fundingStreamResponse); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification)) .Returns(false); EditCalculationPageModel pageModel = CreatePageModel(specsClient, authorizationHelper: authorizationHelper); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <PageResult>() .Which .Should() .NotBeNull(); pageModel .IsAuthorizedToEdit .Should() .BeFalse(); }
public async Task EditCalculationPageModel_OnGetAsync_WhenCalculationFound_ThenEditFormIsDisplayed() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; CalculationCurrentVersion calculation = new CalculationCurrentVersion() { Id = calculationId, Name = "Calculation Name", AllocationLine = new Reference("al1", "Allocation Line"), CalculationType = CalculationSpecificationType.Funding, Description = "Calculation Description", IsPublic = false, PolicyId = "policyId", PolicyName = "Policy Name" }; List <CalculationCurrentVersion> baselineCalculations = new List <CalculationCurrentVersion>(); baselineCalculations.Add(new CalculationCurrentVersion() { AllocationLine = new Reference("AL1", "Allocation Line 1"), }); ApiResponse <IEnumerable <CalculationCurrentVersion> > baselineCalculationsResponse = new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, baselineCalculations); Specification specification = CreateSpecification(specificationId); ISpecsApiClient specsClient = CreateSpecsClient(); specsClient .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)) .Returns(new ApiResponse <CalculationCurrentVersion>(HttpStatusCode.OK, calculation)); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification)); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(baselineCalculationsResponse); EditCalculationPageModel pageModel = CreatePageModel(specsClient); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <PageResult>() .Which .Should() .NotBeNull(); pageModel .EditCalculationViewModel .Should() .BeEquivalentTo <EditCalculationViewModel>(new EditCalculationViewModel() { Name = "Calculation Name", AllocationLineId = "al1", CalculationType = "Funding", Description = "Calculation Description", IsPublic = false, PolicyId = "policyId", }); pageModel .Specification .Should() .BeEquivalentTo(new SpecificationViewModel() { Id = specificationId, Name = "Specification Name", FundingStreams = new List <ReferenceViewModel>() { new ReferenceViewModel("fsId", "Funding Stream 1"), new ReferenceViewModel("fs2Id", "Funding Stream 2"), }, }, c => c.Excluding(m => m.Policies)); pageModel .Policies .Should() .BeEquivalentTo(new List <SelectListItem>() { new SelectListItem() { Disabled = false, Selected = false, Text = "Policy 1", Value = "p1", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 1", Value = "sub1", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 2", Value = "sub2", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 2", Value = "p2", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 3", Value = "p3", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 3", Value = "sub3", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 4", Value = "p4", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, } }); pageModel .AllocationLines .Should() .BeEquivalentTo(new List <SelectListItem>() { new SelectListItem() { Disabled = false, Selected = true, Text = "Funding Stream - Allocation Line 1", Value = "al1", }, new SelectListItem { Disabled = false, Selected = false, Text = "Funding Stream - Allocation Line 2", Value = "al2", }, new SelectListItem { Disabled = false, Selected = false, Text = "Funding Stream 2 - Allocation Line 1", Value = "al3", }, new SelectListItem { Disabled = false, Selected = false, Text = "Funding Stream 2 - Allocation Line 2", Value = "al4", } }); pageModel .HideAllocationLinesForBaselinesJson .Should() .Be("[\"AL1\"]"); pageModel .IsAuthorizedToEdit .Should().BeTrue(); }