Skip to content

Commit 9df775a

Browse files
committed
[test] Add a test for SMTP Auth
1 parent 65e612f commit 9df775a

File tree

1 file changed

+47
-8
lines changed
  • extensions/modules/mail/src/test/java/org/exist/xquery/modules/mail

1 file changed

+47
-8
lines changed

extensions/modules/mail/src/test/java/org/exist/xquery/modules/mail/SendEmailIT.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.exist.xmldb.XmldbURI;
5959
import org.exist.xquery.XPathException;
6060
import org.exist.xquery.value.Sequence;
61+
import org.junit.Before;
6162
import org.junit.BeforeClass;
6263
import org.junit.ClassRule;
6364
import org.junit.Rule;
@@ -68,7 +69,7 @@
6869

6970
import javax.annotation.Nullable;
7071
import java.io.IOException;
71-
import java.util.EnumSet;
72+
import java.util.Arrays;
7273
import java.util.Optional;
7374

7475
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -90,14 +91,26 @@ enum SmtpImplementation {
9091
JAKARTA_MAIL
9192
}
9293

93-
@Parameterized.Parameters(name = "{0}")
94-
public static java.util.Collection<SmtpImplementation> data() {
95-
return EnumSet.allOf(SmtpImplementation.class);
94+
enum AuthenticationOption {
95+
NOT_AUTHENTICATED,
96+
AUTHENTICATED
9697
}
9798

98-
@Parameterized.Parameter
99+
@Parameterized.Parameters(name = "{0} {1}")
100+
public static java.util.Collection<Object[]> data() {
101+
return Arrays.asList(new Object[][] {
102+
{ SmtpImplementation.SMTP_DIRECT_CONNECTION, AuthenticationOption.NOT_AUTHENTICATED },
103+
{ SmtpImplementation.JAKARTA_MAIL, AuthenticationOption.NOT_AUTHENTICATED },
104+
{ SmtpImplementation.JAKARTA_MAIL, AuthenticationOption.AUTHENTICATED },
105+
});
106+
}
107+
108+
@Parameterized.Parameter(0)
99109
public SmtpImplementation smtpImplementation;
100110

111+
@Parameterized.Parameter(1)
112+
public AuthenticationOption authenticationOption;
113+
101114
@ClassRule
102115
public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(true, true);
103116

@@ -107,6 +120,9 @@ public static java.util.Collection<SmtpImplementation> data() {
107120
private static final XmldbURI BIN_DOC1_NAME = XmldbURI.create("doc 1.bin"); // NOTE(AR) intentionally contains a space character to test correct encoding/decoding
108121
private static final byte[] BIN_DOC1_CONTENT = UUIDGenerator.getUUIDversion4().getBytes(UTF_8);
109122

123+
private static final String EMAIL_UID = "emailuid";
124+
private static final String EMAIL_PWD = "emailpwd";
125+
110126
private final int smtpPort = nextFreePort(2525, 2599, 10);
111127

112128
@Rule
@@ -127,6 +143,13 @@ public static void setup() throws PermissionDeniedException, IOException, SAXExc
127143
}
128144
}
129145

146+
@Before
147+
public void setSmtpAuth() {
148+
if (authenticationOption == AuthenticationOption.AUTHENTICATED) {
149+
greenMail.setUser(EMAIL_UID, EMAIL_PWD);
150+
}
151+
}
152+
130153
@Test
131154
public void sendTextEmail() throws EXistException, XPathException, PermissionDeniedException, IOException, MessagingException {
132155
final String messageText = UUIDGenerator.getUUIDversion4();
@@ -528,6 +551,10 @@ private MimeMessage sendEmail(final String message, @Nullable final String[] att
528551
}
529552

530553
private MimeMessage sendEmailBySmtpDirectConnection(final String message, @Nullable final String[] attachmentPaths) throws EXistException, XPathException, PermissionDeniedException, IOException, MessagingException {
554+
if (authenticationOption == AuthenticationOption.AUTHENTICATED) {
555+
throw new UnsupportedOperationException("Authentication is not yet implemented by SMTP direct connection");
556+
}
557+
531558
final String from = "[email protected]";
532559
final String to = "[email protected]";
533560
final String subject = "some email subject";
@@ -601,7 +628,7 @@ private MimeMessage sendEmailByJakartaMail(final String message, final String[]
601628
tmpAttachmentPaths = "'" + tmpAttachmentPaths + "'";
602629
}
603630

604-
final String query =
631+
String query =
605632
"import module namespace mail = \"http://exist-db.org/xquery/mail\";\n" +
606633
"let $attachments := \n" +
607634
" for $attachment-path in (" + tmpAttachmentPaths + ")\n" +
@@ -619,8 +646,20 @@ private MimeMessage sendEmailByJakartaMail(final String message, final String[]
619646
" <properties>\n" +
620647
" <property name=\"mail.transport.protocol\" value=\"smtp\"/>\n" +
621648
" <property name=\"mail.smtp.port\" value=\"" + smtpPort + "\"/>\n" +
622-
" <property name=\"mail.smtp.host\" value=\"127.0.0.1\"/>\n" +
623-
" </properties>\n" +
649+
" <property name=\"mail.smtp.host\" value=\"127.0.0.1\"/>\n";
650+
651+
if (authenticationOption == AuthenticationOption.AUTHENTICATED) {
652+
query +=
653+
" <property name=\"mail.smtp.auth\" value=\"true\"/>" +
654+
" </properties>\n" +
655+
", \n" +
656+
"<authentication username='" + EMAIL_UID + "' password='" + EMAIL_PWD + "'/>";
657+
} else {
658+
query +=
659+
" </properties>\n";
660+
}
661+
662+
query +=
624663
")\n" +
625664
" return\n" +
626665
" mail:send-email(\n" +

0 commit comments

Comments
 (0)