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.servlet.http.HttpServletRequest;
24 import org.codehaus.plexus.redback.authentication.AuthenticationException;
25 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
26 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
27 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
28 import org.codehaus.plexus.redback.system.SecuritySession;
29 import org.codehaus.plexus.redback.users.User;
30 import org.codehaus.plexus.redback.users.UserManager;
32 import org.easymock.MockControl;
35 * ArchivaServletAuthenticatorTest
39 public class ArchivaServletAuthenticatorTest
40 extends AbstractSecurityTest
42 private ServletAuthenticator servletAuth;
44 private MockControl httpServletRequestControl;
46 private HttpServletRequest request;
54 servletAuth = ( ServletAuthenticator ) lookup( ServletAuthenticator.class, "default" );
56 httpServletRequestControl = MockControl.createControl( HttpServletRequest.class );
57 request = ( HttpServletRequest ) httpServletRequestControl.getMock();
59 setupRepository( "corporate" );
63 protected String getPlexusConfigLocation()
65 return "org/apache/maven/archiva/security/ArchivaServletAuthenticatorTest.xml";
68 protected void assignRepositoryManagerRole( String principal, String repoId )
71 roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId, principal );
74 public void testIsAuthenticatedUserExists()
77 AuthenticationResult result = new AuthenticationResult( true, "user", null );
78 boolean isAuthenticated = servletAuth.isAuthenticated( request, result );
80 assertTrue( isAuthenticated );
83 public void testIsAuthenticatedUserDoesNotExist()
86 AuthenticationResult result = new AuthenticationResult( false, "non-existing-user", null );
89 servletAuth.isAuthenticated( request, result );
90 fail( "Authentication exception should have been thrown." );
92 catch ( AuthenticationException e )
94 assertEquals( "User Credentials Invalid", e.getMessage() );
98 public void testIsAuthorizedUserHasWriteAccess()
101 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
103 assignRepositoryManagerRole( USER_ALPACA, "corporate" );
105 UserManager userManager = securitySystem.getUserManager();
106 User user = userManager.findUser( USER_ALPACA );
108 AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
110 SecuritySession session = new DefaultSecuritySession( result, user );
111 boolean isAuthorized = servletAuth.isAuthorized( request, session, "corporate", true );
113 assertTrue( isAuthorized );
116 public void testIsAuthorizedUserHasNoWriteAccess()
119 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
121 assignRepositoryObserverRole( USER_ALPACA, "corporate" );
123 httpServletRequestControl.expectAndReturn( request.getRemoteAddr(), "192.168.111.111" );
125 UserManager userManager = securitySystem.getUserManager();
126 User user = userManager.findUser( USER_ALPACA );
128 AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
130 SecuritySession session = new DefaultSecuritySession( result, user );
132 httpServletRequestControl.replay();
136 servletAuth.isAuthorized( request, session, "corporate", true );
137 fail( "UnauthorizedException should have been thrown." );
139 catch ( UnauthorizedException e )
141 assertEquals( "Access denied for repository corporate", e.getMessage() );
144 httpServletRequestControl.verify();
148 public void testIsAuthorizedUserHasReadAccess()
151 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
153 assignRepositoryObserverRole( USER_ALPACA, "corporate" );
155 UserManager userManager = securitySystem.getUserManager();
156 User user = userManager.findUser( USER_ALPACA );
158 AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
160 SecuritySession session = new DefaultSecuritySession( result, user );
161 boolean isAuthorized = servletAuth.isAuthorized( request, session, "corporate", false );
163 assertTrue( isAuthorized );
166 public void testIsAuthorizedUserHasNoReadAccess()
169 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
171 UserManager userManager = securitySystem.getUserManager();
172 User user = userManager.findUser( USER_ALPACA );
174 AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
176 SecuritySession session = new DefaultSecuritySession( result, user );
179 servletAuth.isAuthorized( request, session, "corporate", false );
180 fail( "UnauthorizedException should have been thrown." );
182 catch ( UnauthorizedException e )
184 assertEquals( "Access denied for repository corporate", e.getMessage() );
188 public void testIsAuthorizedGuestUserHasWriteAccess()
191 assignRepositoryManagerRole( USER_GUEST, "corporate" );
192 boolean isAuthorized = servletAuth.isAuthorized( USER_GUEST, "corporate", true );
194 assertTrue( isAuthorized );
197 public void testIsAuthorizedGuestUserHasNoWriteAccess()
200 assignRepositoryObserverRole( USER_GUEST, "corporate" );
202 boolean isAuthorized = servletAuth.isAuthorized( USER_GUEST, "corporate", true );
203 assertFalse( isAuthorized );
206 public void testIsAuthorizedGuestUserHasReadAccess()
209 assignRepositoryObserverRole( USER_GUEST, "corporate" );
211 boolean isAuthorized = servletAuth.isAuthorized( USER_GUEST, "corporate", false );
213 assertTrue( isAuthorized );
216 public void testIsAuthorizedGuestUserHasNoReadAccess()
219 boolean isAuthorized = servletAuth.isAuthorized( USER_GUEST, "corporate", false );
221 assertFalse( isAuthorized );