Merge branch 'RED-6903' into 'main'
RED-6903 - Return 400 for test SMTP connection in failure cases See merge request fforesight/tenant-user-management-service!31
This commit is contained in:
commit
02f8689769
@ -1,8 +1,6 @@
|
||||
package com.knecon.fforesight.tenantusermanagement.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.ws.rs.BadRequestException;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@ -29,4 +27,10 @@ public class ControllerAdvice {
|
||||
return new ResponseEntity<>(new ErrorMessage(e.getReason()), e.getStatusCode());
|
||||
}
|
||||
|
||||
@ExceptionHandler(BadRequestException.class)
|
||||
public ResponseEntity<ErrorMessage> handleBadRequestException(BadRequestException e) {
|
||||
|
||||
return new ResponseEntity<>(new ErrorMessage(e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -64,7 +64,6 @@ public class SMTPConfigurationController implements SMTPConfigurationResource, P
|
||||
realmService.realm(TenantContext.getTenantId()).update(realmRepresentation);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
@PreAuthorize("hasAuthority('" + WRITE_SMTP_CONFIGURATION + "')")
|
||||
public SMTPResponse testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfiguration) {
|
||||
|
||||
@ -55,6 +55,9 @@ public class EmailService {
|
||||
|
||||
if (auth) {
|
||||
props.setProperty("mail.smtp.auth", "true");
|
||||
if (!config.get("user").equals(address)) {
|
||||
throw new BadRequestException("User and from email should be the same");
|
||||
}
|
||||
}
|
||||
|
||||
if (ssl) {
|
||||
@ -118,9 +121,7 @@ public class EmailService {
|
||||
return SMTPResponse.builder()
|
||||
.statusCode(200);
|
||||
} catch (Exception e) {
|
||||
return SMTPResponse.builder()
|
||||
.statusCode(400)
|
||||
.reasonPhrase(e.getMessage());
|
||||
throw new BadRequestException(e.getMessage());
|
||||
} finally {
|
||||
if (transport != null) {
|
||||
try {
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.knecon.fforesight.tests;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -31,7 +32,7 @@ public class SMTPConfigurationTest extends AbstractTenantUserManagementIntegrati
|
||||
|
||||
var currentSMTPConfiguration = smtpConfigurationClient.getCurrentSMTPConfiguration();
|
||||
assertThat(currentSMTPConfiguration.getPassword()).matches("\\**");
|
||||
assertThat(currentSMTPConfiguration.getUser()).isEqualTo("user");
|
||||
assertThat(currentSMTPConfiguration.getUser()).isEqualTo("from@knecon.com");
|
||||
assertThat(currentSMTPConfiguration.getHost()).isEqualTo("test.knecon.com");
|
||||
assertThat(currentSMTPConfiguration.isSsl()).isFalse();
|
||||
assertThat(currentSMTPConfiguration.isStarttls()).isTrue();
|
||||
@ -49,13 +50,20 @@ public class SMTPConfigurationTest extends AbstractTenantUserManagementIntegrati
|
||||
|
||||
SMTPConfiguration smtpConfiguration = provideTestSMTPConfiguration();
|
||||
|
||||
var response = smtpConfigurationClient.testSMTPConfiguration(smtpConfiguration);
|
||||
assertThatThrownBy(() -> smtpConfigurationClient.testSMTPConfiguration(smtpConfiguration)).hasMessageContaining("Couldn't connect to host, port: test.knecon.com, 25; timeout 10000");
|
||||
|
||||
// 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");
|
||||
assertThat(response.getRecipientEmail()).isEqualTo("from@knecon.com");
|
||||
assertThat(response.isAdminEmail()).isFalse();
|
||||
TenantContext.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSMTPConnectionWithDifferentEmails() {
|
||||
|
||||
TenantContext.setTenantId(AbstractTenantUserManagementIntegrationTest.TEST_TENANT_ID);
|
||||
|
||||
SMTPConfiguration smtpConfiguration = provideTestSMTPConfiguration();
|
||||
smtpConfiguration.setUser("invalid");
|
||||
|
||||
assertThatThrownBy(() -> smtpConfigurationClient.testSMTPConfiguration(smtpConfiguration)).hasMessageContaining("User and from email should be the same");
|
||||
|
||||
TenantContext.clear();
|
||||
}
|
||||
@ -67,7 +75,7 @@ public class SMTPConfigurationTest extends AbstractTenantUserManagementIntegrati
|
||||
smtpConfiguration.setFrom("from@knecon.com");
|
||||
smtpConfiguration.setHost("test.knecon.com");
|
||||
smtpConfiguration.setPassword("secret");
|
||||
smtpConfiguration.setUser("user");
|
||||
smtpConfiguration.setUser("from@knecon.com");
|
||||
smtpConfiguration.setStarttls(true);
|
||||
smtpConfiguration.setSsl(false);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user