1 package org.apache.archiva.redback.role;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
23 import org.apache.archiva.redback.rbac.RBACManager;
24 import org.apache.archiva.redback.rbac.Permission;
25 import org.apache.archiva.redback.rbac.Resource;
26 import org.apache.archiva.redback.rbac.Role;
27 import org.apache.archiva.redback.rbac.UserAssignment;
28 import org.apache.archiva.redback.role.model.ModelPermission;
29 import org.apache.archiva.redback.role.model.ModelTemplate;
30 import org.apache.archiva.redback.role.util.RoleModelUtils;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.springframework.test.context.ContextConfiguration;
34 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36 import java.util.List;
39 * AbstractRoleManagerTest:
41 * @author: Jesse McConnell <jesse@codehaus.org>
44 @RunWith( SpringJUnit4ClassRunner.class )
45 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
46 public abstract class AbstractRoleManagerTest
50 protected RBACManager rbacManager;
52 protected RoleManager roleManager;
54 public AbstractRoleManagerTest()
60 public void testLoading()
64 List<Resource> all = rbacManager.getAllResources();
67 assertTrue( rbacManager.resourceExists( "*" ) );
68 assertTrue( rbacManager.operationExists( "Test Operation" ) );
69 assertTrue( rbacManager.roleExists( "Test Role" ) );
70 assertTrue( rbacManager.roleExists( "Test Role 1" ) );
71 assertTrue( rbacManager.roleExists( "Test Role 2" ) );
73 assertTrue( rbacManager.roleExists( "Role for cornflake eaters drinking milk in the bowl" ) );
75 roleManager.createTemplatedRole( "test-template-2", "foo" );
77 assertTrue( roleManager.templatedRoleExists( "test-template-2", "foo" ) );
78 assertTrue( roleManager.templatedRoleExists( "test-template", "foo" ) );
80 roleManager.updateRole( "test-template-2", "foo", "bar" );
82 assertFalse( roleManager.templatedRoleExists( "test-template-2", "foo" ) );
83 // TODO: bug - assertFalse( roleManager.templatedRoleExists( "test-template", "foo" ) );
84 assertTrue( roleManager.templatedRoleExists( "test-template-2", "bar" ) );
85 assertTrue( roleManager.templatedRoleExists( "test-template", "bar" ) );
87 roleManager.createTemplatedRole( "test-template-2", "hot" );
89 assertTrue( roleManager.templatedRoleExists( "test-template-2", "hot" ) );
93 public void testUserAssignmentUpdate()
96 String principal = "joe";
98 roleManager.assignRole( "test-role", principal );
99 roleManager.assignRoleByName( "Test Role 1", principal );
100 roleManager.createTemplatedRole( "test-template-2", "cold" );
101 roleManager.assignTemplatedRole( "test-template-2", "cold", principal );
103 roleManager.updateRole( "test-template-2", "cold", "frigid" );
105 assertTrue( roleManager.templatedRoleExists( "test-template-2", "frigid" ) );
107 UserAssignment assignment = rbacManager.getUserAssignment( principal );
109 List<String> assignments = assignment.getRoleNames();
111 assertEquals( 3, assignments.size() );
113 for ( String roleName : assignments )
115 System.out.println( roleName );
116 assertTrue( "Test Role".equals( roleName ) || "Foo 2 - frigid".equals( roleName ) || "Test Role 1".equals( roleName ) );
121 public void testVerifyTemplatedRole()
124 roleManager.createTemplatedRole( "test-template-2", "first" );
126 assertTrue( roleManager.templatedRoleExists( "test-template-2", "first" ) );
127 Role role = rbacManager.getRole( "Foo 2 - first" );
128 assertNotNull( role );
129 assertTrue( hasPermissionOnOperation( role, "Eat Cornflakes", "first" ) );
130 assertFalse( hasPermissionOnOperation( role, "Drink Milk" ) );
132 ModelTemplate template = RoleModelUtils.getModelTemplate( roleManager.getModel(), "test-template-2" );
133 ModelPermission p = new ModelPermission();
134 p.setId( "new-permission" );
135 p.setName( "New Permission" );
136 p.setOperation( "drink-milk" );
137 p.setResource( "${resource}" );
138 template.addPermission( p );
139 p = new ModelPermission();
140 p.setId( "eat-cornflakes-permission-3" );
141 p.setName( "Eat 3 Cornflakes" );
142 p.setOperation( "eat-cornflakes" );
143 p.setResource( "${resource}" );
144 template.removePermission( p );
146 roleManager.verifyTemplatedRole( "test-template-2", "first" );
148 assertTrue( roleManager.templatedRoleExists( "test-template-2", "first" ) );
149 role = rbacManager.getRole( "Foo 2 - first" );
150 assertNotNull( role );
151 assertFalse( hasPermissionOnOperation( role, "Eat Cornflakes", "first" ) );
152 assertTrue( hasPermissionOnOperation( role, "Drink Milk", "first" ) );
155 private boolean hasPermissionOnOperation( Role role, String operation )
157 for ( Permission p : role.getPermissions() )
159 if ( p.getOperation().getName().equals( operation ) )
167 private boolean hasPermissionOnOperation( Role role, String operation, String resource )
169 for ( Permission p : role.getPermissions() )
171 if ( p.getOperation().getName().equals( operation ) && p.getResource().getIdentifier().equals( resource ) )
179 public void setRbacManager( RBACManager rbacManager )
181 this.rbacManager = rbacManager;
184 public void setRoleManager( RoleManager roleManager )
186 this.roleManager = roleManager;