1   package com.atlassian.user.impl.delegation.security.authentication;
2   
3   import com.atlassian.user.impl.delegation.DelegatingUserManager;
4   import com.atlassian.user.impl.memory.MemoryUserManager;
5   import com.atlassian.user.impl.memory.provider.MemoryProvider;
6   import com.atlassian.user.impl.osuser.security.password.OSUPasswordEncryptor;
7   import com.atlassian.user.security.authentication.DefaultAuthenticator;
8   import com.atlassian.user.security.authentication.Authenticator;
9   import com.atlassian.user.EntityException;
10  import com.atlassian.user.User;
11  import com.atlassian.user.UserManager;
12  import com.atlassian.user.repository.DefaultRepositoryIdentifier;
13  
14  import java.util.Arrays;
15  
16  import junit.framework.TestCase;
17  
18  public class TestDelegatingAuthenticator extends TestCase
19  {
20      private Authenticator delegatingAuthenticator;
21      private Authenticator memoryAuthenticator1;
22      private Authenticator memoryAuthenticator2;
23  
24      private UserManager memoryUserManager1;
25      private UserManager memoryUserManager2;
26  
27      public void setUp() throws Exception
28      {
29          memoryUserManager1 = new MemoryUserManager(new DefaultRepositoryIdentifier("memory1", "Memory 1"), new MemoryProvider(), new OSUPasswordEncryptor());
30          memoryUserManager2 = new MemoryUserManager(new DefaultRepositoryIdentifier("memory2", "Memory 2"), new MemoryProvider(), new OSUPasswordEncryptor());
31  
32          memoryAuthenticator1 = new DefaultAuthenticator(memoryUserManager1, new OSUPasswordEncryptor());
33          memoryAuthenticator2 = new DefaultAuthenticator(memoryUserManager2, new OSUPasswordEncryptor());
34  
35          final DelegatingUserManager delegatingUserManager = new DelegatingUserManager(
36              Arrays.asList(new UserManager[]{memoryUserManager1, memoryUserManager2}));
37  
38          delegatingAuthenticator = new DelegatingAuthenticator(delegatingUserManager,
39              Arrays.asList(new Authenticator[]{memoryAuthenticator1, memoryAuthenticator2}));
40      }
41  
42      protected void tearDown() throws Exception
43      {
44          delegatingAuthenticator = null;
45          memoryAuthenticator1 = null;
46          memoryAuthenticator2 = null;
47          memoryUserManager1 = null;
48          memoryUserManager2 = null;
49      }
50  
51      public void testAuthenticationOccursOnCorrectDuplicateUser() throws EntityException
52      {
53          final String username = "test1";
54          final String password1 = "testing123";
55          final User firstUserInLine = memoryUserManager1.createUser(username);
56          memoryUserManager1.alterPassword(firstUserInLine, password1);
57  
58          final String password2 = "456omega";
59          final User secondUserInLine = memoryUserManager2.createUser(username);
60          memoryUserManager2.alterPassword(secondUserInLine, password2);
61  
62          assertTrue("Authentication should succeed", memoryAuthenticator1.authenticate(username, password1));
63          assertTrue("Authentication should succeed", memoryAuthenticator2.authenticate(username, password2));
64          assertTrue("Delegating Authentication should succeed with password1", delegatingAuthenticator.authenticate(username, password1));
65          assertFalse("Delegating Authentication should fail with password2", delegatingAuthenticator.authenticate(username, password2));
66      }
67  }