public async Task CompilePreview_OnInvalidModel_ThenBadRequestReturned() { // Arrange ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); string calculationId = "5"; string specificationId = "65"; IAuthorizationHelper authorizationHelper = TestAuthHelper.CreateAuthorizationHelperSubstitute(specificationId, SpecificationActionTypes.CanEditCalculations); CalculationController controller = CreateCalculationController(calcsClient, mapper, authorizationHelper); PreviewCompileRequestViewModel previewViewModel = new PreviewCompileRequestViewModel() { SourceCode = "Updated source code", }; // Force validation failed controller.ModelState.AddModelError(nameof(previewViewModel.SourceCode), "Test"); // Act IActionResult result = await controller.CompilePreview(specificationId, calculationId, previewViewModel); // Assert result.Should().NotBeNull(); result.Should().BeOfType <BadRequestObjectResult>(); await authorizationHelper .DidNotReceive() .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <string>(), Arg.Any <SpecificationActionTypes>()); }
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); PreviewCompileRequestViewModel previewViewModel = new PreviewCompileRequestViewModel() { SourceCode = "Updated source code", }; calcsClient .PreviewCompile(Arg.Any <PreviewCompileRequest>()) .Returns(Task.FromResult(new ApiResponse <PreviewCompileResult>(System.Net.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>(); }
public async Task CompilePreview_OnSuccessfulSaveRequest_ThenResponseSentToClient() { // Arrange ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); string calculationId = "5"; string specificationId = "65"; IAuthorizationHelper authorizationHelper = TestAuthHelper.CreateAuthorizationHelperSubstitute(specificationId, SpecificationActionTypes.CanEditCalculations); CalculationController controller = CreateCalculationController(calcsClient, mapper, authorizationHelper); PreviewCompileRequestViewModel previewViewModel = new PreviewCompileRequestViewModel() { SourceCode = "Updated source code", }; PreviewCompileResult apiResultCalculation = new PreviewCompileResult() { CompilerOutput = new CompilerOutput() { Success = true, } }; calcsClient .PreviewCompile(Arg.Any <PreviewCompileRequest>()) .Returns(new ApiResponse <PreviewCompileResult>(System.Net.HttpStatusCode.OK, apiResultCalculation)); // Act IActionResult result = await controller.CompilePreview(specificationId, calculationId, previewViewModel); // Assert result.Should().NotBeNull(); result.Should().BeOfType <OkObjectResult>(); OkObjectResult typedResult = result as OkObjectResult; PreviewCompileResult previewResult = typedResult.Value as PreviewCompileResult; previewResult.CompilerOutput.Success.Should().Be(apiResultCalculation.CompilerOutput.Success); await authorizationHelper .DidNotReceive() .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <string>(), Arg.Any <SpecificationActionTypes>()); }