1 package org.apache.maven.archiva.xmlrpc.security;
\r
4 * Licensed to the Apache Software Foundation (ASF) under one
\r
5 * or more contributor license agreements. See the NOTICE file
\r
6 * distributed with this work for additional information
\r
7 * regarding copyright ownership. The ASF licenses this file
\r
8 * to you under the Apache License, Version 2.0 (the
\r
9 * "License"); you may not use this file except in compliance
\r
10 * with the License. You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing,
\r
15 * software distributed under the License is distributed on an
\r
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
\r
17 * KIND, either express or implied. See the License for the
\r
18 * specific language governing permissions and limitations
\r
19 * under the License.
\r
22 import java.io.File;
\r
24 import org.apache.commons.io.FileUtils;
\r
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
\r
26 import org.apache.maven.archiva.security.ArchivaRoleConstants;
\r
27 import org.apache.maven.archiva.security.UserRepositories;
\r
28 import org.apache.xmlrpc.XmlRpcRequest;
\r
29 import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
\r
30 import org.codehaus.plexus.redback.rbac.RBACManager;
\r
31 import org.codehaus.plexus.redback.role.RoleManager;
\r
32 import org.codehaus.plexus.redback.system.SecuritySystem;
\r
33 import org.codehaus.plexus.redback.users.User;
\r
34 import org.codehaus.plexus.redback.users.UserManager;
\r
35 import org.codehaus.plexus.redback.users.UserNotFoundException;
\r
36 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
\r
37 import org.easymock.MockControl;
\r
38 import org.easymock.classextension.MockClassControl;
\r
41 * XmlRpcAuthenticatorTest
\r
43 * @version $Id XmlRpcAuthenticatorTest.java
\r
45 public class XmlRpcAuthenticatorTest
\r
46 //extends AbstractDependencyInjectionSpringContextTests
\r
47 extends PlexusInSpringTestCase
\r
49 protected static final String USER_GUEST = "guest";
\r
51 protected static final String USER_ADMIN = "admin";
\r
53 protected static final String USER_ALPACA = "alpaca";
\r
55 private static final String PASSWORD = "password123";
\r
57 protected SecuritySystem securitySystem;
\r
59 protected RoleManager roleManager;
\r
61 private MockControl xmlRpcRequestControl;
\r
63 private XmlRpcRequest xmlRpcRequest;
\r
65 private XmlRpcAuthenticator authenticator;
\r
67 private MockControl configControl;
\r
69 private XmlRpcHttpRequestConfigImpl config;
\r
76 securitySystem = (SecuritySystem) lookup( SecuritySystem.class, "testable" );
\r
77 roleManager = (RoleManager) lookup( RoleManager.class, "default" );
\r
79 // Some basic asserts.
\r
80 assertNotNull( securitySystem );
\r
81 assertNotNull( roleManager );
\r
83 // Setup Admin User.
\r
84 User adminUser = createUser( USER_ADMIN, "Admin User", null );
\r
85 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getPrincipal().toString() );
\r
87 // Setup Guest User.
\r
88 User guestUser = createUser( USER_GUEST, "Guest User", null );
\r
89 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getPrincipal().toString() );
\r
91 configControl = MockClassControl.createControl( XmlRpcHttpRequestConfigImpl.class );
\r
92 config = ( XmlRpcHttpRequestConfigImpl ) configControl.getMock();
\r
94 xmlRpcRequestControl = MockControl.createControl( XmlRpcRequest.class );
\r
95 xmlRpcRequest = ( XmlRpcRequest ) xmlRpcRequestControl.getMock();
\r
97 authenticator = new XmlRpcAuthenticator( securitySystem );
\r
100 private User createUser( String principal, String fullname, String password )
\r
101 throws UserNotFoundException
\r
103 UserManager userManager = securitySystem.getUserManager();
\r
105 User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
\r
106 securitySystem.getPolicy().setEnabled( false );
\r
107 userManager.addUser( user );
\r
108 securitySystem.getPolicy().setEnabled( true );
\r
110 user.setPassword( password );
\r
111 userManager.updateUser( user );
\r
116 public void testIsAuthorizedUserExistsButNotAuthorized()
\r
119 createUser( USER_ALPACA, "Al 'Archiva' Paca", PASSWORD );
\r
121 UserManager userManager = securitySystem.getUserManager();
\r
124 User user = userManager.findUser( USER_ALPACA );
\r
125 assertEquals( USER_ALPACA, user.getPrincipal() );
\r
127 catch ( UserNotFoundException e )
\r
129 fail( "User should exist in the database." );
\r
132 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getConfig(), config, 2 );
\r
134 configControl.expectAndReturn( config.getBasicUserName(), USER_ALPACA );
\r
136 configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
\r
138 xmlRpcRequestControl.replay();
\r
139 configControl.replay();
\r
141 boolean isAuthorized = authenticator.isAuthorized( xmlRpcRequest );
\r
143 xmlRpcRequestControl.verify();
\r
144 configControl.verify();
\r
146 assertFalse( isAuthorized );
\r
149 public void testIsAuthorizedUserExistsAndAuthorized()
\r
152 createUser( USER_ALPACA, "Al 'Archiva' Paca", PASSWORD );
\r
154 UserManager userManager = securitySystem.getUserManager();
\r
157 User user = userManager.findUser( USER_ALPACA );
\r
158 assertEquals( USER_ALPACA, user.getPrincipal() );
\r
160 catch ( UserNotFoundException e )
\r
162 fail( "User should exist in the database." );
\r
165 //TODO cannot assign global repo manager role - it says role does not exist :|
\r
167 //roleManager.assignRole( ArchivaRoleConstants.GLOBAL_REPOSITORY_MANAGER_ROLE, USER_ALPACA );
\r
169 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getConfig(), config, 2 );
\r
171 configControl.expectAndReturn( config.getBasicUserName(), USER_ALPACA );
\r
173 configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
\r
175 xmlRpcRequestControl.replay();
\r
176 configControl.replay();
\r
178 boolean isAuthorized = authenticator.isAuthorized( xmlRpcRequest );
\r
180 xmlRpcRequestControl.verify();
\r
181 configControl.verify();
\r
183 //assertTrue( isAuthorized );
\r
186 public void testIsAuthorizedUserDoesNotExist()
\r
189 UserManager userManager = securitySystem.getUserManager();
\r
192 userManager.findUser( USER_ALPACA );
\r
193 fail( "User should not exist in the database." );
\r
195 catch ( UserNotFoundException e )
\r
197 assertEquals( "Unable to find user 'alpaca'", e.getMessage() );
\r
200 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getConfig(), config, 2 );
\r
202 configControl.expectAndReturn( config.getBasicUserName(), USER_ALPACA );
\r
204 configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
\r
206 xmlRpcRequestControl.replay();
\r
207 configControl.replay();
\r
209 boolean isAuthorized = authenticator.isAuthorized( xmlRpcRequest );
\r
211 xmlRpcRequestControl.verify();
\r
212 configControl.verify();
\r
214 assertFalse( isAuthorized );
\r