public async Task EditCalculationPageModel_OnPostAsync_WhenUserDoesNotHaveEditSpecificationPermission_ThenForbidResultReturned() { // 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)); 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.OnPostAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <ForbidResult>(); pageModel .IsAuthorizedToEdit .Should().BeFalse(); }
async public Task GetCalculationCurrentVersion_GivenCalculationExistsAndWasWasFoundInCache_ReturnsOK() { //Arrange const string specificationId = "specId"; DateTimeOffset lastModifiedDate = DateTimeOffset.Now; CalculationCurrentVersion calculation = new CalculationCurrentVersion { Author = new Reference(UserId, Username), Date = lastModifiedDate, SourceCode = "source code", Version = 1, Name = "any name", Id = CalculationId, CalculationSpecification = new Reference("any name", "any-id"), FundingPeriodName = "2018/2019", FundingPeriodId = "18/19", CalculationType = "Number", SpecificationId = specificationId, }; IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "calculationId", new StringValues(CalculationId) } }); HttpRequest request = Substitute.For <HttpRequest>(); request .Query .Returns(queryStringValues); ILogger logger = CreateLogger(); ICalculationsRepository calculationsRepository = CreateCalculationsRepository(); ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <CalculationCurrentVersion>(Arg.Is($"{CacheKeys.CurrentCalculation}{CalculationId}")) .Returns(calculation); CalculationService service = CreateCalculationService(logger: logger, calculationsRepository: calculationsRepository, cacheProvider: cacheProvider); //Act IActionResult result = await service.GetCalculationCurrentVersion(request); //Assert result .Should() .BeOfType <OkObjectResult>() .Which .Value .Should().BeEquivalentTo(new CalculationCurrentVersion { Author = new Reference(UserId, Username), Date = lastModifiedDate, SourceCode = "source code", Version = 1, Name = "any name", Id = CalculationId, CalculationSpecification = new Reference("any name", "any-id"), FundingPeriodName = "2018/2019", FundingPeriodId = "18/19", CalculationType = "Number", SpecificationId = specificationId, }); await calculationsRepository .Received(0) .GetCalculationById(Arg.Any <string>()); await cacheProvider .Received(1) .GetAsync <CalculationCurrentVersion>(Arg.Is($"{CacheKeys.CurrentCalculation}{CalculationId}")); }
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(); }
public async Task EditCalculationPageModel_OnGetAsync_WhenCalculationFound_ThenEditFormIsDisplayed() { // 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" }; List <CalculationCurrentVersion> baselineCalculations = new List <CalculationCurrentVersion>(); baselineCalculations.Add(new CalculationCurrentVersion() { AllocationLine = new Reference("AL1", "Allocation Line 1"), }); ApiResponse <IEnumerable <CalculationCurrentVersion> > baselineCalculationsResponse = new ApiResponse <IEnumerable <CalculationCurrentVersion> >(HttpStatusCode.OK, baselineCalculations); 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)); specsClient .GetBaselineCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(baselineCalculationsResponse); EditCalculationPageModel pageModel = CreatePageModel(specsClient); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <PageResult>() .Which .Should() .NotBeNull(); pageModel .EditCalculationViewModel .Should() .BeEquivalentTo <EditCalculationViewModel>(new EditCalculationViewModel() { Name = "Calculation Name", AllocationLineId = "al1", CalculationType = "Funding", Description = "Calculation Description", IsPublic = false, PolicyId = "policyId", }); 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 = true, Text = "Funding Stream - Allocation Line 1", Value = "al1", }, new SelectListItem { Disabled = false, Selected = false, 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 .HideAllocationLinesForBaselinesJson .Should() .Be("[\"AL1\"]"); pageModel .IsAuthorizedToEdit .Should().BeTrue(); }