1   /* Copyright 2004, 2005 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  /***
28   * Tests {@link AuthByAdapterProvider}
29   *
30   * @author Ben Alex
31   * @version $Id: AuthByAdapterTests.java,v 1.7 2005/11/30 01:23:33 benalex Exp $
32   */
33  public class AuthByAdapterTests extends TestCase {
34      //~ Constructors ===========================================================
35  
36      public AuthByAdapterTests() {
37          super();
38      }
39  
40      public AuthByAdapterTests(String arg0) {
41          super(arg0);
42      }
43  
44      //~ Methods ================================================================
45  
46      public final void setUp() throws Exception {
47          super.setUp();
48      }
49  
50      public static void main(String[] args) {
51          junit.textui.TestRunner.run(AuthByAdapterTests.class);
52      }
53  
54      public void testAuthByAdapterProviderCorrectAuthenticationOperation()
55          throws Exception {
56          AuthByAdapterProvider provider = new AuthByAdapterProvider();
57          provider.setKey("my_password");
58  
59          PrincipalAcegiUserToken token = new PrincipalAcegiUserToken("my_password",
60                  "Test", "Password",
61                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
62                          "ROLE_TWO")}, null);
63          assertTrue(provider.supports(token.getClass()));
64  
65          Authentication response = provider.authenticate(token);
66          assertTrue(true);
67  
68          assertEquals(token.getCredentials(), response.getCredentials());
69          assertEquals(token.getPrincipal(), response.getPrincipal());
70          assertEquals(token.getAuthorities(), response.getAuthorities());
71  
72          if (!response.getClass().equals(token.getClass())) {
73              fail("Should have returned same type of object it was given");
74          }
75  
76          PrincipalAcegiUserToken castResponse = (PrincipalAcegiUserToken) response;
77          assertEquals(token.getName(), castResponse.getName());
78      }
79  
80      public void testAuthByAdapterProviderNonAuthenticationMethods()
81          throws Exception {
82          AuthByAdapterProvider provider = new AuthByAdapterProvider();
83  
84          try {
85              provider.afterPropertiesSet();
86              fail("Should have thrown IllegalArgumentException as key not set");
87          } catch (IllegalArgumentException expected) {
88              assertTrue(true);
89          }
90  
91          provider.setKey("my_password");
92          provider.afterPropertiesSet();
93          assertTrue(true);
94  
95          assertEquals("my_password", provider.getKey());
96      }
97  
98      public void testAuthByAdapterProviderOnlyAcceptsAuthByAdapterImplementations()
99          throws Exception {
100         AuthByAdapterProvider provider = new AuthByAdapterProvider();
101         provider.setKey("my_password");
102 
103         // Should fail as UsernamePassword is not interface of AuthByAdapter
104         UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test",
105                 "Password");
106 
107         assertTrue(!provider.supports(token.getClass()));
108 
109         try {
110             provider.authenticate(token);
111             fail(
112                 "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",
125                 "Test", "Password", null, null);
126 
127         try {
128             provider.authenticate(token);
129             fail("Should have thrown BadCredentialsException");
130         } catch (BadCredentialsException expected) {
131             assertTrue(true);
132         }
133     }
134 }