1 package org.apache.archiva.redback.authentication.users;
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 junit.framework.TestCase;
23 import org.apache.archiva.redback.authentication.Authenticator;
24 import org.apache.archiva.redback.users.User;
25 import org.apache.archiva.redback.users.UserManager;
26 import org.apache.archiva.redback.users.UserNotFoundException;
27 import org.apache.archiva.redback.authentication.AuthenticationException;
28 import org.apache.archiva.redback.authentication.AuthenticationResult;
29 import org.apache.archiva.redback.authentication.PasswordBasedAuthenticationDataSource;
30 import org.codehaus.plexus.redback.policy.AccountLockedException;
31 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
32 import org.codehaus.plexus.redback.policy.UserSecurityPolicy;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import java.util.Calendar;
42 import java.util.Date;
45 * Tests for {@link org.apache.archiva.redback.authentication.users.UserManagerAuthenticator} implementation.
47 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
49 @RunWith( SpringJUnit4ClassRunner.class )
50 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
51 public class UserManagerAuthenticatorTest
55 private UserSecurityPolicy userSecurityPolicy;
58 @Named(value = "authenticator#user-manager")
59 Authenticator component;
62 @Named(value = "userManager#memory")
70 userSecurityPolicy.setEnabled( false );
74 public void testLookup()
77 assertNotNull( component );
78 assertEquals( UserManagerAuthenticator.class.getName(), component.getClass().getName() );
82 public void testAuthenticate()
85 // Set up a few users for the Authenticator
87 User user = um.createUser( "test", "Test User", "testuser@somedomain.com" );
88 user.setPassword( "testpass" );
91 user = um.createUser( "guest", "Guest User", "testuser@somedomain.com" );
92 user.setPassword( "guestpass" );
95 user = um.createUser( "anonymous", "Anonymous User", "testuser@somedomain.com" );
96 user.setPassword( "nopass" );
99 // test with valid credentials
100 Authenticator auth = component;
101 assertNotNull( auth );
103 AuthenticationResult result = auth.authenticate( createAuthDataSource( "anonymous", "nopass" ) );
104 assertTrue( result.isAuthenticated() );
106 // test with invalid password
107 result = auth.authenticate( createAuthDataSource( "anonymous", "wrongpass" ) );
108 assertFalse( result.isAuthenticated() );
109 assertNull( result.getException() );
111 // test with unknown user
112 result = auth.authenticate( createAuthDataSource( "unknownuser", "wrongpass" ) );
113 assertFalse( result.isAuthenticated() );
114 assertNotNull( result.getException() );
115 assertEquals( result.getException().getClass().getName(), UserNotFoundException.class.getName() );
119 public void testAuthenticateLockedPassword()
120 throws AuthenticationException, MustChangePasswordException, UserNotFoundException
122 userSecurityPolicy.setEnabled( true );
124 // Set up a user for the Authenticator
125 User user = um.createUser( "testuser", "Test User Locked Password", "testuser@somedomain.com" );
126 user.setPassword( "correctpass1" );
127 user.setValidated( true );
128 user.setPasswordChangeRequired( false );
131 Authenticator auth = component;
132 assertNotNull( auth );
134 boolean hasException = false;
135 AuthenticationResult result = null;
139 // test password lock
140 for ( int i = 0; i < 11; i++ )
142 result = auth.authenticate( createAuthDataSource( "testuser", "wrongpass" ) );
145 catch ( AccountLockedException e )
151 assertNotNull( result );
152 assertFalse( result.isAuthenticated() );
153 assertTrue( hasException );
158 public void testAuthenticateExpiredPassword()
159 throws AuthenticationException, AccountLockedException, UserNotFoundException
161 userSecurityPolicy.setEnabled( true );
162 userSecurityPolicy.setPasswordExpirationDays( 15 );
164 // Set up a user for the Authenticator
165 User user = um.createUser( "testuser", "Test User Expired Password", "testuser@somedomain.com" );
166 user.setPassword( "expiredpass1" );
167 user.setValidated( true );
168 user.setPasswordChangeRequired( false );
171 Authenticator auth = component;
172 assertNotNull( auth );
174 boolean hasException = false;
178 // test successful authentication
179 AuthenticationResult result = auth.authenticate( createAuthDataSource( "testuser", "expiredpass1" ) );
180 assertTrue( result.isAuthenticated() );
182 // test expired password
183 user = um.findUser( "testuser" );
185 Calendar currentDate = Calendar.getInstance();
186 currentDate.set( Calendar.YEAR, currentDate.get( Calendar.YEAR ) - 1 );
187 Date lastPasswordChange = currentDate.getTime();
188 user.setLastPasswordChange( lastPasswordChange );
190 um.updateUser( user );
192 auth.authenticate( createAuthDataSource( "testuser", "expiredpass1" ) );
194 catch ( MustChangePasswordException e )
200 assertTrue( hasException );
204 private PasswordBasedAuthenticationDataSource createAuthDataSource( String username, String password )
206 PasswordBasedAuthenticationDataSource source = new PasswordBasedAuthenticationDataSource();
208 source.setPrincipal( username );
209 source.setPassword( password );