JAMES-2244 A message should belong to at least one mailbox

This is an investment for mater (current code overrides Draft mailboxIds 
properties to draft mailboxes).
If this override is removed, we need to check the list of mailboxIds is not 
empty.


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

Branch: refs/heads/master
Commit: b812aaad4c76e90de8c12370f35f7cdc4a06ad26
Parents: 8e9c667
Author: benwa <btell...@linagora.com>
Authored: Tue Dec 12 14:53:56 2017 +0700
Committer: benwa <btell...@linagora.com>
Committed: Thu Dec 14 16:03:11 2017 +0700

----------------------------------------------------------------------
 .../integration/SetMessagesMethodTest.java      | 37 ++++++++++++++++++++
 .../MessageHasNoMailboxException.java           | 24 +++++++++++++
 .../methods/SetMessagesCreationProcessor.java   | 16 +++++++++
 3 files changed, 77 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b812aaad/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 3842a0e..3a6893f 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
@@ -1356,6 +1356,43 @@ public abstract class SetMessagesMethodTest {
     }
 
     @Test
+    public void setMessagesShouldRejectMessageCreationWithNoMailbox() {
+        String messageCreationId = "creationId1337";
+        String fromAddress = USERNAME;
+        String requestBody = "[" +
+            "  [" +
+            "    \"setMessages\","+
+            "    {" +
+            "      \"create\": { \"" + messageCreationId  + "\" : {" +
+            "        \"from\": { \"name\": \"Me\", \"email\": \"" + 
fromAddress + "\"}," +
+            "        \"to\": [{ \"name\": \"BOB\", \"email\": 
\"some...@example.com\"}]," +
+            "        \"subject\": \"subject\"," +
+            "        \"keywords\": {\"$Draft\": true}," +
+            "        \"mailboxIds\": []" +
+            "      }}" +
+            "    }," +
+            "    \"#0\"" +
+            "  ]" +
+            "]";
+
+        given()
+            .header("Authorization", accessToken.serialize())
+            .body(requestBody)
+        .when()
+            .post("/jmap")
+        .then()
+            .log().ifValidationFails()
+            .statusCode(200)
+            .body(NAME, equalTo("messagesSet"))
+            .body(ARGUMENTS + ".created", aMapWithSize(0))
+            .body(ARGUMENTS + ".notCreated", aMapWithSize(1))
+            .body(ARGUMENTS + ".notCreated", hasKey(messageCreationId))
+            .body(ARGUMENTS + ".notCreated." + messageCreationId + ".type", 
equalTo("invalidProperties"))
+            .body(ARGUMENTS + ".notCreated." + messageCreationId + 
".description", equalTo("Message needs to be in at least one mailbox"))
+            .body(ARGUMENTS + ".notCreated." + messageCreationId + 
".properties", contains("mailboxIds"));
+    }
+
+    @Test
     public void setMessagesShouldNotFailWhenSavingADraftInSeveralMailboxes() {
         String messageCreationId = "creationId1337";
         String fromAddress = USERNAME;

http://git-wip-us.apache.org/repos/asf/james-project/blob/b812aaad/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/MessageHasNoMailboxException.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/MessageHasNoMailboxException.java
 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/MessageHasNoMailboxException.java
new file mode 100644
index 0000000..493a926
--- /dev/null
+++ 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/MessageHasNoMailboxException.java
@@ -0,0 +1,24 @@
+/****************************************************************
+ * 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.exceptions;
+
+public class MessageHasNoMailboxException extends RuntimeException {
+    
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/b812aaad/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 7733b46..07c087c 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
@@ -33,6 +33,7 @@ import 
org.apache.james.jmap.exceptions.AttachmentsNotFoundException;
 import org.apache.james.jmap.exceptions.InvalidDraftKeywordsException;
 import org.apache.james.jmap.exceptions.InvalidMailboxForCreationException;
 import org.apache.james.jmap.exceptions.MailboxNotOwnedException;
+import org.apache.james.jmap.exceptions.MessageHasNoMailboxException;
 import org.apache.james.jmap.methods.ValueWithId.CreationMessageEntry;
 import org.apache.james.jmap.methods.ValueWithId.MessageWithId;
 import org.apache.james.jmap.model.CreationMessage;
@@ -111,6 +112,7 @@ public class SetMessagesCreationProcessor implements 
SetMessagesProcessor {
 
     private void handleCreate(CreationMessageEntry create, Builder 
responseBuilder, MailboxSession mailboxSession) {
         try {
+            assertAtLeastOneMailbox(create);
             assertIsUserOwnerOfMailboxes(create, mailboxSession);
             performCreate(create, responseBuilder, mailboxSession);
         } catch (MailboxSendingNotAllowedException e) {
@@ -147,6 +149,14 @@ public class SetMessagesCreationProcessor implements 
SetMessagesProcessor {
                         .description("Message creation is only supported in 
mailboxes with role Draft and Outbox")
                         .build());
 
+        } catch (MessageHasNoMailboxException e) {
+            responseBuilder.notCreated(create.getCreationId(),
+                    SetError.builder()
+                        .type("invalidProperties")
+                        .properties(MessageProperty.mailboxIds)
+                        .description("Message needs to be in at least one 
mailbox")
+                        .build());
+
         } catch (MailboxInvalidMessageCreationException e) {
             responseBuilder.notCreated(create.getCreationId(),
                     
buildSetErrorFromValidationResult(create.getValue().validate()));
@@ -197,6 +207,12 @@ public class SetMessagesCreationProcessor implements 
SetMessagesProcessor {
         }
     }
 
+    private void assertAtLeastOneMailbox(CreationMessageEntry entry) throws 
MailboxException {
+        if (entry.getValue().getMailboxIds().isEmpty()) {
+            throw new MessageHasNoMailboxException();
+        }
+    }
+
     private void sendMailViaOutbox(CreationMessageEntry entry, Builder 
responseBuilder, MailboxSession session) throws AttachmentsNotFoundException, 
MailboxException, MessagingException {
         validateArguments(entry, session);
         MessageWithId created = handleOutboxMessages(entry, session);


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