Skip to content

Commit

Permalink
test(mail): add test scenario including necessary SMTP authentication #…
Browse files Browse the repository at this point in the history
  • Loading branch information
poikilotherm committed Oct 5, 2023
1 parent 4c64051 commit 2178c83
Showing 1 changed file with 113 additions and 45 deletions.
158 changes: 113 additions & 45 deletions src/test/java/edu/harvard/iq/dataverse/util/MailSessionProducerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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"));
}

}

0 comments on commit 2178c83

Please sign in to comment.