james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject james-project git commit: JAMES-2159 Add more tests for collected contacts
Date Thu, 05 Oct 2017 18:05:43 GMT
Repository: james-project
Updated Branches:
  refs/heads/master f52eaa62a -> dc4e2016f


JAMES-2159 Add more tests for collected contacts

 - Mailbox format, mismatches between envelope and mime message, scrambling...


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/dc4e2016
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/dc4e2016
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/dc4e2016

Branch: refs/heads/master
Commit: dc4e2016fe9d61dd2469e37083d6e6dea554174d
Parents: f52eaa6
Author: benwa <btellier@linagora.com>
Authored: Thu Oct 5 15:46:14 2017 +0700
Committer: Matthieu Baechler <matthieu@apache.org>
Committed: Thu Oct 5 20:05:03 2017 +0200

----------------------------------------------------------------------
 .../transport/mailets/ContactExtractor.java     | 20 ++--
 .../transport/mailets/ContactExtractorTest.java | 96 ++++++++++++++++++++
 2 files changed, 109 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/dc4e2016/mailet/standard/src/main/java/org/apache/james/transport/mailets/ContactExtractor.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/ContactExtractor.java
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/ContactExtractor.java
index 468d355..bf5665b 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/ContactExtractor.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/ContactExtractor.java
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.james.transport.mailets;
 
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.Optional;
 
@@ -25,6 +26,8 @@ import javax.mail.Address;
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
+import org.apache.james.core.MailAddress;
+import org.apache.james.mime4j.util.MimeUtil;
 import org.apache.mailet.Mail;
 import org.apache.mailet.Mailet;
 import org.apache.mailet.MailetException;
@@ -84,7 +87,7 @@ public class ContactExtractor extends GenericMailet implements Mailet {
     @Override
     public void service(Mail mail) throws MessagingException {
         try {
-            Optional<String> payload = extractContacts(mail.getMessage());
+            Optional<String> payload = extractContacts(mail);
             LOGGER.debug("payload : {}", payload);
             payload.ifPresent(x -> mail.setAttribute(extractAttributeTo, x));
         } catch (Exception e) {
@@ -92,12 +95,14 @@ public class ContactExtractor extends GenericMailet implements Mailet
{
         }
     }
 
-    private Optional<String> extractContacts(MimeMessage mimeMessage) throws MessagingException
{
-        return Optional.ofNullable(mimeMessage.getSender())
-                .map(Address::toString)
-                .filter(Throwing.predicate(sender -> hasRecipients(mimeMessage)))
-                .map(Throwing.function(sender -> new ExtractedContacts(sender, recipients(mimeMessage))))
-                .map(Throwing.function(message -> objectMapper.writeValueAsString(message)));
+    private Optional<String> extractContacts(Mail mail) throws MessagingException,
IOException {
+        MimeMessage message = mail.getMessage();
+
+        return Optional.of(mail.getSender())
+            .map(MailAddress::asString)
+            .filter(Throwing.predicate(sender -> hasRecipients(message)))
+            .map(Throwing.function(sender -> new ExtractedContacts(sender, recipients(message))))
+            .map(Throwing.function(extractedContacts -> objectMapper.writeValueAsString(extractedContacts)));
     }
 
     private boolean hasRecipients(MimeMessage mimeMessage) throws MessagingException {
@@ -107,6 +112,7 @@ public class ContactExtractor extends GenericMailet implements Mailet
{
     private ImmutableList<String> recipients(MimeMessage mimeMessage) throws MessagingException
{
         return Arrays.stream(mimeMessage.getAllRecipients())
                 .map(Address::toString)
+                .map(MimeUtil::unscrambleHeaderValue)
                 .collect(Guavate.toImmutableList());
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/dc4e2016/mailet/standard/src/test/java/org/apache/james/transport/mailets/ContactExtractorTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/ContactExtractorTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/ContactExtractorTest.java
index 9eda1f2..0ba88ba 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/ContactExtractorTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/ContactExtractorTest.java
@@ -119,7 +119,103 @@ public class ContactExtractorTest {
         mailet.service(mail);
 
         assertThatJson(mail.getAttribute(ATTRIBUTE).toString()).isEqualTo(expectedMessage);
+    }
+
+    @Test
+    public void serviceShouldPreserveRecipientsEmailAddress() throws Exception {
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setSender(SENDER)
+            .addToRecipient("To <" + TO + ">")
+            .setSubject("Contact collection Rocks")
+            .setText("This is my email")
+            .build();
+        FakeMail mail = FakeMail.builder().mimeMessage(message)
+            .sender(new MailAddress(SENDER))
+            .recipient(new MailAddress(TO))
+            .build();
+        mailet.init(mailetConfig);
+
+        String expectedMessage = "{\"userEmail\" : \"" + SENDER + "\", \"emails\" : [ \"To
<" + TO + ">\" ]}";
+        mailet.service(mail);
+
+        assertThatJson(mail.getAttribute(ATTRIBUTE).toString()).isEqualTo(expectedMessage);
+    }
+
+    @Test
+    public void serviceShouldUnscrambleRecipients() throws Exception {
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setSender(SENDER)
+            .addToRecipient("=?ISO-8859-1?Q?Beno=EEt_TELLIER?= <tellier@linagora.com>")
+            .setSubject("Contact collection Rocks")
+            .setText("This is my email")
+            .build();
+        FakeMail mail = FakeMail.builder().mimeMessage(message)
+            .sender(new MailAddress(SENDER))
+            .recipient(new MailAddress(TO))
+            .build();
+        mailet.init(mailetConfig);
+
+        String expectedMessage = "{\"userEmail\" : \"" + SENDER + "\", \"emails\" : [ \"BenoƮt
TELLIER <tellier@linagora.com>\" ]}";
+        mailet.service(mail);
+
+        assertThatJson(mail.getAttribute(ATTRIBUTE).toString()).isEqualTo(expectedMessage);
+    }
+
+    @Test
+    public void serviceShouldNotOverwriteSenderWhenDifferentFromField() throws Exception
{
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .addFrom("other@sender.org")
+            .addToRecipient("To <" + TO + ">")
+            .setSubject("Contact collection Rocks")
+            .setText("This is my email")
+            .build();
+        FakeMail mail = FakeMail.builder().mimeMessage(message)
+            .sender(new MailAddress(SENDER))
+            .recipient(new MailAddress(TO))
+            .build();
+        mailet.init(mailetConfig);
+
+        String expectedMessage = "{\"userEmail\" : \"" + SENDER + "\", \"emails\" : [ \"To
<" + TO + ">\" ]}";
+        mailet.service(mail);
+
+        assertThatJson(mail.getAttribute(ATTRIBUTE).toString()).isEqualTo(expectedMessage);
+    }
+
+    @Test
+    public void serviceShouldNotOverwriteSenderWhenDifferentSenderField() throws Exception
{
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setSender("other@sender.org")
+            .addToRecipient("To <" + TO + ">")
+            .setSubject("Contact collection Rocks")
+            .setText("This is my email")
+            .build();
+        FakeMail mail = FakeMail.builder().mimeMessage(message)
+            .sender(new MailAddress(SENDER))
+            .recipient(new MailAddress(TO))
+            .build();
+        mailet.init(mailetConfig);
+
+        String expectedMessage = "{\"userEmail\" : \"" + SENDER + "\", \"emails\" : [ \"To
<" + TO + ">\" ]}";
+        mailet.service(mail);
+
+        assertThatJson(mail.getAttribute(ATTRIBUTE).toString()).isEqualTo(expectedMessage);
+    }
+
+    @Test
+    public void serviceShouldSkipMessagesWithoutSenderEnvelope() throws Exception {
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .addToRecipient("To <" + TO + ">")
+            .setSubject("Contact collection Rocks")
+            .setText("This is my email")
+            .build();
+        FakeMail mail = FakeMail.builder().mimeMessage(message)
+            .recipient(new MailAddress(TO))
+            .build();
+        mailet.init(mailetConfig);
+
+        mailet.service(mail);
 
+        assertThatJson(mail.getAttribute(ATTRIBUTE)).isEqualTo(null);
     }
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message