You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by an...@apache.org on 2014/08/15 11:24:52 UTC

svn commit: r1618122 [2/2] - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/ oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/autosave/ oak-core/src/main/java/org/apache/jackrabbit/oak/spi/securit...

Copied: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/SystemUserTest.java (from r1616527, jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/CreateUserTest.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/SystemUserTest.java?p2=jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/SystemUserTest.java&p1=jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/CreateUserTest.java&r1=1616527&r2=1618122&rev=1618122&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/CreateUserTest.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/SystemUserTest.java Fri Aug 15 09:24:51 2014
@@ -16,11 +16,12 @@
  */
 package org.apache.jackrabbit.oak.jcr.security.user;
 
-import java.security.Principal;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Iterator;
+import javax.jcr.LoginException;
+import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
-import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.SimpleCredentials;
+import javax.jcr.UnsupportedRepositoryOperationException;
 
 import org.apache.jackrabbit.api.security.user.Authorizable;
 import org.apache.jackrabbit.api.security.user.AuthorizableExistsException;
@@ -28,139 +29,71 @@ import org.apache.jackrabbit.api.securit
 import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
 import org.apache.jackrabbit.test.NotExecutableException;
 import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Tests for {@code User} creation.
+ * Tests for system {@code User} creation.
+ *
+ * @since Oak 1.1
  */
-public class CreateUserTest extends AbstractUserTest {
+public class SystemUserTest extends AbstractUserTest {
 
-    private static Logger log = LoggerFactory.getLogger(CreateUserTest.class);
+    private static Logger log = LoggerFactory.getLogger(SystemUserTest.class);
 
-    private List<Authorizable> createdUsers = new ArrayList<Authorizable>();
+    private String uid;
+    private User user;
+
+    @Before
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        uid = getTestPrincipal().getName();
+    }
 
     @After
     @Override
     protected void tearDown() throws Exception {
         superuser.refresh(false);
         // remove all created groups again
-        for (Object createdUser : createdUsers) {
-            Authorizable auth = (Authorizable) createdUser;
+        if (user != null) {
             try {
-                auth.remove();
+                user.remove();
                 superuser.save();
             } catch (RepositoryException e) {
-                log.warn("Failed to remove User " + auth.getID() + " during tearDown.");
+                log.warn("Failed to remove User " + user.getID() + " during tearDown.");
             }
         }
         super.tearDown();
     }
 
-    private User createUser(String uid, String pw) throws RepositoryException {
-        User u = userMgr.createUser(uid, pw);
-        superuser.save();
-        return u;
+    private User createUser(String uid) throws RepositoryException {
+        return createUser(uid, null);
     }
 
-    private User createUser(String uid, String pw, Principal p, String iPath) throws RepositoryException {
-        User u = userMgr.createUser(uid, pw, p, iPath);
+    private User createUser(String uid, String intermediatePath) throws RepositoryException {
+        User u = userMgr.createSystemUser(uid, intermediatePath);
         superuser.save();
         return u;
     }
 
     @Test
     public void testCreateUser() throws RepositoryException, NotExecutableException {
-        Principal p = getTestPrincipal();
-        String uid = p.getName();
-        User user = createUser(uid, "pw");
-        createdUsers.add(user);
-
-        assertNotNull(user.getID());
-        assertEquals(p.getName(), user.getPrincipal().getName());
-    }
-
-    /**
-     * @since OAK 1.0 In contrast to Jackrabbit core the intermediate path may
-     * not be an absolute path in OAK.
-     */
-    @Test
-    public void testCreateUserWithAbsolutePath() throws RepositoryException, NotExecutableException {
-        Principal p = getTestPrincipal();
-        String uid = p.getName();
-
-        try {
-            User user = createUser(uid, "pw", p, "/any/path/to/the/new/user");
-            createdUsers.add(user);
-            fail("ConstraintViolationException expected");
-        } catch (ConstraintViolationException e) {
-            // success
-        }
-    }
-
-    @Test
-    public void testCreateGroupWithAbsolutePath2() throws RepositoryException, NotExecutableException {
-        Principal p = getTestPrincipal();
-        String uid = p.getName();
-
-        String userRoot = UserConstants.DEFAULT_USER_PATH;
-        String path = userRoot + "/any/path/to/the/new/user";
-        User user = createUser(uid, "pw", p, path);
-        createdUsers.add(user);
-
-        assertTrue(user.getPath().startsWith(path));
-    }
-
-    @Test
-    public void testCreateUserWithRelativePath() throws RepositoryException, NotExecutableException {
-        Principal p = getTestPrincipal();
-        String uid = p.getName();
-        User user = createUser(uid, "pw", p, "any/path");
-        createdUsers.add(user);
-
-        assertNotNull(user.getID());
-        assertTrue(user.getPath().contains("any/path"));
-    }
-
-    @Test
-    public void testCreateUserWithDifferentPrincipalName() throws RepositoryException, NotExecutableException {
-        Principal p = getTestPrincipal();
-        String uid = getTestPrincipal().getName();
-        User user = createUser(uid, "pw", p, "any/path");
-        createdUsers.add(user);
-
+        user = createUser(uid);
         assertNotNull(user.getID());
-        assertEquals(p.getName(), user.getPrincipal().getName());
-    }
-
-    @Test
-    public void testCreateUserWithNullParamerters() throws RepositoryException {
-        try {
-            User user = createUser(null, null);
-            createdUsers.add(user);
-
-            fail("A User cannot be built from 'null' parameters");
-        } catch (Exception e) {
-            // ok
-        }
 
-        try {
-            User user = createUser(null, null, null, null);
-            createdUsers.add(user);
-
-            fail("A User cannot be built from 'null' parameters");
-        } catch (Exception e) {
-            // ok
-        }
+        assertTrue(user.isSystemUser());
+        assertFalse(user.isAdmin());
+        assertFalse(user.isGroup());
     }
 
     @Test
     public void testCreateUserWithNullUserID() throws RepositoryException {
         try {
-            User user = createUser(null, "anyPW");
-            createdUsers.add(user);
-
+            user = createUser(null);
             fail("A User cannot be built with 'null' userID");
         } catch (Exception e) {
             // ok
@@ -170,110 +103,106 @@ public class CreateUserTest extends Abst
     @Test
     public void testCreateUserWithEmptyUserID() throws RepositoryException {
         try {
-            User user = createUser("", "anyPW");
-            createdUsers.add(user);
-
+            user = createUser("");
             fail("A User cannot be built with \"\" userID");
         } catch (Exception e) {
             // ok
         }
-        try {
-            User user = createUser("", "anyPW", getTestPrincipal(), null);
-            createdUsers.add(user);
+    }
 
-            fail("A User cannot be built with \"\" userID");
-        } catch (Exception e) {
-            // ok
+    @Test
+    public void testCreateTwiceWithSameUserID() throws RepositoryException, NotExecutableException {
+        user = createUser(uid);
+        try {
+            User user2 = createUser(uid);
+            fail("Creating 2 users with the same UserID should throw AuthorizableExistsException.");
+        } catch (AuthorizableExistsException e) {
+            // success.
         }
     }
 
     @Test
-    public void testCreateUserWithEmptyPassword() throws RepositoryException, NotExecutableException {
-        Principal p = getTestPrincipal();
-        User user = createUser(p.getName(), "");
-        createdUsers.add(user);
+    public void testGetUserByID() throws RepositoryException, NotExecutableException {
+        user = createUser(uid);
+
+        Authorizable authorizable = userMgr.getAuthorizable(user.getID());
+        assertNotNull(authorizable);
+        assertFalse(authorizable.isGroup());
+        assertFalse(((User) authorizable).isAdmin());
+        assertTrue(((User) authorizable).isSystemUser());
     }
 
     @Test
-    public void testCreateUserWithNullPrincipal() throws RepositoryException {
-        try {
-            Principal p = getTestPrincipal();
-            String uid = p.getName();
-            User user = createUser(uid, "pw", null, "/a/b/c");
-            createdUsers.add(user);
+    public void testGetUserByPrincipal() throws Exception {
+        user = createUser(uid);
 
-            fail("A User cannot be built with 'null' Principal");
-        } catch (Exception e) {
-            // ok
-        }
+        Authorizable authorizable = userMgr.getAuthorizable(user.getPrincipal());
+        assertNotNull(authorizable);
+        assertFalse(authorizable.isGroup());
+        assertFalse(((User) authorizable).isAdmin());
+        assertTrue(((User) authorizable).isSystemUser());
     }
 
-    public void testCreateUserWithEmptyPrincipal() throws RepositoryException {
-        try {
-            Principal p = getTestPrincipal("");
-            String uid = p.getName();
-            User user = createUser(uid, "pw", p, "/a/b/c");
-            createdUsers.add(user);
+    public void testGetUserByPath() throws Exception {
+        user = createUser(uid);
 
-            fail("A User cannot be built with ''-named Principal");
-        } catch (Exception e) {
-            // ok
-        }
-        try {
-            Principal p = getTestPrincipal(null);
-            String uid = p.getName();
-            User user = createUser(uid, "pw", p, "/a/b/c");
-            createdUsers.add(user);
-
-            fail("A User cannot be built with ''-named Principal");
-        } catch (Exception e) {
-            // ok
-        }
+        Authorizable authorizable = userMgr.getAuthorizableByPath(user.getPath());
+        assertNotNull(authorizable);
+        assertFalse(authorizable.isGroup());
+        assertFalse(((User) authorizable).isAdmin());
+        assertTrue(((User) authorizable).isSystemUser());
+        assertEquals(user.getPath(), authorizable.getPath());
     }
 
-    public void testCreateTwiceWithSameUserID() throws RepositoryException, NotExecutableException {
-        String uid = getTestPrincipal().getName();
-        User user = createUser(uid, "pw");
-        createdUsers.add(user);
+    @Test
+    public void testFindAuthorizable() throws Exception {
+        user = createUser(uid);
 
-        try {
-            User user2 = createUser(uid, "anyPW");
-            createdUsers.add(user2);
+        Iterator<Authorizable> iterator = userMgr.findAuthorizables(UserConstants.REP_PRINCIPAL_NAME, user.getPrincipal().getName());
+        assertTrue(iterator.hasNext());
 
-            fail("Creating 2 users with the same UserID should throw AuthorizableExistsException.");
-        } catch (AuthorizableExistsException e) {
-            // success.
-        }
-    }
+        Authorizable authorizable = iterator.next();
+        assertNotNull(authorizable);
+        assertFalse(authorizable.isGroup());
+        assertTrue(((User) authorizable).isSystemUser());
 
-    /**
-     * @since OAK 1.0 : RepositoryException is thrown instead of AuthorizableExistsException
-     */
-    public void testCreateTwiceWithSamePrincipal() throws RepositoryException, NotExecutableException {
-        Principal p = getTestPrincipal();
-        String uid = p.getName();
-        User user = createUser(uid, "pw", p, "a/b/c");
-        createdUsers.add(user);
+        assertFalse(iterator.hasNext());
+    }
 
+    @Test
+    public void testChangePassword() throws Exception {
+        user = createUser(uid);
         try {
-            uid = getTestPrincipal().getName();
-            User user2 = createUser(uid, "pw", p, null);
-            createdUsers.add(user2);
+            user.changePassword("pw");
+            superuser.save();
+            fail();
+        } catch (UnsupportedRepositoryOperationException e) {
+            // success
+        }
+    }
 
-            fail("Creating 2 users with the same Principal should throw AuthorizableExistsException.");
-        } catch (RepositoryException e) {
-            // success.
+    @Test
+    public void testChangePassword2() throws Exception {
+        user = createUser(uid);
+        try {
+            user.changePassword("old", "pw");
+            superuser.save();
+            fail();
+        } catch (UnsupportedRepositoryOperationException e) {
+            // success
         }
     }
 
-    public void testGetUserAfterCreation() throws RepositoryException, NotExecutableException {
-        Principal p = getTestPrincipal();
-        String uid = p.getName();
+    @Test
+    public void testDisable() throws Exception {
+        user = createUser(uid);
+        user.disable("gone");
+        superuser.save();
 
-        User user = createUser(uid, "pw");
-        createdUsers.add(user);
+        assertTrue(user.isDisabled());
+        assertEquals("gone", user.getDisabledReason());
 
-        assertNotNull(userMgr.getAuthorizable(user.getID()));
-        assertNotNull(userMgr.getAuthorizable(p));
+        user.disable(null);
+        assertFalse(user.isDisabled());
     }
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-parent/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-parent/pom.xml?rev=1618122&r1=1618121&r2=1618122&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-parent/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-parent/pom.xml Fri Aug 15 09:24:51 2014
@@ -42,7 +42,7 @@
     <project.reporting.outputEncoding>
       ${project.build.sourceEncoding}
     </project.reporting.outputEncoding>
-    <jackrabbit.version>2.8.0</jackrabbit.version>
+    <jackrabbit.version>2.9-SNAPSHOT</jackrabbit.version>
     <mongo.host>127.0.0.1</mongo.host>
     <mongo.port>27017</mongo.port>
     <mongo.db>MongoMKDB</mongo.db>