-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(mail): add test scenario including necessary SMTP authentication #…
- Loading branch information
1 parent
4c64051
commit 2178c83
Showing
1 changed file
with
113 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,9 @@ | |
import io.restassured.RestAssured; | ||
import jakarta.mail.Session; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
@@ -23,6 +25,8 @@ | |
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.util.Map; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
@@ -39,65 +43,129 @@ | |
@Tag(Tags.USES_TESTCONTAINERS) | ||
@Testcontainers(disabledWithoutDocker = true) | ||
@ExtendWith(MockitoExtension.class) | ||
@LocalJvmSettings | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_HOST, method = "tcSmtpHost") | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_SETTING, method = "tcSmtpPort", varArgs = "port") | ||
class MailSessionProducerIT { | ||
|
||
private static final Integer PORT_SMTP = 1025; | ||
private static final Integer PORT_HTTP = 1080; | ||
|
||
@Mock | ||
SettingsServiceBean settingsServiceBean; | ||
@Mock | ||
DataverseServiceBean dataverseServiceBean; | ||
|
||
@Container | ||
static GenericContainer<?> maildev = new GenericContainer<>("maildev/maildev:2.1.0") | ||
.withExposedPorts(PORT_HTTP, PORT_SMTP) | ||
.waitingFor(Wait.forHttp("/")); | ||
static SettingsServiceBean settingsServiceBean = Mockito.mock(SettingsServiceBean.class);; | ||
static DataverseServiceBean dataverseServiceBean = Mockito.mock(DataverseServiceBean.class);; | ||
|
||
static String tcSmtpHost() { | ||
return maildev.getHost(); | ||
@BeforeAll | ||
static void setUp() { | ||
// Setup mocks behavior, inject as deps | ||
Mockito.when(settingsServiceBean.getValueForKey(SettingsServiceBean.Key.SystemEmail)).thenReturn("[email protected]"); | ||
BrandingUtil.injectServices(dataverseServiceBean, settingsServiceBean); | ||
} | ||
|
||
static String tcSmtpPort() { | ||
return maildev.getMappedPort(PORT_SMTP).toString(); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
RestAssured.baseURI = "http://" + tcSmtpHost(); | ||
RestAssured.port = maildev.getMappedPort(PORT_HTTP);; | ||
@Nested | ||
@LocalJvmSettings | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_HOST, method = "tcSmtpHost") | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_SETTING, method = "tcSmtpPort", varArgs = "port") | ||
class WithoutAuthentication { | ||
@Container | ||
static GenericContainer<?> maildev = new GenericContainer<>("maildev/maildev:2.1.0") | ||
.withExposedPorts(PORT_HTTP, PORT_SMTP) | ||
.waitingFor(Wait.forHttp("/")); | ||
|
||
static String tcSmtpHost() { | ||
return maildev.getHost(); | ||
} | ||
|
||
static String tcSmtpPort() { | ||
return maildev.getMappedPort(PORT_SMTP).toString(); | ||
} | ||
|
||
@BeforeAll | ||
static void setup() { | ||
RestAssured.baseURI = "http://" + tcSmtpHost(); | ||
RestAssured.port = maildev.getMappedPort(PORT_HTTP); | ||
} | ||
|
||
@Test | ||
void createSession() { | ||
given().when().get("/email") | ||
.then() | ||
.statusCode(200) | ||
.body("size()", is(0)); | ||
|
||
// given | ||
Session session = new MailSessionProducer().getSession(); | ||
MailServiceBean mailer = new MailServiceBean(session, settingsServiceBean); | ||
|
||
// when | ||
boolean sent = mailer.sendSystemEmail("[email protected]", "Test", "Test", false); | ||
|
||
// then | ||
assertTrue(sent); | ||
//RestAssured.get("/email").body().prettyPrint(); | ||
given().when().get("/email") | ||
.then() | ||
.statusCode(200) | ||
.body("size()", is(1)) | ||
.body("[0].subject", equalTo("Test")); | ||
} | ||
|
||
// Setup mocks | ||
Mockito.when(settingsServiceBean.getValueForKey(SettingsServiceBean.Key.SystemEmail)).thenReturn("[email protected]"); | ||
BrandingUtil.injectServices(dataverseServiceBean, settingsServiceBean); | ||
} | ||
|
||
@Test | ||
//@JvmSetting(key = JvmSettings.MAIL_DEBUG, value = "true") | ||
void createSessionWithoutAuth() { | ||
given().when().get("/email") | ||
.then() | ||
.statusCode(200) | ||
.body("size()", is(0)); | ||
static final String username = "testuser"; | ||
static final String password = "supersecret"; | ||
|
||
@Nested | ||
@LocalJvmSettings | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_HOST, method = "tcSmtpHost") | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_SETTING, method = "tcSmtpPort", varArgs = "port") | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_AUTH, value = "yes") | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_USER, value = username) | ||
@JvmSetting(key = JvmSettings.MAIL_MTA_PASSWORD, value = password) | ||
class WithAuthentication { | ||
@Container | ||
static GenericContainer<?> maildev = new GenericContainer<>("maildev/maildev:2.1.0") | ||
.withExposedPorts(PORT_HTTP, PORT_SMTP) | ||
.withEnv(Map.of( | ||
"MAILDEV_INCOMING_USER", username, | ||
"MAILDEV_INCOMING_PASS", password | ||
)) | ||
.waitingFor(Wait.forHttp("/")); | ||
|
||
static String tcSmtpHost() { | ||
return maildev.getHost(); | ||
} | ||
|
||
static String tcSmtpPort() { | ||
return maildev.getMappedPort(PORT_SMTP).toString(); | ||
} | ||
|
||
// given | ||
Session session = new MailSessionProducer().getSession(); | ||
MailServiceBean mailer = new MailServiceBean(session, settingsServiceBean); | ||
@BeforeAll | ||
static void setup() { | ||
RestAssured.baseURI = "http://" + tcSmtpHost(); | ||
RestAssured.port = maildev.getMappedPort(PORT_HTTP); | ||
} | ||
|
||
// when | ||
boolean sent = mailer.sendSystemEmail("[email protected]", "Test", "Test", false); | ||
@Test | ||
void createSession() { | ||
given().when().get("/email") | ||
.then() | ||
.statusCode(200) | ||
.body("size()", is(0)); | ||
|
||
// given | ||
Session session = new MailSessionProducer().getSession(); | ||
MailServiceBean mailer = new MailServiceBean(session, settingsServiceBean); | ||
|
||
// when | ||
boolean sent = mailer.sendSystemEmail("[email protected]", "Test", "Test", false); | ||
|
||
// then | ||
assertTrue(sent); | ||
//RestAssured.get("/email").body().prettyPrint(); | ||
given().when().get("/email") | ||
.then() | ||
.statusCode(200) | ||
.body("size()", is(1)) | ||
.body("[0].subject", equalTo("Test")); | ||
} | ||
|
||
// then | ||
assertTrue(sent); | ||
//RestAssured.get("/email").body().prettyPrint(); | ||
given().when().get("/email") | ||
.then() | ||
.statusCode(200) | ||
.body("size()", is(1)) | ||
.body("[0].subject", equalTo("Test")); | ||
} | ||
|
||
} |