You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ma...@apache.org on 2018/05/18 17:44:32 UTC

[2/5] james-project git commit: JAMES-2393 Integration test for Quota mailing

JAMES-2393 Integration test for Quota mailing


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

Branch: refs/heads/master
Commit: 0e53e49532e0f4f3bc723e73343621b7c569676c
Parents: dcdd584
Author: benwa <bt...@linagora.com>
Authored: Wed May 9 15:55:13 2018 +0700
Committer: Matthieu Baechler <ma...@apache.org>
Committed: Fri May 18 19:43:49 2018 +0200

----------------------------------------------------------------------
 .../org/apache/james/CassandraJmapTestRule.java |   4 +-
 .../cassandra/CassandraQuotaMailingTest.java    |  42 ++++++
 .../src/test/resources/listeners.xml            |   3 +
 .../apache/james/jmap/JmapCommonRequests.java   |  11 ++
 .../methods/integration/QuotaMailingTest.java   | 150 +++++++++++++++++++
 .../jmap/memory/MemoryQuotaMailingTest.java     |  38 +++++
 .../src/test/resources/listeners.xml            |   3 +
 7 files changed, 250 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0e53e495/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java
index e5fe99a..08ded89 100644
--- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java
@@ -61,7 +61,9 @@ public class CassandraJmapTestRule implements TestRule {
             .build();
 
         return new GuiceJamesServer(configuration)
-            .combineWith(CassandraJamesServerMain.CASSANDRA_SERVER_MODULE, CassandraJamesServerMain.PROTOCOLS)
+            .combineWith(CassandraJamesServerMain.CASSANDRA_SERVER_MODULE,
+                CassandraJamesServerMain.PROTOCOLS,
+                CassandraJamesServerMain.PLUGINS)
             .overrideWith(binder -> binder.bind(TextExtractor.class).to(PDFTextExtractor.class))
             .overrideWith(new TestJMAPServerModule(LIMIT_TO_3_MESSAGES))
             .overrideWith(new TestESMetricReporterModule())

http://git-wip-us.apache.org/repos/asf/james-project/blob/0e53e495/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraQuotaMailingTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraQuotaMailingTest.java b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraQuotaMailingTest.java
new file mode 100644
index 0000000..6e150fd
--- /dev/null
+++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraQuotaMailingTest.java
@@ -0,0 +1,42 @@
+/****************************************************************
+ * 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 java.io.IOException;
+
+import org.apache.james.CassandraJmapTestRule;
+import org.apache.james.DockerCassandraRule;
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.jmap.methods.integration.QuotaMailingTest;
+import org.junit.ClassRule;
+import org.junit.Rule;
+
+public class CassandraQuotaMailingTest extends QuotaMailingTest {
+    @ClassRule
+    public static DockerCassandraRule cassandra = new DockerCassandraRule();
+
+    @Rule
+    public CassandraJmapTestRule rule = CassandraJmapTestRule.defaultTestRule();
+
+    @Override
+    protected GuiceJamesServer createJmapServer() throws IOException {
+        return rule.jmapServer(cassandra.getModule());
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/0e53e495/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml
index eabdb5e..35cb6b5 100644
--- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml
+++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml
@@ -25,4 +25,7 @@
   <listener>
     <class>org.apache.james.mailbox.cassandra.MailboxOperationLoggingListener</class>
   </listener>
+  <listener>
+    <class>org.apache.james.mailbox.quota.mailing.listeners.QuotaThresholdCrossingListener</class>
+  </listener>
 </listeners>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/0e53e495/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/JmapCommonRequests.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/JmapCommonRequests.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/JmapCommonRequests.java
index 55987a3..2cf3d24 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/JmapCommonRequests.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/JmapCommonRequests.java
@@ -99,6 +99,17 @@ public class JmapCommonRequests {
                 .path(ARGUMENTS + ".messageIds");
     }
 
+    public static String getLastMessageId(AccessToken accessToken) {
+        return with()
+                .header("Authorization", accessToken.serialize())
+                .body("[[\"getMessageList\", {\"sort\":[\"date desc\"]}, \"#0\"]]")
+                .post("/jmap")
+            .then()
+                .extract()
+                .body()
+                .path(ARGUMENTS + ".messageIds[0]");
+    }
+
     public static List<String> listMessageIdsInMailbox(AccessToken accessToken, String mailboxId) {
         return with()
                 .header("Authorization", accessToken.serialize())

http://git-wip-us.apache.org/repos/asf/james-project/blob/0e53e495/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/QuotaMailingTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/QuotaMailingTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/QuotaMailingTest.java
new file mode 100644
index 0000000..cf65c6d
--- /dev/null
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/QuotaMailingTest.java
@@ -0,0 +1,150 @@
+/****************************************************************
+ * 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.methods.integration;
+
+import static com.jayway.restassured.RestAssured.given;
+import static com.jayway.restassured.RestAssured.with;
+import static org.apache.james.jmap.HttpJmapAuthentication.authenticateJamesUser;
+import static org.apache.james.jmap.JmapCommonRequests.getOutboxId;
+import static org.apache.james.jmap.JmapCommonRequests.listMessageIdsForAccount;
+import static org.apache.james.jmap.JmapURIBuilder.baseUri;
+import static org.apache.james.jmap.TestingConstants.ARGUMENTS;
+import static org.apache.james.jmap.TestingConstants.DOMAIN;
+import static org.apache.james.jmap.TestingConstants.calmlyAwait;
+import static org.apache.james.jmap.TestingConstants.jmapRequestSpecBuilder;
+import static org.hamcrest.Matchers.hasItem;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.jmap.api.access.AccessToken;
+import org.apache.james.mailbox.DefaultMailboxes;
+import org.apache.james.mailbox.model.MailboxConstants;
+import org.apache.james.mailbox.quota.QuotaSize;
+import org.apache.james.mailbox.store.mail.model.SerializableQuotaValue;
+import org.apache.james.mailbox.store.probe.MailboxProbe;
+import org.apache.james.modules.MailboxProbeImpl;
+import org.apache.james.modules.QuotaProbesImpl;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.JmapGuiceProbe;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.base.Strings;
+import com.jayway.restassured.RestAssured;
+import com.jayway.restassured.parsing.Parser;
+
+public abstract class QuotaMailingTest {
+    private static final String HOMER = "homer@" + DOMAIN;
+    private static final String BART = "bart@" + DOMAIN;
+    private static final String PASSWORD = "password";
+    private static final String BOB_PASSWORD = "bobPassword";
+
+    protected abstract GuiceJamesServer createJmapServer() throws IOException;
+
+    private AccessToken homerAccessToken;
+    private AccessToken bartAccessToken;
+    private GuiceJamesServer jmapServer;
+
+    @Before
+    public void setup() throws Throwable {
+        jmapServer = createJmapServer();
+        jmapServer.start();
+        MailboxProbe mailboxProbe = jmapServer.getProbe(MailboxProbeImpl.class);
+        DataProbe dataProbe = jmapServer.getProbe(DataProbeImpl.class);
+
+        RestAssured.requestSpecification = jmapRequestSpecBuilder
+            .setPort(jmapServer.getProbe(JmapGuiceProbe.class).getJmapPort())
+            .build();
+        RestAssured.defaultParser = Parser.JSON;
+
+        dataProbe.addDomain(DOMAIN);
+        dataProbe.addUser(HOMER, PASSWORD);
+        dataProbe.addUser(BART, BOB_PASSWORD);
+        mailboxProbe.createMailbox("#private", HOMER, DefaultMailboxes.INBOX);
+        homerAccessToken = authenticateJamesUser(baseUri(jmapServer), HOMER, PASSWORD);
+        bartAccessToken = authenticateJamesUser(baseUri(jmapServer), BART, BOB_PASSWORD);
+    }
+
+    @Test
+    public void shouldSendANoticeWhenThresholdExceeded() throws Exception {
+        jmapServer.getProbe(QuotaProbesImpl.class)
+            .setMaxStorage(MailboxConstants.USER_NAMESPACE + "&" + HOMER,
+                new SerializableQuotaValue<>(QuotaSize.size(100 * 1000)));
+
+        bartSendMessageToHomer();
+        // Homer receives a 83.995 Bytes mail, triggering the 0.80 threshold mailing
+
+        calmlyAwait.atMost(30, TimeUnit.SECONDS)
+            .until(() -> listMessageIdsForAccount(homerAccessToken).size() == 2);
+
+        List<String> ids = listMessageIdsForAccount(homerAccessToken);
+        String idString = ids.stream()
+            .map(id -> "\"" + id + "\"")
+            .collect(Collectors.joining(","));
+
+        given()
+            .header("Authorization", homerAccessToken.serialize())
+            .body("[[\"getMessages\", {\"ids\": [" + idString + "]}, \"#0\"]]")
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .log().ifValidationFails()
+            .body(ARGUMENTS + ".list.subject",
+                hasItem("Warning: Your email usage just exceeded a configured threshold"));
+    }
+
+    private void bartSendMessageToHomer() {
+        String messageCreationId = "creationId";
+        String outboxId = getOutboxId(bartAccessToken);
+        String _80_KB_body = Strings.repeat("123456789\n", 80 * 100);
+        String requestBody = "[" +
+            "  [" +
+            "    \"setMessages\"," +
+            "    {" +
+            "      \"create\": { \"" + messageCreationId  + "\" : {" +
+            "        \"headers\":{\"Disposition-Notification-To\":\"" + BART + "\"}," +
+            "        \"from\": { \"name\": \"Bob\", \"email\": \"" + BART + "\"}," +
+            "        \"to\": [{ \"name\": \"User\", \"email\": \"" + HOMER + "\"}]," +
+            "        \"subject\": \"Message without an attachment\"," +
+            "        \"textBody\": \"" + _80_KB_body + "\"," +
+            "        \"htmlBody\": \"Test <b>body</b>, HTML version\"," +
+            "        \"mailboxIds\": [\"" + outboxId + "\"] " +
+            "      }}" +
+            "    }," +
+            "    \"#0\"" +
+            "  ]" +
+            "]";
+
+        with()
+            .header("Authorization", bartAccessToken.serialize())
+            .body(requestBody)
+            .post("/jmap")
+        .then()
+            .extract()
+            .body()
+            .path(ARGUMENTS + ".created." + messageCreationId + ".id");
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/0e53e495/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryQuotaMailingTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryQuotaMailingTest.java b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryQuotaMailingTest.java
new file mode 100644
index 0000000..8cf5dd7
--- /dev/null
+++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryQuotaMailingTest.java
@@ -0,0 +1,38 @@
+/****************************************************************
+ * 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 java.io.IOException;
+
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.MemoryJmapTestRule;
+import org.apache.james.jmap.methods.integration.QuotaMailingTest;
+import org.junit.Rule;
+
+public class MemoryQuotaMailingTest extends QuotaMailingTest {
+
+    @Rule
+    public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule();
+
+    @Override
+    protected GuiceJamesServer createJmapServer() throws IOException {
+        return memoryJmap.jmapServer();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/0e53e495/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml
index 64f0770..f658226 100644
--- a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml
+++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml
@@ -22,4 +22,7 @@
   <listener>
     <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class>
   </listener>
+  <listener>
+    <class>org.apache.james.mailbox.quota.mailing.listeners.QuotaThresholdCrossingListener</class>
+  </listener>
 </listeners>
\ No newline at end of file


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