Repository: james-project
Updated Branches:
  refs/heads/master 1fb731afb -> 90e28ece8


JAMES-2246 Add integration tests for SMTP authentication


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

Branch: refs/heads/master
Commit: b8c7067688741188def5091be746cafec9fed765
Parents: cc9707f
Author: benwa <btell...@linagora.com>
Authored: Tue Dec 5 14:04:27 2017 +0700
Committer: benwa <btell...@linagora.com>
Committed: Fri Dec 8 17:33:49 2017 +0700

----------------------------------------------------------------------
 .../james/mailets/SmtpAuthIntegrationTest.java  | 171 +++++++++++++++++++
 1 file changed, 171 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b8c70676/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
new file mode 100644
index 0000000..8b6160e
--- /dev/null
+++ 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
@@ -0,0 +1,171 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailets;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.transport.mailets.LocalDelivery;
+import org.apache.james.transport.mailets.Null;
+import org.apache.james.transport.mailets.RemoveMimeHeader;
+import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.SMTPAuthSuccessful;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.IMAPMessageReader;
+import org.apache.james.utils.SMTPMessageSender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+
+public class SmtpAuthIntegrationTest {
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int SMTP_PORT = 1025;
+    private static final int IMAP_PORT = 1143;
+    private static final String PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+    private static final String FROM = "fromuser@" + JAMES_APACHE_ORG;
+
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+
+    @Before
+    public void setup() throws Exception {
+        ProcessorConfiguration rootProcessor = ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(SMTPAuthSuccessful.class)
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "bounces")
+                .build())
+            .build();
+
+        MailetContainer mailetContainer = MailetContainer.builder()
+            .postmaster("postmaster@" + JAMES_APACHE_ORG)
+            .threads(5)
+            .addProcessor(rootProcessor)
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(deliverOnlyTransport())
+            .addProcessor(bounces())
+            .addProcessor(CommonProcessors.sieveManagerCheck())
+            .build();
+
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(MemoryJamesServerMain.SMTP_AND_IMAP_MODULE)
+            .build(temporaryFolder, mailetContainer);
+        DataProbe dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
+            .pollInterval(slowPacedPollInterval)
+            .and()
+            .with()
+            .pollDelay(slowPacedPollInterval)
+            .await();
+    }
+
+    private ProcessorConfiguration deliverOnlyTransport() {
+        return ProcessorConfiguration.builder()
+            .state("transport")
+            .enableJmx(true)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoveMimeHeader.class)
+                .addProperty("name", "bcc")
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(LocalDelivery.class)
+                .build())
+            .build();
+    }
+
+    private ProcessorConfiguration bounces() {
+        return ProcessorConfiguration.builder()
+            .state("bounces")
+            .enableJmx(true)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(Null.class)
+                .build())
+            .build();
+    }
+
+    @After
+    public void tearDown() {
+        jamesServer.shutdown();
+    }
+
+    @Test
+    public void authenticatedSmtpSessionsShouldBeDelivered() throws Exception {
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, 
JAMES_APACHE_ORG, FROM, PASSWORD);
+             IMAPMessageReader imapMessageReader = new 
IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+            
calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, 
PASSWORD));
+        }
+    }
+
+    @Test
+    public void nonAuthenticatedSmtpSessionsShouldNotBeDelivered() throws 
Exception {
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, 
JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new 
IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+
+            
calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+
+            assertThat(imapMessageReader.userReceivedMessage(FROM, 
PASSWORD)).isFalse();
+        }
+    }
+
+}


---------------------------------------------------------------------
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