public void OnGetAsync_GivenToPopulayeFundingPeriodsIsOKButFundingStreamsReturnsBadRequest_ThrowsInvalidOperationException()
        {
            //Arrange
            IEnumerable <Reference> fundingPeriods = new[]
            {
                new Reference {
                    Id = "fp1", Name = "funding"
                }
            };

            ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods);

            ApiResponse <IEnumerable <FundingStream> > fundingStreamsResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.BadRequest);

            ISpecsApiClient apiClient = CreateApiClient();

            apiClient
            .GetFundingPeriods()
            .Returns(fundingPeriodsResponse);

            apiClient
            .GetFundingStreams()
            .Returns(fundingStreamsResponse);

            CreateSpecificationPageModel pageModel = CreatePageModel(apiClient);

            //Act/Assert
            Func <Task> test = async() => await pageModel.OnGetAsync();

            test
            .Should()
            .ThrowExactly <InvalidOperationException>();
        }
        public async Task OnGetAsync_GivenUserDoesNotHaveCreateSpecificationPermissionForAnyFundingStream_ThenFundingStreamsShouldBeEmpty()
        {
            // Arrange
            IEnumerable <Reference> fundingPeriods = new[]
            {
                new Reference {
                    Id = "fp1", Name = "Funding Period 1"
                },
                new Reference {
                    Id = "fp2", Name = "Funding Period 2"
                }
            };

            IEnumerable <FundingStream> fundingStreams = new[]
            {
                new FundingStream {
                    Id = "fp1", Name = "funding"
                }
            };

            ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>();

            specsClient
            .GetFundingPeriods()
            .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods));

            specsClient
            .GetFundingStreams()
            .Returns(new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams));

            IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>();

            authorizationHelper
            .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification))
            .Returns(Enumerable.Empty <FundingStream>());

            CreateSpecificationPageModel pageModel = CreatePageModel(specsClient: specsClient, authorizationHelper: authorizationHelper);

            // Act
            IActionResult result = await pageModel.OnGetAsync();

            // Assert
            result.Should().BeOfType <PageResult>();
            pageModel.FundingStreams.Should().BeEmpty();
            pageModel
            .IsAuthorizedToCreate
            .Should().BeFalse();
        }
        public void OnGetAsync_GivenToPopulateFundingPeriodsReturnsOKButNullContent_ThrowsInvalidOperationException()
        {
            //Arrange
            ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK);

            ISpecsApiClient apiClient = CreateApiClient();

            apiClient
            .GetFundingPeriods()
            .Returns(fundingPeriodsResponse);

            CreateSpecificationPageModel pageModel = CreatePageModel(apiClient);

            //Act/Assert
            Func <Task> test = async() => await pageModel.OnGetAsync();

            test
            .Should()
            .ThrowExactly <InvalidOperationException>();
        }
        public async Task OnGetAsync_GivenPagePopulatesAndPeriodIdProvided_ReturnsPageSetsPeriodInSelectAsDefault()
        {
            //Arrange
            IEnumerable <Reference> fundingPeriods = new[]
            {
                new Reference {
                    Id = "fp1", Name = "Funding Period 1"
                },
                new Reference {
                    Id = "fp2", Name = "Funding Period 2"
                }
            };

            IEnumerable <FundingStream> fundingStreams = new[]
            {
                new FundingStream {
                    Id = "fp1", Name = "funding"
                }
            };

            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
            .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);

            //Act
            IActionResult result = await pageModel.OnGetAsync("fp2");

            //Assert
            result
            .Should()
            .BeOfType <PageResult>();

            pageModel
            .FundingStreams
            .Count()
            .Should()
            .Be(1);

            pageModel
            .FundingPeriods
            .Count()
            .Should()
            .Be(2);

            pageModel
            .IsAuthorizedToCreate
            .Should().BeTrue();

            pageModel
            .FundingPeriods
            .First(m => m.Value == "fp2")
            .Selected
            .Should()
            .BeTrue();
        }