예제 #1
0
        public async Task OnPostAsync_GivenUserDoesNotHaveEditSpecificationPermission_ThenForbidResultReturned()
        {
            // Arrange
            ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>();

            specsClient
            .GetSpecification(Arg.Is(specificationId))
            .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, new Specification {
                Id = specificationId
            }));

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

            authorizationHelper
            .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification))
            .Returns(false);

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

            // Act
            IActionResult result = await pageModel.OnPostAsync(specificationId);

            // Assert
            pageModel
            .IsAuthorizedToEdit
            .Should().BeFalse();

            result.Should().BeOfType <ForbidResult>();
        }
예제 #2
0
        public void OnPostAsync_GivenNameAlreadyExistsAndPopulateFundingPeriodsReturnsOKButNullContent_ThrowsInvalidOperationException()
        {
            //Arrange
            ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK);

            ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK);

            ISpecsApiClient apiClient = CreateApiClient();

            apiClient
            .GetSpecificationByName(Arg.Is(specName))
            .Returns(existingSpecificationResponse);

            apiClient
            .GetFundingPeriods()
            .Returns(fundingPeriodsResponse);

            EditSpecificationPageModel pageModel = CreatePageModel(apiClient);

            pageModel.PageContext = new PageContext();

            pageModel.EditSpecificationViewModel = new EditSpecificationViewModel
            {
                Name = specName
            };

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

            test
            .Should()
            .ThrowExactly <InvalidOperationException>();
        }
예제 #3
0
        private static EditSpecificationPageModel CreatePageModel(ISpecsApiClient specsClient = null, IMapper mapper = null, IAuthorizationHelper authorizationHelper = null)
        {
            EditSpecificationPageModel pageModel = new EditSpecificationPageModel(specsClient ?? CreateApiClient(), mapper ?? CreateMapper(), authorizationHelper ?? TestAuthHelper.CreateAuthorizationHelperSubstitute(SpecificationActionTypes.CanEditSpecification));

            pageModel.PageContext = TestAuthHelper.CreatePageContext();

            return(pageModel);
        }
예제 #4
0
        public async Task OnPostAsync_GivenViewModelIsValidAndRedirectToSpecificationsActionProvided_ThenSpecificationIsEditedAndReturnsRedirectToSpecificationsPage()
        {
            //Arrange
            ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.NotFound);

            EditSpecificationModel editModel = new EditSpecificationModel();

            EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel();

            ISpecsApiClient apiClient = CreateApiClient();

            apiClient
            .GetSpecificationByName(Arg.Is(specName))
            .Returns(existingSpecificationResponse);

            apiClient
            .UpdateSpecification(Arg.Is(specificationId), Arg.Any <EditSpecificationModel>())
            .Returns(HttpStatusCode.OK);

            IMapper mapper = CreateMapper();

            mapper
            .Map <EditSpecificationModel>(Arg.Is(viewModel))
            .Returns(editModel);

            EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper);

            pageModel.EditSpecificationViewModel = viewModel;

            pageModel.PageContext = new PageContext();

            //Act
            IActionResult result = await pageModel.OnPostAsync(specificationId, EditSpecificationRedirectAction.Specifications);

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

            RedirectResult redirectResult = result as RedirectResult;

            redirectResult
            .Url
            .Should()
            .Be("/specs?operationType=SpecificationUpdated&operationId=spec-id");

            await
            apiClient
            .Received(1)
            .UpdateSpecification(Arg.Is(specificationId), Arg.Is(editModel));
        }
예제 #5
0
        public void OnGetAsync_GivenToPopulateFundingPeriodsIsOKButFundingStreamsReturnsOKButNullContent_ThrowsInvalidOperationException()
        {
            //Arrange
            Specification specification = new Specification();

            EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel();

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

            ApiResponse <Specification> specificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK, specification);

            ISpecsApiClient apiClient = CreateApiClient();

            apiClient
            .GetSpecification(Arg.Is(specificationId))
            .Returns(specificationResponse);

            apiClient
            .GetFundingPeriods()
            .Returns(fundingPeriodsResponse);

            apiClient
            .GetFundingStreams()
            .Returns(fundingStreamsResponse);

            IMapper mapper = CreateMapper();

            mapper
            .Map <EditSpecificationViewModel>(Arg.Is(specification))
            .Returns(viewModel);

            EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper);

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

            test
            .Should()
            .ThrowExactly <InvalidOperationException>();
        }
예제 #6
0
        public async Task OnPostAsync_GivenViewModelIsValidAndUpdateSpecificationCallFails_ThenInternalServerErrorReturned()
        {
            // Arrange
            ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.NotFound);

            EditSpecificationModel editModel = new EditSpecificationModel();

            EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel();

            ISpecsApiClient apiClient = CreateApiClient();

            apiClient
            .GetSpecificationByName(Arg.Is(specName))
            .Returns(existingSpecificationResponse);

            apiClient
            .UpdateSpecification(Arg.Is(specificationId), Arg.Any <EditSpecificationModel>())
            .Returns(HttpStatusCode.InternalServerError);

            IMapper mapper = CreateMapper();

            mapper
            .Map <EditSpecificationModel>(Arg.Is(viewModel))
            .Returns(editModel);

            EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper);

            pageModel.EditSpecificationViewModel = viewModel;

            pageModel.PageContext = new PageContext();

            // Act
            IActionResult result = await pageModel.OnPostAsync(specificationId, EditSpecificationRedirectAction.Specifications);

            //Assert
            result
            .Should()
            .BeOfType <InternalServerErrorResult>()
            .Which
            .Value
            .Should()
            .Be("Unable to update specification. API returned 'InternalServerError'");

            await
            apiClient
            .Received(1)
            .UpdateSpecification(Arg.Is(specificationId), Arg.Is(editModel));
        }
예제 #7
0
        public void OnGetAsync_GivenNullOrEmptySpecificationId_ThrowsArgumentException()
        {
            //Arrange
            EditSpecificationPageModel pageModel = CreatePageModel();

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

            test
            .Should()
            .ThrowExactly <ArgumentNullException>()
            .Which
            .Message
            .Should()
            .Be("Value cannot be null.\r\nParameter name: specificationId");
        }
예제 #8
0
        public void OnGetAsync_GivenSpecificationIdWithOKResponseButContentIsNull_ThrowsInvalidOperationException()
        {
            //Arrange
            ApiResponse <Specification> specificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK);

            ISpecsApiClient apiClient = CreateApiClient();

            apiClient
            .GetSpecification(Arg.Is(specificationId))
            .Returns(specificationResponse);

            EditSpecificationPageModel pageModel = CreatePageModel(apiClient);

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

            test
            .Should()
            .ThrowExactly <InvalidOperationException>()
            .Which
            .Message
            .Should()
            .Be("Unable to retreive specification. Status Code = OK");
        }
예제 #9
0
        public async Task OnPostAsync_GivenPagePopulatesButModelStateIsInvalid_ReturnsPage()
        {
            //Arrange
            IEnumerable <Reference> fundingPeriods = new[]
            {
                new Reference {
                    Id = "fp1", Name = "funding"
                }
            };

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

            ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK);

            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
            .GetSpecificationByName(Arg.Is(specName))
            .Returns(existingSpecificationResponse);

            apiClient
            .GetFundingPeriods()
            .Returns(fundingPeriodsResponse);

            apiClient
            .GetFundingStreams()
            .Returns(fundingStreamsResponse);

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

            authorizationHelper
            .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification))
            .Returns(true);
            authorizationHelper
            .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification))
            .Returns(fundingStreams);

            EditSpecificationPageModel pageModel = CreatePageModel(specsClient: apiClient, authorizationHelper: authorizationHelper);

            pageModel.EditSpecificationViewModel = new EditSpecificationViewModel
            {
                Name             = specName,
                FundingStreamIds = new List <string> {
                    "fs1"
                }
            };

            pageModel.PageContext = new PageContext();

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

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

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

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

            pageModel
            .IsAuthorizedToEdit
            .Should().BeTrue();
        }
예제 #10
0
        public async Task OnGetAsync_WhenUserDoesNotHaveCreateSpecPermissionOnAllExistingFundingStream_ThenExistingFundingStreamsAvailableForList()
        {
            // Arrange
            Specification specification = new Specification
            {
                Id             = specificationId,
                Name           = "Test Spec",
                FundingStreams = new List <FundingStream>
                {
                    new FundingStream {
                        Id = "fs1", Name = "FS One"
                    },
                    new FundingStream {
                        Id = "fs2", Name = "FS Two"
                    }
                },
                FundingPeriod = new Reference {
                    Id = "fp1", Name = "FP One"
                }
            };

            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 = "fs1", Name = "FS One"
                },
                new FundingStream {
                    Id = "fs2", Name = "FS Two"
                }
            };

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

            specsClient
            .GetSpecification(Arg.Is(specificationId))
            .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification));

            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
            .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification))
            .Returns(true);
            authorizationHelper
            .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Any <IEnumerable <FundingStream> >(), Arg.Is(FundingStreamActionTypes.CanCreateSpecification))
            .Returns(new List <FundingStream>
            {
                new FundingStream {
                    Id = "fs1", Name = "FS One"
                }
            });

            EditSpecificationPageModel pageModel = CreatePageModel(specsClient: specsClient, authorizationHelper: authorizationHelper, mapper: MappingHelper.CreateFrontEndMapper());

            // Act
            await pageModel.OnGetAsync(specificationId);

            // Assert
            pageModel.FundingStreams.Should().HaveCount(2);
        }
예제 #11
0
        public async Task OnGetAsync_GivenUserDoesNotHaveEditSpecificationPermission_ThenReturnPageResultWithAuthorizedToEditFlagSetToFalse()
        {
            // 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"
                }
            };
            Specification specification = new Specification {
                Id = specificationId, Name = specName
            };

            ApiResponse <IEnumerable <Reference> >     fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods);
            ApiResponse <IEnumerable <FundingStream> > fundingStreamsResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams);
            ApiResponse <Specification> specificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK, specification);

            EditSpecificationViewModel viewModelReturned = CreateEditSpecificationViewModel();

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

            mockSpecsClient
            .GetSpecification(Arg.Is(specificationId))
            .Returns(specificationResponse);

            mockSpecsClient
            .GetFundingPeriods()
            .Returns(fundingPeriodsResponse);

            mockSpecsClient
            .GetFundingStreams()
            .Returns(fundingStreamsResponse);

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

            mockAuthorizationHelper
            .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification))
            .Returns(false);
            mockAuthorizationHelper
            .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification))
            .Returns(fundingStreams);

            IMapper mockMapper = CreateMapper();

            mockMapper
            .Map <EditSpecificationViewModel>(Arg.Is(specification))
            .Returns(viewModelReturned);

            EditSpecificationPageModel pageModel = CreatePageModel(specsClient: mockSpecsClient, authorizationHelper: mockAuthorizationHelper, mapper: mockMapper);

            // Act
            IActionResult pageResult = await pageModel.OnGetAsync(specificationId);

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

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

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

            pageModel
            .EditSpecificationViewModel
            .OriginalSpecificationName
            .Should()
            .BeEquivalentTo(specName);

            pageModel
            .IsAuthorizedToEdit
            .Should().BeFalse();
        }
예제 #12
0
        public async Task OnGetAsync_GivenPagePopulatesAndPeriodIdProvided_ReturnsPageSetsPeriodInSelectAsDefault()
        {
            //Arrange
            Specification specification = new Specification
            {
                Id   = specificationId,
                Name = specName
            };

            EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel();

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

            ApiResponse <Specification> specificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK, specification);

            ISpecsApiClient apiClient = CreateApiClient();

            apiClient
            .GetSpecification(Arg.Is(specificationId))
            .Returns(specificationResponse);

            apiClient
            .GetFundingPeriods()
            .Returns(fundingPeriodsResponse);

            apiClient
            .GetFundingStreams()
            .Returns(fundingStreamsResponse);

            IMapper mapper = CreateMapper();

            mapper
            .Map <EditSpecificationViewModel>(Arg.Is(specification))
            .Returns(viewModel);

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

            authorizationHelper
            .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification))
            .Returns(true);
            authorizationHelper
            .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification))
            .Returns(fundingStreams);

            EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper, authorizationHelper);

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

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

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

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

            pageModel
            .IsAuthorizedToEdit
            .Should().BeTrue();

            viewModel
            .OriginalSpecificationName
            .Should()
            .BeEquivalentTo(specName);
        }