Repository: james-project Updated Branches: refs/heads/master c7787f919 -> dbc653d16
JAMES-2015 add integration test for Vacation mailet with external SMTP server Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/469a074b Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/469a074b Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/469a074b Branch: refs/heads/master Commit: 469a074b0d447609b42a5e85635c6d834dd478fb Parents: d174d5e Author: Luc DUZAN <[email protected]> Authored: Thu May 4 19:47:54 2017 +0200 Committer: benwa <[email protected]> Committed: Wed May 17 15:09:07 2017 +0700 ---------------------------------------------------------------------- .../CassandraVacationRelayIntegrationTest.java | 53 +++++ .../jmap-integration-testing-common/pom.xml | 1 - .../jmap/VacationRelayIntegrationTest.java | 192 +++++++++++++++++++ .../memory-jmap-integration-testing/pom.xml | 10 + .../MemoryVacationRelayIntegrationTest.java | 50 +++++ 5 files changed, 305 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/469a074b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraVacationRelayIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraVacationRelayIntegrationTest.java b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraVacationRelayIntegrationTest.java new file mode 100644 index 0000000..6559c72 --- /dev/null +++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraVacationRelayIntegrationTest.java @@ -0,0 +1,53 @@ +/**************************************************************** + * 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.jmap.cassandra; + +import org.apache.james.CassandraJmapTestRule; +import org.apache.james.GuiceJamesServer; +import org.apache.james.dnsservice.api.DNSService; +import org.apache.james.dnsservice.api.InMemoryDNSService; +import org.apache.james.jmap.VacationRelayIntegrationTest; +import org.junit.Ignore; +import org.junit.Rule; + +@Ignore +public class CassandraVacationRelayIntegrationTest extends VacationRelayIntegrationTest { + + private final InMemoryDNSService inMemoryDNSService = new InMemoryDNSService(); + + @Rule + public CassandraJmapTestRule rule = CassandraJmapTestRule.defaultTestRule(); + + @Override + protected GuiceJamesServer getJmapServer() { + return rule.jmapServer((binder) -> binder.bind(DNSService.class).toInstance(inMemoryDNSService)); + } + + @Override + protected void await() { + rule.await(); + } + + @Override + protected InMemoryDNSService getInMemoryDns() { + return inMemoryDNSService; + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/469a074b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/pom.xml ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/pom.xml b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/pom.xml index cf68b3a..7efb7bd 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/pom.xml +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/pom.xml @@ -181,7 +181,6 @@ <dependency> <groupId>org.apache.james</groupId> <artifactId>james-server-util-java8</artifactId> - <scope>test</scope> <type>test-jar</type> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/469a074b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java new file mode 100644 index 0000000..149d3ce --- /dev/null +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java @@ -0,0 +1,192 @@ +/**************************************************************** + * 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.jmap; + +import static com.jayway.restassured.RestAssured.given; +import static com.jayway.restassured.config.EncoderConfig.encoderConfig; +import static com.jayway.restassured.config.RestAssuredConfig.newConfig; +import static org.hamcrest.Matchers.equalTo; + +import java.net.InetAddress; +import java.util.concurrent.TimeUnit; + +import com.jayway.restassured.specification.RequestSpecification; +import org.apache.commons.net.smtp.SMTPClient; +import org.apache.http.client.utils.URIBuilder; +import org.apache.james.GuiceJamesServer; +import org.apache.james.dnsservice.api.InMemoryDNSService; +import org.apache.james.jmap.api.access.AccessToken; +import org.apache.james.mailbox.model.MailboxConstants; +import org.apache.james.mailbox.store.probe.MailboxProbe; +import org.apache.james.modules.MailboxProbeImpl; +import org.apache.james.probe.DataProbe; +import org.apache.james.util.streams.SwarmGenericContainer; +import org.apache.james.utils.DataProbeImpl; +import org.apache.james.utils.JmapGuiceProbe; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import com.google.common.base.Charsets; +import com.jayway.awaitility.Awaitility; +import com.jayway.awaitility.Duration; +import com.jayway.awaitility.core.ConditionFactory; +import com.jayway.restassured.builder.RequestSpecBuilder; +import com.jayway.restassured.http.ContentType; +import org.testcontainers.shaded.com.google.common.net.InetAddresses; + +public abstract class VacationRelayIntegrationTest { + + + @Rule + public final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer("weave/rest-smtp-sink:latest"); + + private static final String DOMAIN = "mydomain.tld"; + private static final String USER = "benwa"; + private static final String USER_WITH_DOMAIN = USER + '@' + DOMAIN; + private static final String PASSWORD = "secret"; + private static final String REASON = "Message explaining my wonderful vacations"; + + private static final String LOCALHOST_IP = "127.0.0.1"; + private static final int SMTP_PORT = 1025; + + private ConditionFactory calmlyAwait; + private GuiceJamesServer guiceJamesServer; + private JmapGuiceProbe jmapGuiceProbe; + + private RequestSpecification jmapRequestSpecification; + private RequestSpecification restSmtpSinkSpec; + + protected abstract void await(); + + protected abstract GuiceJamesServer getJmapServer(); + + protected abstract InMemoryDNSService getInMemoryDns(); + + @Before + public void setUp() throws Exception { + + InetAddress containerIp = InetAddresses.forString(fakeSmtp.getIp()); + getInMemoryDns() + .registerRecord("yopmail.com", containerIp, "yopmail.com"); + + guiceJamesServer = getJmapServer(); + guiceJamesServer.start(); + + DataProbe dataProbe = guiceJamesServer.getProbe(DataProbeImpl.class); + dataProbe.addDomain(DOMAIN); + dataProbe.addUser(USER_WITH_DOMAIN, PASSWORD); + MailboxProbe mailboxProbe = guiceJamesServer.getProbe(MailboxProbeImpl.class); + mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, USER_WITH_DOMAIN, DefaultMailboxes.SENT); + mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, USER_WITH_DOMAIN, DefaultMailboxes.INBOX); + await(); + + jmapGuiceProbe = guiceJamesServer.getProbe(JmapGuiceProbe.class); + jmapRequestSpecification = new RequestSpecBuilder() + .setContentType(ContentType.JSON) + .setAccept(ContentType.JSON) + .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8))) + .setPort(jmapGuiceProbe + .getJmapPort()) + .build(); + + restSmtpSinkSpec = new RequestSpecBuilder() + .setContentType(ContentType.JSON) + .setAccept(ContentType.JSON) + .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8))) + .setPort(80) + .setBaseUri("http://" + containerIp.getHostAddress()) + .build(); + + Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS; + calmlyAwait = Awaitility.with().pollInterval(slowPacedPollInterval).and().with().pollDelay(slowPacedPollInterval).await(); + } + + @After + public void teardown() { + guiceJamesServer.stop(); + } + + private void setVacationResponse(AccessToken accessToken) { + String bodyRequest = "[[" + + "\"setVacationResponse\", " + + "{" + + " \"update\":{" + + " \"singleton\" : {" + + " \"id\": \"singleton\"," + + " \"isEnabled\": \"true\"," + + " \"textBody\": \"" + REASON + "\"" + + " }" + + " }" + + "}, \"#0\"" + + "]]"; + given() + .spec(jmapRequestSpecification) + .header("Authorization", accessToken.serialize()) + .body(bodyRequest) + .when() + .post("/jmap") + .then() + .statusCode(200); + } + + @Test + public void forwardingAnEmailShouldWork() throws Exception { + AccessToken accessToken = HttpJmapAuthentication.authenticateJamesUser(baseUri(), USER_WITH_DOMAIN, PASSWORD); + setVacationResponse(accessToken); + String externalMail = "[email protected]"; + + SMTPClient smtpClient = new SMTPClient(); + smtpClient.connect(LOCALHOST_IP, SMTP_PORT); + smtpClient.helo(DOMAIN); + smtpClient.setSender(externalMail); + smtpClient.rcpt("<" + USER_WITH_DOMAIN + ">"); + smtpClient.sendShortMessageData("content"); + + calmlyAwait.atMost(1, TimeUnit.MINUTES) + .until(() -> { + try { + given() + .spec(restSmtpSinkSpec) + .when() + .get("/api/email") + .then() + .statusCode(200) + .body("[0].from", equalTo(USER_WITH_DOMAIN)) + .body("[0].to[0]", equalTo(externalMail)) + .body("[0].text", equalTo(REASON)); + + return true; + } catch(AssertionError e) { + return false; + } + }); + } + + private URIBuilder baseUri() { + return new URIBuilder() + .setScheme("http") + .setHost("localhost") + .setPort(guiceJamesServer.getProbe(JmapGuiceProbe.class) + .getJmapPort()) + .setCharset(Charsets.UTF_8); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/469a074b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/pom.xml ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/pom.xml b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/pom.xml index d9fef6d..6b33376 100644 --- a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/pom.xml +++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/pom.xml @@ -178,6 +178,11 @@ </dependency> <dependency> <groupId>org.apache.james</groupId> + <artifactId>james-server-util-java8</artifactId> + <type>test-jar</type> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> <artifactId>james-server-memory-guice</artifactId> <type>test-jar</type> <scope>test</scope> @@ -208,6 +213,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>${assertj-3.version}</version> http://git-wip-us.apache.org/repos/asf/james-project/blob/469a074b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryVacationRelayIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryVacationRelayIntegrationTest.java b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryVacationRelayIntegrationTest.java new file mode 100644 index 0000000..9eea53a --- /dev/null +++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryVacationRelayIntegrationTest.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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.jmap.memory; + +import org.apache.james.GuiceJamesServer; +import org.apache.james.MemoryJmapTestRule; +import org.apache.james.dnsservice.api.DNSService; +import org.apache.james.dnsservice.api.InMemoryDNSService; +import org.apache.james.jmap.VacationRelayIntegrationTest; +import org.junit.Ignore; +import org.junit.Rule; + +@Ignore +public class MemoryVacationRelayIntegrationTest extends VacationRelayIntegrationTest { + + @Rule + public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule(); + + private final InMemoryDNSService inMemoryDNSService = new InMemoryDNSService(); + + @Override + protected void await() {} + + @Override + protected GuiceJamesServer getJmapServer() { + return memoryJmap.jmapServer((binder) -> binder.bind(DNSService.class).toInstance(inMemoryDNSService)); + } + + @Override + protected InMemoryDNSService getInMemoryDns() { + return inMemoryDNSService; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
