JAMES-2169 implement Mailbox.sharedWith filtering based on user rights

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

Branch: refs/heads/master
Commit: 5edd32af4096b430eaf92c5c2ca47bb12fc687e0
Parents: d75d565
Author: Matthieu Baechler <[email protected]>
Authored: Fri Sep 29 17:20:38 2017 +0200
Committer: benwa <[email protected]>
Committed: Wed Oct 4 16:19:58 2017 +0700

----------------------------------------------------------------------
 .../apache/james/mailbox/MessageManager.java    |   3 +-
 .../apache/james/mailbox/model/MailboxACL.java  |   1 -
 .../james/mailbox/fixture/MailboxFixture.java   |   3 +-
 .../inmemory/InMemoryMessageManagerTest.java    |  38 +++++++
 .../InMemoryMessageManagerTestSystem.java       | 110 +++++++++++++++++++
 .../MessageManagerTestSystemProvider.java       |  73 ++++++++++++
 .../mailbox/store/StoreMessageManager.java      |  30 +++--
 .../store/AbstractMessageManagerTest.java       |  84 ++++++++++++++
 .../mailbox/store/MessageManagerTestSystem.java |  64 +++++++++++
 .../mailbox/store/StoreMailboxManagerTest.java  |   6 +-
 .../mailbox/store/StoreMessageManagerTest.java  |  68 ++++++++++++
 .../integration/GetMailboxesMethodTest.java     |  17 ++-
 12 files changed, 482 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java
----------------------------------------------------------------------
diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java 
b/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java
index edad736..ee152d6 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java
@@ -184,8 +184,7 @@ public interface MessageManager {
      *            context, not null
      * @param fetchGroup
      *            describes which optional data should be returned
-     * @return meta data, not null
-     * @throws MailboxException
+     * @return metadata view filtered for the session's user, not null
      */
     MetaData getMetaData(boolean resetRecent, MailboxSession mailboxSession, 
MessageManager.MetaData.FetchGroup fetchGroup) throws MailboxException;
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java
----------------------------------------------------------------------
diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java 
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java
index 569eeae..d87d85e 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java
@@ -527,7 +527,6 @@ public class MailboxACL {
                 return this;
             }
 
-
             public Builder key(EntryKey key) {
                 this.key = key;
                 return this;

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/api/src/test/java/org/apache/james/mailbox/fixture/MailboxFixture.java
----------------------------------------------------------------------
diff --git 
a/mailbox/api/src/test/java/org/apache/james/mailbox/fixture/MailboxFixture.java
 
b/mailbox/api/src/test/java/org/apache/james/mailbox/fixture/MailboxFixture.java
index 645a04a..90d2095 100644
--- 
a/mailbox/api/src/test/java/org/apache/james/mailbox/fixture/MailboxFixture.java
+++ 
b/mailbox/api/src/test/java/org/apache/james/mailbox/fixture/MailboxFixture.java
@@ -26,7 +26,8 @@ public class MailboxFixture {
 
     public static final String USER = "user";
     public static final String OTHER_USER = "otheruser";
-    
+    public static final String THIRD_USER = "thirdUser";
+
     public static final MailboxPath MAILBOX_PATH1 = new 
MailboxPath(PRIVATE_NAMESPACE, USER, "INBOX");
     public static final MailboxPath MAILBOX_PATH2 = new 
MailboxPath(PRIVATE_NAMESPACE, USER, "OUTBOX");
     public static final MailboxPath MAILBOX_PATH3 = new 
MailboxPath(PRIVATE_NAMESPACE, USER, "SENT");

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMessageManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMessageManagerTest.java
 
b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMessageManagerTest.java
new file mode 100644
index 0000000..2422cff
--- /dev/null
+++ 
b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMessageManagerTest.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.mailbox.inmemory;
+
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.store.AbstractMessageManagerTest;
+import org.apache.james.mailbox.store.MessageManagerTestSystem;
+import org.junit.Before;
+
+public class InMemoryMessageManagerTest extends AbstractMessageManagerTest {
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+    
+    @Override
+    protected MessageManagerTestSystem createTestSystem() throws 
MailboxException {
+        return MessageManagerTestSystemProvider.createTestSystem();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMessageManagerTestSystem.java
----------------------------------------------------------------------
diff --git 
a/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMessageManagerTestSystem.java
 
b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMessageManagerTestSystem.java
new file mode 100644
index 0000000..0e9d9d2
--- /dev/null
+++ 
b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMessageManagerTestSystem.java
@@ -0,0 +1,110 @@
+/****************************************************************
+ * 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.mailbox.inmemory;
+
+import java.io.ByteArrayInputStream;
+import java.util.Date;
+import java.util.Optional;
+
+import javax.mail.Flags;
+
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxId;
+import org.apache.james.mailbox.model.MailboxMetaData;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MailboxQuery;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.store.MessageIdManagerTestSystem;
+import org.apache.james.mailbox.store.MessageManagerTestSystem;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+
+import com.google.common.base.Charsets;
+import com.google.common.base.Throwables;
+
+public class InMemoryMessageManagerTestSystem extends MessageManagerTestSystem 
{
+
+    private static final MessageId FIRST_MESSAGE_ID = InMemoryMessageId.of(1);
+    private static final long ONE_HUNDRED = 100;
+    private static final int UID_VALIDITY = 1024;
+    public static final byte[] CONTENT = "Subject: 
test\r\n\r\ntestmail".getBytes(Charsets.UTF_8);
+
+    private final MailboxManager mailboxManager;
+    private Optional<MessageId> lastMessageIdUsed;
+
+    public InMemoryMessageManagerTestSystem(MailboxManager mailboxManager) 
throws MailboxException {
+        super(mailboxManager);
+        this.mailboxManager = mailboxManager;
+        this.lastMessageIdUsed = Optional.empty();
+    }
+
+    @Override
+    public Mailbox createMailbox(MailboxPath mailboxPath, MailboxSession 
mailboxSession) throws MailboxException {
+        mailboxManager.createMailbox(mailboxPath, mailboxSession);
+        MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, 
mailboxSession);
+        return new SimpleMailbox(mailboxPath, UID_VALIDITY, 
messageManager.getId());
+    }
+
+    @Override
+    public MessageId persist(MailboxId mailboxId, MessageUid uid, Flags flags, 
MailboxSession session) {
+        try {
+            MessageManager messageManager = 
mailboxManager.getMailbox(mailboxId, session);
+            MessageId messageId = messageManager.appendMessage(new 
ByteArrayInputStream(CONTENT), new Date(), session, false, flags)
+                    .getMessageId();
+            lastMessageIdUsed = Optional.of(messageId);
+            return messageId;
+        } catch (MailboxException e) {
+            throw Throwables.propagate(e);
+        }
+    }
+
+    @Override
+    public MessageId createNotUsedMessageId() {
+        return 
InMemoryMessageId.of(Long.valueOf(lastMessageIdUsed.orElse(FIRST_MESSAGE_ID).serialize())
 + ONE_HUNDRED);
+    }
+
+    @Override
+    public void deleteMailbox(final MailboxId mailboxId, MailboxSession 
session) {
+        try {
+            Optional<MailboxMetaData> mailbox = retrieveMailbox(mailboxId, 
session);
+            if (mailbox.isPresent()) {
+                mailboxManager.deleteMailbox(mailbox.get().getPath(), session);
+            }
+        } catch (MailboxException e) {
+            Throwables.propagate(e);
+        }
+    }
+
+    private Optional<MailboxMetaData> retrieveMailbox(final MailboxId 
mailboxId, MailboxSession mailboxSession) throws MailboxException {
+        MailboxQuery userMailboxesQuery = 
MailboxQuery.builder(mailboxSession).expression("*").build();
+        return mailboxManager.search(userMailboxesQuery, mailboxSession)
+            .stream()
+            .filter(mailboxMetaData -> 
mailboxMetaData.getId().equals(mailboxId))
+            .findFirst();
+    }
+
+    @Override
+    public int getConstantMessageSize() {
+        return CONTENT.length;
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/MessageManagerTestSystemProvider.java
----------------------------------------------------------------------
diff --git 
a/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/MessageManagerTestSystemProvider.java
 
b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/MessageManagerTestSystemProvider.java
new file mode 100644
index 0000000..3776033
--- /dev/null
+++ 
b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/MessageManagerTestSystemProvider.java
@@ -0,0 +1,73 @@
+/****************************************************************
+ * 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.mailbox.inmemory;
+
+import org.apache.james.mailbox.acl.GroupMembershipResolver;
+import org.apache.james.mailbox.acl.MailboxACLResolver;
+import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
+import org.apache.james.mailbox.acl.UnionMailboxACLResolver;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.fixture.MailboxFixture;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.store.CombinationManagerTestSystem;
+import org.apache.james.mailbox.store.FakeAuthenticator;
+import org.apache.james.mailbox.store.FakeAuthorizator;
+import org.apache.james.mailbox.store.MessageManagerTestSystem;
+import org.apache.james.mailbox.store.mail.model.impl.MessageParser;
+
+import com.google.common.base.Throwables;
+
+public class MessageManagerTestSystemProvider {
+
+    private static final int LIMIT_ANNOTATIONS = 3;
+    private static final int LIMIT_ANNOTATION_SIZE = 30;
+
+    private static final String PASSWORD = "password";
+
+    public static MessageManagerTestSystem createTestSystem() throws 
MailboxException {
+        return new InMemoryMessageManagerTestSystem(createMailboxManager());
+    }
+
+    public static CombinationManagerTestSystem createManagersTestingData() {
+        InMemoryMailboxManager mailboxManager = createMailboxManager();
+        return new InMemoryCombinationManagerTestSystem(mailboxManager, new 
InMemoryMessageIdManager(mailboxManager));
+    }
+
+    private static InMemoryMailboxManager createMailboxManager() {
+        MailboxACLResolver aclResolver = new UnionMailboxACLResolver();
+        GroupMembershipResolver groupMembershipResolver = new 
SimpleGroupMembershipResolver();
+        MessageParser messageParser = new MessageParser();
+
+        InMemoryMailboxSessionMapperFactory mailboxSessionMapperFactory = new 
InMemoryMailboxSessionMapperFactory();
+        MessageId.Factory messageIdFactory = new InMemoryMessageId.Factory();
+        FakeAuthenticator authenticator = new FakeAuthenticator();
+        FakeAuthorizator authorizator = FakeAuthorizator.defaultReject();
+        authenticator.addUser(MailboxFixture.USER, PASSWORD);
+        authenticator.addUser(MailboxFixture.OTHER_USER, PASSWORD);
+        InMemoryMailboxManager mailboxManager = new 
InMemoryMailboxManager(mailboxSessionMapperFactory, authenticator, authorizator,
+                aclResolver, groupMembershipResolver, messageParser, 
messageIdFactory, LIMIT_ANNOTATIONS, LIMIT_ANNOTATION_SIZE);
+
+        try {
+            mailboxManager.init();
+        } catch (MailboxException e) {
+            Throwables.propagate(e);
+        }
+        return mailboxManager;
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
index 998ec96..7966e38 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
@@ -91,6 +91,7 @@ import org.apache.james.util.IteratorWrapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
@@ -469,11 +470,8 @@ public class StoreMessageManager implements 
org.apache.james.mailbox.MessageMana
         return aclResolver.isReadWrite(myRights(session), 
getSharedPermanentFlags(session));
     }
 
-    /**
-     * @see MessageManager#getMetaData(boolean, MailboxSession,
-     *      org.apache.james.mailbox.MessageManager.MetaData.FetchGroup)
-     */
-    public MetaData getMetaData(boolean resetRecent, MailboxSession 
mailboxSession, org.apache.james.mailbox.MessageManager.MetaData.FetchGroup 
fetchGroup) throws MailboxException {
+    @Override
+    public MetaData getMetaData(boolean resetRecent, MailboxSession 
mailboxSession, MetaData.FetchGroup fetchGroup) throws MailboxException {
 
         final List<MessageUid> recent;
         final Flags permanentFlags = getPermanentFlags(mailboxSession);
@@ -816,9 +814,27 @@ public class StoreMessageManager implements 
org.apache.james.mailbox.MessageMana
      * @throws UnsupportedRightException
      */
     protected MailboxACL getResolvedMailboxACL(MailboxSession mailboxSession) 
throws UnsupportedRightException {
-        return aclResolver.applyGlobalACL(mailbox.getACL(), new 
GroupFolderResolver(mailboxSession).isGroupFolder(mailbox));
+        return filteredForSession(mailbox, 
aclResolver.applyGlobalACL(mailbox.getACL(), new 
GroupFolderResolver(mailboxSession).isGroupFolder(mailbox)), mailboxSession);
     }
-    
+
+    /**
+     * ACL is sensible information and as such we should expose as few 
information as possible
+     * to users. This method allows to filter a {@link MailboxACL} in order to 
present it to
+     * the connected user.
+     */
+    @VisibleForTesting static MailboxACL filteredForSession(Mailbox mailbox, 
MailboxACL acl, MailboxSession mailboxSession) throws UnsupportedRightException 
{
+        if (mailboxSession.getUser().isSameUser(mailbox.getUser())) {
+            return acl;
+        }
+        MailboxACL.EntryKey userAsKey = 
MailboxACL.EntryKey.createUser(mailboxSession.getUser().getUserName());
+        Rfc4314Rights rights = acl.getEntries().getOrDefault(userAsKey, new 
Rfc4314Rights());
+        if (rights.contains(MailboxACL.Right.Administer)) {
+            return acl;
+        }
+        return new MailboxACL(ImmutableMap.of(userAsKey, rights));
+    }
+
+
     @Override
     public MailboxId getId() {
         return mailbox.getMailboxId();

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/store/src/test/java/org/apache/james/mailbox/store/AbstractMessageManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/AbstractMessageManagerTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/AbstractMessageManagerTest.java
new file mode 100644
index 0000000..d85d342
--- /dev/null
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/AbstractMessageManagerTest.java
@@ -0,0 +1,84 @@
+/****************************************************************
+ * 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.mailbox.store;
+
+import static org.apache.james.mailbox.fixture.MailboxFixture.MAILBOX_PATH1;
+import static org.apache.james.mailbox.fixture.MailboxFixture.OTHER_USER;
+import static org.apache.james.mailbox.fixture.MailboxFixture.THIRD_USER;
+import static org.apache.james.mailbox.fixture.MailboxFixture.USER;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import javax.mail.Flags;
+
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSession.SessionType;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.fixture.MailboxFixture;
+import org.apache.james.mailbox.mock.MockMailboxSession;
+import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+import org.junit.Test;
+
+public abstract class AbstractMessageManagerTest {
+
+    private static final boolean NO_RESET_RECENT = false;
+
+    private MessageManagerTestSystem testSystem;
+    private MailboxManager mailboxManager;
+    private MailboxSession session;
+    private MailboxSession otherSession;
+
+    protected abstract MessageManagerTestSystem createTestSystem() throws 
Exception;
+
+    public void setUp() throws Exception {
+        session = new MockMailboxSession(USER);
+        otherSession = new MockMailboxSession(OTHER_USER);
+        testSystem = createTestSystem();
+        mailboxManager = testSystem.getMailboxManager();
+
+        testSystem.createMailbox(MAILBOX_PATH1, session);
+        testSystem.createMailbox(MailboxFixture.MAILBOX_PATH2, session);
+        testSystem.createMailbox(MailboxFixture.MAILBOX_PATH3, session);
+        testSystem.createMailbox(MailboxFixture.MAILBOX_PATH4, otherSession);
+    }
+
+    @Test
+    public void getMetadataShouldListUsersAclWhenShared() throws Exception {
+        mailboxManager.applyRightsCommand(MAILBOX_PATH1, 
MailboxACL.command().forUser(OTHER_USER).rights(MailboxACL.Right.Read).asAddition(),
 session);
+        mailboxManager.applyRightsCommand(MAILBOX_PATH1, 
MailboxACL.command().forUser(THIRD_USER).rights(MailboxACL.Right.Read).asAddition(),
 session);
+        MessageManager messageManager = 
mailboxManager.getMailbox(MAILBOX_PATH1, session);
+
+        MessageManager.MetaData actual = 
messageManager.getMetaData(NO_RESET_RECENT, session, 
MessageManager.MetaData.FetchGroup.NO_COUNT);
+        
assertThat(actual.getACL().getEntries()).containsKeys(MailboxACL.EntryKey.createUser(OTHER_USER),
 MailboxACL.EntryKey.createUser(THIRD_USER));
+    }
+
+    @Test
+    public void getMetadataShouldNotExposeOtherUsersWhenSessionIsNotOwner() 
throws Exception {
+        mailboxManager.applyRightsCommand(MAILBOX_PATH1, 
MailboxACL.command().forUser(OTHER_USER).rights(MailboxACL.Right.Read).asAddition(),
 session);
+        mailboxManager.applyRightsCommand(MAILBOX_PATH1, 
MailboxACL.command().forUser(THIRD_USER).rights(MailboxACL.Right.Read).asAddition(),
 session);
+        MessageManager messageManager = 
mailboxManager.getMailbox(MAILBOX_PATH1, session);
+
+        MessageManager.MetaData actual = 
messageManager.getMetaData(NO_RESET_RECENT, otherSession, 
MessageManager.MetaData.FetchGroup.NO_COUNT);
+        
assertThat(actual.getACL().getEntries()).containsOnlyKeys(MailboxACL.EntryKey.createUser(OTHER_USER));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/store/src/test/java/org/apache/james/mailbox/store/MessageManagerTestSystem.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/MessageManagerTestSystem.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/MessageManagerTestSystem.java
new file mode 100644
index 0000000..f1715d6
--- /dev/null
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/MessageManagerTestSystem.java
@@ -0,0 +1,64 @@
+/****************************************************************
+ * 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.mailbox.store;
+
+import javax.mail.Flags;
+
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxId;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public abstract class MessageManagerTestSystem {
+
+    private final MailboxManager mailboxManager;
+
+    public MessageManagerTestSystem(MailboxManager mailboxManager) {
+        this.mailboxManager = mailboxManager;
+    }
+
+    public MailboxManager getMailboxManager() {
+        return mailboxManager;
+    }
+
+    public abstract Mailbox createMailbox(MailboxPath mailboxPath, 
MailboxSession session) throws MailboxException;
+
+    /**
+     * Should take care of find returning the MailboxMessage
+     * Should take care of findMailboxes returning the mailbox the message is 
in
+     * Should persist flags 
+     * Should keep track of flag state for setFlags
+     * 
+     * @param mailboxId
+     * @param flags
+     * @return the id of persisted message
+     */
+    public abstract MessageId persist(MailboxId mailboxId, MessageUid uid, 
Flags flags, MailboxSession session);
+
+    public abstract MessageId createNotUsedMessageId();
+
+    public abstract void deleteMailbox(MailboxId mailboxId, MailboxSession 
session);
+
+    public abstract int getConstantMessageSize();
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
index e0db1ec..8c3819d 100644
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMailboxManagerTest.java
@@ -20,6 +20,7 @@
 package org.apache.james.mailbox.store;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -33,6 +34,7 @@ import 
org.apache.james.mailbox.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.exception.NotAdminException;
 import org.apache.james.mailbox.exception.UserDoesNotExistException;
 import org.apache.james.mailbox.mock.MockMailboxSession;
+import org.apache.james.mailbox.model.MailboxACL;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.MessageId;
@@ -167,11 +169,13 @@ public class StoreMailboxManagerTest {
     }
 
     @Test(expected = MailboxNotFoundException.class)
-    public void getMailboxShouldThrowWhenMailboxDoesNotMatchUser() throws 
Exception {
+    public void getMailboxShouldThrowWhenMailboxDoesNotMatchUserWithoutRight() 
throws Exception {
         Mailbox mockedMailbox = mock(Mailbox.class);
+        when(mockedMailbox.getACL()).thenReturn(new MailboxACL());
         when(mockedMailbox.getUser()).thenReturn("other.user");
         when(mockedMailbox.getMailboxId()).thenReturn(MAILBOX_ID);
         
when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(mockedMailbox);
+        
when(mockedMailboxMapper.findMailboxByPath(any())).thenReturn(mockedMailbox);
 
         MessageManager expected = storeMailboxManager.getMailbox(MAILBOX_ID, 
mockedMailboxSession);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMessageManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMessageManagerTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMessageManagerTest.java
new file mode 100644
index 0000000..9172412
--- /dev/null
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreMessageManagerTest.java
@@ -0,0 +1,68 @@
+/****************************************************************
+ * 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.mailbox.store;
+
+import static org.apache.james.mailbox.fixture.MailboxFixture.MAILBOX_PATH1;
+import static org.apache.james.mailbox.fixture.MailboxFixture.OTHER_USER;
+import static org.apache.james.mailbox.fixture.MailboxFixture.THIRD_USER;
+import static org.apache.james.mailbox.fixture.MailboxFixture.USER;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.mailbox.exception.UnsupportedRightException;
+import org.apache.james.mailbox.mock.MockMailboxSession;
+import org.apache.james.mailbox.model.MailboxACL;
+import org.apache.james.mailbox.model.MailboxACL.Right;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+import org.junit.Test;
+
+public class StoreMessageManagerTest {
+
+    public static final long UID_VALIDITY = 3421l;
+
+    @Test
+    public void filteredForSessionShouldBeIdentityWhenOwner() throws 
UnsupportedRightException {
+        MailboxACL acl = new MailboxACL()
+            .apply(MailboxACL.command().rights(Right.Read, 
Right.Write).forUser(OTHER_USER).asAddition())
+            .apply(MailboxACL.command().rights(Right.Read, Right.Write, 
Right.Administer).forUser(THIRD_USER).asAddition());
+        MailboxACL actual = StoreMessageManager.filteredForSession(
+            new SimpleMailbox(MAILBOX_PATH1, UID_VALIDITY), acl, new 
MockMailboxSession(USER));
+        assertThat(actual).isEqualTo(acl);
+    }
+
+    @Test
+    public void filteredForSessionShouldBeIdentityWhenAdmin() throws 
UnsupportedRightException {
+        MailboxACL acl = new MailboxACL()
+            .apply(MailboxACL.command().rights(Right.Read, 
Right.Write).forUser(OTHER_USER).asAddition())
+            .apply(MailboxACL.command().rights(Right.Read, Right.Write, 
Right.Administer).forUser(THIRD_USER).asAddition());
+        MailboxACL actual = StoreMessageManager.filteredForSession(
+            new SimpleMailbox(MAILBOX_PATH1, UID_VALIDITY), acl, new 
MockMailboxSession(THIRD_USER));
+        assertThat(actual).isEqualTo(acl);
+    }
+
+    @Test
+    public void 
filteredForSessionShouldContainOnlyLoggedUserWhenReadWriteAccess() throws 
UnsupportedRightException {
+        MailboxACL acl = new MailboxACL()
+            .apply(MailboxACL.command().rights(Right.Read, 
Right.Write).forUser(OTHER_USER).asAddition())
+            .apply(MailboxACL.command().rights(Right.Read, Right.Write, 
Right.Administer).forUser(THIRD_USER).asAddition());
+        MailboxACL actual = StoreMessageManager.filteredForSession(
+            new SimpleMailbox(MAILBOX_PATH1, UID_VALIDITY), acl, new 
MockMailboxSession(OTHER_USER));
+        
assertThat(actual.getEntries()).containsKey(MailboxACL.EntryKey.createUser(OTHER_USER));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/5edd32af/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java
 
b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java
index 1a9f568..7c49289 100644
--- 
a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java
+++ 
b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java
@@ -22,6 +22,7 @@ package org.apache.james.jmap.methods.integration;
 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.assertj.core.api.Assertions.assertThat;
 import static org.hamcrest.Matchers.empty;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasEntry;
@@ -37,6 +38,7 @@ import java.io.ByteArrayInputStream;
 import java.util.Date;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 
 import javax.mail.Flags;
 
@@ -83,6 +85,7 @@ public abstract class GetMailboxesMethodTest {
     private AccessToken accessToken;
     private String alice;
     private String bob;
+    private String cedric;
     private GuiceJamesServer jmapServer;
     private MailboxProbe mailboxProbe;
     private ACLProbe aclProbe;
@@ -105,6 +108,7 @@ public abstract class GetMailboxesMethodTest {
         String domain = "domain.tld";
         alice = "alice@" + domain;
         bob = "bob@" + domain;
+        cedric = "cedric@" + domain;
         String password = "password";
         DataProbe dataProbe = jmapServer.getProbe(DataProbeImpl.class);
         dataProbe.addDomain(domain);
@@ -507,9 +511,11 @@ public abstract class GetMailboxesMethodTest {
     public void getMailboxesShouldReturnMailboxesWhenShared() throws Exception 
{
         String mailboxName = "name";
         MailboxId bobMailbox = 
mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, bob, mailboxName);
-        aclProbe.replaceRights(MailboxPath.forUser(bob, mailboxName), alice, 
new Rfc4314Rights(Right.Read));
+        MailboxPath bobMailboxPath = MailboxPath.forUser(bob, mailboxName);
+        aclProbe.replaceRights(bobMailboxPath, alice, new 
Rfc4314Rights(Right.Read));
+        aclProbe.replaceRights(bobMailboxPath, cedric, new 
Rfc4314Rights(Right.Read));
 
-        given()
+        Map<String, String> sharedWith = given()
             .header("Authorization", accessToken.serialize())
             .body("[[\"getMailboxes\", {\"ids\": [\"" + bobMailbox.serialize() 
+ "\"]}, \"#0\"]]")
         .when()
@@ -517,7 +523,12 @@ public abstract class GetMailboxesMethodTest {
         .then()
             .statusCode(200)
             .body(NAME, equalTo("mailboxes"))
-            .body(ARGUMENTS + ".list.name", hasItem(mailboxName));
+            .body(ARGUMENTS + ".list.name", hasSize(1))
+        .extract()
+            .jsonPath()
+            .get(FIRST_MAILBOX + ".sharedWith");
+
+        assertThat(sharedWith).containsOnlyKeys(alice);
     }
 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to