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>(); }
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"); }
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"); }
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); }
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(); }
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); }