1 package org.apache.archiva.redback.tests;
4 * Copyright 2001-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import junit.framework.TestCase;
20 import org.apache.archiva.redback.rbac.Operation;
21 import org.apache.archiva.redback.rbac.RBACManager;
22 import org.apache.archiva.redback.rbac.RbacManagerException;
23 import org.apache.archiva.redback.rbac.Resource;
24 import org.apache.archiva.redback.rbac.Role;
25 import org.apache.archiva.redback.rbac.UserAssignment;
26 import org.apache.archiva.redback.rbac.Permission;
27 import org.apache.archiva.redback.rbac.RbacPermanentException;
28 import org.apache.archiva.redback.tests.utils.RBACDefaults;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.springframework.test.context.ContextConfiguration;
32 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.List;
40 * AbstractRbacManagerTestCase
42 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
45 @RunWith( SpringJUnit4ClassRunner.class )
46 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
47 public abstract class AbstractRbacManagerTestCase
50 private RBACManager rbacManager;
52 protected RbacManagerEventTracker eventTracker;
54 private RBACDefaults rbacDefaults;
56 public void setRbacManager( RBACManager store )
58 this.rbacManager = store;
59 if ( this.rbacManager != null )
61 this.eventTracker = new RbacManagerEventTracker();
62 this.rbacManager.addListener( eventTracker );
64 rbacDefaults = new RBACDefaults( rbacManager );
67 public RBACManager getRbacManager()
69 return this.rbacManager;
78 public void tearDown()
84 private Role getAdminRole()
85 throws RbacManagerException
87 Role role = rbacManager.createRole( "ADMIN" );
88 role.setAssignable( false );
90 Permission perm = rbacManager.createPermission( "EDIT_ANY_USER", "EDIT", "User:*" );
92 role.addPermission( perm );
97 private Role getDeveloperRole()
98 throws RbacManagerException
100 Role role = rbacManager.createRole( "DEVELOPER" );
101 role.setAssignable( true );
103 Permission perm = rbacManager.createPermission( "EDIT_MY_USER", "EDIT", "User:Self" );
105 role.addPermission( perm );
110 private Role getProjectAdminRole()
111 throws RbacManagerException
113 Role role = rbacManager.createRole( "PROJECT_ADMIN" );
114 role.setAssignable( true );
116 Permission perm = rbacManager.createPermission( "EDIT_PROJECT", "EDIT", "Project:Foo" );
118 role.addPermission( perm );
123 private Role getSuperDeveloperRole()
125 Role role = rbacManager.createRole( "SUPER_DEVELOPER" );
126 role.setAssignable( true );
132 public void testStoreInitialization()
135 assertNotNull( rbacManager );
137 Role role = getAdminRole();
139 assertNotNull( role );
141 Role added = rbacManager.saveRole( role );
143 assertEquals( 1, rbacManager.getAllRoles().size() );
145 assertNotNull( added );
147 rbacManager.removeRole( added );
149 assertEquals( 0, rbacManager.getAllRoles().size() );
151 /* Assert some event tracker stuff */
152 assertNotNull( eventTracker );
153 //assertEquals( 1, eventTracker.initCount );
154 //assertTrue( eventTracker.lastDbFreshness.booleanValue() );
156 assertEquals( 1, eventTracker.addedRoleNames.size() );
157 assertEquals( 1, eventTracker.removedRoleNames.size() );
158 assertEquals( 1, eventTracker.addedPermissionNames.size() );
159 assertEquals( 0, eventTracker.removedPermissionNames.size() );
163 public void testResources()
166 assertNotNull( rbacManager );
168 rbacManager.eraseDatabase();
169 eventTracker.rbacInit( true );
171 Resource resource = rbacManager.createResource( "foo" );
172 Resource resource2 = rbacManager.createResource( "bar" );
174 assertNotNull( resource );
176 Resource added = rbacManager.saveResource( resource );
177 assertNotNull( added );
178 Resource added2 = rbacManager.saveResource( resource2 );
179 assertNotNull( added2 );
181 assertEquals( 2, rbacManager.getAllResources().size() );
183 rbacManager.removeResource( added );
185 assertEquals( 1, rbacManager.getAllResources().size() );
187 /* Assert some event tracker stuff */
188 assertNotNull( eventTracker );
189 assertEquals( 1, eventTracker.initCount );
190 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
192 assertEquals( 0, eventTracker.addedRoleNames.size() );
193 assertEquals( 0, eventTracker.removedRoleNames.size() );
194 assertEquals( 0, eventTracker.addedPermissionNames.size() );
195 assertEquals( 0, eventTracker.removedPermissionNames.size() );
199 public void testAddGetPermission()
200 throws RbacManagerException
202 assertNotNull( rbacManager );
204 rbacManager.eraseDatabase();
205 eventTracker.rbacInit( true );
207 Role adminRole = rbacManager.saveRole( getAdminRole() );
208 rbacManager.saveRole( getDeveloperRole() );
210 assertEquals( 2, rbacManager.getAllRoles().size() );
211 assertEquals( 2, rbacManager.getAllPermissions().size() );
213 Permission createUserPerm = rbacManager.createPermission( "CREATE_USER", "CREATE", "User" );
215 // perm shouldn't exist in manager (yet)
216 assertEquals( 2, rbacManager.getAllPermissions().size() );
218 adminRole.addPermission( createUserPerm );
219 rbacManager.saveRole( adminRole );
221 // perm should exist in manager now.
222 assertEquals( 3, rbacManager.getAllPermissions().size() );
223 Permission fetched = rbacManager.getPermission( "CREATE_USER" );
224 assertNotNull( fetched );
226 /* Assert some event tracker stuff */
227 assertNotNull( eventTracker );
228 assertEquals( 1, eventTracker.initCount );
229 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
231 assertEquals( 2, eventTracker.addedRoleNames.size() );
232 assertEquals( 0, eventTracker.removedRoleNames.size() );
233 assertEquals( 3, eventTracker.addedPermissionNames.size() );
234 assertEquals( 0, eventTracker.removedPermissionNames.size() );
238 public void testAddGetRole()
239 throws RbacManagerException
241 assertNotNull( rbacManager );
243 rbacManager.eraseDatabase();
244 eventTracker.rbacInit( true );
246 Role adminRole = rbacManager.saveRole( getAdminRole() );
247 Role develRole = rbacManager.saveRole( getDeveloperRole() );
249 assertEquals( 2, rbacManager.getAllRoles().size() );
251 Role actualAdmin = rbacManager.getRole( adminRole.getName() );
252 Role actualDevel = rbacManager.getRole( develRole.getName() );
254 assertEquals( adminRole, actualAdmin );
255 assertEquals( develRole, actualDevel );
257 /* Assert some event tracker stuff */
258 assertNotNull( eventTracker );
259 assertEquals( 1, eventTracker.initCount );
260 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
262 assertEquals( 2, eventTracker.addedRoleNames.size() );
263 assertEquals( 0, eventTracker.removedRoleNames.size() );
264 assertEquals( 2, eventTracker.addedPermissionNames.size() );
265 assertEquals( 0, eventTracker.removedPermissionNames.size() );
269 public void testAllowRoleWithoutPermissions()
270 throws RbacManagerException
272 assertNotNull( rbacManager );
274 rbacManager.eraseDatabase();
275 eventTracker.rbacInit( true );
277 String rolename = "Test Role";
279 Role testRole = rbacManager.createRole( rolename );
280 testRole = rbacManager.saveRole( testRole );
282 assertNotNull( testRole );
283 assertEquals( 1, rbacManager.getAllRoles().size() );
284 assertEquals( 0, rbacManager.getAllPermissions().size() );
286 Role actualRole = rbacManager.getRole( rolename );
288 assertEquals( testRole, actualRole );
289 assertEquals( 1, rbacManager.getAllRoles().size() );
290 assertEquals( 0, rbacManager.getAllPermissions().size() );
292 /* Assert some event tracker stuff */
293 assertNotNull( eventTracker );
294 assertEquals( 1, eventTracker.initCount );
295 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
297 assertEquals( 1, eventTracker.addedRoleNames.size() );
298 assertEquals( 0, eventTracker.removedRoleNames.size() );
299 assertEquals( 0, eventTracker.addedPermissionNames.size() );
300 assertEquals( 0, eventTracker.removedPermissionNames.size() );
304 public void testAddGetChildRole()
305 throws RbacManagerException
307 RBACManager manager = rbacManager;
308 assertNotNull( manager );
310 rbacManager.eraseDatabase();
311 eventTracker.rbacInit( true );
313 Role adminRole = manager.saveRole( getAdminRole() );
314 Role develRole = manager.saveRole( getDeveloperRole() );
316 assertEquals( 2, manager.getAllRoles().size() );
318 Role actualAdmin = manager.getRole( adminRole.getName() );
319 Role actualDevel = manager.getRole( develRole.getName() );
321 assertEquals( adminRole, actualAdmin );
322 assertEquals( develRole, actualDevel );
324 // Now add a child role.
325 manager.addChildRole( develRole, getProjectAdminRole() );
327 manager.saveRole( develRole );
329 assertEquals( 3, manager.getAllRoles().size() );
331 /* Assert some event tracker stuff */
332 assertNotNull( eventTracker );
333 assertEquals( 1, eventTracker.initCount );
334 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
336 assertEquals( 3, eventTracker.addedRoleNames.size() );
337 assertEquals( 0, eventTracker.removedRoleNames.size() );
338 assertEquals( 3, eventTracker.addedPermissionNames.size() );
339 assertEquals( 0, eventTracker.removedPermissionNames.size() );
343 public void testAddGetChildRoleViaName()
344 throws RbacManagerException
346 RBACManager manager = rbacManager;
347 rbacManager.eraseDatabase();
348 eventTracker.rbacInit( true );
349 assertNotNull( manager );
351 Role adminRole = manager.saveRole( getAdminRole() );
352 Role develRole = manager.saveRole( getDeveloperRole() );
354 assertEquals( 2, manager.getAllRoles().size() );
356 Role actualAdmin = manager.getRole( adminRole.getName() );
357 Role actualDevel = manager.getRole( develRole.getName() );
359 assertEquals( adminRole, actualAdmin );
360 assertEquals( develRole, actualDevel );
362 // Now do a child role.
363 Role projectRole = getProjectAdminRole();
364 String projectRoleName = projectRole.getName();
365 manager.saveRole( projectRole );
367 develRole.addChildRoleName( projectRoleName );
369 manager.saveRole( develRole );
371 assertEquals( 3, manager.getAllRoles().size() );
373 /* Assert some event tracker stuff */
374 assertNotNull( eventTracker );
375 assertEquals( 1, eventTracker.initCount );
376 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
378 assertEquals( 3, eventTracker.addedRoleNames.size() );
379 assertEquals( 0, eventTracker.removedRoleNames.size() );
380 assertEquals( 3, eventTracker.addedPermissionNames.size() );
381 assertEquals( 0, eventTracker.removedPermissionNames.size() );
385 public void testUserAssignmentAddRole()
386 throws RbacManagerException
388 RBACManager manager = rbacManager;
390 rbacManager.eraseDatabase();
391 eventTracker.rbacInit( true );
393 Role adminRole = manager.saveRole( getAdminRole() );
395 assertEquals( 1, manager.getAllRoles().size() );
397 String adminPrincipal = "admin";
399 UserAssignment assignment = manager.createUserAssignment( adminPrincipal );
401 assignment.addRoleName( adminRole );
403 manager.saveUserAssignment( assignment );
405 assertEquals( 1, manager.getAllUserAssignments().size() );
406 assertEquals( 1, manager.getAllRoles().size() );
408 UserAssignment ua = manager.getUserAssignment( adminPrincipal );
411 Role fetched = manager.getRole( "ADMIN" );
412 assertNotNull( fetched );
414 /* Assert some event tracker stuff */
415 assertNotNull( eventTracker );
416 assertEquals( 1, eventTracker.initCount );
417 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
419 assertEquals( 1, eventTracker.addedRoleNames.size() );
420 assertEquals( 0, eventTracker.removedRoleNames.size() );
421 assertEquals( 1, eventTracker.addedPermissionNames.size() );
422 assertEquals( 0, eventTracker.removedPermissionNames.size() );
426 public void testUserAssignmentWithChildRoles()
427 throws RbacManagerException
429 RBACManager manager = rbacManager;
430 rbacManager.eraseDatabase();
431 Role developerRole = manager.saveRole( getDeveloperRole() );
433 Role adminRole = getAdminRole();
435 adminRole.addChildRoleName( developerRole.getName() );
437 adminRole = manager.saveRole( adminRole );
439 String adminPrincipal = "admin";
440 UserAssignment assignment = manager.createUserAssignment( adminPrincipal );
441 assignment.addRoleName( adminRole );
442 assignment = manager.saveUserAssignment( assignment );
444 assertEquals( 1, assignment.getRoleNames().size() );
445 assertEquals( 1, manager.getAssignedRoles( adminPrincipal ).size() );
449 public void testGetAssignedPermissionsNoChildRoles()
450 throws RbacManagerException
452 RBACManager manager = rbacManager;
454 rbacManager.eraseDatabase();
455 eventTracker.rbacInit( true );
457 Role admin = getAdminRole();
459 admin = manager.saveRole( admin );
461 assertEquals( 1, manager.getAllRoles().size() );
463 String adminPrincipal = "admin";
465 UserAssignment ua = manager.createUserAssignment( adminPrincipal );
467 ua.addRoleName( admin );
469 manager.saveUserAssignment( ua );
471 assertEquals( 1, manager.getAllUserAssignments().size() );
473 Set<Permission> assignedPermissions = manager.getAssignedPermissions( adminPrincipal );
475 assertNotNull( assignedPermissions );
476 assertEquals( 1, assignedPermissions.size() );
478 /* Assert some event tracker stuff */
479 assertNotNull( eventTracker );
480 assertEquals( 1, eventTracker.initCount );
481 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
483 assertEquals( 1, eventTracker.addedRoleNames.size() );
484 assertEquals( 0, eventTracker.removedRoleNames.size() );
485 assertEquals( 1, eventTracker.addedPermissionNames.size() );
486 assertEquals( 0, eventTracker.removedPermissionNames.size() );
490 public void testGlobalResource()
491 throws RbacManagerException
493 RBACManager manager = rbacManager;
494 rbacManager.eraseDatabase();
495 eventTracker.rbacInit( true );
496 Permission editConfiguration = manager.createPermission( "Edit Configuration" );
497 editConfiguration.setOperation( manager.createOperation( "edit-configuration" ) );
498 editConfiguration.setResource( manager.getGlobalResource() );
499 manager.savePermission( editConfiguration );
501 assertEquals( 1, manager.getAllPermissions().size() );
502 assertEquals( 1, manager.getAllOperations().size() );
503 assertEquals( 1, manager.getAllResources().size() );
505 Permission deleteConfiguration = manager.createPermission( "Delete Configuration" );
506 deleteConfiguration.setOperation( manager.createOperation( "delete-configuration" ) );
507 deleteConfiguration.setResource( manager.getGlobalResource() );
508 manager.savePermission( deleteConfiguration );
510 assertEquals( 2, manager.getAllPermissions().size() );
511 assertEquals( 2, manager.getAllOperations().size() );
512 assertEquals( 1, manager.getAllResources().size() );
514 /* Assert some event tracker stuff */
515 assertNotNull( eventTracker );
516 assertEquals( 1, eventTracker.initCount );
517 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
519 assertEquals( 0, eventTracker.addedRoleNames.size() );
520 assertEquals( 0, eventTracker.removedRoleNames.size() );
521 assertEquals( 2, eventTracker.addedPermissionNames.size() );
522 assertEquals( 0, eventTracker.removedPermissionNames.size() );
526 public void testGlobalResourceOneLiner()
527 throws RbacManagerException
529 RBACManager manager = rbacManager;
530 rbacManager.eraseDatabase();
531 eventTracker.rbacInit( true );
532 manager.savePermission(
533 manager.createPermission( "Edit Configuration", "edit-configuration", Resource.GLOBAL ) );
534 manager.savePermission(
535 manager.createPermission( "Delete Configuration", "delete-configuration", Resource.GLOBAL ) );
537 /* Assert some event tracker stuff */
538 assertNotNull( eventTracker );
539 assertEquals( 1, eventTracker.initCount );
540 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
542 assertEquals( 0, eventTracker.addedRoleNames.size() );
543 assertEquals( 0, eventTracker.removedRoleNames.size() );
544 assertEquals( 2, eventTracker.addedPermissionNames.size() );
545 assertEquals( 0, eventTracker.removedPermissionNames.size() );
549 public void testUserAssignmentAddRemoveSecondRole()
550 throws RbacManagerException
552 RBACManager manager = rbacManager;
554 rbacManager.eraseDatabase();
555 eventTracker.rbacInit( true );
557 Role developerRole = getDeveloperRole();
558 manager.saveRole( developerRole );
560 // Setup User / Assignment with 1 role.
561 String username = "bob";
562 UserAssignment assignment = manager.createUserAssignment( username );
563 assignment.addRoleName( developerRole );
564 manager.saveUserAssignment( assignment );
566 assertEquals( 1, manager.getAllUserAssignments().size() );
567 assertEquals( 1, manager.getAllRoles().size() );
569 // Create another role add it to manager.
570 Role projectAdmin = getProjectAdminRole();
571 String projectAdminRoleName = projectAdmin.getName();
572 manager.saveRole( projectAdmin );
574 // Get User Assignment, add a second role
575 UserAssignment bob = manager.getUserAssignment( username );
576 bob.addRoleName( projectAdminRoleName );
577 bob = manager.saveUserAssignment( bob );
579 assertEquals( 1, manager.getAllUserAssignments().size() );
580 assertEquals( 2, manager.getAllRoles().size() );
581 assertEquals( 2, bob.getRoleNames().size() );
582 assertEquals( 0, manager.getUnassignedRoles( bob.getPrincipal() ).size() );
584 List<String> roles = bob.getRoleNames();
585 assertEquals( 2, roles.size() );
587 // Remove 1 role from bob, end up with 1 role for bob.
588 roles.remove( projectAdminRoleName );
589 assertEquals( 1, roles.size() );
590 bob.setRoleNames( roles );
591 bob = manager.saveUserAssignment( bob );
592 assertEquals( "Should only have 1 role under bob now.", 1, bob.getRoleNames().size() );
593 assertEquals( "Should have 2 total roles still.", 2, manager.getAllRoles().size() );
594 assertEquals( "Should have 1 assignable role", 1, manager.getUnassignedRoles( bob.getPrincipal() ).size() );
596 // Fetch bob again. see if role is missing.
597 UserAssignment cousin = manager.getUserAssignment( username );
598 assertEquals( 1, cousin.getRoleNames().size() );
600 assertEquals( "Should only have 1 role under bob now.", 1, cousin.getRoleNames().size() );
601 assertEquals( "Should have 2 total roles still.", 2, manager.getAllRoles().size() );
603 // remove the last role
604 roles.remove( developerRole.getName() );
605 bob.setRoleNames( roles );
606 bob = manager.saveUserAssignment( bob );
607 assertEquals( "Should have 2 assignable roles.", 2, manager.getUnassignedRoles( bob.getPrincipal() ).size() );
609 /* Assert some event tracker stuff */
610 assertNotNull( eventTracker );
611 assertEquals( 1, eventTracker.initCount );
612 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
614 assertEquals( 2, eventTracker.addedRoleNames.size() );
615 assertEquals( 0, eventTracker.removedRoleNames.size() );
616 assertEquals( 2, eventTracker.addedPermissionNames.size() );
617 assertEquals( 0, eventTracker.removedPermissionNames.size() );
621 public void testUserAssignmentMultipleRoles()
622 throws RbacManagerException
624 RBACManager manager = rbacManager;
626 rbacManager.eraseDatabase();
627 eventTracker.rbacInit( true );
629 Role devRole = getDeveloperRole();
630 manager.saveRole( devRole );
632 // Setup User / Assignment with 1 role.
633 String username = "bob";
634 UserAssignment assignment = manager.createUserAssignment( username );
635 assignment.addRoleName( devRole );
636 assignment = manager.saveUserAssignment( assignment );
638 assertEquals( 1, manager.getAllUserAssignments().size() );
639 assertEquals( 1, manager.getAllRoles().size() );
641 // assign the same role again to the same user
642 assignment.addRoleName( devRole.getName() );
643 manager.saveUserAssignment( assignment );
645 // we certainly shouldn't have 2 roles here now
646 assertEquals( 1, assignment.getRoleNames().size() );
648 /* Assert some event tracker stuff */
649 assertNotNull( eventTracker );
650 assertEquals( 1, eventTracker.initCount );
651 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
653 assertEquals( 1, eventTracker.addedRoleNames.size() );
654 assertEquals( 0, eventTracker.removedRoleNames.size() );
655 assertEquals( 1, eventTracker.addedPermissionNames.size() );
656 assertEquals( 0, eventTracker.removedPermissionNames.size() );
660 public void testUserAssignmentMultipleRolesWithChildRoles()
661 throws RbacManagerException
663 RBACManager manager = rbacManager;
665 rbacManager.eraseDatabase();
666 eventTracker.rbacInit( true );
668 Role devRole = getDeveloperRole();
669 Role devPlusRole = getSuperDeveloperRole();
670 devPlusRole.setChildRoleNames( Collections.singletonList( devRole.getName() ) );
671 manager.saveRole( devRole );
672 manager.saveRole( devPlusRole );
674 // Setup User / Assignment with 1 role.
675 String username = "bob";
676 UserAssignment assignment = manager.createUserAssignment( username );
677 assignment.addRoleName( devRole );
678 assignment = manager.saveUserAssignment( assignment );
680 assertEquals( 1, manager.getAllUserAssignments().size() );
681 assertEquals( "should be only one role assigned", 1,
682 manager.getAssignedRoles( assignment.getPrincipal() ).size() );
683 assertEquals( "should be one role left to assign", 1,
684 manager.getUnassignedRoles( assignment.getPrincipal() ).size() );
685 assertEquals( 2, manager.getAllRoles().size() );
687 // assign the same role again to the same user
688 assignment.addRoleName( devRole.getName() );
689 manager.saveUserAssignment( assignment );
691 // we certainly shouldn't have 2 roles here now
692 assertEquals( 1, assignment.getRoleNames().size() );
694 /* Assert some event tracker stuff */
695 assertNotNull( eventTracker );
696 assertEquals( 1, eventTracker.initCount );
697 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
699 assertEquals( 2, eventTracker.addedRoleNames.size() );
700 assertEquals( 0, eventTracker.removedRoleNames.size() );
701 assertEquals( 1, eventTracker.addedPermissionNames.size() );
702 assertEquals( 0, eventTracker.removedPermissionNames.size() );
706 public void testGetAssignedRoles()
707 throws RbacManagerException
709 RBACManager manager = rbacManager;
710 rbacManager.eraseDatabase();
712 Role adminRole = manager.saveRole( getAdminRole() );
713 Role projectAdminRole = manager.saveRole( getProjectAdminRole() );
714 Role developerRole = manager.saveRole( getDeveloperRole() );
717 assertEquals( 3, manager.getAllRoles().size() );
719 // Setup User / Assignment with 3 roles.
720 String username = "bob";
722 UserAssignment assignment = manager.createUserAssignment( username );
723 assignment.addRoleName( developerRole.getName() );
724 assignment.addRoleName( projectAdminRole.getName() );
725 assignment.addRoleName( adminRole.getName() );
726 assignment = manager.saveUserAssignment( assignment );
728 assertEquals( 3, assignment.getRoleNames().size() );
729 assertEquals( 1, manager.getAllUserAssignments().size() );
730 assertEquals( 3, manager.getAllRoles().size() );
734 // Get the List of Assigned Roles for user bob.
735 Collection<Role> assignedRoles = manager.getAssignedRoles( username );
737 assertNotNull( assignedRoles );
738 assertEquals( 3, assignedRoles.size() );
742 public void testGetAssignedPermissions()
743 throws RbacManagerException
745 RBACManager manager = rbacManager;
746 rbacManager.eraseDatabase();
748 manager.saveRole( getAdminRole() );
749 manager.saveRole( getProjectAdminRole() );
750 Role added = manager.saveRole( getDeveloperRole() );
751 String roleName = added.getName();
753 assertEquals( 3, manager.getAllRoles().size() );
754 assertEquals( 3, manager.getAllPermissions().size() );
756 // Setup User / Assignment with 1 role.
757 String username = "bob";
759 UserAssignment assignment = manager.createUserAssignment( username );
760 assignment.addRoleName( roleName );
761 manager.saveUserAssignment( assignment );
763 assertEquals( 1, manager.getAllUserAssignments().size() );
764 assertEquals( 3, manager.getAllRoles().size() );
765 assertEquals( 3, manager.getAllPermissions().size() );
767 // Get the List of Assigned Roles for user bob.
768 Collection<Permission> assignedPermissions = manager.getAssignedPermissions( username );
770 assertNotNull( assignedPermissions );
771 assertEquals( 1, assignedPermissions.size() );
774 public Role getChildRole( RBACManager manager, Role role, String expectedChildRoleName, int childRoleCount )
775 throws RbacManagerException
777 assertTrue( role.hasChildRoles() );
778 List<String> childNames = role.getChildRoleNames();
779 assertNotNull( childNames );
780 assertEquals( 1, childNames.size() );
781 String childName = (String) childNames.get( 0 );
782 assertNotNull( childName );
783 Role childRole = manager.getRole( childName );
784 assertNotNull( childRole );
785 assertEquals( expectedChildRoleName, childRole.getName() );
791 public void testGetRolesDeep()
792 throws RbacManagerException
794 rbacManager.eraseDatabase();
795 rbacDefaults.createDefaults();
797 // Setup User / Assignment with 1 role.
798 String username = "bob";
800 UserAssignment assignment = rbacManager.createUserAssignment( username );
801 assignment.addRoleName( "Developer" );
802 rbacManager.saveUserAssignment( assignment );
804 assertEquals( 1, rbacManager.getAllUserAssignments().size() );
805 assertEquals( 4, rbacManager.getAllRoles().size() );
806 assertEquals( 6, rbacManager.getAllPermissions().size() );
808 // Get the List of Assigned Roles for user bob.
809 Role devel = rbacManager.getRole( "Developer" );
810 assertNotNull( devel );
813 Role trusted = getChildRole( rbacManager, devel, "Trusted Developer", 1 );
816 Role sysAdmin = getChildRole( rbacManager, trusted, "System Administrator", 1 );
819 getChildRole( rbacManager, sysAdmin, "User Administrator", 1 );
823 public void testGetAssignedPermissionsDeep()
824 throws RbacManagerException
826 rbacDefaults.createDefaults();
828 // Setup User / Assignment with 1 role.
829 String username = "bob";
831 UserAssignment assignment = rbacManager.createUserAssignment( username );
832 assignment.addRoleName( "Developer" );
833 rbacManager.saveUserAssignment( assignment );
835 assertEquals( 1, rbacManager.getAllUserAssignments().size() );
836 assertEquals( 4, rbacManager.getAllRoles().size() );
837 assertEquals( 6, rbacManager.getAllPermissions().size() );
841 // Get the List of Assigned Roles for user bob.
842 Collection<Permission> assignedPermissions = rbacManager.getAssignedPermissions( username );
844 assertNotNull( assignedPermissions );
845 assertEquals( 6, assignedPermissions.size() );
849 public void testLargeApplicationInit()
850 throws RbacManagerException
853 rbacManager.eraseDatabase();
854 rbacDefaults.createDefaults();
855 assertEquals( 6, rbacManager.getAllPermissions().size() );
856 assertEquals( 11, rbacManager.getAllOperations().size() );
857 assertEquals( 4, rbacManager.getAllRoles().size() );
861 public void testAddRemovePermanentPermission()
862 throws RbacManagerException
864 assertNotNull( rbacManager );
866 rbacManager.eraseDatabase();
867 eventTracker.rbacInit( true );
869 Role adminRole = rbacManager.saveRole( getAdminRole() );
870 rbacManager.saveRole( getDeveloperRole() );
872 assertEquals( 2, rbacManager.getAllRoles().size() );
873 assertEquals( 2, rbacManager.getAllPermissions().size() );
875 Permission createUserPerm = rbacManager.createPermission( "CREATE_USER", "CREATE", "User" );
876 createUserPerm.setPermanent( true );
878 // perm shouldn't exist in manager (yet)
879 assertEquals( 2, rbacManager.getAllPermissions().size() );
881 adminRole.addPermission( createUserPerm );
882 rbacManager.saveRole( adminRole );
884 // perm should exist in manager now.
885 assertEquals( 3, rbacManager.getAllPermissions().size() );
886 Permission fetched = rbacManager.getPermission( "CREATE_USER" );
887 assertNotNull( fetched );
889 // Attempt to remove perm now.
892 // Use permission name technique first.
893 rbacManager.removePermission( "CREATE_USER" );
895 catch ( RbacPermanentException e )
902 // Use permission object technique next.
903 rbacManager.removePermission( fetched );
905 catch ( RbacPermanentException e )
910 // Assert some event tracker stuff
911 assertNotNull( eventTracker );
912 assertEquals( 1, eventTracker.initCount );
913 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
915 assertEquals( 2, eventTracker.addedRoleNames.size() );
916 assertEquals( 0, eventTracker.removedRoleNames.size() );
917 assertEquals( 3, eventTracker.addedPermissionNames.size() );
918 assertEquals( 0, eventTracker.removedPermissionNames.size() );
922 public void testAddRemovePermanentRole()
923 throws RbacManagerException
925 assertNotNull( rbacManager );
926 rbacManager.eraseDatabase();
927 eventTracker.rbacInit( true );
929 Role adminRole = getAdminRole();
930 adminRole.setPermanent( true );
932 adminRole = rbacManager.saveRole( adminRole );
933 Role develRole = rbacManager.saveRole( getDeveloperRole() );
935 assertEquals( 2, rbacManager.getAllRoles().size() );
937 Role actualAdmin = rbacManager.getRole( adminRole.getName() );
938 Role actualDevel = rbacManager.getRole( develRole.getName() );
940 assertEquals( adminRole, actualAdmin );
941 assertEquals( develRole, actualDevel );
943 // Attempt to remove perm now.
946 // Use role name technique first.
947 rbacManager.removeRole( adminRole.getName() );
949 catch ( RbacPermanentException e )
956 // Use role object technique next.
957 rbacManager.removeRole( adminRole );
959 catch ( RbacPermanentException e )
964 /* Assert some event tracker stuff */
965 assertNotNull( eventTracker );
966 assertEquals( 1, eventTracker.initCount );
967 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
969 assertEquals( 2, eventTracker.addedRoleNames.size() );
970 assertEquals( 0, eventTracker.removedRoleNames.size() );
971 assertEquals( 2, eventTracker.addedPermissionNames.size() );
972 assertEquals( 0, eventTracker.removedPermissionNames.size() );
976 public void testAddRemovePermanentOperation()
977 throws RbacManagerException
980 rbacManager.eraseDatabase();
981 eventTracker.rbacInit( true );
982 assertNotNull( rbacManager );
984 Role adminRole = rbacManager.saveRole( getAdminRole() );
985 rbacManager.saveRole( getDeveloperRole() );
987 assertEquals( 2, rbacManager.getAllRoles().size() );
988 assertEquals( 2, rbacManager.getAllPermissions().size() );
990 Permission createUserPerm = rbacManager.createPermission( "CREATE_USER", "CREATE", "User" );
991 createUserPerm.getOperation().setPermanent( true );
993 // perm shouldn't exist in manager (yet)
994 assertEquals( 2, rbacManager.getAllPermissions().size() );
995 assertEquals( 1, rbacManager.getAllOperations().size() );
997 adminRole.addPermission( createUserPerm );
998 rbacManager.saveRole( adminRole );
1000 // perm should exist in manager now.
1001 assertEquals( 2, rbacManager.getAllOperations().size() );
1002 Operation fetched = rbacManager.getOperation( "CREATE" );
1003 assertNotNull( fetched );
1005 // Attempt to remove operation now.
1008 // Use operation name technique first.
1009 rbacManager.removeOperation( "CREATE" );
1011 catch ( RbacPermanentException e )
1018 // Use operation object technique next.
1019 rbacManager.removeOperation( fetched );
1021 catch ( RbacPermanentException e )
1026 // Assert some event tracker stuff
1027 assertNotNull( eventTracker );
1028 assertEquals( 1, eventTracker.initCount );
1029 assertTrue( eventTracker.lastDbFreshness.booleanValue() );
1031 assertEquals( 2, eventTracker.addedRoleNames.size() );
1032 assertEquals( 0, eventTracker.removedRoleNames.size() );
1033 assertEquals( 3, eventTracker.addedPermissionNames.size() );
1034 assertEquals( 0, eventTracker.removedPermissionNames.size() );
1038 * Allows subclasses to hook code after a test case has finished it's setup
1040 protected void afterSetup()