1 package org.apache.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 org.apache.archiva.web.xmlrpc.security.XmlRpcAuthenticator;
\r
23 import org.apache.maven.archiva.security.ArchivaRoleConstants;
\r
24 import org.apache.xmlrpc.XmlRpcRequest;
\r
25 import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
\r
26 import org.codehaus.plexus.redback.role.RoleManager;
\r
27 import org.codehaus.plexus.redback.system.SecuritySystem;
\r
28 import org.codehaus.plexus.redback.users.User;
\r
29 import org.codehaus.plexus.redback.users.UserManager;
\r
30 import org.codehaus.plexus.redback.users.UserNotFoundException;
\r
31 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
\r
32 import org.easymock.MockControl;
\r
33 import org.easymock.classextension.MockClassControl;
\r
36 * XmlRpcAuthenticatorTest
\r
38 * @version $Id XmlRpcAuthenticatorTest.java
\r
40 public class XmlRpcAuthenticatorTest
\r
41 //extends AbstractDependencyInjectionSpringContextTests
\r
42 extends PlexusInSpringTestCase
\r
44 protected static final String USER_GUEST = "guest";
\r
46 protected static final String USER_ADMIN = "admin";
\r
48 protected static final String USER_ALPACA = "alpaca";
\r
50 private static final String PASSWORD = "password123";
\r
52 protected SecuritySystem securitySystem;
\r
54 protected RoleManager roleManager;
\r
56 private MockControl xmlRpcRequestControl;
\r
58 private XmlRpcRequest xmlRpcRequest;
\r
60 private XmlRpcAuthenticator authenticator;
\r
62 private MockControl configControl;
\r
64 private XmlRpcHttpRequestConfigImpl config;
\r
71 securitySystem = (SecuritySystem) lookup( SecuritySystem.class, "testable" );
\r
72 roleManager = (RoleManager) lookup( RoleManager.class, "default" );
\r
74 // Some basic asserts.
\r
75 assertNotNull( securitySystem );
\r
76 assertNotNull( roleManager );
\r
78 // Setup Admin User.
\r
79 User adminUser = createUser( USER_ADMIN, "Admin User", null );
\r
80 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getPrincipal().toString() );
\r
82 // Setup Guest User.
\r
83 User guestUser = createUser( USER_GUEST, "Guest User", null );
\r
84 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getPrincipal().toString() );
\r
86 configControl = MockClassControl.createControl( XmlRpcHttpRequestConfigImpl.class );
\r
87 config = ( XmlRpcHttpRequestConfigImpl ) configControl.getMock();
\r
89 xmlRpcRequestControl = MockControl.createControl( XmlRpcRequest.class );
\r
90 xmlRpcRequest = ( XmlRpcRequest ) xmlRpcRequestControl.getMock();
\r
92 authenticator = new XmlRpcAuthenticator( securitySystem, null );
\r
95 private User createUser( String principal, String fullname, String password )
\r
96 throws UserNotFoundException
\r
98 UserManager userManager = securitySystem.getUserManager();
\r
100 User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
\r
101 securitySystem.getPolicy().setEnabled( false );
\r
102 userManager.addUser( user );
\r
103 securitySystem.getPolicy().setEnabled( true );
\r
105 user.setPassword( password );
\r
106 userManager.updateUser( user );
\r
111 public void testIsAuthorizedUserExistsButNotAuthorized()
\r
114 createUser( USER_ALPACA, "Al 'Archiva' Paca", PASSWORD );
\r
116 UserManager userManager = securitySystem.getUserManager();
\r
119 User user = userManager.findUser( USER_ALPACA );
\r
120 assertEquals( USER_ALPACA, user.getPrincipal() );
\r
122 catch ( UserNotFoundException e )
\r
124 fail( "User should exist in the database." );
\r
127 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getConfig(), config, 2 );
\r
129 configControl.expectAndReturn( config.getBasicUserName(), USER_ALPACA );
\r
131 configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
\r
133 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getMethodName(),
\r
134 "AdministrationService.getAllManagedRepositories" );
\r
136 xmlRpcRequestControl.replay();
\r
137 configControl.replay();
\r
139 boolean isAuthorized = authenticator.isAuthorized( xmlRpcRequest );
\r
141 xmlRpcRequestControl.verify();
\r
142 configControl.verify();
\r
144 assertFalse( isAuthorized );
\r
147 public void testIsAuthorizedUserExistsAndAuthorized()
\r
150 createUser( USER_ALPACA, "Al 'Archiva' Paca", PASSWORD );
\r
152 UserManager userManager = securitySystem.getUserManager();
\r
155 User user = userManager.findUser( USER_ALPACA );
\r
156 assertEquals( USER_ALPACA, user.getPrincipal() );
\r
158 catch ( UserNotFoundException e )
\r
160 fail( "User should exist in the database." );
\r
163 //TODO cannot assign global repo manager role - it says role does not exist :|
\r
165 //roleManager.assignRole( ArchivaRoleConstants.GLOBAL_REPOSITORY_MANAGER_ROLE, USER_ALPACA );
\r
167 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getConfig(), config, 2 );
\r
169 configControl.expectAndReturn( config.getBasicUserName(), USER_ALPACA );
\r
171 configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
\r
173 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getMethodName(),
\r
174 "AdministrationService.getAllManagedRepositories" );
\r
176 xmlRpcRequestControl.replay();
\r
177 configControl.replay();
\r
179 boolean isAuthorized = authenticator.isAuthorized( xmlRpcRequest );
\r
180 assertTrue( isAuthorized );
\r
182 xmlRpcRequestControl.verify();
\r
183 configControl.verify();
\r
185 //assertTrue( isAuthorized );
\r
188 public void testIsAuthorizedUserDoesNotExist()
\r
191 UserManager userManager = securitySystem.getUserManager();
\r
194 userManager.findUser( USER_ALPACA );
\r
195 fail( "User should not exist in the database." );
\r
197 catch ( UserNotFoundException e )
\r
199 assertEquals( "Unable to find user 'alpaca'", e.getMessage() );
\r
202 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getConfig(), config, 2 );
\r
204 configControl.expectAndReturn( config.getBasicUserName(), USER_ALPACA );
\r
206 configControl.expectAndReturn( config.getBasicPassword(), PASSWORD );
\r
208 xmlRpcRequestControl.expectAndReturn( xmlRpcRequest.getMethodName(),
\r
209 "AdministrationService.getAllManagedRepositories" );
\r
211 xmlRpcRequestControl.replay();
\r
212 configControl.replay();
\r
214 boolean isAuthorized = authenticator.isAuthorized( xmlRpcRequest );
\r
216 xmlRpcRequestControl.verify();
\r
217 configControl.verify();
\r
219 assertFalse( isAuthorized );
\r