1 package org.apache.maven.archiva.security;
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 javax.inject.Inject;
23 import javax.servlet.http.HttpServletRequest;
25 import org.codehaus.plexus.redback.authentication.AuthenticationException;
26 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
27 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
28 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
29 import org.codehaus.plexus.redback.system.SecuritySession;
30 import org.codehaus.plexus.redback.users.User;
31 import org.codehaus.plexus.redback.users.UserManager;
33 import org.easymock.MockControl;
34 import org.junit.Before;
35 import org.junit.Test;
38 * ArchivaServletAuthenticatorTest
42 public class ArchivaServletAuthenticatorTest
43 extends AbstractSecurityTest
46 private ServletAuthenticator servletAuth;
48 private MockControl httpServletRequestControl;
50 private HttpServletRequest request;
58 httpServletRequestControl = MockControl.createControl( HttpServletRequest.class );
59 request = (HttpServletRequest) httpServletRequestControl.getMock();
61 setupRepository( "corporate" );
64 protected String getPlexusConfigLocation()
66 return "org/apache/maven/archiva/security/ArchivaServletAuthenticatorTest.xml";
69 protected void assignRepositoryManagerRole( String principal, String repoId )
72 roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId, principal );
76 public void testIsAuthenticatedUserExists()
79 AuthenticationResult result = new AuthenticationResult( true, "user", null );
80 boolean isAuthenticated = servletAuth.isAuthenticated( request, result );
82 assertTrue( isAuthenticated );
86 public void testIsAuthenticatedUserDoesNotExist()
89 AuthenticationResult result = new AuthenticationResult( false, "non-existing-user", null );
92 servletAuth.isAuthenticated( request, result );
93 fail( "Authentication exception should have been thrown." );
95 catch ( AuthenticationException e )
97 assertEquals( "User Credentials Invalid", e.getMessage() );
102 public void testIsAuthorizedUserHasWriteAccess()
105 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
107 assignRepositoryManagerRole( USER_ALPACA, "corporate" );
109 UserManager userManager = securitySystem.getUserManager();
110 User user = userManager.findUser( USER_ALPACA );
112 AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
114 SecuritySession session = new DefaultSecuritySession( result, user );
115 boolean isAuthorized =
116 servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
118 assertTrue( isAuthorized );
122 public void testIsAuthorizedUserHasNoWriteAccess()
125 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
127 assignRepositoryObserverRole( USER_ALPACA, "corporate" );
129 httpServletRequestControl.expectAndReturn( request.getRemoteAddr(), "192.168.111.111" );
131 UserManager userManager = securitySystem.getUserManager();
132 User user = userManager.findUser( USER_ALPACA );
134 AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
136 SecuritySession session = new DefaultSecuritySession( result, user );
138 httpServletRequestControl.replay();
142 servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
143 fail( "UnauthorizedException should have been thrown." );
145 catch ( UnauthorizedException e )
147 assertEquals( "Access denied for repository corporate", e.getMessage() );
150 httpServletRequestControl.verify();
154 public void testIsAuthorizedUserHasReadAccess()
157 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
159 assignRepositoryObserverRole( USER_ALPACA, "corporate" );
161 UserManager userManager = securitySystem.getUserManager();
162 User user = userManager.findUser( USER_ALPACA );
164 AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
166 SecuritySession session = new DefaultSecuritySession( result, user );
167 boolean isAuthorized =
168 servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
170 assertTrue( isAuthorized );
174 public void testIsAuthorizedUserHasNoReadAccess()
177 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
179 UserManager userManager = securitySystem.getUserManager();
180 User user = userManager.findUser( USER_ALPACA );
182 AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
184 SecuritySession session = new DefaultSecuritySession( result, user );
187 servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
188 fail( "UnauthorizedException should have been thrown." );
190 catch ( UnauthorizedException e )
192 assertEquals( "Access denied for repository corporate", e.getMessage() );
197 public void testIsAuthorizedGuestUserHasWriteAccess()
200 assignRepositoryManagerRole( USER_GUEST, "corporate" );
201 boolean isAuthorized =
202 servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
204 assertTrue( isAuthorized );
208 public void testIsAuthorizedGuestUserHasNoWriteAccess()
211 assignRepositoryObserverRole( USER_GUEST, "corporate" );
213 boolean isAuthorized =
214 servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
215 assertFalse( isAuthorized );
219 public void testIsAuthorizedGuestUserHasReadAccess()
222 assignRepositoryObserverRole( USER_GUEST, "corporate" );
224 boolean isAuthorized =
225 servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
227 assertTrue( isAuthorized );
231 public void testIsAuthorizedGuestUserHasNoReadAccess()
234 boolean isAuthorized =
235 servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
237 assertFalse( isAuthorized );