1 package org.codehaus.plexus.redback.struts2.action.admin;
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 net.sf.ehcache.CacheManager;
23 import org.apache.archiva.redback.users.UserManager;
24 import org.apache.struts2.StrutsSpringTestCase;
25 import org.codehaus.plexus.redback.authentication.AuthenticationException;
26 import org.codehaus.plexus.redback.authentication.PasswordBasedAuthenticationDataSource;
27 import org.codehaus.plexus.redback.policy.AccountLockedException;
28 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
29 import org.codehaus.plexus.redback.rbac.RBACManager;
30 import org.codehaus.plexus.redback.rbac.RbacManagerException;
31 import org.codehaus.plexus.redback.rbac.RbacObjectInvalidException;
32 import org.codehaus.plexus.redback.rbac.UserAssignment;
33 import org.codehaus.plexus.redback.role.RoleManager;
34 import org.codehaus.plexus.redback.struts2.action.AbstractUserCredentialsAction;
35 import org.codehaus.plexus.redback.system.SecuritySession;
36 import org.codehaus.plexus.redback.system.SecuritySystem;
37 import org.codehaus.plexus.redback.system.SecuritySystemConstants;
38 import org.apache.archiva.redback.users.User;
39 import org.apache.archiva.redback.users.UserNotFoundException;
40 import org.codehaus.plexus.redback.users.memory.SimpleUser;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.JUnit4;
46 import java.util.Collections;
48 @RunWith( JUnit4.class )
49 public abstract class AbstractUserCredentialsActionTest
50 extends StrutsSpringTestCase
52 protected static final String PASSWORD = "password1";
55 //@Named( value = "rBACManager#memory" )
56 protected RBACManager rbacManager;
59 private RoleManager roleManager;
62 protected SecuritySystem system;
64 protected SecuritySession session;
67 protected String[] getContextLocations()
69 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
76 CacheManager.getInstance().clearAll();
79 rbacManager = applicationContext.getBean( "rBACManager#memory" , RBACManager.class );
80 roleManager = applicationContext.getBean( RoleManager.class );
81 system = applicationContext.getBean( SecuritySystem.class );
84 roleManager.loadRoleModel( getClass().getResource( "/redback.xml" ) );
85 roleManager.createTemplatedRole( "project-administrator", "default" );
86 roleManager.createTemplatedRole( "project-administrator", "other" );
87 roleManager.createTemplatedRole( "project-grant-only", "default" );
89 UserManager userManager = system.getUserManager();
91 User user = new SimpleUser();
92 user.setUsername( "user" );
93 user.setPassword( PASSWORD );
94 userManager.addUserUnchecked( user );
96 user = new SimpleUser();
97 user.setUsername( "user2" );
98 user.setPassword( PASSWORD );
99 userManager.addUserUnchecked( user );
101 user = new SimpleUser();
102 user.setUsername( "user3" );
103 user.setPassword( PASSWORD );
104 userManager.addUserUnchecked( user );
106 user = new SimpleUser();
107 user.setUsername( "admin" );
108 user.setPassword( PASSWORD );
109 userManager.addUserUnchecked( user );
111 user = new SimpleUser();
112 user.setUsername( "user-admin" );
113 user.setPassword( PASSWORD );
114 userManager.addUserUnchecked( user );
116 UserAssignment assignment = rbacManager.createUserAssignment( "admin" );
117 assignment.addRoleName( "System Administrator" );
118 rbacManager.saveUserAssignment( assignment );
120 assignment = rbacManager.createUserAssignment( "user-admin" );
121 assignment.addRoleName( "User Administrator" );
122 rbacManager.saveUserAssignment( assignment );
124 assignment = rbacManager.createUserAssignment( "user2" );
125 rbacManager.saveUserAssignment( assignment );
131 CacheManager.getInstance().clearAll();
134 protected void addAssignment( String principal, String roleName )
135 throws RbacManagerException, RbacObjectInvalidException
137 UserAssignment assignment;
139 if ( rbacManager.userAssignmentExists( principal ) )
141 assignment = rbacManager.getUserAssignment( principal );
145 assignment = rbacManager.createUserAssignment( principal );
147 assignment.addRoleName( roleName );
148 rbacManager.saveUserAssignment( assignment );
151 protected void login( AbstractUserCredentialsAction action, String principal, String password )
152 throws AuthenticationException, UserNotFoundException, AccountLockedException, MustChangePasswordException
154 PasswordBasedAuthenticationDataSource authdatasource = new PasswordBasedAuthenticationDataSource();
155 authdatasource.setPrincipal( principal );
156 authdatasource.setPassword( password );
157 session = system.authenticate( authdatasource );
158 assertTrue( session.isAuthenticated() );
160 action.setSession( Collections.singletonMap( SecuritySystemConstants.SECURITY_SESSION_KEY, (Object) session ) );