UserProvider extends AuthorizableBaseProvider { @NotNull Tree createUser(@NotNull String userID, @Nullable String intermediateJcrPath) throws RepositoryException { return createAuthorizableNode(userID, NT_REP_USER, intermediateJcrPath); } UserProvider(@NotNull Root root, @NotNull ConfigurationParameters config);   }
@Test public void testCreateUser() throws Exception { UserProvider up = createUserProvider(); Tree userTree = up.createUser("user1", null); assertNotNull(userTree); assertTrue(Text.isDescendant(defaultUserPath, userTree.getPath())); int level = defaultConfig.getConfigValue(UserConstants.PARAM_DEFAULT_DEPTH, UserConstants.DEFAULT_DEPTH) + 1; assertEquals(defaultUserPath, Text.getRelativeParent(userTree.getPath(), level)); userTree = up.createUser("b", null); assertEquals(defaultUserPath + "/b/bb/b", userTree.getPath()); Map<String, String> m = new HashMap<>(); m.put("bb", "/b/bb/bb"); m.put("bbb", "/b/bb/bbb"); m.put("bbbb", "/b/bb/bbbb"); m.put("bh", "/b/bh/bh"); m.put("bHbh", "/b/bH/bHbh"); m.put("b_Hb", "/b/b_/b_Hb"); m.put("basim", "/b/ba/basim"); for (String uid : m.keySet()) { userTree = up.createUser(uid, null); assertEquals(defaultUserPath + m.get(uid), userTree.getPath()); } }
@Test public void testCreateUserWithPath() throws Exception { UserProvider up = createUserProvider(1); Tree userTree = up.createUser("nadine", "a/b/c"); assertNotNull(userTree); assertTrue(Text.isDescendant(customUserPath, userTree.getPath())); String userPath = customUserPath + "/a/b/c/nadine"; assertEquals(userPath, userTree.getPath()); }
@Test(expected = AccessDeniedException.class) public void testCreateUserMissingAccessOnFolders() throws RepositoryException { Tree t = mock(Tree.class); when(t.getParent()).thenReturn(t); when(t.exists()).thenReturn(false); when(t.isRoot()).thenReturn(false, false, true); Root r = when(mock(Root.class).getTree(anyString())).thenReturn(t).getMock(); UserProvider up = new UserProvider(r, ConfigurationParameters.EMPTY); up.createUser("uid", null); }
@Test public void testCreateWithCustomDepth() throws Exception { UserProvider userProvider = createUserProvider(3); Tree userTree = userProvider.createUser("b", null); assertEquals(customUserPath + "/b/bb/bbb/b", userTree.getPath()); Map<String, String> m = new HashMap<>(); m.put("bb", "/b/bb/bbb/bb"); m.put("bbb", "/b/bb/bbb/bbb"); m.put("bbbb", "/b/bb/bbb/bbbb"); m.put("bL", "/b/bL/bLL/bL"); m.put("bLbh", "/b/bL/bLb/bLbh"); m.put("b_Lb", "/b/b_/b_L/b_Lb"); m.put("basiL", "/b/ba/bas/basiL"); for (String uid : m.keySet()) { userTree = userProvider.createUser(uid, null); assertEquals(customUserPath + m.get(uid), userTree.getPath()); } }
@Test public void testCreateWithCollidingFolder() throws Exception { Tree ut = mock(Tree.class); Tree t = mock(Tree.class); when(t.getParent()).thenReturn(t); when(t.exists()).thenReturn(true); when(t.isRoot()).thenReturn(true); when(t.hasChild("uid")).thenReturn(true, false); when(t.addChild("uid")).thenReturn(ut); when(t.getChild(anyString())).thenReturn(t); when(t.getPath()).thenReturn(UserConstants.DEFAULT_USER_PATH); when(t.getProperty(JCR_PRIMARYTYPE)).thenReturn(PropertyStates.createProperty(JCR_PRIMARYTYPE, NT_REP_AUTHORIZABLE_FOLDER, Type.NAME)); Root r = when(mock(Root.class).getTree(anyString())).thenReturn(t).getMock(); when(r.getContentSession()).thenReturn(root.getContentSession()); UserProvider up = new UserProvider(r, ConfigurationParameters.EMPTY); Tree tree = up.createUser("uid", null); assertSame(ut, tree); }
@Test public void testCreateUserRFC7613Disabled() throws Exception { String userHalfWidth = "Amalia"; String userFullWidth = "\uff21\uff4d\uff41\uff4c\uff49\uff41"; UserProvider userProvider = createUserProvider(); Tree userTreeHalfWidth = userProvider.createUser(userHalfWidth, null); Tree userTreeFullWidth = userProvider.createUser(userFullWidth, null); root.commit(); assertEquals(userHalfWidth, UserUtil.getAuthorizableId(userTreeHalfWidth)); assertEquals(userFullWidth, UserUtil.getAuthorizableId(userTreeFullWidth)); }
@Test public void testCreateUserRFC7613Enabled() throws Exception { String userHalfWidth = "Amalia"; String userFullWidth = "\uff21\uff4d\uff41\uff4c\uff49\uff41"; UserProvider userProvider = createUserProviderRFC7612(); userProvider.createUser(userHalfWidth, null); try { userProvider.createUser(userFullWidth, null); root.commit(); fail("userID collision must be detected"); } catch (CommitFailedException e) { } }
@Test public void testCollisions() throws Exception { ConfigurationParameters config = ConfigurationParameters.of(UserConstants.PARAM_AUTHORIZABLE_NODE_NAME, (AuthorizableNodeName) authorizableId -> "aaa"); UserProvider up = new UserProvider(root, config); try { Tree u1 = up.createUser("a", null); assertEquals("aaa", u1.getName()); Tree u2 = up.createUser("b", null); assertEquals("aaa1", u2.getName()); Tree u3 = up.createUser("c", null); assertEquals("aaa2", u3.getName()); Tree u4 = up.createUser("d", null); assertEquals("aaa3", u4.getName()); } finally { root.refresh(); } }
@Test public void testAutoCreatedItemsUponUserCreation() throws Exception { UserProvider up = createUserProvider(); assertAutoCreatedItems(up.createUser("c", null), UserConstants.NT_REP_USER, root); }