|
| 1 | +package org.sakaiproject.kernel.personal; |
| 2 | + |
| 3 | +import static org.easymock.EasyMock.createMock; |
| 4 | +import static org.easymock.EasyMock.expect; |
| 5 | +import static org.easymock.EasyMock.isA; |
| 6 | +import static org.easymock.EasyMock.replay; |
| 7 | +import static org.sakaiproject.kernel.api.user.UserConstants.SYSTEM_USER_MANAGER_GROUP_PATH; |
| 8 | +import static org.sakaiproject.kernel.api.user.UserConstants.SYSTEM_USER_MANAGER_GROUP_PREFIX; |
| 9 | +import static org.sakaiproject.kernel.api.user.UserConstants.SYSTEM_USER_MANAGER_USER_PATH; |
| 10 | +import static org.sakaiproject.kernel.api.user.UserConstants.SYSTEM_USER_MANAGER_USER_PREFIX; |
| 11 | + |
| 12 | +import org.apache.jackrabbit.api.JackrabbitSession; |
| 13 | +import org.apache.jackrabbit.api.security.user.Authorizable; |
| 14 | +import org.apache.jackrabbit.api.security.user.UserManager; |
| 15 | +import org.apache.sling.api.SlingHttpServletRequest; |
| 16 | +import org.apache.sling.api.request.RequestParameter; |
| 17 | +import org.apache.sling.api.request.RequestPathInfo; |
| 18 | +import org.apache.sling.api.resource.ResourceResolver; |
| 19 | +import org.apache.sling.servlets.post.Modification; |
| 20 | +import org.apache.sling.servlets.post.SlingPostConstants; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import java.security.Principal; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import javax.jcr.Node; |
| 28 | +import javax.jcr.Property; |
| 29 | +import javax.jcr.Session; |
| 30 | +import javax.jcr.Value; |
| 31 | +import javax.jcr.nodetype.PropertyDefinition; |
| 32 | + |
| 33 | +public class UserPostProcessorTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testProcessEmptyPath() throws Exception { |
| 37 | + testProcess(""); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testProcessManagerUser() throws Exception { |
| 42 | + testProcess(SYSTEM_USER_MANAGER_USER_PATH); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testProcessManagerGroup() throws Exception { |
| 47 | + testProcess(SYSTEM_USER_MANAGER_GROUP_PATH); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testProcessManagerUserPrefix() throws Exception { |
| 52 | + testProcess(SYSTEM_USER_MANAGER_USER_PREFIX); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testProcessManagerGroupPrefix() throws Exception { |
| 57 | + testProcess(SYSTEM_USER_MANAGER_GROUP_PREFIX); |
| 58 | + } |
| 59 | + |
| 60 | + private void testProcess(String path) throws Exception { |
| 61 | + UserPostProcessorImpl uppi = new UserPostProcessorImpl(); |
| 62 | + |
| 63 | + Principal principal = createMock(Principal.class); |
| 64 | + expect(principal.getName()).andReturn("foo"); |
| 65 | + |
| 66 | + ArrayList<String> propNames = new ArrayList<String>(); |
| 67 | + propNames.add("rep:userId"); |
| 68 | + |
| 69 | + Authorizable authorizable = createMock(Authorizable.class); |
| 70 | + expect(authorizable.getID()).andReturn("bar"); |
| 71 | + expect(authorizable.isGroup()).andReturn(false); |
| 72 | + expect(authorizable.getPrincipal()).andReturn(principal); |
| 73 | + expect(authorizable.getPropertyNames()).andReturn(propNames.iterator()); |
| 74 | + expect(authorizable.getProperty("rep:userId")).andReturn(new Value[] {}); |
| 75 | + |
| 76 | + UserManager userManager = createMock(UserManager.class); |
| 77 | + expect(userManager.getAuthorizable(isA(String.class))).andReturn(authorizable); |
| 78 | + |
| 79 | + PropertyDefinition uidDef = createMock(PropertyDefinition.class); |
| 80 | + expect(uidDef.isProtected()).andReturn(true); |
| 81 | + |
| 82 | + Property userId = createMock(Property.class); |
| 83 | + expect(userId.getDefinition()).andReturn(uidDef); |
| 84 | + |
| 85 | + Node profileNode = createMock(Node.class); |
| 86 | + expect(profileNode.hasProperty("sakai:privateproperties")).andReturn(false); |
| 87 | + expect(profileNode.hasProperty("rep:userId")).andReturn(true); |
| 88 | + expect(profileNode.getProperty("rep:userId")).andReturn(userId); |
| 89 | + |
| 90 | + Node node = createMock(Node.class); |
| 91 | + expect(node.hasNode("authprofile")).andReturn(true); |
| 92 | + expect(node.getNode("authprofile")).andReturn(profileNode); |
| 93 | + expect(node.getParent()).andReturn(null); |
| 94 | + expect(node.getPath()).andReturn("/"); |
| 95 | + |
| 96 | + expect(profileNode.getParent()).andReturn(node); |
| 97 | + |
| 98 | + JackrabbitSession session = createMock(JackrabbitSession.class); |
| 99 | + expect(session.getUserManager()).andReturn(userManager); |
| 100 | + expect(session.itemExists("/_user/public/62/cd/b7/02/bar/authprofile")).andReturn( |
| 101 | + true); |
| 102 | + expect(session.getItem("/_user/public/62/cd/b7/02/bar/authprofile")).andReturn( |
| 103 | + profileNode); |
| 104 | + |
| 105 | + ResourceResolver rr = createMock(ResourceResolver.class); |
| 106 | + expect(rr.adaptTo(Session.class)).andReturn(session); |
| 107 | + |
| 108 | + RequestPathInfo requestPathInfo = createMock(RequestPathInfo.class); |
| 109 | + expect(requestPathInfo.getResourcePath()).andReturn(path); |
| 110 | + |
| 111 | + RequestParameter requestParameter = createMock(RequestParameter.class); |
| 112 | + expect(requestParameter.getString()).andReturn("foo"); |
| 113 | + |
| 114 | + SlingHttpServletRequest request = createMock(SlingHttpServletRequest.class); |
| 115 | + expect(request.getResourceResolver()).andReturn(rr); |
| 116 | + expect(request.getRequestPathInfo()).andReturn(requestPathInfo); |
| 117 | + expect(request.getRequestParameter(SlingPostConstants.RP_NODE_NAME)).andReturn( |
| 118 | + requestParameter); |
| 119 | + |
| 120 | + List<Modification> changes = new ArrayList<Modification>(); |
| 121 | + |
| 122 | + replay(principal, authorizable, userManager, uidDef, userId, profileNode, node, |
| 123 | + session, rr, requestPathInfo, requestParameter, request); |
| 124 | + uppi.process(session, request, changes); |
| 125 | + } |
| 126 | +} |
0 commit comments