View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.test.service;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.kuali.rice.kim.bo.impl.RoleImpl;
26  import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
27  import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo;
28  import org.kuali.rice.kim.bo.role.impl.KimPermissionImpl;
29  import org.kuali.rice.kim.bo.role.impl.KimPermissionTemplateImpl;
30  import org.kuali.rice.kim.bo.role.impl.RoleMemberImpl;
31  import org.kuali.rice.kim.bo.role.impl.RolePermissionImpl;
32  import org.kuali.rice.kim.service.KIMServiceLocator;
33  import org.kuali.rice.kim.service.PermissionService;
34  import org.kuali.rice.kim.service.RoleService;
35  import org.kuali.rice.kim.test.KIMTestCase;
36  import org.kuali.rice.kns.service.KNSServiceLocator;
37  
38  /**
39   * This is a description of what this class does - kellerj don't forget to fill this in. 
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   *
43   */
44  public class AuthorizationServiceImplTest extends KIMTestCase {
45  
46  	private PermissionService permissionService;
47  	private RoleService roleService;
48  
49  	private String principal1Id = "p1";
50  	private String principal2Id = "p2";
51  	private String principal3Id = "p3";
52  	
53  	private String group1Id = "g1";
54  	
55  	private String role1Id = "r1";
56  	private String role1NamespaceCode = "AUTH_SVC_TEST1";
57  	private String role1Description = "Role 1 Description";
58  	private String role1Name = "RoleOne";
59  	
60  	private String role2Id = "r2";
61  	private String role2NamespaceCode = "AUTH_SVC_TEST2";
62  	private String role2Description = "Role 2 Description";
63  	private String role2Name = "RoleTwo";
64  	
65  	private String permission1Name = "perm1";
66  	private String permission1NamespaceCode = "KR-NS";
67  	private String permission1Id = "p1";
68  	
69  	private String permission2Name = "perm2";
70  	private String permission2NamespaceCode = "KR-NS";
71  	private String permission2Id = "p2";
72  
73  	private String permission3Name = "perm3";
74  	private String permission3NamespaceCode = "KR-NS";
75  	private String permission3Id = "p3";
76  
77  	
78  	@Before
79  	public void setUp() throws Exception {
80  		super.setUp();
81  		
82  		permissionService = KIMServiceLocator.getPermissionService();
83  		roleService = KIMServiceLocator.getRoleService();
84  		
85  		if (true) return;
86  		
87  		// set up Role "r1" with principal p1
88  		RoleImpl role1 = new RoleImpl();
89  		role1.setRoleId(role1Id);
90  		role1.setActive(true);
91  		role1.setKimTypeId(getDefaultKimType().getKimTypeId());
92  		role1.setNamespaceCode(role1NamespaceCode);
93  		role1.setRoleDescription(role1Description);
94  		role1.setRoleName(role1Name);
95  		List<RoleMemberImpl> members1 = new ArrayList<RoleMemberImpl>();
96  		role1.setMembers(members1);
97  		RoleMemberImpl p1Member = new RoleMemberImpl();
98  		p1Member.setMemberId(principal1Id);
99  		p1Member.setMemberTypeCode("P");
100 		p1Member.setRoleId(role1Id);
101 		p1Member.setRoleMemberId(getNewRoleMemberId());
102 		members1.add(p1Member);
103 		KNSServiceLocator.getBusinessObjectService().save(role1);
104 		
105 		// set up Role "r2" with principal p3, group g1 and role r1
106 		RoleImpl role2 = new RoleImpl();
107 		role2.setRoleId(role2Id);
108 		role2.setActive(true);
109 		role2.setKimTypeId(getDefaultKimType().getKimTypeId());
110 		role2.setNamespaceCode(role2NamespaceCode);
111 		role2.setRoleDescription(role2Description);
112 		role2.setRoleName(role2Name);
113 		List<RoleMemberImpl> members2 = new ArrayList<RoleMemberImpl>();
114 		role2.setMembers(members2);
115 		RoleMemberImpl p3Member = new RoleMemberImpl();
116 		p3Member.setMemberId(principal3Id);
117 		p3Member.setMemberTypeCode("P");
118 		p3Member.setRoleId(role2Id);
119 		p3Member.setRoleMemberId(getNewRoleMemberId());
120 		members2.add(p3Member);
121 		RoleMemberImpl g1Member = new RoleMemberImpl();
122 		g1Member.setMemberId(group1Id);
123 		g1Member.setMemberTypeCode("G");
124 		g1Member.setRoleId(role2Id);
125 		g1Member.setRoleMemberId(getNewRoleMemberId());
126 		members2.add(g1Member);
127 		RoleMemberImpl r1Member = new RoleMemberImpl();
128 		r1Member.setMemberId(role1Id);
129 		r1Member.setMemberTypeCode("R");
130 		r1Member.setRoleId(role2Id);
131 		r1Member.setRoleMemberId(getNewRoleMemberId());
132 		members2.add(r1Member);
133 		KNSServiceLocator.getBusinessObjectService().save(role2);
134 		
135 		// setup permissions
136 		
137 		KimPermissionTemplateImpl defaultTemplate = getDefaultPermissionTemplate();
138 		
139 		KimPermissionImpl permission1 = new KimPermissionImpl();
140 		permission1.setActive(true);
141 		permission1.setDescription("permission1");
142 		permission1.setName(permission1Name);
143 		permission1.setNamespaceCode(permission1NamespaceCode);
144 		permission1.setPermissionId(permission1Id);
145 		permission1.setTemplateId(defaultTemplate.getPermissionTemplateId());
146 		permission1.setTemplate(defaultTemplate);
147 		KNSServiceLocator.getBusinessObjectService().save(permission1);
148 		
149 		KimPermissionImpl permission2 = new KimPermissionImpl();
150 		permission2.setActive(true);
151 		permission2.setDescription("permission2");
152 		permission2.setName(permission2Name);
153 		permission2.setNamespaceCode(permission2NamespaceCode);
154 		permission2.setPermissionId(permission2Id);
155 		permission2.setTemplateId(defaultTemplate.getPermissionTemplateId());
156 		permission2.setTemplate(defaultTemplate);
157 		KNSServiceLocator.getBusinessObjectService().save(permission2);
158 		
159 		KimPermissionImpl permission3 = new KimPermissionImpl();
160 		permission3.setActive(true);
161 		permission3.setDescription("permission3");
162 		permission3.setName(permission3Name);
163 		permission3.setNamespaceCode(permission3NamespaceCode);
164 		permission3.setPermissionId(permission3Id);
165 		permission3.setTemplateId(defaultTemplate.getPermissionTemplateId());
166 		permission3.setTemplate(defaultTemplate);
167 		KNSServiceLocator.getBusinessObjectService().save(permission3);
168 
169 		// assign permissions to roles
170 		// p1 -> r1
171 		// p2 -> r1
172 		// p3 -> r2
173 		
174 		RolePermissionImpl role1Perm1 = new RolePermissionImpl();
175 		role1Perm1.setActive(true);
176 		role1Perm1.setRoleId(role1Id);
177 		role1Perm1.setPermissionId(permission1Id);
178 		role1Perm1.setRolePermissionId(getNewRolePermissionId());
179 		KNSServiceLocator.getBusinessObjectService().save(role1Perm1);
180 		
181 		RolePermissionImpl role1Perm2 = new RolePermissionImpl();
182 		role1Perm2.setActive(true);
183 		role1Perm2.setRoleId(role1Id);
184 		role1Perm2.setPermissionId(permission2Id);
185 		role1Perm2.setRolePermissionId(getNewRolePermissionId());
186 		KNSServiceLocator.getBusinessObjectService().save(role1Perm2);
187 		
188 		RolePermissionImpl role2Perm3 = new RolePermissionImpl();
189 		role2Perm3.setActive(true);
190 		role2Perm3.setRoleId(role2Id);
191 		role2Perm3.setPermissionId(permission3Id);
192 		role2Perm3.setRolePermissionId(getNewRolePermissionId());
193 		KNSServiceLocator.getBusinessObjectService().save(role2Perm3);
194 	}
195 
196 	@Test
197 	public void testRoleMembership() {
198 		KimRoleInfo role = roleService.getRole( role2Id );
199 		assertNotNull( "r2 must exist", role );
200 		ArrayList<String> roleList = new ArrayList<String>( 1 );
201 		roleList.add( role2Id );
202 		
203 		Collection<String> memberPrincipalIds = roleService.getRoleMemberPrincipalIds(role2NamespaceCode, role2Name, null);
204 		assertNotNull(memberPrincipalIds);
205 		assertEquals("RoleTwo should have 6 principal ids", 6, memberPrincipalIds.size());
206 		assertTrue( "p3 must belong to role", memberPrincipalIds.contains(principal3Id) );
207 		assertTrue( "p2 must belong to role (assigned via group)", memberPrincipalIds.contains(principal2Id) );
208 		assertTrue( "p1 must belong to r2 (via r1)", memberPrincipalIds.contains(principal1Id) );
209 		
210 		Collection<RoleMembershipInfo> members = roleService.getRoleMembers( roleList, null );
211 		assertNotNull( "returned list may not be null", members );
212 		assertFalse( "list must not be empty", members.isEmpty() );
213 		assertEquals("Returned list must have 4 members.", 4, members.size());
214 		boolean foundP3 = false;
215 		boolean foundG1 = false;
216 		boolean foundR1 = false;
217 		for (RoleMembershipInfo member : members) {
218 			if (member.getMemberId().equals(principal3Id) && member.getMemberTypeCode().equals("P")) {
219 				foundP3 = true;
220 			} else if (member.getMemberId().equals(group1Id) && member.getMemberTypeCode().equals("G")) {
221 				foundG1 = true;
222 			} else if (member.getMemberId().equals(principal1Id) && member.getMemberTypeCode().equals("P")) {
223 				foundR1 = true;
224 				assertEquals("Should have r1 embedded role id.", role1Id, member.getEmbeddedRoleId());
225 			}
226 		}
227 		assertTrue("Failed to find p3 principal member", foundP3);
228 		assertTrue("Failed to find g1 group member", foundG1);
229 		assertTrue("Failed to find r1 role member", foundR1);
230 		
231 		role = roleService.getRole( role1Id );
232 		assertNotNull( "r1 must exist", role );
233 		roleList.clear();
234 		roleList.add( role1Id );
235 		members = roleService.getRoleMembers( roleList, null );
236 		assertNotNull( "returned list may not be null", members );
237 		assertEquals("Should have 2 members", 2, members.size());
238 		Iterator<RoleMembershipInfo> iter = members.iterator();
239 		assertTrue("One of those members should be p1.", principal1Id.equals(iter.next().getMemberId()) || principal1Id.equals(iter.next().getMemberId()));
240 	}
241 	
242 //	@Test
243 //	public void testGetPermissionsForRole() {
244 //		List<PermissionDetailInfo> perms = authorizationService.getPermissionsForRole( "r1" );
245 //		System.out.println( "r1: " + perms );
246 //		assertTrue( "r1 must have perm1 (direct)", hasPermission( perms, "perm1" ) );
247 //		assertTrue( "r1 must have perm2 (direct)", hasPermission( perms, "perm2" ) );
248 //		assertTrue( "r1 must have perm3 (via r2)", hasPermission( perms, "perm3" ) );
249 //		perms = authorizationService.getPermissionsForRole( "r2" );
250 //		System.out.println( "r2: " + perms );
251 //		assertTrue( "r2 must have perm3 (direct)", hasPermission( perms, "perm3" ) );
252 //		assertFalse( "r2 must not have perm1", hasPermission( perms, "perm1" ) );
253 //		assertFalse( "r2 must not have perm2", hasPermission( perms, "perm2" ) );
254 //	}
255 	
256 	@Test
257 	public void testHasPermission() {
258 		
259 		assertTrue( "p1 must have perm1 (via r1)", permissionService.hasPermission( "p1", "KR-NS", "perm1", null ) );		
260 		assertTrue( "p1 must have perm2 (via r1)", permissionService.hasPermission( "p1", "KR-NS", "perm2", null ) );
261 		assertTrue( "p1 must have perm3 (via r2)", permissionService.hasPermission( "p1", "KR-NS", "perm3", null ) );
262 		assertTrue( "p3 must have perm3 (via r2)", permissionService.hasPermission( "p3", "KR-NS", "perm3", null ) );
263 		assertFalse( "p3 must not have perm1", permissionService.hasPermission( "p3", "KR-NS", "perm1", null ) );
264 		assertFalse( "p3 must not have perm2", permissionService.hasPermission( "p3", "KR-NS", "perm2", null ) );
265 	}
266 	
267 //	protected boolean hasPermission( List<PermissionDetailsInfo> perms, String permissionId ) {
268 //		for ( PermissionDetailsInfo perm : perms ) {
269 //			if ( perm.getPermissionId().equals( permissionId ) ) {
270 //				return true;
271 //			}
272 //		}
273 //		return false;
274 //	}
275 	// test that only active roles/permissions are used
276 	// test that only roles attached to active groups are returned
277 	// check that implied/implying lists are correct
278 	// check qualification matching
279 	// need hierarchical test for qualification matching
280 	// check namespace filters
281 	
282 	// non-qualified role/permission checks
283 	// qualified role/permission checks
284 	// add type services in test spring startup? - how in rice?
285 	
286 }