public async Task <IActionResult> CreateExternalService(CreateExternalServiceDto dto) { // exclude configs since it may contain secret values var requestBodyToLog = new CreateExternalServiceDto { Name = dto.Name, Description = dto.Description, ExternalServiceTypeId = dto.ExternalServiceTypeId, IsGlobal = dto.IsGlobal }; _logger.LogRequest("Creating external service. Request body: {@requestBodyToLog}", requestBodyToLog); try { var currentUserId = User.GetUserId(); var serviceId = await _externalServiceService.AddExternalService(dto.Name, dto.Description, dto.ExternalServiceTypeId, JsonConvert.SerializeObject(dto.Config), currentUserId, dto.IsGlobal); var externalService = await _externalServiceService.GetExternalService(serviceId); var result = _mapper.Map <ExternalServiceDto>(externalService); _logger.LogResponse("External service created. Response body: {@result}", result); return(CreatedAtRoute("GetExternalServiceById", new { serviceId }, result)); } catch (DuplicateExternalServiceException ex) { _logger.LogWarning(ex, "Duplicate external service name"); return(BadRequest(ex.Message)); } }
public async void CreateExternalService_ReturnsCreatedExternalService() { _externalServiceService .Setup(s => s.AddExternalService(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())) .ReturnsAsync(1); _externalServiceService.Setup(s => s.GetExternalService(It.IsAny <int>(), It.IsAny <CancellationToken>())) .ReturnsAsync((int id, CancellationToken cancellationToken) => new ExternalService { Id = id, Name = "my connection" }); var httpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, "1") }) }) }; var controller = new ExternalServiceController(_externalServiceService.Object, _mapper, _logger.Object) { ControllerContext = new ControllerContext { HttpContext = httpContext } }; var dto = new CreateExternalServiceDto { Name = "my connection" }; var result = await controller.CreateExternalService(dto); var createAtRouteActionResult = Assert.IsType <CreatedAtRouteResult>(result); var returnValue = Assert.IsType <ExternalServiceDto>(createAtRouteActionResult.Value); Assert.Equal(1, returnValue.Id); }
public async Task <ExternalServiceDto> CreateExternalService(CreateExternalServiceDto dto) { var path = "service"; return(await Api.Post <CreateExternalServiceDto, ExternalServiceDto>(path, dto)); }