From ace591834220144d383c596699c9be29de5cfaf0 Mon Sep 17 00:00:00 2001 From: Max Dor Date: Sun, 30 Dec 2018 16:45:30 +0100 Subject: [PATCH] Continue structural port from Spring Boot to Undertow - Notification template generator - Add tests for email notification handler --- build.gradle | 1 + .../threepid/medium/EmailTemplateConfig.java | 8 +- .../medium/PhoneSmsTemplateConfig.java | 8 +- .../GenericTemplateNotificationGenerator.java | 20 +++-- .../kamax/mxisd/test/MxisdEmailNotifTest.java | 78 +++++++++++++++++++ 5 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 src/test/java/io/kamax/mxisd/test/MxisdEmailNotifTest.java diff --git a/build.gradle b/build.gradle index 022f115..13e3c4e 100644 --- a/build.gradle +++ b/build.gradle @@ -148,6 +148,7 @@ dependencies { testCompile 'junit:junit:4.12' testCompile 'com.github.tomakehurst:wiremock:2.8.0' testCompile 'com.unboundid:unboundid-ldapsdk:4.0.9' + testCompile 'com.icegreen:greenmail:1.5.9' } shadowJar { diff --git a/src/main/java/io/kamax/mxisd/config/threepid/medium/EmailTemplateConfig.java b/src/main/java/io/kamax/mxisd/config/threepid/medium/EmailTemplateConfig.java index 366263a..bd8b849 100644 --- a/src/main/java/io/kamax/mxisd/config/threepid/medium/EmailTemplateConfig.java +++ b/src/main/java/io/kamax/mxisd/config/threepid/medium/EmailTemplateConfig.java @@ -28,10 +28,10 @@ public class EmailTemplateConfig extends GenericTemplateConfig { private transient final Logger log = LoggerFactory.getLogger(EmailTemplateConfig.class); public EmailTemplateConfig() { - setInvite("classpath:threepids/email/invite-template.eml"); - getGeneric().put("matrixId", "classpath:threepids/email/mxid-template.eml"); - getSession().getValidation().setLocal("classpath:threepids/email/validate-local-template.eml"); - getSession().getValidation().setRemote("classpath:threepids/email/validate-remote-template.eml"); + setInvite("classpath:/threepids/email/invite-template.eml"); + getGeneric().put("matrixId", "classpath:/threepids/email/mxid-template.eml"); + getSession().getValidation().setLocal("classpath:/threepids/email/validate-local-template.eml"); + getSession().getValidation().setRemote("classpath:/threepids/email/validate-remote-template.eml"); } public EmailTemplateConfig build() { diff --git a/src/main/java/io/kamax/mxisd/config/threepid/medium/PhoneSmsTemplateConfig.java b/src/main/java/io/kamax/mxisd/config/threepid/medium/PhoneSmsTemplateConfig.java index 3a347b7..42352b2 100644 --- a/src/main/java/io/kamax/mxisd/config/threepid/medium/PhoneSmsTemplateConfig.java +++ b/src/main/java/io/kamax/mxisd/config/threepid/medium/PhoneSmsTemplateConfig.java @@ -28,10 +28,10 @@ public class PhoneSmsTemplateConfig extends GenericTemplateConfig { private transient final Logger log = LoggerFactory.getLogger(EmailTemplateConfig.class); public PhoneSmsTemplateConfig() { - setInvite("classpath:threepids/sms/invite-template.txt"); - getGeneric().put("matrixId", "classpath:threepids/email/mxid-template.eml"); - getSession().getValidation().setLocal("classpath:threepids/sms/validate-local-template.txt"); - getSession().getValidation().setRemote("classpath:threepids/sms/validate-remote-template.txt"); + setInvite("classpath:/threepids/sms/invite-template.txt"); + getGeneric().put("matrixId", "classpath:/threepids/email/mxid-template.eml"); + getSession().getValidation().setLocal("classpath:/threepids/sms/validate-local-template.txt"); + getSession().getValidation().setRemote("classpath:/threepids/sms/validate-remote-template.txt"); } public PhoneSmsTemplateConfig build() { diff --git a/src/main/java/io/kamax/mxisd/threepid/generator/GenericTemplateNotificationGenerator.java b/src/main/java/io/kamax/mxisd/threepid/generator/GenericTemplateNotificationGenerator.java index a891024..bf12fc6 100644 --- a/src/main/java/io/kamax/mxisd/threepid/generator/GenericTemplateNotificationGenerator.java +++ b/src/main/java/io/kamax/mxisd/threepid/generator/GenericTemplateNotificationGenerator.java @@ -27,11 +27,16 @@ import io.kamax.mxisd.config.threepid.medium.GenericTemplateConfig; import io.kamax.mxisd.exception.InternalServerError; import io.kamax.mxisd.invitation.IThreePidInviteReply; import io.kamax.mxisd.threepid.session.IThreePidSession; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.nio.charset.StandardCharsets; public abstract class GenericTemplateNotificationGenerator extends PlaceholderNotificationGenerator implements NotificationGenerator { @@ -46,13 +51,14 @@ public abstract class GenericTemplateNotificationGenerator extends PlaceholderNo private String getTemplateContent(String location) { try { - // FIXME make it work again - /* - InputStream is = StringUtils.startsWith(location, "classpath:") ? - app.getResource(location).getInputStream() : new FileInputStream(location); + URI loc = URI.create(location); + InputStream is; + if (StringUtils.equals("classpath", loc.getScheme())) { + is = getClass().getResourceAsStream(loc.getSchemeSpecificPart()); + } else { + is = new FileInputStream(location); + } return IOUtils.toString(is, StandardCharsets.UTF_8); - */ - throw new IOException("Not implemented!"); } catch (IOException e) { throw new InternalServerError("Unable to read template content at " + location + ": " + e.getMessage()); } diff --git a/src/test/java/io/kamax/mxisd/test/MxisdEmailNotifTest.java b/src/test/java/io/kamax/mxisd/test/MxisdEmailNotifTest.java new file mode 100644 index 0000000..c24cb2b --- /dev/null +++ b/src/test/java/io/kamax/mxisd/test/MxisdEmailNotifTest.java @@ -0,0 +1,78 @@ +package io.kamax.mxisd.test; + +import com.icegreen.greenmail.util.GreenMail; +import com.icegreen.greenmail.util.ServerSetupTest; +import io.kamax.matrix.MatrixID; +import io.kamax.matrix.ThreePidMedium; +import io.kamax.matrix._MatrixID; +import io.kamax.matrix.json.GsonUtil; +import io.kamax.mxisd.Mxisd; +import io.kamax.mxisd.as.MatrixIdInvite; +import io.kamax.mxisd.config.MxisdConfig; +import io.kamax.mxisd.config.threepid.connector.EmailSmtpConfig; +import io.kamax.mxisd.config.threepid.medium.EmailConfig; +import io.kamax.mxisd.threepid.connector.email.EmailSmtpConnector; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.util.Collections; + +import static junit.framework.TestCase.assertEquals; + +public class MxisdEmailNotifTest { + + private final String domain = "localhost"; + private Mxisd m; + private GreenMail gm; + + @Before + public void before() { + EmailSmtpConfig smtpCfg = new EmailSmtpConfig(); + smtpCfg.setPort(3025); + smtpCfg.setLogin("mxisd"); + smtpCfg.setPassword("mxisd"); + + EmailConfig eCfg = new EmailConfig(); + eCfg.setConnector(EmailSmtpConnector.ID); + eCfg.getIdentity().setFrom("mxisd@" + domain); + eCfg.getIdentity().setName("Mxisd Server (Unit Test)"); + eCfg.getConnectors().put(EmailSmtpConnector.ID, GsonUtil.makeObj(smtpCfg)); + + MxisdConfig cfg = new MxisdConfig(); + cfg.getMatrix().setDomain(domain); + cfg.getKey().setPath(":memory:"); + cfg.getStorage().getProvider().getSqlite().setDatabase(":memory:"); + cfg.getThreepid().getMedium().put(ThreePidMedium.Email.getId(), GsonUtil.makeObj(eCfg)); + + m = new Mxisd(cfg); + m.start(); + + gm = new GreenMail(ServerSetupTest.SMTP_IMAP); + gm.start(); + } + + @After + public void after() { + gm.stop(); + m.stop(); + } + + @Test + public void forMatrixIdInvite() throws MessagingException { + _MatrixID sender = MatrixID.asAcceptable("mxisd", domain); + _MatrixID recipient = MatrixID.asAcceptable("john", domain); + MatrixIdInvite idInvite = new MatrixIdInvite("!rid:" + domain, sender, recipient, ThreePidMedium.Email.getId(), "john@" + domain, Collections.emptyMap()); + m.getNotif().sendForInvite(idInvite); + + assertEquals(1, gm.getReceivedMessages().length); + MimeMessage msg = gm.getReceivedMessages()[0]; + assertEquals(1, msg.getFrom().length); + assertEquals("\"Mxisd Server (Unit Test)\" ", msg.getFrom()[0].toString()); + assertEquals(1, msg.getRecipients(Message.RecipientType.TO).length); + } + +}