1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.adapters;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.Authentication;
21  import org.acegisecurity.BadCredentialsException;
22  import org.acegisecurity.GrantedAuthority;
23  import org.acegisecurity.GrantedAuthorityImpl;
24  
25  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
26  
27  import java.util.Arrays;
28  
29  
30  /**
31   * Tests {@link AuthByAdapterProvider}
32   *
33   * @author Ben Alex
34   * @version $Id: AuthByAdapterTests.java 1496 2006-05-23 13:38:33Z benalex $
35   */
36  public class AuthByAdapterTests extends TestCase {
37      //~ Constructors ===================================================================================================
38  
39      public AuthByAdapterTests() {
40          super();
41      }
42  
43      public AuthByAdapterTests(String arg0) {
44          super(arg0);
45      }
46  
47      //~ Methods ========================================================================================================
48  
49      public static void main(String[] args) {
50          junit.textui.TestRunner.run(AuthByAdapterTests.class);
51      }
52  
53      public final void setUp() throws Exception {
54          super.setUp();
55      }
56  
57      public void testAuthByAdapterProviderCorrectAuthenticationOperation()
58          throws Exception {
59          AuthByAdapterProvider provider = new AuthByAdapterProvider();
60          provider.setKey("my_password");
61  
62          PrincipalAcegiUserToken token = new PrincipalAcegiUserToken("my_password", "Test", "Password",
63                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
64                  null);
65          assertTrue(provider.supports(token.getClass()));
66  
67          Authentication response = provider.authenticate(token);
68          assertTrue(true);
69  
70          assertEquals(token.getCredentials(), response.getCredentials());
71          assertEquals(token.getPrincipal(), response.getPrincipal());
72          assertTrue(Arrays.equals(token.getAuthorities(), response.getAuthorities()));
73  
74          if (!response.getClass().equals(token.getClass())) {
75              fail("Should have returned same type of object it was given");
76          }
77  
78          PrincipalAcegiUserToken castResponse = (PrincipalAcegiUserToken) response;
79          assertEquals(token.getName(), castResponse.getName());
80      }
81  
82      public void testAuthByAdapterProviderNonAuthenticationMethods()
83          throws Exception {
84          AuthByAdapterProvider provider = new AuthByAdapterProvider();
85  
86          try {
87              provider.afterPropertiesSet();
88              fail("Should have thrown IllegalArgumentException as key not set");
89          } catch (IllegalArgumentException expected) {
90              assertTrue(true);
91          }
92  
93          provider.setKey("my_password");
94          provider.afterPropertiesSet();
95          assertTrue(true);
96  
97          assertEquals("my_password", provider.getKey());
98      }
99  
100     public void testAuthByAdapterProviderOnlyAcceptsAuthByAdapterImplementations()
101         throws Exception {
102         AuthByAdapterProvider provider = new AuthByAdapterProvider();
103         provider.setKey("my_password");
104 
105         // Should fail as UsernamePassword is not interface of AuthByAdapter
106         UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password");
107 
108         assertTrue(!provider.supports(token.getClass()));
109 
110         try {
111             provider.authenticate(token);
112             fail("Should have thrown ClassCastException (supports() false response was ignored)");
113         } catch (ClassCastException expected) {
114             assertTrue(true);
115         }
116     }
117 
118     public void testAuthByAdapterProviderRequiresCorrectKey()
119         throws Exception {
120         AuthByAdapterProvider provider = new AuthByAdapterProvider();
121         provider.setKey("my_password");
122 
123         // Should fail as PrincipalAcegiUserToken has different key
124         PrincipalAcegiUserToken token = new PrincipalAcegiUserToken("wrong_password", "Test", "Password", null, null);
125 
126         try {
127             provider.authenticate(token);
128             fail("Should have thrown BadCredentialsException");
129         } catch (BadCredentialsException expected) {
130             assertTrue(true);
131         }
132     }
133 }