diff --git a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/UserImportTest.java b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/UserImportTest.java index 790f0fa..be3b421 100755 --- a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/UserImportTest.java +++ b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/UserImportTest.java @@ -19,6 +19,7 @@ package org.apache.jackrabbit.oak.jcr.security.user; import java.security.Principal; import java.util.ArrayList; import java.util.List; + import javax.jcr.ImportUUIDBehavior; import javax.jcr.ItemExistsException; import javax.jcr.Node; @@ -33,8 +34,10 @@ import org.apache.jackrabbit.api.security.user.Authorizable; import org.apache.jackrabbit.api.security.user.AuthorizableExistsException; import org.apache.jackrabbit.api.security.user.Impersonation; import org.apache.jackrabbit.api.security.user.User; +import org.apache.jackrabbit.core.security.principal.PrincipalImpl; import org.apache.jackrabbit.oak.spi.security.user.UserConstants; import org.apache.jackrabbit.test.NotExecutableException; +import org.apache.jackrabbit.test.api.util.Text; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -99,6 +102,117 @@ public class UserImportTest extends AbstractImportTest { } /** + * @since Oak 1.2 + */ + @Test + public void testImportUserFromJr2Replace() throws Exception { + // create authorizable + User u = userMgr.createUser("t", "n/a", new PrincipalImpl("t"), getTargetPath() + "/foo/bar/test"); + String initialPath = u.getPath(); + adminSession.save(); + + String xml = "\n" + + "" + + " rep:User" + + " e358efa4-89f5-3062-b10d-d7316b65649e" + + " {sha1}8efd86fb78a56a5145ed7739dcb00c78581c5375" + + " t" + + " disabledUser" + + ""; + + doImport(getTargetPath(), xml, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING); + + Authorizable newUser = userMgr.getAuthorizable("t"); + + // replace should retain path + assertEquals(initialPath, newUser.getPath()); + + Node n = adminSession.getNode(newUser.getPath()); + assertTrue(n.hasProperty(UserConstants.REP_AUTHORIZABLE_ID)); + assertEquals("t", n.getProperty(UserConstants.REP_AUTHORIZABLE_ID).getString()); + + // saving changes of the import -> must succeed + adminSession.save(); + } + + /** + * @since Oak 1.2 + */ + @Test + public void testImportUserFromJr2Remove() throws Exception { + // create authorizable + User u = userMgr.createUser("t", "n/a", new PrincipalImpl("t"), getTargetPath() + "/foo/bar/test"); + String initialPath = u.getPath(); + adminSession.save(); + + String xml = "\n" + + "" + + " rep:User" + + " e358efa4-89f5-3062-b10d-d7316b65649e" + + " {sha1}8efd86fb78a56a5145ed7739dcb00c78581c5375" + + " t" + + " disabledUser" + + ""; + + doImport(getTargetPath(), xml, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING); + + Authorizable newUser = userMgr.getAuthorizable("t"); + + // replace should be on new path + assertEquals(getTargetPath(), Text.getRelativeParent(newUser.getPath(), 1)); + + Node n = adminSession.getNode(newUser.getPath()); + assertTrue(n.hasProperty(UserConstants.REP_AUTHORIZABLE_ID)); + assertEquals("t", n.getProperty(UserConstants.REP_AUTHORIZABLE_ID).getString()); + + // saving changes of the import -> must succeed + adminSession.save(); + } + + /** + * @since Oak 1.2 + */ + @Test + public void testImportUserFromJr2ReplaceDifferentName() throws Exception { + // create authorizable + User u = userMgr.createUser("t", "n/a", new PrincipalImpl("t"), getTargetPath() + "/foo/bar/test"); + String randomNodeName = "f5aj6fp7q9834jof"; + String initialPath = u.getPath(); + String movedPath = Text.getRelativeParent(initialPath, 1) + "/" + randomNodeName; + adminSession.move(initialPath, movedPath); + adminSession.save(); + + // we need to include the new node name in the sysview import, so that the importer uses the correct name. + String xml = "\n" + + "" + + " rep:User" + + " e358efa4-89f5-3062-b10d-d7316b65649e" + + " {sha1}8efd86fb78a56a5145ed7739dcb00c78581c5375" + + " t" + + " disabledUser" + + ""; + + doImport(getTargetPath(), xml, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING); + + Authorizable newUser = userMgr.getAuthorizable("t"); + + // replace should retain path + assertEquals("user path", movedPath, newUser.getPath()); + + // saving changes of the import -> must succeed + // save here produces: + // javax.jcr.nodetype.ConstraintViolationException: OakConstraint0022: Authorizable property rep:authorizableId may not be altered after user/group creation. + //adminSession.save(); + + Node n = adminSession.getNode(newUser.getPath()); + assertTrue(n.hasProperty(UserConstants.REP_AUTHORIZABLE_ID)); + assertEquals(UserConstants.REP_AUTHORIZABLE_ID, "t", n.getProperty(UserConstants.REP_AUTHORIZABLE_ID).getString()); + + // saving changes of the import -> must succeed + adminSession.save(); + } + + /** * @since OAK 1.0 : constraintviolation is no longer detected during import * but only upon save. */