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 ad...@apache.org on 2017/11/16 14:19:51 UTC

[04/18] james-project git commit: JAMES-2218 Disable creating Outbox messages with Draft flags

JAMES-2218 Disable creating Outbox messages with Draft flags


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

Branch: refs/heads/master
Commit: ad893d026fc73c07cc6204926d9413b951f7df4d
Parents: 45e7f3a
Author: benwa <bt...@linagora.com>
Authored: Wed Nov 15 15:28:12 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Thu Nov 16 12:28:58 2017 +0100

----------------------------------------------------------------------
 .../integration/SetMessagesMethodTest.java      | 10 +++++-----
 .../cucumber/SetMessagesMethodStepdefs.java     | 21 +++++++++++++-------
 .../test/resources/cucumber/SetMessages.feature | 13 ++++++++----
 .../InvalidDraftKeywordsException.java          |  4 +++-
 .../methods/SetMessagesCreationProcessor.java   | 20 ++++++++++++++-----
 5 files changed, 46 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/ad893d02/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
index 951d369..054b589 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
@@ -1288,7 +1288,7 @@ public abstract class SetMessagesMethodTest {
                 "        \"to\": [{ \"name\": \"BOB\", \"email\": \"someone@example.com\"}]," +
                 "        \"subject\": \"subject\"," +
                 "        \"isDraft\": true," +
-                "        \"keywords\": {\"$Draft\": true}," +
+                "        \"keywords\": {\"$Answered\": true}," +
                 "        \"mailboxIds\": [\"" + getOutboxId(accessToken) + "\"]" +
                 "      }}" +
                 "    }," +
@@ -1321,7 +1321,7 @@ public abstract class SetMessagesMethodTest {
                 "        \"from\": { \"name\": \"Me\", \"email\": \"" + fromAddress + "\"}," +
                 "        \"to\": [{ \"name\": \"BOB\", \"email\": \"someone@example.com\"}]," +
                 "        \"subject\": \"subject\"," +
-                "        \"keywords\": {\"$Draft\": true, \"$Flagged\": true}," +
+                "        \"keywords\": {\"$Answered\": true, \"$Flagged\": true}," +
                 "        \"mailboxIds\": [\"" + getOutboxId(accessToken) + "\"]" +
                 "      }}" +
                 "    }," +
@@ -1341,7 +1341,7 @@ public abstract class SetMessagesMethodTest {
             .body(ARGUMENTS + ".notCreated", aMapWithSize(0))
             .body(ARGUMENTS + ".created", aMapWithSize(1))
             .body(ARGUMENTS + ".created", hasKey(messageCreationId))
-            .body(ARGUMENTS + ".created[\""+messageCreationId+"\"].keywords.$Draft", equalTo(true))
+            .body(ARGUMENTS + ".created[\""+messageCreationId+"\"].keywords.$Answered", equalTo(true))
             .body(ARGUMENTS + ".created[\""+messageCreationId+"\"].keywords.$Flagged", equalTo(true));
     }
 
@@ -4497,7 +4497,7 @@ public abstract class SetMessagesMethodTest {
                 "        \"from\": { \"name\": \"Me\", \"email\": \"" + fromAddress + "\"}," +
                 "        \"to\": [{ \"name\": \"BOB\", \"email\": \"someone@example.com\"}]," +
                 "        \"subject\": \"subject\"," +
-                "        \"keywords\": {\"$Draft\": true, \"$Unknown\": true}," +
+                "        \"keywords\": {\"$Answered\": true, \"$Unknown\": true}," +
                 "        \"mailboxIds\": [\"" + getOutboxId(accessToken) + "\"]" +
                 "      }}" +
                 "    }," +
@@ -4530,7 +4530,7 @@ public abstract class SetMessagesMethodTest {
             "        \"from\": { \"name\": \"Me\", \"email\": \"" + fromAddress + "\"}," +
             "        \"to\": [{ \"name\": \"BOB\", \"email\": \"someone@example.com\"}]," +
             "        \"subject\": \"subject\"," +
-            "        \"keywords\": {\"$Draft\": true, \"$Deleted\": true}," +
+            "        \"keywords\": {\"$Answered\": true, \"$Deleted\": true}," +
             "        \"mailboxIds\": [\"" + getOutboxId(accessToken) + "\"]" +
             "      }}" +
             "    }," +

http://git-wip-us.apache.org/repos/asf/james-project/blob/ad893d02/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java
index fc9370f..3522d3c 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java
@@ -23,12 +23,12 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 import javax.inject.Inject;
 import javax.mail.Flags;
 
-import org.apache.james.jmap.DefaultMailboxes;
 import org.apache.james.jmap.model.Keywords;
 import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxId;
@@ -195,13 +195,12 @@ public class SetMessagesMethodStepdefs {
         });
     }
 
-
-    @Given("^\"([^\"]*)\" creates a draft message \"([^\"]*)\"")
-    public void createDraft(String username, String message) throws Throwable {
+    @Given("^\"([^\"]*)\" creates a draft message \"([^\"]*)\" in mailbox \"([^\"]*)\"")
+    public void createDraft(String username, String message, String mailboxName) throws Throwable {
         userStepdefs.execWithUser(username, () -> {
             Mailbox mailbox = mainStepdefs.mailboxProbe.getMailbox(MailboxConstants.USER_NAMESPACE,
                 username,
-                DefaultMailboxes.DRAFTS);
+                mailboxName);
             httpClient.post("[" +
                 "  [" +
                 "    \"setMessages\","+
@@ -216,8 +215,10 @@ public class SetMessagesMethodStepdefs {
                 "  ]" +
                 "]");
             mainStepdefs.awaitMethod.run();
-            String messageId = httpClient.jsonPath.read("[0][1].created." + message + ".id");
-            getMessagesMethodStepdefs.addMessageId(message, mainStepdefs.messageIdFactory.fromString(messageId));
+            Optional.ofNullable(
+                httpClient.jsonPath.<String>read("[0][1].created." + message + ".id"))
+                .map(mainStepdefs.messageIdFactory::fromString)
+                .ifPresent(id -> getMessagesMethodStepdefs.addMessageId(message, id));
         });
     }
 
@@ -264,4 +265,10 @@ public class SetMessagesMethodStepdefs {
             .containsOnlyKeys(id.serialize());
     }
 
+    @Then("^message \"([^\"]*)\" is not created")
+    public void assertNotCreated(String messageName) throws Exception {;
+        assertThat(httpClient.jsonPath.<Map<String, String>>read("[0][1].notCreated"))
+            .containsOnlyKeys(messageName);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/ad893d02/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/SetMessages.feature
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/SetMessages.feature b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/SetMessages.feature
index 2294513..9e6dcce 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/SetMessages.feature
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/resources/cucumber/SetMessages.feature
@@ -106,20 +106,25 @@ Feature: SetMessages method on shared folders
 
   Scenario: A user can update the flags on a draft
     Given "bob@domain.tld" has a mailbox "Drafts"
-    And "bob@domain.tld" creates a draft message "mDraft"
+    And "bob@domain.tld" creates a draft message "mDraft" in mailbox "Drafts"
     When "bob@domain.tld" sets flags "$Draft,$Seen" on message "mDraft"
     Then "bob@domain.tld" should see message "mDraft" with keywords $Draft,$Seen
 
   Scenario: A user can not remove a draft flag on a draft messages
     Given "bob@domain.tld" has a mailbox "Drafts"
-    And "bob@domain.tld" creates a draft message "mDraft"
+    And "bob@domain.tld" creates a draft message "mDraft" in mailbox "Drafts"
     When "bob@domain.tld" sets flags "$Seen" on message "mDraft"
     Then message "mDraft" is not updated
     And "bob@domain.tld" should see message "mDraft" with keywords $Draft
 
   Scenario: A user can destroy a draft
     Given "bob@domain.tld" has a mailbox "Drafts"
-    And "bob@domain.tld" creates a draft message "mDraft"
+    And "bob@domain.tld" creates a draft message "mDraft" in mailbox "Drafts"
     When "bob@domain.tld" destroys message "mDraft"
     Then "bob@domain.tld" ask for message "mDraft"
-    And the notFound list should contain "mDraft"
\ No newline at end of file
+    And the notFound list should contain "mDraft"
+
+  Scenario: Draft creation in outbox is not allowed
+    Given "bob@domain.tld" has a mailbox "Outbox"
+    When "bob@domain.tld" creates a draft message "mDraft" in mailbox "Outbox"
+    Then message "mDraft" is not created
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/ad893d02/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/InvalidDraftKeywordsException.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/InvalidDraftKeywordsException.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/InvalidDraftKeywordsException.java
index 08dd9b6..a9f7e2f 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/InvalidDraftKeywordsException.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/InvalidDraftKeywordsException.java
@@ -20,5 +20,7 @@
 package org.apache.james.jmap.exceptions;
 
 public class InvalidDraftKeywordsException extends IllegalArgumentException {
-
+    public InvalidDraftKeywordsException(String s) {
+        super(s);
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/ad893d02/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
index 4bb43dc..400834e 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
@@ -126,7 +126,7 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor {
                 SetError.builder()
                     .type("invalidProperties")
                     .properties(MessageProperty.keywords)
-                    .description("A draft message should be flagged as Draft")
+                    .description(e.getMessage())
                     .build());
 
         } catch (AttachmentsNotFoundException e) {
@@ -188,6 +188,7 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor {
 
     private void sendMailViaOutbox(CreationMessageEntry entry, Builder responseBuilder, MailboxSession session) throws AttachmentsNotFoundException, MailboxException, MessagingException {
         validateArguments(entry, session);
+        assertNoDraftKeywords(entry);
         MessageWithId created = handleOutboxMessages(entry, session);
         responseBuilder.created(created.getCreationId(), created.getValue());
     }
@@ -200,13 +201,22 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor {
     }
 
     private void assertDraftKeywords(CreationMessageEntry entry) {
-        Boolean isDraft = entry.getValue()
+        if (!isDraft(entry)) {
+            throw new InvalidDraftKeywordsException("A draft message should be flagged as Draft");
+        }
+    }
+
+    private void assertNoDraftKeywords(CreationMessageEntry entry) {
+        if (isDraft(entry)) {
+            throw new InvalidDraftKeywordsException("A draft message can not be created out of draft mailbox");
+        }
+    }
+
+    private Boolean isDraft(CreationMessageEntry entry) {
+        return entry.getValue()
             .getKeywords()
             .map(keywords -> keywords.contains(Keyword.DRAFT))
             .orElse(false);
-        if (!isDraft) {
-            throw new InvalidDraftKeywordsException();
-        }
     }
 
     private void validateArguments(CreationMessageEntry entry, MailboxSession session) throws MailboxInvalidMessageCreationException, AttachmentsNotFoundException, MailboxException {


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