JAMES-2186 add test to StoreRightManager

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

Branch: refs/heads/master
Commit: 7c0099f189c92b8c364eb95f525682d13f99733f
Parents: 571b3b8
Author: Luc DUZAN <[email protected]>
Authored: Tue Oct 17 15:31:50 2017 +0700
Committer: Raphael Ouazana <[email protected]>
Committed: Wed Oct 25 17:40:11 2017 +0200

----------------------------------------------------------------------
 .../org/apache/james/mailbox/RightManager.java  |   3 -
 .../james/mailbox/store/StoreRightManager.java  |   5 +-
 .../mailbox/store/StoreRightManagerTest.java    | 159 +++++++++++++++++++
 3 files changed, 163 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/7c0099f1/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java
----------------------------------------------------------------------
diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java 
b/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java
index cd64cd6..7700a86 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java
@@ -24,9 +24,6 @@ import org.apache.james.mailbox.model.MailboxACL;
 import org.apache.james.mailbox.model.MailboxACL.Right;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
-import org.apache.james.mime4j.dom.address.Mailbox;
-
-import javax.mail.Flags;
 
 public interface RightManager {
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/7c0099f1/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
index ea03bc1..10dabdb 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
@@ -67,7 +67,10 @@ public class StoreRightManager implements RightManager {
     public boolean hasRight(Mailbox mailbox, Right right, MailboxSession 
session) throws MailboxException {
         MailboxSession.User user = session.getUser();
         String userName = user != null ? user.getUserName() : null;
-        return aclResolver.hasRight(userName, groupMembershipResolver, right, 
mailbox.getACL(), mailbox.getUser(), new 
GroupFolderResolver(session).isGroupFolder(mailbox));
+        boolean resourceOwnerIsGroup = new 
GroupFolderResolver(session).isGroupFolder(mailbox);
+
+        return aclResolver.hasRight(userName, groupMembershipResolver, right, 
mailbox.getACL(), mailbox.getUser(),
+                                    resourceOwnerIsGroup);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/7c0099f1/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
index 09f57fc..81493fd 100644
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
@@ -24,17 +24,176 @@ import static 
org.apache.james.mailbox.fixture.MailboxFixture.BOB;
 import static org.apache.james.mailbox.fixture.MailboxFixture.CEDRIC;
 import static org.apache.james.mailbox.fixture.MailboxFixture.INBOX_ALICE;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
+import javax.mail.Flags;
+
+import com.google.common.collect.ImmutableMap;
+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.exception.MailboxNotFoundException;
 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.model.MailboxPath;
+import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 public class StoreRightManagerTest {
 
     public static final long UID_VALIDITY = 3421l;
+    private StoreRightManager storeRightManager;
+    private MailboxACLResolver mailboxAclResolver;
+    private GroupMembershipResolver groupMembershipResolver;
+    private String alice;
+    private MockMailboxSession aliceSession;
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+    private MailboxMapper mockedMailboxMapper;
+
+    @Before
+    public void setup() throws MailboxException {
+        alice = "Alice";
+        aliceSession = new MockMailboxSession(alice);
+        MailboxSessionMapperFactory mockedMapperFactory = 
mock(MailboxSessionMapperFactory.class);
+        mockedMailboxMapper = mock(MailboxMapper.class);
+        mailboxAclResolver = new UnionMailboxACLResolver();
+        groupMembershipResolver = new SimpleGroupMembershipResolver();
+        when(mockedMapperFactory.getMailboxMapper(aliceSession))
+            .thenReturn(mockedMailboxMapper);
+        storeRightManager = new StoreRightManager(mockedMapperFactory,
+                                                  mailboxAclResolver,
+                                                  groupMembershipResolver);
+    }
+
+    @Test
+    public void 
hasRightShouldThrowMailboxNotFoundExceptionWhenMailboxDoesNotExist() throws 
MailboxException {
+        expectedException.expect(MailboxNotFoundException.class);
+
+        MailboxPath mailboxPath = MailboxPath.forUser(alice, "unexisting 
mailbox");
+        
when(mockedMailboxMapper.findMailboxByPath(mailboxPath)).thenThrow(MailboxNotFoundException.class);
+        storeRightManager.hasRight(mailboxPath, Right.Read, aliceSession);
+    }
+
+    @Test
+    public void hasRightShouldReturnTrueWhenTheUserOwnTheMailbox() throws 
MailboxException {
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getUser()).thenReturn(alice);
+
+        assertThat(storeRightManager.hasRight(mailbox, Right.Write, 
aliceSession))
+            .isFalse();
+    }
+
+    @Test
+    public void 
hasRightShouldReturnTrueWhenTheUserDoesnotOwnTheMailboxButHaveTheCorrectRightOnIt()
 throws MailboxException {
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getUser()).thenReturn("bob");
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Write)));
+
+        assertThat(storeRightManager.hasRight(mailbox, Right.Write, 
aliceSession))
+            .isTrue();
+    }
+
+    @Test
+    public void 
hasRightShouldReturnTrueWhenTheUserDoesnotOwnTheMailboxButHasAtLeastTheCorrectRightOnIt()
 throws MailboxException {
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getUser()).thenReturn("bob");
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Write, Right.Lookup)));
+
+        assertThat(storeRightManager.hasRight(mailbox, Right.Write, 
aliceSession))
+            .isTrue();
+    }
+
+    @Test
+    public void 
hasRightShouldReturnFalseWhenTheUserDoesNotOwnTheMailboxAndHasNoRightOnIt() 
throws MailboxException {
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getUser()).thenReturn("bob");
+
+        assertThat(storeRightManager.hasRight(mailbox, Right.Write, 
aliceSession))
+            .isFalse();
+    }
+
+    @Test
+    public void isReadWriteShouldReturnTrueWhenUserHasInsertRightOnMailbox() 
throws Exception {
+        Mailbox mailbox = mock(Mailbox.class);
+        Flags flags = new Flags();
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Insert)));
+        assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
+            .isTrue();
+    }
+
+    @Test
+    public void 
isReadWriteShouldReturnTrueWhenUserHasPerformExpungeRightOnMailbox() throws 
Exception {
+        Mailbox mailbox = mock(Mailbox.class);
+        Flags flags = new Flags();
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.PerformExpunge)));
+        assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
+            .isTrue();
+    }
+
+    @Test
+    public void 
isReadWriteShouldReturnTrueWhenUserHasDeleteMessagesRightOnMailboxAndFlagsContainDeletedFlag()
 throws Exception {
+        Mailbox mailbox = mock(Mailbox.class);
+        Flags flags = new Flags(Flags.Flag.DELETED);
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.DeleteMessages)));
+        assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
+            .isTrue();
+    }
+
+    @Test
+    public void 
isReadWriteShouldReturnFalseWhenUserHasDeleteMessagesRightOnMailboxButFlagsDoesNotContainDeletedFlag()
 throws Exception {
+        Mailbox mailbox = mock(Mailbox.class);
+        Flags flags = new Flags();
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.DeleteMessages)));
+        assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
+            .isFalse();
+    }
+
+    @Test
+    public void 
isReadWriteShouldReturnTrueWhenUserHasWriteSeenFlagRightOnMailboxAndFlagsContainSeenFlag()
 throws Exception {
+        Mailbox mailbox = mock(Mailbox.class);
+        Flags flags = new Flags(Flags.Flag.SEEN);
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.WriteSeenFlag)));
+        assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
+            .isTrue();
+    }
+
+    @Test
+    public void 
isReadWriteShouldReturnFalseWhenUserHasWriteSeenFlagRightOnMailboxAndFlagsDoesNotContainSeenFlag()
 throws Exception {
+        Mailbox mailbox = mock(Mailbox.class);
+        Flags flags = new Flags();
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.WriteSeenFlag)));
+        assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
+            .isFalse();
+    }
+
+    @Test
+    public void 
isReadWriteShouldReturnTrueWhenUserHasWriteRightOnMailboxAndFlagsContainAnsweredFlag()
 throws Exception {
+        Mailbox mailbox = mock(Mailbox.class);
+        Flags flags = new Flags(Flags.Flag.ANSWERED);
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Write)));
+        assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
+            .isTrue();
+    }
+
+    @Test
+    public void 
isReadWriteShouldReturnFalseWhenUserDoesNotHaveInsertOrPerformExpungeRightOnMailboxAndNullFlag()
 throws Exception {
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Administer)));
+        assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, null))
+            .isFalse();
+    }
 
     @Test
     public void filteredForSessionShouldBeIdentityWhenOwner() throws 
UnsupportedRightException {


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

Reply via email to