]> source.dussan.org Git - archiva.git/blob
b64a558d8e0faccdb4b43abda2a799f48ea27bdf
[archiva.git] /
1 package org.codehaus.plexus.redback.struts2.action.admin;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
45
46 import java.util.Collections;
47
48 @RunWith( JUnit4.class )
49 public abstract class AbstractUserCredentialsActionTest
50     extends StrutsSpringTestCase
51 {
52     protected static final String PASSWORD = "password1";
53
54     //@Inject
55     //@Named( value = "rBACManager#memory" )
56     protected RBACManager rbacManager;
57
58     //@Inject
59     private RoleManager roleManager;
60
61     //@Inject
62     protected SecuritySystem system;
63
64     protected SecuritySession session;
65
66     @Override
67     protected String[] getContextLocations()
68     {
69         return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
70     }
71
72     @Before
73     public void setUp()
74         throws Exception
75     {
76         CacheManager.getInstance().clearAll();
77         super.setUp();
78
79         rbacManager = applicationContext.getBean( "rBACManager#memory" , RBACManager.class );
80         roleManager = applicationContext.getBean( RoleManager.class );
81         system = applicationContext.getBean( SecuritySystem.class );
82
83
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" );
88
89         UserManager userManager = system.getUserManager();
90
91         User user = new SimpleUser();
92         user.setUsername( "user" );
93         user.setPassword( PASSWORD );
94         userManager.addUserUnchecked( user );
95
96         user = new SimpleUser();
97         user.setUsername( "user2" );
98         user.setPassword( PASSWORD );
99         userManager.addUserUnchecked( user );
100
101         user = new SimpleUser();
102         user.setUsername( "user3" );
103         user.setPassword( PASSWORD );
104         userManager.addUserUnchecked( user );
105
106         user = new SimpleUser();
107         user.setUsername( "admin" );
108         user.setPassword( PASSWORD );
109         userManager.addUserUnchecked( user );
110
111         user = new SimpleUser();
112         user.setUsername( "user-admin" );
113         user.setPassword( PASSWORD );
114         userManager.addUserUnchecked( user );
115
116         UserAssignment assignment = rbacManager.createUserAssignment( "admin" );
117         assignment.addRoleName( "System Administrator" );
118         rbacManager.saveUserAssignment( assignment );
119
120         assignment = rbacManager.createUserAssignment( "user-admin" );
121         assignment.addRoleName( "User Administrator" );
122         rbacManager.saveUserAssignment( assignment );
123
124         assignment = rbacManager.createUserAssignment( "user2" );
125         rbacManager.saveUserAssignment( assignment );
126     }
127
128     @After
129     public void after()
130     {
131         CacheManager.getInstance().clearAll();
132     }
133
134     protected void addAssignment( String principal, String roleName )
135         throws RbacManagerException, RbacObjectInvalidException
136     {
137         UserAssignment assignment;
138
139         if ( rbacManager.userAssignmentExists( principal ) )
140         {
141             assignment = rbacManager.getUserAssignment( principal );
142         }
143         else
144         {
145             assignment = rbacManager.createUserAssignment( principal );
146         }
147         assignment.addRoleName( roleName );
148         rbacManager.saveUserAssignment( assignment );
149     }
150
151     protected void login( AbstractUserCredentialsAction action, String principal, String password )
152         throws AuthenticationException, UserNotFoundException, AccountLockedException, MustChangePasswordException
153     {
154         PasswordBasedAuthenticationDataSource authdatasource = new PasswordBasedAuthenticationDataSource();
155         authdatasource.setPrincipal( principal );
156         authdatasource.setPassword( password );
157         session = system.authenticate( authdatasource );
158         assertTrue( session.isAuthenticated() );
159
160         action.setSession( Collections.singletonMap( SecuritySystemConstants.SECURITY_SESSION_KEY, (Object) session ) );
161     }
162
163 }