1   package com.atlassian.user.impl.osuser.security.authentication;
2   
3   import junit.framework.TestCase;
4   import com.opensymphony.user.authenticator.AuthenticationException;
5   import com.atlassian.user.EntityException;
6   import com.atlassian.user.repository.DefaultRepositoryIdentifier;
7   
8   import javax.servlet.http.HttpServletRequest;
9   import java.util.Properties;
10  
11  public class TestOSUserAuthenticator extends TestCase
12  {
13      OSUAuthenticator authenticator;
14      TestAuthenticator wrappedAuthenticator;
15  
16      public void setUp() throws Exception
17      {
18          super.setUp();
19          wrappedAuthenticator = new TestAuthenticator("test1", "pass1");
20          DefaultRepositoryIdentifier identifier = new DefaultRepositoryIdentifier("osuser1", "OSUser repository");
21          authenticator = new OSUAuthenticator(identifier, wrappedAuthenticator);
22      }
23  
24      public void testAuthenticate() throws EntityException
25      {
26          boolean result = authenticator.authenticate("no", "way");
27          assertFalse(result);
28          wrappedAuthenticator.reset();
29          assertTrue(authenticator.authenticate("test1", "pass1"));
30      }
31  
32      class TestAuthenticator implements com.opensymphony.user.authenticator.Authenticator
33      {
34          String username;
35          String password;
36          boolean called;
37  
38          void reset()
39          {
40              called = false;
41          }
42  
43          public TestAuthenticator(String username, String password)
44          {
45              this.username = username;
46              this.password = password;
47          }
48  
49          public boolean login(String uid, String uid1) throws AuthenticationException
50          {
51              called = true;
52              return (this.username.equals(uid) && this.password.equals(uid1));
53          }
54  
55          public boolean init(Properties properties)
56          {
57              return false;
58          }
59  
60          public boolean login(String uid, String uid1, HttpServletRequest request) throws AuthenticationException
61          {
62              return false;
63          }
64      }
65  }