This is an automated email from the ASF dual-hosted git repository.

enorman pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-jcr-mock.git


The following commit(s) were added to refs/heads/master by this push:
     new 0a6b419  SLING-12992 treat create user/group intermediatePath as 
relative (#40)
0a6b419 is described below

commit 0a6b419999e9345c2c832d66b2461bef188b7111
Author: Eric Norman <[email protected]>
AuthorDate: Thu Nov 6 12:36:56 2025 -0800

    SLING-12992 treat create user/group intermediatePath as relative (#40)
    
    MockUserManager is interpreting the intermediatePath argument incorrectly 
when creating users and groups
---
 .../sling/testing/mock/jcr/MockUserManager.java    | 15 +++++-
 .../testing/mock/jcr/MockUserManagerTest.java      | 56 +++++++++++++++-------
 2 files changed, 52 insertions(+), 19 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/testing/mock/jcr/MockUserManager.java 
b/src/main/java/org/apache/sling/testing/mock/jcr/MockUserManager.java
index 31a44e3..8d7e5e7 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockUserManager.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockUserManager.java
@@ -23,6 +23,7 @@ import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
+import javax.jcr.nodetype.ConstraintViolationException;
 
 import java.security.Principal;
 import java.util.HashMap;
@@ -202,7 +203,9 @@ public class MockUserManager implements UserManager {
             throw new AuthorizableExistsException("Group already exists");
         }
         String principalName = toPrincipalName(groupID, principal);
-        if (intermediatePath == null) {
+        if (intermediatePath != null) {
+            intermediatePath = "/home/groups/" + intermediatePath;
+        } else {
             intermediatePath = "/home/groups"; // NOSONAR
         }
 
@@ -318,8 +321,16 @@ public class MockUserManager implements UserManager {
         }
         String principalName = toPrincipalName(userID, principal);
         boolean isSystemUser = principal instanceof SystemUserPrincipal;
+
         String authorizableNodeType = isSystemUser ? 
UserConstants.NT_REP_SYSTEM_USER : UserConstants.NT_REP_USER;
-        if (intermediatePath == null) {
+        if (intermediatePath != null) {
+            if (isSystemUser && !(intermediatePath.equals("system") || 
intermediatePath.startsWith("system/"))) {
+                throw new ConstraintViolationException(
+                        "System users must be located in the 'system' subtree 
of the user root.");
+            }
+
+            intermediatePath = "/home/users/" + intermediatePath;
+        } else {
             if (isSystemUser) {
                 intermediatePath = "/home/users/system"; // NOSONAR
             } else {
diff --git 
a/src/test/java/org/apache/sling/testing/mock/jcr/MockUserManagerTest.java 
b/src/test/java/org/apache/sling/testing/mock/jcr/MockUserManagerTest.java
index 24290ad..473f217 100644
--- a/src/test/java/org/apache/sling/testing/mock/jcr/MockUserManagerTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/jcr/MockUserManagerTest.java
@@ -24,6 +24,7 @@ import javax.jcr.Session;
 import javax.jcr.SimpleCredentials;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.ValueFactory;
+import javax.jcr.nodetype.ConstraintViolationException;
 
 import java.io.UnsupportedEncodingException;
 import java.security.NoSuchAlgorithmException;
@@ -199,7 +200,7 @@ public class MockUserManagerTest {
      */
     @Test
     public void testCreateGroupPrincipalString() throws 
AuthorizableExistsException, RepositoryException {
-        @NotNull Group group1 = userManager.createGroup(() -> "group1", 
"/home/groups/path1");
+        @NotNull Group group1 = userManager.createGroup(() -> "group1", 
"path1");
         assertNotNull(group1);
         assertEquals("/home/groups/path1/group1", group1.getPath());
     }
@@ -209,26 +210,47 @@ public class MockUserManagerTest {
      */
     @Test
     public void testCreateGroupStringPrincipalString() throws 
AuthorizableExistsException, RepositoryException {
-        @NotNull Group group1 = userManager.createGroup("group1", () -> 
"group1", "/home/groups/path1");
+        @NotNull Group group1 = userManager.createGroup("group1", () -> 
"group1", "path1");
         assertNotNull(group1);
         assertEquals("/home/groups/path1/group1", group1.getPath());
     }
 
+    /**
+     * Parameterized helper to avoid duplication
+     *
+     * @param intermediatePath the intermediate path
+     * @param expectedPath the expected path
+     * @return the user that was created
+     */
+    protected @NotNull User createSystemUser1(@Nullable String 
intermediatePath, @NotNull String expectedPath)
+            throws RepositoryException {
+        @NotNull User user = userManager.createSystemUser("systemuser1", 
intermediatePath);
+        assertTrue(user.isSystemUser());
+        assertEquals(expectedPath, ResourceUtil.getParent(user.getPath()));
+        return user;
+    }
+
     /**
      * Test method for {@link 
org.apache.sling.testing.mock.jcr.MockUserManager#createSystemUser(java.lang.String,
 java.lang.String)}.
      */
     @Test
     public void testCreateSystemUser() throws RepositoryException {
-        @NotNull User user = userManager.createSystemUser("systemuser1", 
"/home/users/system/test");
-        assertTrue(user.isSystemUser());
-        assertEquals("/home/users/system/test", 
ResourceUtil.getParent(user.getPath()));
+        createSystemUser1("system", "/home/users/system");
+    }
+
+    @Test
+    public void testCreateSystemUserWithTwoSegmentIntermediatePath() throws 
RepositoryException {
+        createSystemUser1("system/test", "/home/users/system/test");
     }
 
     @Test
     public void testCreateSystemUserWithNullIntermediatePath() throws 
RepositoryException {
-        @NotNull User user = userManager.createSystemUser("systemuser1", null);
-        assertTrue(user.isSystemUser());
-        assertEquals("/home/users/system", 
ResourceUtil.getParent(user.getPath()));
+        createSystemUser1(null, "/home/users/system");
+    }
+
+    @Test
+    public void testCreateSystemUserWithInvalidIntermediatePath() {
+        assertThrows(ConstraintViolationException.class, () -> 
userManager.createSystemUser("systemuser1", "invalid"));
     }
 
     /**
@@ -247,7 +269,7 @@ public class MockUserManagerTest {
      */
     @Test
     public void testCreateUserStringStringPrincipalString() throws 
AuthorizableExistsException, RepositoryException {
-        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "/home/users/path1");
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "path1");
         assertNotNull(user1);
         assertEquals("/home/users/path1/user1", user1.getPath());
     }
@@ -309,7 +331,7 @@ public class MockUserManagerTest {
      */
     @Test
     public void testFindAuthorizablesStringString() throws RepositoryException 
{
-        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "/home/users/path1");
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "path1");
         user1.setProperty("prop1", vf.createValue("prop1Value"));
         user1.setProperty("sub1/prop2", vf.createValue("prop2Value"));
 
@@ -333,11 +355,11 @@ public class MockUserManagerTest {
      */
     @Test
     public void testFindAuthorizablesStringStringInt() throws 
RepositoryException {
-        @NotNull Group group1 = userManager.createGroup("group1", () -> 
"group1", "/home/groups/path1");
+        @NotNull Group group1 = userManager.createGroup("group1", () -> 
"group1", "path1");
         group1.setProperty("prop1", vf.createValue("prop1Value"));
         group1.setProperty("sub1/prop2", vf.createValue("prop2Value"));
 
-        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "/home/users/path1");
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "path1");
         user1.setProperty("prop1", vf.createValue("prop1Value"));
         user1.setProperty("sub1/prop2", vf.createValue("prop2Value"));
 
@@ -386,7 +408,7 @@ public class MockUserManagerTest {
      */
     @Test
     public void testGetAuthorizableString() throws 
AuthorizableExistsException, RepositoryException {
-        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "/home/users/path1");
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "path1");
         @Nullable Authorizable authorizable = 
userManager.getAuthorizable(user1.getID());
         assertEquals(user1, authorizable);
     }
@@ -396,7 +418,7 @@ public class MockUserManagerTest {
      */
     @Test
     public void testGetAuthorizablePrincipal() throws 
AuthorizableExistsException, RepositoryException {
-        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "/home/users/path1");
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "path1");
         @Nullable Authorizable authorizable = userManager.getAuthorizable(() 
-> "user1");
         assertEquals(user1, authorizable);
     }
@@ -406,7 +428,7 @@ public class MockUserManagerTest {
      */
     @Test
     public void testGetAuthorizableStringClassOfT() throws 
AuthorizableExistsException, RepositoryException {
-        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "/home/users/path1");
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "path1");
         @Nullable Authorizable authorizable = 
userManager.getAuthorizable("user1", User.class);
         assertEquals(user1, authorizable);
 
@@ -427,7 +449,7 @@ public class MockUserManagerTest {
      */
     @Test
     public void testGetAuthorizableByPath() throws 
AuthorizableExistsException, RepositoryException {
-        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "/home/users/path1");
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "path1");
         @Nullable Authorizable authorizable = 
userManager.getAuthorizableByPath(user1.getPath());
         assertEquals(user1, authorizable);
 
@@ -436,7 +458,7 @@ public class MockUserManagerTest {
 
     @Test
     public void testGetAuthorizableByPathCatchRepositoryException() throws 
Exception {
-        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "/home/users/path1");
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", "path1");
         User mockAuthorizable = Mockito.spy(user1);
         // replace the original with our mocked copy
         userManager.authorizables.put(mockAuthorizable.getID(), 
mockAuthorizable);

Reply via email to