Author: angela
Date: Wed May 29 15:56:44 2019
New Revision: 1860352
URL: http://svn.apache.org/viewvc?rev=1860352&view=rev
Log:
OAK-8361 : Improve tests for o.a.j.oak.security.user package
Added:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableIteratorTest.java
(with props)
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/principal/AbstractPrincipalProviderTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AbstractGroupPrincipalTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheValidatorProviderTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/GroupImplTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationImplEmptyTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/IntermediatePathTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/MembershipProviderTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandlerTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/SystemUserImplTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImplTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserContextTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserInitializerTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserManagerImplTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserPrincipalProviderTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserPrincipalProviderWithCacheTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserValidatorTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/action/ClearMembershipActionTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/action/GroupActionBestEffortTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/action/GroupActionTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/action/PasswordValidationActionTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/action/UserActionTest.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/whiteboard/WhiteboardUserAuthenticationFactoryTest.java
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/principal/AbstractPrincipalProviderTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/principal/AbstractPrincipalProviderTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/principal/AbstractPrincipalProviderTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/principal/AbstractPrincipalProviderTest.java
Wed May 29 15:56:44 2019
@@ -138,18 +138,7 @@ public abstract class AbstractPrincipalP
@Test
public void testGetPrincipalsForGroup() throws Exception {
- Authorizable group = null;
- try {
- group = getUserManager(root).createGroup("testGroup");
- root.commit();
-
-
assertTrue(principalProvider.getPrincipals(group.getID()).isEmpty());
- } finally {
- if (group != null) {
- group.remove();
- root.commit();
- }
- }
+
assertTrue(principalProvider.getPrincipals(testGroup.getID()).isEmpty());
}
@Test
@@ -181,19 +170,19 @@ public abstract class AbstractPrincipalP
}
@Test
- public void testGetItemBasedPrincipalNonExisting() throws Exception {
+ public void testGetItemBasedPrincipalNonExisting() {
assertNull(principalProvider.getItemBasedPrincipal(UserConstants.DEFAULT_GROUP_PATH));
}
@Test
- public void testUserPrincipal() throws Exception {
+ public void testUserPrincipal() {
Principal principal =
principalProvider.getPrincipal(userPrincipal.getName());
assertNotNull(principal);
}
@Test
- public void testNonExistingPrincipal() throws Exception {
+ public void testNonExistingPrincipal() {
assertNull(principalProvider.getPrincipal(nonExisting.getName()));
}
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AbstractGroupPrincipalTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AbstractGroupPrincipalTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AbstractGroupPrincipalTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AbstractGroupPrincipalTest.java
Wed May 29 15:56:44 2019
@@ -16,12 +16,8 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import java.security.Principal;
-import java.util.Iterator;
-import java.util.List;
-import javax.jcr.RepositoryException;
-
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterators;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;
@@ -30,11 +26,20 @@ import org.apache.jackrabbit.oak.spi.sec
import org.apache.jackrabbit.oak.spi.security.principal.PrincipalImpl;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
-import org.mockito.Mockito;
+import org.mockito.internal.stubbing.answers.ThrowsException;
+
+import javax.jcr.RepositoryException;
+import java.security.Principal;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
public class AbstractGroupPrincipalTest extends AbstractSecurityTest {
@@ -86,7 +91,7 @@ public class AbstractGroupPrincipalTest
}
@Test
- public void testIsMemberMissingAuthorizable() throws RepositoryException {
+ public void testIsMemberMissingAuthorizable() {
List<Principal> principals = ImmutableList.of(
new PrincipalImpl("name"),
new Principal() {
@@ -117,7 +122,7 @@ public class AbstractGroupPrincipalTest
}
@Test
- public void testIsMemberOfEveryoneMissingAuthorizable() throws
RepositoryException {
+ public void testIsMemberOfEveryoneMissingAuthorizable() {
List<Principal> principals = ImmutableList.of(
new PrincipalImpl("name"),
new Principal() {
@@ -140,18 +145,51 @@ public class AbstractGroupPrincipalTest
}
@Test(expected = IllegalStateException.class)
- public void testMembersInternalError() throws Exception {
+ public void testMembersInternalError() {
throwing.members();
}
@Test
- public void testEveryoneIsMemberOfEveryone() throws RepositoryException {
- AbstractGroupPrincipal member =
Mockito.mock(AbstractGroupPrincipal.class);
+ public void testMembersFiltersNull() throws Exception {
+ List l = new ArrayList();
+ l.add(null);
+ AbstractGroupPrincipal agp = mock(AbstractGroupPrincipal.class);
+ when(agp.getMembers()).thenReturn(l.iterator());
+ when(agp.members()).thenCallRealMethod();
+
+ Enumeration<? extends Principal> members = agp.members();
+ assertFalse(members.hasMoreElements());
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testMembersHandlesFailingPrincipalAccess() throws Exception {
+ Authorizable a =
when(mock(Authorizable.class).getPrincipal()).thenThrow(new
RepositoryException()).getMock();
+ AbstractGroupPrincipal agp = mock(AbstractGroupPrincipal.class);
+ when(agp.getMembers()).thenReturn(Iterators.singletonIterator(a));
+ when(agp.members()).thenCallRealMethod();
+
+ Enumeration<? extends Principal> members = agp.members();
+ assertFalse(members.hasMoreElements());
+ }
+
+ @Test
+ public void testEveryoneIsMemberOfEveryone() {
+ AbstractGroupPrincipal member = mock(AbstractGroupPrincipal.class);
when(member.getName()).thenReturn(EveryonePrincipal.NAME);
assertFalse(everyoneAgp.isMember(member));
}
+ @Test(expected = UnsupportedOperationException.class)
+ public void testAddMember() throws Exception {
+ new AGP().addMember(new PrincipalImpl("p"));
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testRemoveMember() throws Exception {
+ new AGP().removeMember(new PrincipalImpl("p"));
+ }
+
private class AGP extends AbstractGroupPrincipal {
private Authorizable member;
@@ -192,9 +230,7 @@ public class AbstractGroupPrincipalTest
@Override
UserManager getUserManager() {
- UserManager userManager = Mockito.mock(UserManager.class);
- Mockito.doThrow(RepositoryException.class);
- return userManager;
+ return mock(UserManager.class, withSettings().defaultAnswer(new
ThrowsException(new RepositoryException())));
}
@Override
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
Wed May 29 15:56:44 2019
@@ -102,11 +102,14 @@ public class AuthorizableImplTest extend
Authorizable user = otherUserManager.getAuthorizable(testUser.getID());
Authorizable group =
otherUserManager.getAuthorizable(testGroup.getID());
+ User differentId = userMgr.createUser(user.getID()+"_", null);
+
Map<Authorizable, Authorizable> notEqual = new HashMap();
notEqual.put(testUser, testGroup);
notEqual.put(user, group);
notEqual.put(testUser, user);
notEqual.put(testGroup, group);
+ notEqual.put(testUser, differentId);
for (Map.Entry entry : notEqual.entrySet()) {
assertFalse(entry.getKey().equals(entry.getValue()));
@@ -150,13 +153,13 @@ public class AuthorizableImplTest extend
}
@Test
- public void testGetTree() throws Exception {
+ public void testGetTree() {
Tree t = root.getTree(authorizable.getPath());
assertEquals(t.getPath(), authorizable.getTree().getPath());
}
@Test(expected = IllegalStateException.class)
- public void testGetTreeNotExisting() throws Exception {
+ public void testGetTreeNotExisting() {
root.getTree(authorizable.getPath()).remove();
// getTree must throw
Added:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableIteratorTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableIteratorTest.java?rev=1860352&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableIteratorTest.java
(added)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableIteratorTest.java
Wed May 29 15:56:44 2019
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.security.user;
+
+import com.google.common.collect.Iterators;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.jackrabbit.commons.iterator.RangeIteratorAdapter;
+import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.spi.security.user.AuthorizableType;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Iterator;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class AuthorizableIteratorTest extends AbstractSecurityTest {
+
+ private Iterator<String> userOakPathIterator;
+
+ @Before
+ @Override
+ public void before() throws Exception {
+ super.before();
+
+ userOakPathIterator =
Iterators.singletonIterator(getNamePathMapper().getOakPath(getTestUser().getPath()));
+ }
+
+ @Test
+ public void testGetSize() {
+ AuthorizableIterator it =
AuthorizableIterator.create(userOakPathIterator, (UserManagerImpl)
getUserManager(root), AuthorizableType.USER);
+ assertEquals(-1, it.getSize());
+ }
+
+ @Test
+ public void testGetSize2() {
+ AuthorizableIterator it =
AuthorizableIterator.create(RangeIteratorAdapter.EMPTY, (UserManagerImpl)
getUserManager(root), AuthorizableType.USER);
+ assertEquals(RangeIteratorAdapter.EMPTY.getSize(), it.getSize());
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testRemove() {
+ AuthorizableIterator it =
AuthorizableIterator.create(userOakPathIterator, (UserManagerImpl)
getUserManager(root), AuthorizableType.USER);
+ it.next();
+ it.remove();
+ }
+
+ @Test
+ public void testTypeMatch() {
+ AuthorizableIterator it =
AuthorizableIterator.create(userOakPathIterator, (UserManagerImpl)
getUserManager(root), AuthorizableType.USER);
+ assertTrue(it.hasNext());
+ assertTrue(it.next() instanceof User);
+ }
+
+ @Test
+ public void testTypeMatch2() {
+ AuthorizableIterator it =
AuthorizableIterator.create(userOakPathIterator, (UserManagerImpl)
getUserManager(root), AuthorizableType.AUTHORIZABLE);
+ assertTrue(it.hasNext());
+ assertTrue(it.next() instanceof User);
+ }
+
+ @Test
+ public void testTypeMismatch() {
+ AuthorizableIterator it =
AuthorizableIterator.create(userOakPathIterator, (UserManagerImpl)
getUserManager(root), AuthorizableType.GROUP);
+ assertFalse(it.hasNext());
+ }
+
+ @Test
+ public void testInvalidPath() {
+ AuthorizableIterator it =
AuthorizableIterator.create(Iterators.singletonIterator(PathUtils.ROOT_PATH),
(UserManagerImpl) getUserManager(root), AuthorizableType.AUTHORIZABLE);
+ assertFalse(it.hasNext());
+ }
+}
\ No newline at end of file
Propchange:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableIteratorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheValidatorProviderTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheValidatorProviderTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheValidatorProviderTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheValidatorProviderTest.java
Wed May 29 15:56:44 2019
@@ -16,16 +16,6 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import java.security.PrivilegedExceptionAction;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
-
-import javax.jcr.NoSuchWorkspaceException;
-import javax.jcr.RepositoryException;
-import javax.security.auth.Subject;
-import javax.security.auth.login.LoginException;
-
import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
@@ -36,14 +26,26 @@ import org.apache.jackrabbit.oak.api.Pro
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
+import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
+import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
+import org.apache.jackrabbit.oak.spi.commit.Validator;
import org.apache.jackrabbit.oak.spi.nodetype.NodeTypeConstants;
import org.apache.jackrabbit.oak.spi.security.authentication.SystemSubject;
import org.apache.jackrabbit.oak.spi.security.principal.EveryonePrincipal;
-import org.apache.jackrabbit.oak.util.NodeUtil;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
+import javax.jcr.RepositoryException;
+import javax.security.auth.Subject;
+import java.security.PrivilegedExceptionAction;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -66,6 +68,7 @@ public class CacheValidatorProviderTest
@Override
public void after() throws Exception {
try {
+ root.refresh();
if (testGroup != null) {
testGroup.remove();
root.commit();
@@ -80,21 +83,12 @@ public class CacheValidatorProviderTest
}
private Tree getCache(@NotNull Authorizable authorizable) throws Exception
{
- ContentSession cs = Subject.doAs(SystemSubject.INSTANCE, new
PrivilegedExceptionAction<ContentSession>() {
- @Override
- public ContentSession run() throws LoginException,
NoSuchWorkspaceException {
- return login(null);
-
- }
- });
- try {
+ try (ContentSession cs = Subject.doAs(SystemSubject.INSTANCE,
(PrivilegedExceptionAction<ContentSession>) () -> login(null))) {
Root r = cs.getLatestRoot();
- NodeUtil n = new NodeUtil(r.getTree(authorizable.getPath()));
- NodeUtil c = n.getOrAddChild(CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE);
- c.setLong(CacheConstants.REP_EXPIRATION, 1);
+ Tree n = r.getTree(authorizable.getPath());
+ Tree c = TreeUtil.getOrAddChild(n, CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE);
+ c.setProperty(CacheConstants.REP_EXPIRATION, 1L, Type.LONG);
r.commit(CacheValidatorProvider.asCommitAttributes());
- } finally {
- cs.close();
}
root.refresh();
@@ -105,15 +99,13 @@ public class CacheValidatorProviderTest
public void testCreateCacheByName() throws RepositoryException {
for (Authorizable a : authorizables) {
try {
- NodeUtil node = new NodeUtil(getAuthorizableTree(a));
- node.addChild(CacheConstants.REP_CACHE,
JcrConstants.NT_UNSTRUCTURED);
+ Tree node = getAuthorizableTree(a);
+ TreeUtil.addChild(node, CacheConstants.REP_CACHE,
JcrConstants.NT_UNSTRUCTURED);
root.commit();
fail("Creating rep:cache node below a user or group must
fail.");
} catch (CommitFailedException e) {
assertTrue(e.isConstraintViolation());
assertEquals(34, e.getCode());
- } finally {
- root.refresh();
}
}
}
@@ -122,16 +114,14 @@ public class CacheValidatorProviderTest
public void testCreateCacheByNodeType() throws RepositoryException {
for (Authorizable a : authorizables) {
try {
- NodeUtil node = new NodeUtil(getAuthorizableTree(a));
- NodeUtil cache = node.addChild("childNode",
CacheConstants.NT_REP_CACHE);
- cache.setLong(CacheConstants.REP_EXPIRATION, 1);
+ Tree node = getAuthorizableTree(a);
+ Tree cache = TreeUtil.addChild(node, "childNode",
CacheConstants.NT_REP_CACHE);
+ cache.setProperty(CacheConstants.REP_EXPIRATION, 1L,
Type.LONG);
root.commit();
fail("Creating node with nt rep:Cache below a user or group
must fail.");
} catch (CommitFailedException e) {
assertTrue(e.isConstraintViolation());
assertEquals(34, e.getCode());
- } finally {
- root.refresh();
}
}
}
@@ -140,19 +130,17 @@ public class CacheValidatorProviderTest
public void testChangePrimaryType() throws RepositoryException {
for (Authorizable a : authorizables) {
try {
- NodeUtil node = new NodeUtil(getAuthorizableTree(a));
- NodeUtil cache = node.addChild("childNode",
JcrConstants.NT_UNSTRUCTURED);
+ Tree node = getAuthorizableTree(a);
+ Tree cache = TreeUtil.addChild(node, "childNode",
JcrConstants.NT_UNSTRUCTURED);
root.commit();
- cache.setName(JcrConstants.JCR_PRIMARYTYPE,
CacheConstants.NT_REP_CACHE);
- cache.setLong(CacheConstants.REP_EXPIRATION, 1);
+ cache.setProperty(JCR_PRIMARYTYPE,
CacheConstants.NT_REP_CACHE, Type.NAME);
+ cache.setProperty(CacheConstants.REP_EXPIRATION, 1L,
Type.LONG);
root.commit();
fail("Changing primary type of residual node below an
user/group to rep:Cache must fail.");
} catch (CommitFailedException e) {
assertTrue(e.isConstraintViolation());
assertEquals(34, e.getCode());
- } finally {
- root.refresh();
}
}
}
@@ -161,16 +149,14 @@ public class CacheValidatorProviderTest
public void testCreateCacheWithCommitInfo() throws RepositoryException {
for (Authorizable a : authorizables) {
try {
- NodeUtil node = new NodeUtil(getAuthorizableTree(a));
- NodeUtil cache = node.addChild(CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE);
- cache.setLong(CacheConstants.REP_EXPIRATION, 1);
+ Tree node = getAuthorizableTree(a);
+ Tree cache = TreeUtil.addChild(node, CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE);
+ cache.setProperty(CacheConstants.REP_EXPIRATION, 1L,
Type.LONG);
root.commit(CacheValidatorProvider.asCommitAttributes());
fail("Creating rep:cache node below a user or group must
fail.");
} catch (CommitFailedException e) {
assertTrue(e.isConstraintViolation());
assertEquals(34, e.getCode());
- } finally {
- root.refresh();
}
}
}
@@ -178,34 +164,30 @@ public class CacheValidatorProviderTest
@Test
public void testCreateCacheBelowProfile() throws Exception {
try {
- NodeUtil node = new NodeUtil(getAuthorizableTree(getTestUser()));
- NodeUtil child = node.addChild("profile",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
- child.addChild(CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE).setLong(CacheConstants.REP_EXPIRATION, 23);
+ Tree node = getAuthorizableTree(getTestUser());
+ Tree child = TreeUtil.addChild(node, "profile",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
+ TreeUtil.addChild(child, CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE).setProperty(CacheConstants.REP_EXPIRATION, 23L,
Type.LONG);
root.commit(CacheValidatorProvider.asCommitAttributes());
fail("Creating rep:cache node below a user or group must fail.");
} catch (CommitFailedException e) {
assertTrue(e.isConstraintViolation());
assertEquals(34, e.getCode());
- } finally {
- root.refresh();
}
}
@Test
public void testCreateCacheBelowPersistedProfile() throws Exception {
try {
- NodeUtil node = new NodeUtil(getAuthorizableTree(getTestUser()));
- NodeUtil child = node.addChild("profile",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
+ Tree node = getAuthorizableTree(getTestUser());
+ Tree child = TreeUtil.addChild(node, "profile",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
root.commit();
- child.addChild(CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE).setLong(CacheConstants.REP_EXPIRATION, 23);
+ TreeUtil.addChild(child, CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE).setProperty(CacheConstants.REP_EXPIRATION, 23L,
Type.LONG);
root.commit(CacheValidatorProvider.asCommitAttributes());
fail("Creating rep:cache node below a user or group must fail.");
} catch (CommitFailedException e) {
assertTrue(e.isConstraintViolation());
assertEquals(34, e.getCode());
- } finally {
- root.refresh();
}
}
@@ -214,7 +196,7 @@ public class CacheValidatorProviderTest
List<PropertyState> props = new ArrayList();
props.add(PropertyStates.createProperty(CacheConstants.REP_EXPIRATION,
25));
props.add(PropertyStates.createProperty(CacheConstants.REP_GROUP_PRINCIPAL_NAMES,
EveryonePrincipal.NAME));
- props.add(PropertyStates.createProperty(JcrConstants.JCR_PRIMARYTYPE,
JcrConstants.NT_UNSTRUCTURED, Type.NAME));
+ props.add(PropertyStates.createProperty(JCR_PRIMARYTYPE,
JcrConstants.NT_UNSTRUCTURED, Type.NAME));
props.add(PropertyStates.createProperty("residualProp", "anyvalue"));
Tree cache = getCache(getTestUser());
@@ -235,18 +217,16 @@ public class CacheValidatorProviderTest
@Test
public void testNestedCache() throws Exception {
- NodeUtil cache = new NodeUtil(getCache(getTestUser()));
+ Tree cache = getCache(getTestUser());
try {
- NodeUtil c = cache.getOrAddChild(CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE);
- c.setLong(CacheConstants.REP_EXPIRATION, 223);
+ Tree c = TreeUtil.getOrAddChild(cache, CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE);
+ c.setProperty(CacheConstants.REP_EXPIRATION, 223L, Type.LONG);
root.commit(CacheValidatorProvider.asCommitAttributes());
fail("Creating nested cache must fail.");
} catch (CommitFailedException e) {
assertTrue(e.isConstraintViolation());
assertEquals(34, e.getCode());
- } finally {
- root.refresh();
}
}
@@ -259,10 +239,10 @@ public class CacheValidatorProviderTest
@Test
public void testCreateCacheOutsideOfAuthorizable() throws Exception {
- NodeUtil n = new NodeUtil(root.getTree("/"));
+ Tree n = root.getTree(PathUtils.ROOT_PATH);
try {
- NodeUtil child = n.addChild(CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE);
- child.setLong(CacheConstants.REP_EXPIRATION, 1);
+ Tree child = TreeUtil.addChild(n, CacheConstants.REP_CACHE,
CacheConstants.NT_REP_CACHE);
+ child.setProperty(CacheConstants.REP_EXPIRATION, 1L, Type.LONG);
root.commit();
fail("Using rep:cache/rep:Cache outside a user or group must
fail.");
} catch (CommitFailedException e) {
@@ -270,12 +250,65 @@ public class CacheValidatorProviderTest
assertEquals(34, e.getCode());
} finally {
root.refresh();
- Tree c = n.getTree().getChild(CacheConstants.REP_CACHE);
+ Tree c = n.getChild(CacheConstants.REP_CACHE);
if (c.exists()) {
c.remove();
root.commit();
}
}
+ }
+
+ @Test(expected = CommitFailedException.class)
+ public void testChangeAuthorizableChildToCache() throws Exception {
+ Tree authorizableTree = getAuthorizableTree(getTestUser());
+ TreeUtil.addChild(authorizableTree, "child",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
+ root.commit();
+
+ try {
+ Tree child = getAuthorizableTree(getTestUser()).getChild("child");
+ child.setProperty(JCR_PRIMARYTYPE, CacheConstants.NT_REP_CACHE,
Type.NAME);
+ root.commit();
+ } catch (CommitFailedException e) {
+ assertTrue(e.isConstraintViolation());
+ assertEquals(34, e.getCode());
+ throw e;
+ }
+ }
+
+ @Test(expected = CommitFailedException.class)
+ public void testChildNodeAddedToCache() throws Exception {
+ try {
+ Tree cache = getCache(getTestUser());
+ Tree child = TreeUtil.addChild(cache, "child",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
+
+ Validator validator = createCacheValidator(cache);
+ validator.childNodeAdded("child",
getTreeProvider().asNodeState(child));
+ } catch (CommitFailedException e) {
+ assertTrue(e.isConstraintViolation());
+ assertEquals(34, e.getCode());
+ throw e;
+ }
+ }
+ @Test(expected = CommitFailedException.class)
+ public void testChildNodeChangedToCache() throws Exception {
+ try {
+ Tree cache = getCache(getTestUser());
+ Tree child = TreeUtil.addChild(cache, "child",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
+
+ Validator validator = createCacheValidator(cache);
+ NodeState ns = getTreeProvider().asNodeState(child);
+ validator.childNodeChanged("child", ns, ns);
+ } catch (CommitFailedException e) {
+ assertTrue(e.isConstraintViolation());
+ assertEquals(34, e.getCode());
+ throw e;
+ }
}
+
+ private Validator createCacheValidator(@NotNull Tree rootTree) {
+ CacheValidatorProvider provider = new
CacheValidatorProvider(root.getContentSession().getAuthInfo().getPrincipals(),
getTreeProvider());
+ NodeState nodeState = getTreeProvider().asNodeState(rootTree);
+ return provider.getRootValidator(nodeState, nodeState, new
CommitInfo("sid", "uid"));
+ };
}
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/GroupImplTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/GroupImplTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/GroupImplTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/GroupImplTest.java
Wed May 29 15:56:44 2019
@@ -34,9 +34,12 @@ import org.apache.jackrabbit.oak.spi.sec
import org.junit.Test;
import org.mockito.Mockito;
+import javax.jcr.nodetype.ConstraintViolationException;
+
import static
org.apache.jackrabbit.oak.spi.security.user.UserConstants.REP_MEMBERS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
@@ -66,6 +69,17 @@ public class GroupImplTest extends Abstr
}
}
+ @Test
+ public void testIsGroup() {
+ assertTrue(group.isGroup());
+ }
+
+ @Test
+ public void testRemove() throws Exception {
+ group.remove();
+ assertNull(uMgr.getAuthorizable(groupId, Group.class));
+ }
+
@Test(expected = IllegalArgumentException.class)
public void testCheckValidTree() throws Exception {
new GroupImpl(getTestUser().getID(),
root.getTree(getTestUser().getPath()), uMgr);
@@ -87,6 +101,13 @@ public class GroupImplTest extends Abstr
assertFalse(group.addMember(group));
}
+ @Test(expected = ConstraintViolationException.class)
+ public void testAddMemberWithCycle() throws Exception {
+ Group g2 = uMgr.createGroup("group2");
+ g2.addMember(group);
+ group.addMember(g2);
+ }
+
@Test
public void testRemoveMemberInvalidAuthorizable() throws Exception {
assertFalse(group.removeMember(Mockito.mock(Authorizable.class)));
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationImplEmptyTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationImplEmptyTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationImplEmptyTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationImplEmptyTest.java
Wed May 29 15:56:44 2019
@@ -16,25 +16,25 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import java.security.Principal;
-import java.util.Enumeration;
-import java.util.UUID;
-
-import javax.security.auth.Subject;
-
import com.google.common.collect.ImmutableSet;
-
import org.apache.jackrabbit.api.security.principal.GroupPrincipal;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.spi.security.principal.AdminPrincipal;
import org.apache.jackrabbit.oak.spi.security.principal.PrincipalImpl;
import org.apache.jackrabbit.oak.spi.security.principal.SystemPrincipal;
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
+import org.jetbrains.annotations.NotNull;
import org.junit.Test;
+import javax.security.auth.Subject;
+import java.security.Principal;
+import java.util.Enumeration;
+import java.util.UUID;
+
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -102,6 +102,18 @@ public class ImpersonationImplEmptyTest
}
@Test
+ public void testGrantNonExistingTreeBasedPrincipal() throws Exception {
+ TreeBasedPrincipal tbPrincipal = new TreeBasedPrincipal("name",
"/nonExisting", getNamePathMapper());
+ assertFalse(impersonation.grantImpersonation(tbPrincipal));
+ }
+
+ @Test
+ public void testGrantNonAuthorizableTreeBasedPrincipal() throws Exception {
+ TreeBasedPrincipal tbPrincipal = new TreeBasedPrincipal("name",
PathUtils.ROOT_PATH, getNamePathMapper());
+ assertFalse(impersonation.grantImpersonation(tbPrincipal));
+ }
+
+ @Test
public void testGrantAdminPrincipal() throws Exception {
assertFalse(impersonation.grantImpersonation(new AdminPrincipal() {
@Override
@@ -153,13 +165,7 @@ public class ImpersonationImplEmptyTest
assertFalse(impersonation.grantImpersonation(principal));
assertFalse(impersonation.grantImpersonation(new
PrincipalImpl(principal.getName())));
- assertFalse(impersonation.grantImpersonation(new Principal() {
-
- @Override
- public String getName() {
- return principal.getName();
- }
- }));
+ assertFalse(impersonation.grantImpersonation(() ->
principal.getName()));
}
@Test
@@ -173,40 +179,42 @@ public class ImpersonationImplEmptyTest
}
@Test
- public void testAllowsNull() throws Exception {
+ public void testAllowsNull() {
assertFalse(impersonation.allows(null));
}
@Test
- public void testAllowsEmpty() throws Exception {
+ public void testAllowsEmpty() {
assertFalse(impersonation.allows(new Subject()));
}
@Test
- public void testAllowsGroup() throws Exception {
- assertFalse(impersonation.allows(new Subject(true,
ImmutableSet.of(groupPrincipal), ImmutableSet.of(), ImmutableSet.of())));
+ public void testAllowsGroup() {
+ assertFalse(impersonation.allows(createSubject(groupPrincipal)));
}
@Test
public void testAllowsAdminPrincipal() throws Exception {
- Subject subject = new Subject(true,
ImmutableSet.of(getAdminPrincipal()), ImmutableSet.of(), ImmutableSet.of());
- assertTrue(impersonation.allows(subject));
+ assertTrue(impersonation.allows(createSubject(getAdminPrincipal())));
}
@Test
- public void testAllowsAdminPrincipal2() throws Exception {
- Subject subject = new Subject(true, ImmutableSet.of(new
AdminPrincipal() {
- @Override
- public String getName() {
- return "principalName";
- }
- }), ImmutableSet.of(), ImmutableSet.of());
- assertTrue(impersonation.allows(subject));
+ public void testAllowsAdminPrincipal2() {
+ assertTrue(impersonation.allows(createSubject((AdminPrincipal) () ->
"principalName")));
}
@Test
- public void testAllowsSystemPrincipal() throws Exception {
- Subject subject = new Subject(true,
ImmutableSet.of(SystemPrincipal.INSTANCE), ImmutableSet.of(),
ImmutableSet.of());
- assertFalse(impersonation.allows(subject));
+ public void testAllowsSystemPrincipal() {
+
assertFalse(impersonation.allows(createSubject(SystemPrincipal.INSTANCE)));
+ }
+
+ @Test
+ public void testAllowsNonExistingPrincipal() {
+ assertFalse(impersonation.allows(createSubject(new
PrincipalImpl("nonExisting"))));
+ }
+
+ @NotNull
+ private static Subject createSubject(@NotNull Principal... principals) {
+ return new Subject(true, ImmutableSet.copyOf(principals),
ImmutableSet.of(), ImmutableSet.of());
}
}
\ No newline at end of file
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/IntermediatePathTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/IntermediatePathTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/IntermediatePathTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/IntermediatePathTest.java
Wed May 29 15:56:44 2019
@@ -27,10 +27,10 @@ import org.apache.jackrabbit.api.securit
import org.apache.jackrabbit.oak.AbstractSecurityTest;
import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
import org.apache.jackrabbit.oak.spi.nodetype.NodeTypeConstants;
import org.apache.jackrabbit.oak.spi.security.principal.PrincipalImpl;
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
-import org.apache.jackrabbit.oak.util.NodeUtil;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
@@ -137,7 +137,7 @@ public class IntermediatePathTest extend
@Test
public void testInvalidAbsolutePaths() throws Exception {
- new NodeUtil(root.getTree("/")).addChild("testNode",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
+ TreeUtil.addChild(root.getTree(PathUtils.ROOT_PATH), "testNode",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
List<String> invalidPaths = ImmutableList.of(
"/",
PathUtils.getAncestorPath(UserConstants.DEFAULT_GROUP_PATH, 1),
@@ -181,8 +181,7 @@ public class IntermediatePathTest extend
@Test
public void testRelativePaths() throws Exception {
- new NodeUtil(root.getTree("/")).addChild("testNode",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
-
+ TreeUtil.addChild(root.getTree(PathUtils.ROOT_PATH), "testNode",
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
List<String> invalidPaths = ImmutableList.of("..", "../..",
"../../..", "../../../testNode","a/b/../../../c");
for (String relPath : invalidPaths) {
try {
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/MembershipProviderTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/MembershipProviderTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/MembershipProviderTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/MembershipProviderTest.java
Wed May 29 15:56:44 2019
@@ -16,23 +16,23 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.oak.api.Tree;
-import org.apache.jackrabbit.oak.util.NodeUtil;
import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
+import org.apache.jackrabbit.oak.util.NodeUtil;
import org.junit.Before;
import org.junit.Test;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandlerTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandlerTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandlerTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandlerTest.java
Wed May 29 15:56:44 2019
@@ -16,23 +16,21 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
+import com.google.common.collect.Lists;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
+import org.apache.jackrabbit.oak.spi.commit.ThreeWayConflictHandler;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.apache.jackrabbit.oak.spi.xml.ImportBehavior;
import org.apache.jackrabbit.oak.spi.xml.ProtectedItemImporter;
import org.jetbrains.annotations.NotNull;
@@ -41,7 +39,18 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
-import com.google.common.collect.Lists;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+import static
org.apache.jackrabbit.oak.spi.security.user.UserConstants.NT_REP_MEMBER_REFERENCES;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
@RunWith(Parameterized.class)
public class RepMembersConflictHandlerTest extends AbstractSecurityTest {
@@ -387,4 +396,91 @@ public class RepMembersConflictHandlerTe
r.getTree(g.getPath()).removeProperty(UserConstants.REP_MEMBERS);
r.commit();
}
+
+ @Test
+ public void testChangeDeletedPropertyRepMembers() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ PropertyState ours =
PropertyStates.createProperty(UserConstants.REP_MEMBERS, "value");
+ assertSame(ThreeWayConflictHandler.Resolution.THEIRS,
handler.changeDeletedProperty(mock(NodeBuilder.class), ours,
mock(PropertyState.class)));
+ }
+
+ @Test
+ public void testChangeDeletedPropertyOther() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ PropertyState ours = PropertyStates.createProperty("any", "value");
+ assertSame(ThreeWayConflictHandler.Resolution.IGNORED,
handler.changeDeletedProperty(mock(NodeBuilder.class), ours,
mock(PropertyState.class)));
+ }
+
+ @Test
+ public void testDeleteDeletedPropertyRepMembers() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ PropertyState base =
PropertyStates.createProperty(UserConstants.REP_MEMBERS, "value");
+ assertSame(ThreeWayConflictHandler.Resolution.MERGED,
handler.deleteDeletedProperty(mock(NodeBuilder.class), base));
+ }
+
+ @Test
+ public void testDeleteDeletedPropertyOther() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ PropertyState base = PropertyStates.createProperty("any", "value");
+ assertSame(ThreeWayConflictHandler.Resolution.IGNORED,
handler.deleteDeletedProperty(mock(NodeBuilder.class), base));
+ }
+
+ @Test
+ public void testDeleteChangedPropertyRepMembers() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ PropertyState theirs =
PropertyStates.createProperty(UserConstants.REP_MEMBERS, "value");
+ assertSame(ThreeWayConflictHandler.Resolution.OURS,
handler.deleteChangedProperty(mock(NodeBuilder.class), theirs,
mock(PropertyState.class)));
+ }
+
+ @Test
+ public void testDeleteChangedPropertyOther() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ PropertyState theirs = PropertyStates.createProperty("any", "value");
+ assertSame(ThreeWayConflictHandler.Resolution.IGNORED,
handler.deleteChangedProperty(mock(NodeBuilder.class), theirs,
mock(PropertyState.class)));
+ }
+
+ @Test
+ public void testAddExistingNode() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ assertSame(ThreeWayConflictHandler.Resolution.IGNORED,
handler.addExistingNode(mock(NodeBuilder.class), "name", mock(NodeState.class),
mock(NodeState.class)));
+ }
+
+ @Test
+ public void testChangeDeletedNodeMemberRef() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ NodeState base =
when(mock(NodeState.class).getName(JCR_PRIMARYTYPE)).thenReturn(NT_REP_MEMBER_REFERENCES).getMock();
+ assertSame(ThreeWayConflictHandler.Resolution.THEIRS,
handler.changeDeletedNode(mock(NodeBuilder.class), "name",
mock(NodeState.class), base));
+ }
+
+ @Test
+ public void testChangeDeletedNodeOther() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ assertSame(ThreeWayConflictHandler.Resolution.IGNORED,
handler.changeDeletedNode(mock(NodeBuilder.class), "name",
mock(NodeState.class), mock(NodeState.class)));
+ }
+
+ @Test
+ public void testDeleteChangedNodeMemberRef() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ NodeState base =
when(mock(NodeState.class).getName(JCR_PRIMARYTYPE)).thenReturn(NT_REP_MEMBER_REFERENCES).getMock();
+ assertSame(ThreeWayConflictHandler.Resolution.OURS,
handler.deleteChangedNode(mock(NodeBuilder.class), "name",
mock(NodeState.class), base));
+ }
+
+ @Test
+ public void testDeleteChangedNodeOther() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ assertSame(ThreeWayConflictHandler.Resolution.IGNORED,
handler.deleteChangedNode(mock(NodeBuilder.class), "name",
mock(NodeState.class), mock(NodeState.class)));
+ }
+
+ @Test
+ public void testDeleteDeletedNodeMemberRef() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ NodeState base =
when(mock(NodeState.class).getName(JCR_PRIMARYTYPE)).thenReturn(NT_REP_MEMBER_REFERENCES).getMock();
+ assertSame(ThreeWayConflictHandler.Resolution.MERGED,
handler.deleteDeletedNode(mock(NodeBuilder.class), "name", base));
+ }
+
+ @Test
+ public void testDeleteDeletedNodeOther() {
+ RepMembersConflictHandler handler = new RepMembersConflictHandler();
+ assertSame(ThreeWayConflictHandler.Resolution.IGNORED,
handler.deleteDeletedNode(mock(NodeBuilder.class), "name",
mock(NodeState.class)));
+ }
}
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/SystemUserImplTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/SystemUserImplTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/SystemUserImplTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/SystemUserImplTest.java
Wed May 29 15:56:44 2019
@@ -16,15 +16,6 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import java.util.Iterator;
-import java.util.List;
-import java.util.UUID;
-import javax.jcr.Credentials;
-import javax.jcr.SimpleCredentials;
-import javax.jcr.UnsupportedRepositoryOperationException;
-import javax.jcr.nodetype.ConstraintViolationException;
-import javax.security.auth.login.LoginException;
-
import com.google.common.collect.ImmutableList;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
@@ -35,10 +26,11 @@ import org.apache.jackrabbit.oak.api.Con
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
+import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
-import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
import
org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials;
import
org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AccessControlConstants;
+import org.apache.jackrabbit.oak.spi.security.principal.AdminPrincipal;
import org.apache.jackrabbit.oak.spi.security.principal.SystemUserPrincipal;
import org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeConstants;
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
@@ -48,12 +40,18 @@ import org.apache.jackrabbit.oak.spi.sec
import org.apache.jackrabbit.oak.spi.security.user.action.AuthorizableAction;
import
org.apache.jackrabbit.oak.spi.security.user.action.AuthorizableActionProvider;
import org.apache.jackrabbit.oak.util.NodeUtil;
-import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
-import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Before;
import org.junit.Test;
+import javax.jcr.Credentials;
+import javax.jcr.SimpleCredentials;
+import javax.jcr.UnsupportedRepositoryOperationException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.security.auth.login.LoginException;
+import java.util.Iterator;
+import java.util.UUID;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -95,14 +93,10 @@ public class SystemUserImplTest extends
protected ConfigurationParameters getSecurityConfigParameters() {
return ConfigurationParameters.of(
UserConfiguration.NAME,
-
ConfigurationParameters.of(UserConstants.PARAM_AUTHORIZABLE_ACTION_PROVIDER,
new AuthorizableActionProvider() {
- @NotNull
- @Override
- public List<? extends AuthorizableAction>
getAuthorizableActions(@NotNull SecurityProvider securityProvider) {
- AuthorizableAction action = new AccessControlAction();
- action.init(securityProvider,
ConfigurationParameters.of(AccessControlAction.USER_PRIVILEGE_NAMES, new
String[]{PrivilegeConstants.JCR_ALL}));
- return ImmutableList.of(action);
- }
+
ConfigurationParameters.of(UserConstants.PARAM_AUTHORIZABLE_ACTION_PROVIDER,
(AuthorizableActionProvider) securityProvider -> {
+ AuthorizableAction action = new AccessControlAction();
+ action.init(securityProvider,
ConfigurationParameters.of(AccessControlAction.USER_PRIVILEGE_NAMES, new
String[]{PrivilegeConstants.JCR_ALL}));
+ return ImmutableList.of(action);
}));
}
@@ -118,11 +112,21 @@ public class SystemUserImplTest extends
}
@Test
+ public void testIsAdmin() throws Exception {
+ assertFalse(createUser(null).isAdmin());
+ }
+
+ @Test
public void testIsSystemUser() throws Exception {
assertTrue(createUser(null).isSystemUser());
}
@Test
+ public void testIsGroup() throws Exception {
+ assertFalse(createUser(null).isGroup());
+ }
+
+ @Test
public void testSystemUserTree() throws Exception {
Tree t = root.getTree(createUser(null).getPath());
assertFalse(t.hasProperty(UserConstants.REP_PASSWORD));
@@ -239,15 +243,10 @@ public class SystemUserImplTest extends
}
}
- @Test
+ @Test(expected = LoginException.class)
public void testLoginAsSystemUser2() throws Exception {
User user = createUser(null);
- try {
- login(user.getCredentials()).close();
- fail();
- } catch (LoginException e) {
- // success
- }
+ login(user.getCredentials()).close();
}
@Test
@@ -257,7 +256,6 @@ public class SystemUserImplTest extends
cs.close();
}
-
@Test
public void testImpersonateDisabledSystemUser() throws Exception {
User user = createUser(null);
@@ -318,4 +316,15 @@ public class SystemUserImplTest extends
Tree t = root.getTree(user.getPath());
assertFalse(t.hasChild(AccessControlConstants.REP_POLICY));
}
+
+ @Test
+ public void testReplacesAdministrator() throws Exception {
+ String adminId =
getUserConfiguration().getParameters().getConfigValue(UserConstants.PARAM_ADMIN_ID,
UserConstants.DEFAULT_ADMIN_ID);
+ User admin = userMgr.getAuthorizable(adminId, User.class);
+ root.getTree(admin.getPath()).remove();
+
+ User adminAsSystemUser = userMgr.createSystemUser(adminId, null);
+ assertTrue(adminAsSystemUser.isAdmin());
+ assertTrue(adminAsSystemUser.getPrincipal() instanceof AdminPrincipal);
+ }
}
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImplTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImplTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImplTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImplTest.java
Wed May 29 15:56:44 2019
@@ -16,25 +16,39 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import java.security.Principal;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
+import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.apache.jackrabbit.oak.plugins.value.jcr.PartialValueFactory;
import org.apache.jackrabbit.oak.spi.commit.MoveTracker;
+import org.apache.jackrabbit.oak.spi.commit.ThreeWayConflictHandler;
import org.apache.jackrabbit.oak.spi.commit.ValidatorProvider;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
+import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
import org.apache.jackrabbit.oak.spi.security.user.util.PasswordUtil;
+import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardAware;
import org.apache.jackrabbit.oak.spi.xml.ImportBehavior;
import org.apache.jackrabbit.oak.spi.xml.ProtectedItemImporter;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Rule;
import org.junit.Test;
+import java.lang.reflect.Field;
+import java.security.Principal;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+
+import static
org.apache.jackrabbit.oak.spi.security.user.UserConstants.PARAM_DEFAULT_DEPTH;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
public class UserConfigurationImplTest extends AbstractSecurityTest {
@@ -51,12 +65,24 @@ public class UserConfigurationImplTest e
private static final Integer PASSWORD_HISTORY_SIZE = 12;
private static final boolean ENABLE_RFC7613_USERCASE_MAPPED_PROFILE = true;
+ @Rule
+ public final OsgiContext context = new OsgiContext();
+
@Override
protected ConfigurationParameters getSecurityConfigParameters() {
return ConfigurationParameters.of(UserConfiguration.NAME, getParams());
}
@Test
+ public void testActivate() {
+ UserConfiguration userConfiguration = new
UserConfigurationImpl(getSecurityProvider());
+ context.registerInjectActivateService(userConfiguration,
ImmutableMap.of(PARAM_DEFAULT_DEPTH, "8"));
+
+ ConfigurationParameters params = userConfiguration.getParameters();
+ assertEquals(8, params.getConfigValue(PARAM_DEFAULT_DEPTH,
UserConstants.DEFAULT_DEPTH).intValue());
+ }
+
+ @Test
public void testValidators() {
UserConfigurationImpl configuration = new
UserConfigurationImpl(getSecurityProvider());
configuration.setRootProvider(getRootProvider());
@@ -77,22 +103,60 @@ public class UserConfigurationImplTest e
}
@Test
- public void testUserConfigurationWithConstructor() throws Exception {
+ public void testGetConflictHandlers() {
+ UserConfigurationImpl configuration = new UserConfigurationImpl();
+
+ List<ThreeWayConflictHandler> conflictHandlers =
configuration.getConflictHandlers();
+ assertEquals(1, conflictHandlers.size());
+ assertTrue(conflictHandlers.get(0) instanceof
RepMembersConflictHandler);
+ }
+
+ @Test
+ public void testGetProtectedItemImporters() {
+ UserConfigurationImpl configuration = new
UserConfigurationImpl(getSecurityProvider());
+
+ List<ProtectedItemImporter> importers =
configuration.getProtectedItemImporters();
+ assertEquals(1, importers.size());
+ assertTrue(importers.get(0) instanceof UserImporter);
+ }
+
+ @Test
+ public void testBlobAccessProviderFromNullWhiteboard() throws Exception {
+ SecurityProvider sp = mock(SecurityProvider.class,
withSettings().extraInterfaces(WhiteboardAware.class));
+
+ UserConfigurationImpl uc = new UserConfigurationImpl(sp);
+ uc.setParameters(ConfigurationParameters.EMPTY);
+ uc.setRootProvider(getRootProvider());
+ uc.setTreeProvider(getTreeProvider());
+
+ when(sp.getConfiguration(UserConfiguration.class)).thenReturn(uc);
+
+ UserManager um = uc.getUserManager(root, getNamePathMapper());
+ assertTrue(um instanceof UserManagerImpl);
+
+ PartialValueFactory vf = ((UserManagerImpl)
um).getPartialValueFactory();
+ Field f =
PartialValueFactory.class.getDeclaredField("blobAccessProvider");
+ f.setAccessible(true);
+ assertSame(PartialValueFactory.DEFAULT_BLOB_ACCESS_PROVIDER,
f.get(vf));
+ }
+
+ @Test
+ public void testUserConfigurationWithConstructor() {
UserConfigurationImpl userConfiguration = new
UserConfigurationImpl(getSecurityProvider());
testConfigurationParameters(userConfiguration.getParameters());
}
@Test
- public void testUserConfigurationWithSetParameters() throws Exception {
+ public void testUserConfigurationWithSetParameters() {
UserConfigurationImpl userConfiguration = new UserConfigurationImpl();
userConfiguration.setParameters(getParams());
testConfigurationParameters(userConfiguration.getParameters());
}
- private void testConfigurationParameters(ConfigurationParameters
parameters) throws Exception {
+ private void testConfigurationParameters(ConfigurationParameters
parameters) {
assertEquals(parameters.getConfigValue(UserConstants.PARAM_USER_PATH,
UserConstants.DEFAULT_USER_PATH), USER_PATH);
assertEquals(parameters.getConfigValue(UserConstants.PARAM_GROUP_PATH,
UserConstants.DEFAULT_GROUP_PATH), GROUP_PATH);
-
assertEquals(parameters.getConfigValue(UserConstants.PARAM_DEFAULT_DEPTH,
UserConstants.DEFAULT_DEPTH), DEFAULT_DEPTH);
+ assertEquals(parameters.getConfigValue(PARAM_DEFAULT_DEPTH,
UserConstants.DEFAULT_DEPTH), DEFAULT_DEPTH);
assertEquals(parameters.getConfigValue(ProtectedItemImporter.PARAM_IMPORT_BEHAVIOR,
ImportBehavior.NAME_IGNORE), IMPORT_BEHAVIOR);
assertEquals(parameters.getConfigValue(UserConstants.PARAM_PASSWORD_HASH_ALGORITHM,
PasswordUtil.DEFAULT_ALGORITHM), HASH_ALGORITHM);
assertEquals(parameters.getConfigValue(UserConstants.PARAM_PASSWORD_HASH_ITERATIONS,
PasswordUtil.DEFAULT_ITERATIONS), HASH_ITERATIONS);
@@ -108,7 +172,7 @@ public class UserConfigurationImplTest e
ConfigurationParameters params = ConfigurationParameters.of(new
HashMap<String, Object>() {{
put(UserConstants.PARAM_USER_PATH, USER_PATH);
put(UserConstants.PARAM_GROUP_PATH, GROUP_PATH);
- put(UserConstants.PARAM_DEFAULT_DEPTH, DEFAULT_DEPTH);
+ put(PARAM_DEFAULT_DEPTH, DEFAULT_DEPTH);
put(ProtectedItemImporter.PARAM_IMPORT_BEHAVIOR, IMPORT_BEHAVIOR);
put(UserConstants.PARAM_PASSWORD_HASH_ALGORITHM, HASH_ALGORITHM);
put(UserConstants.PARAM_PASSWORD_HASH_ITERATIONS, HASH_ITERATIONS);
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserContextTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserContextTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserContextTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserContextTest.java
Wed May 29 15:56:44 2019
@@ -273,12 +273,23 @@ public class UserContextTest implements
}
@Test
- public void testNoTreeDefinesLocation() {
+ public void testNoTreeDefinesLocationReservedEnding() {
for (String name : Iterables.concat(USER_PROPERTY_NAMES,
GROUP_PROPERTY_NAMES)) {
String path = "/some/path/endingWith/reservedName/" + name;
TreeLocation location = Mockito.mock(TreeLocation.class);
when(location.getPath()).thenReturn(path);
+ assertTrue(path, ctx.definesLocation(location));
+ }
+ }
+
+ @Test
+ public void testNoTreeDefinesLocationIntermediate() {
+ for (String name : new String[] {REP_MEMBERS, REP_MEMBERS_LIST,
REP_PWD}) {
+ String path = "/some/path/with/intermediate/" +name+ "/reserved";
+ TreeLocation location = Mockito.mock(TreeLocation.class);
+ when(location.getPath()).thenReturn(path);
+
assertTrue(path, ctx.definesLocation(location));
}
}
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java
Wed May 29 15:56:44 2019
@@ -16,68 +16,176 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import java.util.UUID;
-
import javax.jcr.Credentials;
+import javax.jcr.RepositoryException;
import org.apache.jackrabbit.api.security.user.User;
-import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.UUIDUtils;
+import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
+import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
import org.apache.jackrabbit.oak.spi.security.user.UserIdCredentials;
import org.apache.jackrabbit.oak.spi.security.user.util.PasswordUtil;
+import org.jetbrains.annotations.NotNull;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import static javax.jcr.Property.JCR_PRIMARY_TYPE;
+import static
org.apache.jackrabbit.oak.spi.security.user.UserConstants.NT_REP_GROUP;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class UserImplTest extends AbstractSecurityTest {
- private UserManager userMgr;
- private String uid;
private User user;
@Override
@Before
public void before() throws Exception {
super.before();
-
- userMgr = getUserManager(root);
- uid = "testUser" + UUID.randomUUID();
+ user = getTestUser();
}
@Override
+ @After
public void after() throws Exception {
try {
- if (user != null) {
- user.remove();
- root.commit();
- }
+ root.refresh();
} finally {
super.after();
}
}
+ @NotNull
+ private User getAdminUser() throws Exception {
+ User admin =
getUserManager(root).getAuthorizable(UserConstants.DEFAULT_ADMIN_ID,
User.class);
+ assertNotNull(admin);
+ return admin;
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreateFromInvalidTree() throws Exception {
+ Tree t =
when(mock(Tree.class).getProperty(JCR_PRIMARY_TYPE)).thenReturn(PropertyStates.createProperty(JCR_PRIMARY_TYPE,
NT_REP_GROUP, Type.NAME)).getMock();
+ User u = new UserImpl("uid", t, (UserManagerImpl)
getUserManager(root));
+ }
+
@Test
- public void testGetCredentials() throws Exception {
- user = userMgr.createUser(uid, uid);
- root.commit();
+ public void testIsAdmin() {
+ assertFalse(user.isAdmin());
+ }
+
+ @Test
+ public void testAdministratorIsAdmin() throws Exception {
+ assertTrue(getAdminUser().isAdmin());
+ }
+
+ @Test
+ public void testIsSystemUser() {
+ assertFalse(user.isSystemUser());
+ }
+
+ @Test
+ public void testIsGroup() {
+ assertFalse(user.isGroup());
+ }
+
+ @Test
+ public void testRemove() throws Exception {
+ String id = user.getID();
+ user.remove();
+ assertNull(getUserManager(root).getAuthorizable(id, User.class));
+ }
+
+ @Test(expected = RepositoryException.class)
+ public void testRemoveAdmin() throws Exception {
+ getAdminUser().remove();
+ }
+
+ @Test(expected = RepositoryException.class)
+ public void testChangePasswordToNull() throws Exception {
+ user.changePassword(null);
+ }
+ @Test
+ public void testChangePassword() throws Exception {
+ String pwHash =
root.getTree(user.getPath()).getProperty(UserConstants.REP_PASSWORD).getValue(Type.STRING);
+ assertTrue(PasswordUtil.isSame(pwHash, user.getID()));
+
+ user.changePassword("different");
+ String pwHash2 =
root.getTree(user.getPath()).getProperty(UserConstants.REP_PASSWORD).getValue(Type.STRING);
+ assertTrue(PasswordUtil.isSame(pwHash2, "different"));
+ }
+
+ @Test(expected = RepositoryException.class)
+ public void testChangePasswordWithOldMismatch() throws Exception {
+ user.changePassword("different", "wrongOldPassword");
+ }
+
+ @Test
+ public void testChangePasswordWithOld() throws Exception {
+ String pwHash =
root.getTree(user.getPath()).getProperty(UserConstants.REP_PASSWORD).getValue(Type.STRING);
+ assertTrue(PasswordUtil.isSame(pwHash, user.getID()));
+
+ user.changePassword("different", user.getID());
+
+ String pwHash2 =
root.getTree(user.getPath()).getProperty(UserConstants.REP_PASSWORD).getValue(Type.STRING);
+ assertTrue(PasswordUtil.isSame(pwHash2, "different"));
+ }
+
+ @Test
+ public void testDisable() throws Exception {
+ assertNull(user.getDisabledReason());
+ assertFalse(user.isDisabled());
+
+ user.disable("reason");
+ assertEquals("reason", user.getDisabledReason());
+ assertTrue(user.isDisabled());
+
+ user.disable(null);
+ assertNull(user.getDisabledReason());
+ assertFalse(user.isDisabled());
+ }
+
+ @Test
+ public void testDisableNullReason() throws Exception {
+ assertNull(user.getDisabledReason());
+ assertFalse(user.isDisabled());
+
+ user.disable(null);
+
+ assertNull(user.getDisabledReason());
+ assertFalse(user.isDisabled());
+ }
+
+ @Test(expected = RepositoryException.class)
+ public void testDisableAdministrator() throws Exception {
+ getAdminUser().disable("reason");
+ }
+
+ @Test
+ public void testGetCredentials() throws Exception {
Credentials creds = user.getCredentials();
assertTrue(creds instanceof CredentialsImpl);
CredentialsImpl cImpl = (CredentialsImpl) creds;
- assertEquals(uid, cImpl.getUserId());
- assertTrue(PasswordUtil.isSame(cImpl.getPasswordHash(), uid));
+ assertEquals(user.getID(), cImpl.getUserId());
+ assertTrue(PasswordUtil.isSame(cImpl.getPasswordHash(), user.getID()));
}
@Test
public void testGetCredentialsUserWithoutPassword() throws Exception {
- user = userMgr.createUser(uid, null);
- root.commit();
+ User u = getUserManager(root).createUser("u"+
UUIDUtils.generateUUID(), null);
- Credentials creds = user.getCredentials();
+ Credentials creds = u.getCredentials();
assertTrue(creds instanceof UserIdCredentials);
- assertEquals(uid, ((UserIdCredentials) creds).getUserId());
+ assertEquals(u.getID(), ((UserIdCredentials) creds).getUserId());
}
}
\ No newline at end of file
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserInitializerTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserInitializerTest.java?rev=1860352&r1=1860351&r2=1860352&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserInitializerTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserInitializerTest.java
Wed May 29 15:56:44 2019
@@ -16,30 +16,24 @@
*/
package org.apache.jackrabbit.oak.security.user;
-import java.security.PrivilegedExceptionAction;
-import java.util.HashMap;
-import java.util.Map;
-import javax.jcr.GuestCredentials;
-import javax.jcr.SimpleCredentials;
-import javax.security.auth.Subject;
-import javax.security.auth.login.LoginException;
-
import com.google.common.collect.Iterables;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.apache.jackrabbit.oak.InitialContent;
import org.apache.jackrabbit.oak.Oak;
import org.apache.jackrabbit.oak.api.ContentRepository;
import org.apache.jackrabbit.oak.api.ContentSession;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
import
org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexProvider;
import org.apache.jackrabbit.oak.plugins.nodetype.TypeEditorProvider;
-import org.apache.jackrabbit.oak.InitialContent;
+import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
import org.apache.jackrabbit.oak.security.internal.SecurityProviderBuilder;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
@@ -48,10 +42,18 @@ import org.apache.jackrabbit.oak.spi.sec
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
import org.apache.jackrabbit.oak.spi.security.user.util.UserUtil;
-import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
import org.junit.Before;
import org.junit.Test;
+import javax.jcr.GuestCredentials;
+import javax.jcr.SimpleCredentials;
+import javax.security.auth.Subject;
+import javax.security.auth.login.LoginException;
+import java.security.PrivilegedExceptionAction;
+import java.util.HashMap;
+import java.util.Map;
+
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -59,6 +61,10 @@ import static org.junit.Assert.assertNot
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
/**
* @since OAK 1.0
@@ -117,7 +123,7 @@ public class UserInitializerTest extends
}
@Test
- public void testUserIndexDefinitions() throws Exception {
+ public void testUserIndexDefinitions() {
Tree oakIndex = root.getTree('/' +
IndexConstants.INDEX_DEFINITIONS_NAME);
assertTrue(oakIndex.exists());
@@ -139,6 +145,15 @@ public class UserInitializerTest extends
Iterables.toArray(declaringNtNames, String.class));
}
+ private static void assertIndexDefinition(Tree tree, String propName,
boolean isUnique) {
+ assertTrue(tree.exists());
+
+ assertEquals(isUnique, TreeUtil.getBoolean(tree,
IndexConstants.UNIQUE_PROPERTY_NAME));
+ assertArrayEquals(
+ propName, new String[]{propName},
+ Iterables.toArray(TreeUtil.getStrings(tree,
IndexConstants.PROPERTY_NAMES), String.class));
+ }
+
/**
* @since OAK 1.0 The configuration defines if the password of the
* admin user is being set.
@@ -239,12 +254,13 @@ public class UserInitializerTest extends
}
}
- private static void assertIndexDefinition(Tree tree, String propName,
boolean isUnique) {
- assertTrue(tree.exists());
+ @Test
+ public void testSecondInit() {
+ NodeBuilder builder =
spy(getTreeProvider().asNodeState(root.getTree(PathUtils.ROOT_PATH)).builder());
- assertEquals(isUnique, TreeUtil.getBoolean(tree,
IndexConstants.UNIQUE_PROPERTY_NAME));
- assertArrayEquals(
- propName, new String[]{propName},
- Iterables.toArray(TreeUtil.getStrings(tree,
IndexConstants.PROPERTY_NAMES), String.class));
+ UserInitializer ui = new UserInitializer(getSecurityProvider());
+ ui.initialize(builder, adminSession.getWorkspaceName());
+
+ verify(builder, never()).child(anyString());
}
}