1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.security.spi;
18
19 import java.security.Permission;
20 import java.security.Permissions;
21 import java.security.Principal;
22
23 import org.apache.jetspeed.security.PortletPermission;
24 import org.apache.jetspeed.security.impl.RolePrincipalImpl;
25 import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 /***
31 * <p>
32 * Unit testing for {@link RoleSecurityHandler}.
33 * </p>
34 *
35 * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
36 */
37 public class TestRoleSecurityHandler extends AbstractSecurityTestcase
38 {
39
40
41 /***
42 * @see junit.framework.TestCase#setUp()
43 */
44 protected void setUp() throws Exception
45 {
46 super.setUp();
47 }
48
49 /***
50 * @see junit.framework.TestCase#tearDown()
51 */
52 public void tearDown() throws Exception
53 {
54 super.tearDown();
55 }
56
57 /***
58 * <p>
59 * Constructs the suite.
60 * </p>
61 *
62 * @return The {@Test}.
63 */
64 public static Test suite()
65 {
66 return new TestSuite(TestRoleSecurityHandler.class);
67 }
68
69 /***
70 * <p>
71 * Test <code>getRolePrincipal</code>.
72 * </p>
73 */
74 public void testGetRolePrincipal() throws Exception
75 {
76 initRole();
77 Principal principal = rsh.getRolePrincipal("testusertorole1");
78 assertNotNull(principal);
79 assertEquals("testusertorole1", principal.getName());
80 destroyRole();
81 }
82
83 /***
84 * <p>
85 * Test <code>removeRolePrincipal</code>.
86 * </p>
87 */
88 public void testRemoveRolePrincipal() throws Exception
89 {
90 initMappedRole();
91 rsh.removeRolePrincipal(new RolePrincipalImpl("mappedrole"));
92
93 assertTrue(ums.userExists("mappedroleuser"));
94
95 assertTrue(gms.groupExists("mappedgroup"));
96
97 assertTrue(pms.permissionExists(new PortletPermission("myportlet", "view")));
98
99 assertFalse(rms.isUserInRole("mappedroleuser", "mappedrole"));
100
101 assertFalse(rms.isGroupInRole("mappedgroup", "mappedroleuser"));
102
103 Permissions perms = pms.getPermissions(new RolePrincipalImpl("mappedrole"));
104 assertFalse(perms.implies(new PortletPermission("myportlet", "view")));
105
106 destroyMappedRole();
107 }
108
109 /***
110 * <p>
111 * Initialize role test object.
112 * </p>
113 */
114 protected void initRole() throws Exception
115 {
116 rms.addRole("testusertorole1");
117 }
118
119 /***
120 * <p>
121 * Destroy role test object.
122 * </p>
123 */
124 protected void destroyRole() throws Exception
125 {
126 rms.removeRole("testusertorole1");
127 }
128
129 protected void initMappedRole() throws Exception
130 {
131 destroyMappedRole();
132 ums.addUser("mappedroleuser", "password");
133 rms.addRole("mappedrole");
134 rms.addRole("mappedrole.role1");
135 gms.addGroup("mappedgroup");
136
137 Permission perm = new PortletPermission("myportlet", "view");
138 pms.addPermission(perm);
139 pms.grantPermission(new RolePrincipalImpl("mappedrole"), perm);
140
141 rms.addRoleToUser("mappedroleuser", "mappedrole");
142 rms.addRoleToGroup("mappedrole", "mappedgroup");
143 }
144
145 protected void destroyMappedRole() throws Exception
146 {
147 if (ums.userExists("mappedroleuser"))
148 ums.removeUser("mappedroleuser");
149 if (rms.roleExists("mappedrole"))
150 rms.removeRole("mappedrole.role1");
151 if (rms.roleExists("mappedrole.role1"))
152 rms.removeRole("mappedrole");
153 if (gms.groupExists("mappedgroup"))
154 gms.removeGroup("mappedgroup");
155 PortletPermission pp = new PortletPermission("myportlet", "view");
156 if (pms.permissionExists(pp))
157 pms.removePermission(pp);
158 }
159 }