Example #1
0
        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();
        }
Example #2
0
        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
                    ));
        }