public async Task OnGet_WhenUserDoesHaveEditCalculationsPermission_ThenDoesUserHavePermissionToApproveOrEditIsTrue() { // Arrange string calculationId = "5"; Calculation calcsCalculation = new Calculation() { SpecificationId = "abc123", CalculationSpecification = new Reference { Id = "cs345", Name = "calc name" } }; ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); calcsClient .GetCalculationById(calculationId) .Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, calcsCalculation)); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditCalculations)) .Returns(true); EditCalculationPageModel pageModel = CreatePageModel(calcsClient: calcsClient, authorizationHelper: authorizationHelper); // Act IActionResult result = await pageModel.OnGet("5"); // Assert pageModel.DoesUserHavePermissionToApproveOrEdit.Should().Be("true"); }
public async Task EditCalculationPageModel_OnGetAsync_WhenSpecificationNotFound_ThenPreconditionFailedReturned() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; ISpecsApiClient specsClient = CreateSpecsClient(); ILogger logger = CreateLogger(); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.NotFound, new Specification())); EditCalculationPageModel pageModel = CreatePageModel(specsClient, logger); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <PreconditionFailedResult>() .Which .Value .Should() .Be("Specification not found"); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); await specsClient .Received(0) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task EditCalculationPageModel_OnPostAsync_WhenUserDoesNotHaveEditSpecificationPermission_ThenForbidResultReturned() { // 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)); 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.OnPostAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <ForbidResult>(); pageModel .IsAuthorizedToEdit .Should().BeFalse(); }
public async Task OnGet_WhenCalculationExistdCalculationTypeIsFunding_ThenCalculationDisplayTypeIsFunding() { // Arrange ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); string calculationId = "5"; Calculation calcsCalculation = new Calculation() { Id = calculationId, CalculationSpecification = new Reference(calculationId, "Test Calculation Specification"), SpecificationId = "54", SourceCode = "Test Source Code", CalculationType = Clients.SpecsClient.Models.CalculationSpecificationType.Funding }; Clients.SpecsClient.Models.CalculationCurrentVersion specsCalculation = new Clients.SpecsClient.Models.CalculationCurrentVersion() { Id = calculationId, Name = "Specs Calculation", Description = "Spec Description", }; calcsClient .GetCalculationById(calculationId) .Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, calcsCalculation)); specsClient .GetCalculationById(calcsCalculation.SpecificationId, calculationId) .Returns(new ApiResponse <Clients.SpecsClient.Models.CalculationCurrentVersion>(System.Net.HttpStatusCode.OK, specsCalculation)); EditCalculationPageModel pageModel = CreatePageModel(specsClient: specsClient, calcsClient: calcsClient, mapper: mapper); // Act IActionResult result = await pageModel.OnGet(calculationId); // Assert result.Should().NotBeNull(); result.Should().BeOfType <PageResult>(); pageModel.Calculation.Should().NotBeNull(); pageModel.Calculation.Name.Should().Be(calcsCalculation.Name); pageModel.Calculation.Description.Should().Be(specsCalculation.Description); pageModel.SpecificationId.Should().Be(calcsCalculation.SpecificationId); pageModel.EditModel.SourceCode.Should().Be(calcsCalculation.SourceCode); pageModel.Calculation.CalculationType.Should().Be(CalculationSpecificationTypeViewModel.Funding); }
public static EditCalculationPageModel CreatePageModel( ISpecsApiClient specsClient = null, ILogger logger = null, IMapper mapper = null, IAuthorizationHelper authorizationHelper = null) { EditCalculationPageModel pageModel = new EditCalculationPageModel( specsClient ?? CreateSpecsClient(), logger ?? CreateLogger(), mapper ?? MappingHelper.CreateFrontEndMapper(), authorizationHelper ?? TestAuthHelper.CreateAuthorizationHelperSubstitute(SpecificationActionTypes.CanEditSpecification)); pageModel.PageContext = TestAuthHelper.CreatePageContext(); return(pageModel); }
public async Task OnGet_WhenCalculationDoesNotExistThenNotFoundReturned() { // Arrange IMapper mapper = MappingHelper.CreateFrontEndMapper(); string calculationId = "5"; EditCalculationPageModel pageModel = CreatePageModel(mapper: mapper); // Act IActionResult result = await pageModel.OnGet(calculationId); // Assert result.Should().NotBeNull(); result.Should().BeOfType <NotFoundObjectResult>(); }
private static EditCalculationPageModel CreatePageModel( ISpecsApiClient specsClient = null, ICalculationsApiClient calcsClient = null, IMapper mapper = null, IFeatureToggle features = null, IAuthorizationHelper authorizationHelper = null, IResultsApiClient resultsApiClient = null) { EditCalculationPageModel pageModel = new EditCalculationPageModel( specsClient ?? CreateSpecsApiClient(), calcsClient ?? CreateCalcsApiClient(), mapper ?? CreateMapper(), features ?? CreateFeatureToggle(), authorizationHelper ?? TestAuthHelper.CreateAuthorizationHelperSubstitute(SpecificationActionTypes.CanEditCalculations), resultsApiClient ?? CreateResultsApiClient()); pageModel.PageContext = TestAuthHelper.CreatePageContext(); return(pageModel); }
public async Task EditCalculationPageModel_OnGetAsync_WhenCalculationApiResponseContentIsNull_ThenInternalErrorIsReturned() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; ISpecsApiClient specsClient = CreateSpecsClient(); ILogger logger = CreateLogger(); specsClient .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)) .Returns(new ApiResponse <CalculationCurrentVersion>(HttpStatusCode.OK, null)); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, new Specification())); EditCalculationPageModel pageModel = CreatePageModel(specsClient, logger); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <InternalServerErrorResult>() .Which .Value .Should() .Be("Calculation content returned null"); logger .Received(1) .Warning("Calculation Result API response content returned null for calculation ID '{calculationId}' on specificationId '{specificationId}'", calculationId, specificationId); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); await specsClient .Received(1) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task EditCalculationPageModel_OnGetAsync_WhenCalculationApiResponseIsNotSuccessful_ThenInternalErrorIsReturned() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; ISpecsApiClient specsClient = CreateSpecsClient(); ILogger logger = CreateLogger(); specsClient .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)) .Returns(new ApiResponse <CalculationCurrentVersion>(HttpStatusCode.InternalServerError, null)); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, new Specification())); EditCalculationPageModel pageModel = CreatePageModel(specsClient, logger); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <InternalServerErrorResult>() .Which .Value .Should() .Be("Unexpected status code from Calculation API call 'InternalServerError'"); logger .Received(1) .Warning($"Unexpected status code from Calculation API call 'InternalServerError'"); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); await specsClient .Received(1) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task OnGet_WhenCalculationIdNotProvidedThenBadResultReturned() { // Arrange IMapper mapper = MappingHelper.CreateFrontEndMapper(); string calculationId = null; EditCalculationPageModel pageModel = CreatePageModel(mapper: mapper); // Act IActionResult result = await pageModel.OnGet(calculationId); // Assert result.Should().NotBeNull(); result.Should().BeOfType <BadRequestObjectResult>(); BadRequestObjectResult typedResult = result as BadRequestObjectResult; typedResult.Value.Should().Be("Enter a unique name"); }
public async Task OnGet_WhenUserDoesNotHaveEditCalculationsPermission_ThenDoesUserHavePermissionToApproveOrEditIsFalse() { // Arrange IMapper mapper = MappingHelper.CreateFrontEndMapper(); string calculationId = "5"; Calculation calcsCalculation = new Calculation() { SpecificationId = "abc123", CalculationSpecification = new Reference { Id = "cs345", Name = "calc name" } }; ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); calcsClient .GetCalculationById(calculationId) .Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, calcsCalculation)); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditCalculations)) .Returns(false); EditCalculationPageModel pageModel = CreatePageModel(calcsClient: calcsClient, mapper: mapper, authorizationHelper: authorizationHelper); // Act IActionResult result = await pageModel.OnGet(calculationId); // Assert pageModel.DoesUserHavePermissionToApproveOrEdit.Should().Be("false"); string shouldShowGreyBackground = pageModel.ViewData["GreyBackground"].ToString(); shouldShowGreyBackground .Should() .Be("False"); }
public async Task EditCalculationPageModel_OnGetAsync_WhenCalculationNotFound_ThenNotFoundIsReturned() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; ISpecsApiClient specsClient = CreateSpecsClient(); specsClient .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)) .Returns(new ApiResponse <CalculationCurrentVersion>(HttpStatusCode.NotFound, null)); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, new Specification())); EditCalculationPageModel pageModel = CreatePageModel(specsClient); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <NotFoundObjectResult>() .Which .Value .Should() .Be("Calculation not found"); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); await specsClient .Received(1) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task OnGet_WhenSpecCalculationDoesNotExistThenNotFoundReturned() { // Arrange ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); string calculationId = "5"; calcsClient.GetCalculationById(calculationId).Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, new Calculation() { Id = calculationId, CalculationSpecification = new Reference("1", "Test Calculation Specification"), SpecificationId = "54", })); EditCalculationPageModel pageModel = CreatePageModel(calcsClient: calcsClient, mapper: mapper); // Act IActionResult result = await pageModel.OnGet(calculationId); // Assert result.Should().NotBeNull(); result.Should().BeOfType <NotFoundObjectResult>(); }
public async Task EditCalculationPageModel_OnPostAsync_WhenValidCalculationDetailsProvided_ThenCalculationIsEdited() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; EditCalculationViewModel viewModel = new EditCalculationViewModel() { Name = "Updated Name", 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, }; ISpecsApiClient specsClient = CreateSpecsClient(); specsClient .UpdateCalculation(Arg.Is(specificationId), Arg.Is(calculationId), Arg.Any <CalculationUpdateModel>()) .Returns(new ValidatedApiResponse <Calculation>(HttpStatusCode.OK, resultCalculation)); EditCalculationPageModel pageModel = CreatePageModel(specsClient); pageModel.PageContext = new PageContext(); pageModel.EditCalculationViewModel = viewModel; // Act IActionResult result = await pageModel.OnPostAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <RedirectResult>() .Which .Url .Should() .Be($"/specs/policies/{specificationId}?operationId={calculationId}&operationType=CalculationUpdated"); await specsClient .Received(1) .UpdateCalculation( Arg.Is(specificationId), Arg.Is(calculationId), Arg.Is <CalculationUpdateModel>( m => m.AllocationLineId == viewModel.AllocationLineId && m.Description == viewModel.Description && m.IsPublic == viewModel.IsPublic && m.Name == viewModel.Name && m.PolicyId == viewModel.PolicyId )); }
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(); }
public async Task OnGet_WhenCalculationExistdCalculationTypeIsFundingAndShouldNewEditCalculationPageBeEnabledIsTurnedOnAndResultFound_ThenCalculationDisplayTypeIsFundingSetsHasCalculationResultToTrue() { // Arrange ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); IFeatureToggle featureToggle = CreateFeatureToggle(); featureToggle .IsNewEditCalculationPageEnabled() .Returns(true); string calculationId = "5"; Calculation calcsCalculation = new Calculation() { Id = calculationId, CalculationSpecification = new Reference(calculationId, "Test Calculation Specification"), SpecificationId = "54", SourceCode = "Test Source Code", CalculationType = Clients.SpecsClient.Models.CalculationSpecificationType.Funding }; Clients.SpecsClient.Models.CalculationCurrentVersion specsCalculation = new Clients.SpecsClient.Models.CalculationCurrentVersion() { Id = calculationId, Name = "Specs Calculation", Description = "Spec Description", }; calcsClient .GetCalculationById(calculationId) .Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, calcsCalculation)); specsClient .GetCalculationById(calcsCalculation.SpecificationId, calculationId) .Returns(new ApiResponse <Clients.SpecsClient.Models.CalculationCurrentVersion>(System.Net.HttpStatusCode.OK, specsCalculation)); ApiResponse <bool> hasCalcsResponse = new ApiResponse <bool>(HttpStatusCode.OK, true); IResultsApiClient resultsApiClient = CreateResultsApiClient(); resultsApiClient .HasCalculationResults(Arg.Is(calcsCalculation.Id)) .Returns(hasCalcsResponse); EditCalculationPageModel pageModel = CreatePageModel(specsClient: specsClient, calcsClient: calcsClient, mapper: mapper, features: featureToggle, resultsApiClient: resultsApiClient); // Act IActionResult result = await pageModel.OnGet(calculationId); // Assert result.Should().NotBeNull(); result.Should().BeOfType <PageResult>(); pageModel.Calculation.Should().NotBeNull(); pageModel.Calculation.Name.Should().Be(calcsCalculation.Name); pageModel.Calculation.Description.Should().Be(specsCalculation.Description); pageModel.SpecificationId.Should().Be(calcsCalculation.SpecificationId); pageModel.EditModel.SourceCode.Should().Be(calcsCalculation.SourceCode); pageModel.Calculation.CalculationType.Should().Be(CalculationSpecificationTypeViewModel.Funding); pageModel.CalculationHasResults.Should().BeTrue(); string shouldShowGreyBackground = pageModel.ViewData["GreyBackground"].ToString(); shouldShowGreyBackground .Should() .Be("True"); }
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 OnGet_WhenCalculationExistsThenCalculationReturned() { // Arrange ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); const string calculationId = "5"; const string specificationId = "specId"; const string specificationName = "Spec Name"; Calculation calcsCalculation = new Calculation() { Id = calculationId, CalculationSpecification = new Reference(calculationId, "Test Calculation Specification"), SpecificationId = specificationId, SourceCode = "Test Source Code" }; Clients.SpecsClient.Models.CalculationCurrentVersion specsCalculation = new Clients.SpecsClient.Models.CalculationCurrentVersion() { Id = calculationId, Name = "Specs Calculation", Description = "Spec Description", }; calcsClient .GetCalculationById(calculationId) .Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, calcsCalculation)); specsClient .GetCalculationById(calcsCalculation.SpecificationId, calculationId) .Returns(new ApiResponse <Clients.SpecsClient.Models.CalculationCurrentVersion>(System.Net.HttpStatusCode.OK, specsCalculation)); Clients.SpecsClient.Models.SpecificationSummary specificationSummary = new Clients.SpecsClient.Models.SpecificationSummary() { Id = specificationId, Name = specificationName }; specsClient .GetSpecificationSummary(Arg.Is(specificationId)) .Returns(new ApiResponse <Clients.SpecsClient.Models.SpecificationSummary>(System.Net.HttpStatusCode.OK, specificationSummary)); EditCalculationPageModel pageModel = CreatePageModel(specsClient: specsClient, calcsClient: calcsClient, mapper: mapper); // Act IActionResult result = await pageModel.OnGet(calculationId); // Assert result.Should().NotBeNull(); result.Should().BeOfType <PageResult>(); pageModel.Calculation.Should().NotBeNull(); pageModel.Calculation.Name.Should().Be(calcsCalculation.Name); pageModel.Calculation.Description.Should().Be(specsCalculation.Description); pageModel.SpecificationId.Should().Be(calcsCalculation.SpecificationId); pageModel.EditModel.SourceCode.Should().Be(calcsCalculation.SourceCode); pageModel.SpecificationName.Should().Be(specificationName); await calcsClient .Received(1) .GetCalculationById(Arg.Is(calculationId)); await specsClient .Received(1) .GetSpecificationSummary(Arg.Is(specificationId)); await specsClient .Received(1) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task EditCalculationPageModel_OnPostAsync_WhenInvalidCalculationDetailsProvidedAndValidationMessagesComeFromApi_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, }; ISpecsApiClient specsClient = CreateSpecsClient(); ValidatedApiResponse <Calculation> validatedResponse = new ValidatedApiResponse <Calculation>(HttpStatusCode.BadRequest, resultCalculation) { ModelState = new Dictionary <string, IEnumerable <string> >() { { "name", new string[] { "Name was not provided" } } } }; specsClient .UpdateCalculation(Arg.Is(specificationId), Arg.Is(calculationId), Arg.Any <CalculationUpdateModel>()) .Returns(validatedResponse); Specification specification = CreateSpecification(specificationId); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification)); EditCalculationPageModel pageModel = CreatePageModel(specsClient); pageModel.PageContext = new PageContext(); pageModel.EditCalculationViewModel = viewModel; // 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 .ModelState .Values .First() .Errors .First() .ErrorMessage .Should() .Be("Name was not provided"); pageModel .IsAuthorizedToEdit .Should().BeTrue(); }