Exemple #1
0
        public void SaveCalculation_OnBackendError_ThenExceptionThrown()
        {
            // Arrange
            ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

            string specificationId = "1";
            string calculationId   = "5";

            IAuthorizationHelper  authorizationHelper = TestAuthHelper.CreateAuthorizationHelperSubstitute(specificationId, SpecificationActionTypes.CanEditCalculations);
            CalculationController controller          = CreateCalculationController(calcsClient, mapper, authorizationHelper);

            calcsClient
            .UpdateCalculation(calculationId, Arg.Any <CalculationUpdateModel>())
            .Returns(Task.FromResult(new ApiResponse <Calculation>(System.Net.HttpStatusCode.ServiceUnavailable, null)));

            CalculationUpdateViewModel updateViewModel = new CalculationUpdateViewModel()
            {
                SourceCode = "Source code",
            };

            // Act
            Action a = new Action(() =>
            {
                IActionResult result = controller.SaveCalculation(specificationId, calculationId, updateViewModel).Result;
            });

            // Assert
            a.Should().Throw <InvalidOperationException>();
        }
Exemple #2
0
        public async Task SaveCalculation_OnInvalidModel_ThenBadRequestReturned()
        {
            // Arrange
            ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

            string specificationId = "1";
            string calculationId   = "5";

            IAuthorizationHelper  authorizationHelper = TestAuthHelper.CreateAuthorizationHelperSubstitute(specificationId, SpecificationActionTypes.CanEditCalculations);
            CalculationController controller          = CreateCalculationController(calcsClient, mapper, authorizationHelper);

            CalculationUpdateViewModel updateViewModel = new CalculationUpdateViewModel()
            {
                SourceCode = null,
            };

            // Force validation failed
            controller.ModelState.AddModelError(nameof(updateViewModel.SourceCode), "Test");

            // Act
            IActionResult result = await controller.SaveCalculation(specificationId, calculationId, updateViewModel);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeOfType <BadRequestObjectResult>();
        }
Exemple #3
0
        public async Task SaveCalculation_OnSuccessfulSaveRequest_ThenResponseSentToClient()
        {
            // Arrange
            ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

            string specificationId = "1";
            string calculationId   = "5";

            IAuthorizationHelper  authorizationHelper = TestAuthHelper.CreateAuthorizationHelperSubstitute(specificationId, SpecificationActionTypes.CanEditCalculations);
            CalculationController controller          = CreateCalculationController(calcsClient, mapper, authorizationHelper, resultsApiClient);

            CalculationUpdateViewModel updateViewModel = new CalculationUpdateViewModel()
            {
                SourceCode = "Updated source code"
            };

            Calculation existingCalculation = new Calculation()
            {
                Id = calculationId,
                SpecificationId = specificationId,
                SourceCode      = "Existing source code"
            };

            calcsClient
            .GetCalculationById(Arg.Is(calculationId))
            .Returns(new ApiResponse <Calculation>(HttpStatusCode.OK, existingCalculation));

            Calculation apiResultCalculation = new Calculation()
            {
                Id = calculationId,
                SpecificationId = specificationId,
                SourceCode      = updateViewModel.SourceCode
            };

            calcsClient
            .EditCalculation(specificationId, calculationId, Arg.Any <CalculationEditModel>())
            .Returns(new ValidatedApiResponse <Common.ApiClient.Calcs.Models.Calculation>(HttpStatusCode.OK, apiResultCalculation));

            // Act
            IActionResult result = await controller.SaveCalculation(specificationId, calculationId, updateViewModel);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeOfType <OkObjectResult>();

            OkObjectResult typedResult       = result as OkObjectResult;
            Calculation    resultCalculation = typedResult.Value as Calculation;

            resultCalculation?.SourceCode.Should().Be(updateViewModel.SourceCode);
        }
Exemple #4
0
        public async Task SaveCalculation_GivenUserDoesNotHaveEditCalculationPermission_ThenReturnForbidResult()
        {
            // Arrange
            ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

            string specificationId = "1";
            string calculationId   = "5";

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

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

            CalculationController controller = CreateCalculationController(calcsClient, mapper, authorizationHelper);

            CalculationUpdateViewModel updateViewModel = new CalculationUpdateViewModel()
            {
                SourceCode = "Updated source code"
            };

            Calculation apiResultCalculation = new Calculation()
            {
                Id = calculationId,
                SpecificationId = specificationId,
                SourceCode      = updateViewModel.SourceCode,
            };

            calcsClient
            .UpdateCalculation(calculationId, Arg.Any <CalculationUpdateModel>())
            .Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, apiResultCalculation));

            // Act
            IActionResult result = await controller.SaveCalculation(specificationId, calculationId, updateViewModel);

            // Assert
            result.Should().BeOfType <ForbidResult>();
        }