diff --git a/src/main/java/com/knecon/fforesight/tenantusermanagement/api/external/SMTPConfigurationResource.java b/src/main/java/com/knecon/fforesight/tenantusermanagement/api/external/SMTPConfigurationResource.java index 0d292fb..c1e032e 100644 --- a/src/main/java/com/knecon/fforesight/tenantusermanagement/api/external/SMTPConfigurationResource.java +++ b/src/main/java/com/knecon/fforesight/tenantusermanagement/api/external/SMTPConfigurationResource.java @@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import com.knecon.fforesight.tenantusermanagement.model.SMTPConfiguration; +import com.knecon.fforesight.tenantusermanagement.model.SMTPResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -43,7 +44,7 @@ public interface SMTPConfigurationResource { @PostMapping(value = SMTP_PATH + TEST_PATH, consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Test SMTP Settings to KeyCloak") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "SMTP Configuration is valid."), @ApiResponse(responseCode = "400", description = "SMTP test failed.")}) - void testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfigurationModel, @RequestParam(value = TEST_EMAIL, required = false) String testEmail); + SMTPResponse testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfigurationModel, @RequestParam(value = TEST_EMAIL, required = false) String testEmail); @ResponseStatus(value = HttpStatus.NO_CONTENT) diff --git a/src/main/java/com/knecon/fforesight/tenantusermanagement/controller/external/SMTPConfigurationController.java b/src/main/java/com/knecon/fforesight/tenantusermanagement/controller/external/SMTPConfigurationController.java index 06d8d06..6ed41a2 100644 --- a/src/main/java/com/knecon/fforesight/tenantusermanagement/controller/external/SMTPConfigurationController.java +++ b/src/main/java/com/knecon/fforesight/tenantusermanagement/controller/external/SMTPConfigurationController.java @@ -18,6 +18,7 @@ import com.knecon.fforesight.tenantcommons.TenantContext; import com.knecon.fforesight.tenantusermanagement.api.external.PublicResource; import com.knecon.fforesight.tenantusermanagement.api.external.SMTPConfigurationResource; import com.knecon.fforesight.tenantusermanagement.model.SMTPConfiguration; +import com.knecon.fforesight.tenantusermanagement.model.SMTPResponse; import com.knecon.fforesight.tenantusermanagement.service.EmailService; import com.knecon.fforesight.tenantusermanagement.service.RealmService; @@ -68,7 +69,7 @@ public class SMTPConfigurationController implements SMTPConfigurationResource, P @SneakyThrows @Override @PreAuthorize("hasAuthority('" + WRITE_SMTP_CONFIGURATION + "')") - public void testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfiguration, @RequestParam(value = TEST_EMAIL, required = false) String testEmail) { + public SMTPResponse testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfiguration, @RequestParam(value = TEST_EMAIL, required = false) String testEmail) { String targetEmail; if (StringUtils.isBlank(testEmail)) { @@ -81,7 +82,7 @@ public class SMTPConfigurationController implements SMTPConfigurationResource, P updatePassword(smtpConfiguration); smtpConfiguration.setPassword(encryptionDecryptionService.decrypt(smtpConfiguration.getPassword())); - emailService.send(convertSMTPConfigurationModelToMap(smtpConfiguration), targetEmail, "Redaction Test message", "This is a test message"); + return emailService.send(convertSMTPConfigurationModelToMap(smtpConfiguration), targetEmail, "Redaction Test message", "This is a test message"); } diff --git a/src/main/java/com/knecon/fforesight/tenantusermanagement/model/SMTPResponse.java b/src/main/java/com/knecon/fforesight/tenantusermanagement/model/SMTPResponse.java new file mode 100644 index 0000000..d8b7646 --- /dev/null +++ b/src/main/java/com/knecon/fforesight/tenantusermanagement/model/SMTPResponse.java @@ -0,0 +1,22 @@ + +package com.knecon.fforesight.tenantusermanagement.model; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Schema(description = "Object containing a simplified version of the SMTP test connection response.") +public class SMTPResponse { + + @Schema(description = "Parameter containing status code of the response.") + private int statusCode; + @Schema(description = "Parameter containing the reason phrase of the response.") + private String reasonPhrase; + +} diff --git a/src/main/java/com/knecon/fforesight/tenantusermanagement/service/EmailService.java b/src/main/java/com/knecon/fforesight/tenantusermanagement/service/EmailService.java index f227631..445ecec 100644 --- a/src/main/java/com/knecon/fforesight/tenantusermanagement/service/EmailService.java +++ b/src/main/java/com/knecon/fforesight/tenantusermanagement/service/EmailService.java @@ -11,6 +11,8 @@ import javax.ws.rs.BadRequestException; import org.springframework.stereotype.Service; +import com.knecon.fforesight.tenantusermanagement.model.SMTPResponse; + import jakarta.mail.Address; import jakarta.mail.Message; import jakarta.mail.MessagingException; @@ -32,7 +34,7 @@ import lombok.extern.slf4j.Slf4j; public class EmailService { - public void send(Map config, String address, String subject, String textBody) { + public SMTPResponse send(Map config, String address, String subject, String textBody) { Transport transport = null; try { @@ -112,8 +114,15 @@ public class EmailService { } transport.sendMessage(msg, new InternetAddress[]{new InternetAddress(address)}); + + return SMTPResponse.builder() + .statusCode(200) + .build(); } catch (Exception e) { - throw new BadRequestException("Failed to send e-mail", e); + return SMTPResponse.builder() + .statusCode(400) + .reasonPhrase(e.getMessage()) + .build(); } finally { if (transport != null) { try { diff --git a/src/test/java/com/knecon/fforesight/tests/SMTPConfigurationTest.java b/src/test/java/com/knecon/fforesight/tests/SMTPConfigurationTest.java index 1cf095f..373431f 100644 --- a/src/test/java/com/knecon/fforesight/tests/SMTPConfigurationTest.java +++ b/src/test/java/com/knecon/fforesight/tests/SMTPConfigurationTest.java @@ -27,14 +27,7 @@ public class SMTPConfigurationTest extends AbstractTenantUserManagementIntegrati System.out.println(e.getMessage()); } - SMTPConfiguration newConfig = new SMTPConfiguration(); - newConfig.setAuth(true); - newConfig.setFrom("from@knecon.com"); - newConfig.setHost("test.knecon.com"); - newConfig.setPassword("secret"); - newConfig.setUser("user"); - newConfig.setStarttls(true); - newConfig.setSsl(false); + SMTPConfiguration newConfig = provideTestSMTPConfiguration(); smtpConfigurationClient.updateSMTPConfiguration(newConfig); var currentSMTPConfiguration = smtpConfigurationClient.getCurrentSMTPConfiguration(); @@ -50,4 +43,34 @@ public class SMTPConfigurationTest extends AbstractTenantUserManagementIntegrati } + @Test + public void testSMTPConnection() { + + TenantContext.setTenantId(AbstractTenantUserManagementIntegrationTest.TEST_TENANT_ID); + + SMTPConfiguration smtpConfiguration = provideTestSMTPConfiguration(); + + var response = smtpConfigurationClient.testSMTPConfiguration(smtpConfiguration, ""); + + // Fails because we are not using a smtp config + assertThat(response.getStatusCode()).isEqualTo(400); + assertThat(response.getReasonPhrase()).isEqualTo("Couldn't connect to host, port: test.knecon.com, 25; timeout 10000"); + + TenantContext.clear(); + } + + private SMTPConfiguration provideTestSMTPConfiguration() { + + SMTPConfiguration smtpConfiguration = new SMTPConfiguration(); + smtpConfiguration.setAuth(true); + smtpConfiguration.setFrom("from@knecon.com"); + smtpConfiguration.setHost("test.knecon.com"); + smtpConfiguration.setPassword("secret"); + smtpConfiguration.setUser("user"); + smtpConfiguration.setStarttls(true); + smtpConfiguration.setSsl(false); + + return smtpConfiguration; + } + }