JAMES−2246 Sanitize SMTPIsAuthNetwork

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

Branch: refs/heads/master
Commit: 4311b7d143061ff3de9bd30bc3ae5f3e55f84d37
Parents: 8eb8bf1
Author: benwa <btell...@linagora.com>
Authored: Wed Dec 6 17:27:53 2017 +0700
Committer: benwa <btell...@linagora.com>
Committed: Fri Dec 8 17:34:45 2017 +0700

----------------------------------------------------------------------
 .../transport/matchers/SMTPIsAuthNetwork.java   | 19 +++--
 .../matchers/SMTPIsAuthNetworkTest.java         | 79 ++++++++------------
 2 files changed, 41 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4311b7d1/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
index bc9afa9..9773993 100644
--- 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
@@ -17,16 +17,16 @@
  * under the License.                                           *
  ****************************************************************/
 
-
-
 package org.apache.james.transport.matchers;
 
-import org.apache.mailet.Experimental;
-import org.apache.mailet.base.GenericMatcher;
-import org.apache.mailet.Mail;
+import java.util.Collection;
+import java.util.Objects;
+
 import org.apache.james.core.MailAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMatcher;
 
-import java.util.Collection;
+import com.google.common.collect.ImmutableList;
 
 /**
  * <P>
@@ -37,21 +37,20 @@ import java.util.Collection;
  * class=&quot;&lt;any-class&gt;&quot;&gt; </CODE></PRE>
  * 
  */
-@Experimental
 public class SMTPIsAuthNetwork extends GenericMatcher {
 
     /**
      * The mail attribute which is set if the client is allowed to relay
      */
-    private final static String SMTP_AUTH_NETWORK_NAME = 
"org.apache.james.SMTPIsAuthNetwork";
+    public static final String SMTP_AUTH_NETWORK_NAME = 
"org.apache.james.SMTPIsAuthNetwork";
 
     public Collection<MailAddress> match(Mail mail) {
         String relayingAllowed = (String) mail
                 .getAttribute(SMTP_AUTH_NETWORK_NAME);
-        if (relayingAllowed != null && relayingAllowed.equals("true")) {
+        if (Objects.equals(relayingAllowed, "true")) {
             return mail.getRecipients();
         } else {
-            return null;
+            return ImmutableList.of();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/4311b7d1/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
index 8f90d00..3ce6235 100644
--- 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
@@ -17,71 +17,56 @@
  * under the License.                                           *
  ****************************************************************/
 
-
 package org.apache.james.transport.matchers;
 
-import java.util.Collection;
-
-import javax.mail.MessagingException;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import org.apache.james.core.MailAddress;
-import org.apache.mailet.Matcher;
+import org.apache.mailet.Mail;
 import org.apache.mailet.base.test.FakeMail;
-import org.apache.mailet.base.test.FakeMatcherConfig;
-import org.apache.mailet.base.test.MailUtil;
-import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 
 public class SMTPIsAuthNetworkTest {
 
-    private FakeMail mockedMail;
-
-    private Matcher matcher;
-
-    private boolean isAuthorized = false;
-
-    private void setIsAuthorized(boolean isAuthorized) {
-        this.isAuthorized = isAuthorized;
-    }
+    private SMTPIsAuthNetwork testee;
+    private MailAddress recipient;
 
-    private void setupMockedMail() throws MessagingException {
-        mockedMail = MailUtil.createMockMail2Recipients();
-        if (isAuthorized) {
-            String MAIL_ATTRIBUTE_NAME = "org.apache.james.SMTPIsAuthNetwork";
-            mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME, "true");
-        }
+    @Before
+    public void setUp() throws Exception {
+        testee = new SMTPIsAuthNetwork();
+        recipient = new MailAddress("recipi...@domain.com");
     }
 
-    private void setupMatcher() throws MessagingException {
-        matcher = new SMTPIsAuthNetwork();
-        FakeMatcherConfig mci = FakeMatcherConfig.builder()
-                .matcherName("SMTPIsAuthNetwork")
-                .build();
+    @Test
+    public void matchShouldReturnEmptyWhenNoSmtpInformation() throws Exception 
{
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .build();
 
-        matcher.init(mci);
+        assertThat(testee.match(mail))
+            .isEmpty();
     }
 
     @Test
-    public void testIsAuthNetwork() throws MessagingException {
-        setIsAuthorized(true);
-        setupMockedMail();
-        setupMatcher();
-
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-
-        Assert.assertNotNull(matchedRecipients);
-        Assert.assertEquals(matchedRecipients.size(), 
mockedMail.getRecipients()
-                .size());
+    public void matchShouldReturnAddressesWhenAuthorizedNetwork() throws 
Exception {
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .attribute(SMTPIsAuthNetwork.SMTP_AUTH_NETWORK_NAME, "true")
+            .build();
+
+        assertThat(testee.match(mail))
+            .containsOnly(recipient);
     }
 
     @Test
-    public void testIsNotAuthNetwork() throws MessagingException {
-        setIsAuthorized(false);
-        setupMockedMail();
-        setupMatcher();
-
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-
-        Assert.assertNull(matchedRecipients);
+    public void matchShouldReturnEmptyWhenNonAuthorizedNetwork() throws 
Exception {
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .attribute(SMTPIsAuthNetwork.SMTP_AUTH_NETWORK_NAME, "false")
+            .build();
+
+        assertThat(testee.match(mail))
+            .isEmpty();
     }
 }


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

Reply via email to