Пример #1
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>();
        }
Пример #2
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>();
        }
Пример #3
0
        public void CompilePreview_OnBackendError_ThenExceptionThrown()
        {
            // Arrange
            string calculationId   = "5";
            string specificationId = "65";

            ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

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

            PreviewCompileRequestViewModel previewViewModel = new PreviewCompileRequestViewModel()
            {
                SourceCode = "Updated source code",
            };

            calcsClient
            .PreviewCompile(Arg.Any <PreviewRequest>())
            .Returns(Task.FromResult(new ApiResponse <PreviewResponse>(HttpStatusCode.ServiceUnavailable, null)));

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

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

            // Assert
            a.Should().Throw <InvalidOperationException>();
        }
Пример #4
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);
        }
Пример #5
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>();
        }
Пример #6
0
        public async Task <IActionResult> SaveCalculation(string specificationId, string calculationId, [FromBody] CalculationUpdateViewModel vm)
        {
            Guard.ArgumentNotNull(specificationId, nameof(specificationId));
            Guard.ArgumentNotNull(calculationId, nameof(calculationId));
            Guard.ArgumentNotNull(vm, nameof(vm));

            if (!await _authorizationHelper.DoesUserHavePermission(User, specificationId, SpecificationActionTypes.CanEditCalculations))
            {
                return(new ForbidResult());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ApiResponse <Calculation> existingCalculationResponse = await _calcClient.GetCalculationById(calculationId);

            IActionResult errorResult = existingCalculationResponse.IsSuccessOrReturnFailureResult("Calculation");

            if (errorResult != null)
            {
                return(errorResult);
            }
            Calculation existingCalculation = existingCalculationResponse.Content;

            CalculationEditModel update = new CalculationEditModel()
            {
                CalculationId   = calculationId,
                Description     = existingCalculation.Description,
                Name            = existingCalculation.Name,
                SpecificationId = specificationId,
                ValueType       = existingCalculation.ValueType,
                SourceCode      = vm.SourceCode,
            };

            ValidatedApiResponse <Calculation> response = await _calcClient.EditCalculation(specificationId, calculationId, update);

            if (response.IsBadRequest(out BadRequestObjectResult badRequest))
            {
                return(badRequest);
            }

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(Ok(response.Content));
            }
            else
            {
                throw new InvalidOperationException($"An error occurred while saving calculation. Status code={response.StatusCode}");
            }
        }
Пример #7
0
        public async Task <IActionResult> SaveCalculation(string specificationId, string calculationId, [FromBody] CalculationUpdateViewModel vm)
        {
            Guard.ArgumentNotNull(specificationId, nameof(specificationId));
            Guard.ArgumentNotNull(calculationId, nameof(calculationId));
            Guard.ArgumentNotNull(vm, nameof(vm));

            if (!await _authorizationHelper.DoesUserHavePermission(User, specificationId, SpecificationActionTypes.CanEditCalculations))
            {
                return(new ForbidResult());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CalculationUpdateModel    update   = _mapper.Map <CalculationUpdateModel>(vm);
            ApiResponse <Calculation> response = await _calcClient.UpdateCalculation(calculationId, update);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return(Ok(response.Content));
            }
            else
            {
                throw new InvalidOperationException($"An error occurred while saving calculation. Status code={response.StatusCode}");
            }
        }