1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // The user should still exist.
93          assertTrue(ums.userExists("mappedroleuser"));
94          // The group should still exist.
95          assertTrue(gms.groupExists("mappedgroup"));
96          // The permission should still exist.
97          assertTrue(pms.permissionExists(new PortletPermission("myportlet", "view")));
98          // The user-role mapping should be gone.
99          assertFalse(rms.isUserInRole("mappedroleuser", "mappedrole"));
100         // The group-role mapping should be gone.
101         assertFalse(rms.isGroupInRole("mappedgroup", "mappedroleuser"));
102         // The permission-role mapping should be gone.
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 }