private async Task <IActionResult> PopulateAllocationLines(string specificationId)
        {
            List <SelectListItem> result = new List <SelectListItem>();

            ApiResponse <IEnumerable <FundingStream> > fundingStreamResponse = await _specsClient.GetFundingStreamsForSpecification(specificationId);

            if (fundingStreamResponse == null)
            {
                return(new InternalServerErrorResult("Funding Stream lookup API call returned null"));
            }

            if (fundingStreamResponse.StatusCode != HttpStatusCode.OK)
            {
                return(new InternalServerErrorResult($"Funding Stream lookup API call returned HTTP Error '{fundingStreamResponse.StatusCode}'"));
            }

            if (fundingStreamResponse.Content == null)
            {
                return(new InternalServerErrorResult("Funding Stream lookup API call content returned null"));
            }

            foreach (FundingStream fundingStream in fundingStreamResponse.Content)
            {
                result.AddRange(fundingStream.AllocationLines.Select(m => new SelectListItem
                {
                    Value    = m.Id,
                    Text     = m.Name,
                    Selected = m.Id == AllocationLineId
                }));
            }

            AllocationLines = result.OrderBy(c => c.Text);

            return(null);
        }
        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>();
        }
        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 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 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\"]");
        }
Exemple #6
0
        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();
        }