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.resin;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.GrantedAuthority;
21  import org.acegisecurity.GrantedAuthorityImpl;
22  
23  import org.acegisecurity.adapters.PrincipalAcegiUserToken;
24  
25  import java.security.Principal;
26  
27  import javax.servlet.ServletException;
28  
29  
30  /***
31   * Tests {@link ResinAcegiAuthenticator}.
32   *
33   * @author Ben Alex
34   * @version $Id: ResinAcegiAuthenticatorTests.java,v 1.4 2005/11/25 00:26:30 benalex Exp $
35   */
36  public class ResinAcegiAuthenticatorTests extends TestCase {
37      //~ Instance fields ========================================================
38  
39      private final String ADAPTER_KEY = "my_key";
40  
41      //~ Constructors ===========================================================
42  
43      public ResinAcegiAuthenticatorTests() {
44          super();
45      }
46  
47      public ResinAcegiAuthenticatorTests(String arg0) {
48          super(arg0);
49      }
50  
51      //~ Methods ================================================================
52  
53      public final void setUp() throws Exception {
54          super.setUp();
55      }
56  
57      public static void main(String[] args) {
58          junit.textui.TestRunner.run(ResinAcegiAuthenticatorTests.class);
59      }
60  
61      public void testAdapterAbortsIfAppContextDoesNotContainAnAuthenticationBean()
62          throws Exception {
63          ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
64          adapter.setAppContextLocation(
65              "org/acegisecurity/adapters/adaptertest-invalid.xml");
66          adapter.setKey(ADAPTER_KEY);
67  
68          try {
69              adapter.init();
70              fail("Should have thrown ServletException");
71          } catch (ServletException expected) {
72              assertEquals("Bean context must contain at least one bean of type AuthenticationManager",
73                  expected.getMessage());
74          }
75      }
76  
77      public void testAdapterAbortsIfNoAppContextSpecified()
78          throws Exception {
79          ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
80          adapter.setKey(ADAPTER_KEY);
81  
82          try {
83              adapter.init();
84              fail("Should have thrown ServletException");
85          } catch (ServletException expected) {
86              assertEquals("appContextLocation must be defined",
87                  expected.getMessage());
88          }
89  
90          adapter.setAppContextLocation("");
91  
92          try {
93              adapter.init();
94              fail("Should have thrown ServletException");
95          } catch (ServletException expected) {
96              assertEquals("appContextLocation must be defined",
97                  expected.getMessage());
98          }
99      }
100 
101     public void testAdapterAbortsIfNoKeySpecified() throws Exception {
102         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
103         adapter.setAppContextLocation(
104             "org/acegisecurity/adapters/adaptertest-valid.xml");
105 
106         try {
107             adapter.init();
108             fail("Should have thrown ServletException");
109         } catch (ServletException expected) {
110             assertEquals("key must be defined", expected.getMessage());
111         }
112 
113         adapter.setKey("");
114 
115         try {
116             adapter.init();
117             fail("Should have thrown ServletException");
118         } catch (ServletException expected) {
119             assertEquals("key must be defined", expected.getMessage());
120         }
121     }
122 
123     public void testAdapterAbortsWithIncorrectApplicationContextLocation()
124         throws Exception {
125         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
126         adapter.setAppContextLocation("FILE_DOES_NOT_EXIST");
127         adapter.setKey(ADAPTER_KEY);
128 
129         try {
130             adapter.init();
131             fail("Should have thrown ServletException");
132         } catch (ServletException expected) {
133             assertTrue(expected.getMessage().startsWith("Cannot locate"));
134         }
135     }
136 
137     public void testAdapterStartsUpSuccess() throws Exception {
138         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
139         adapter.setAppContextLocation(
140             "org/acegisecurity/adapters/adaptertest-valid.xml");
141         adapter.setKey(ADAPTER_KEY);
142         adapter.init();
143         assertTrue(true);
144     }
145 
146     public void testAuthenticationFailsForIncorrectPassword()
147         throws Exception {
148         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
149         adapter.setAppContextLocation(
150             "org/acegisecurity/adapters/adaptertest-valid.xml");
151         adapter.setKey(ADAPTER_KEY);
152         adapter.init();
153         assertEquals(null, adapter.loginImpl("marissa", "kangaroo"));
154     }
155 
156     public void testAuthenticationFailsForIncorrectUserName()
157         throws Exception {
158         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
159         adapter.setAppContextLocation(
160             "org/acegisecurity/adapters/adaptertest-valid.xml");
161         adapter.setKey(ADAPTER_KEY);
162         adapter.init();
163         assertEquals(null, adapter.loginImpl("melissa", "koala"));
164     }
165 
166     public void testAuthenticationSuccess() throws Exception {
167         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
168         adapter.setAppContextLocation(
169             "org/acegisecurity/adapters/adaptertest-valid.xml");
170         adapter.setKey(ADAPTER_KEY);
171         adapter.init();
172 
173         Principal result = adapter.loginImpl("marissa", "koala");
174 
175         if (!(result instanceof PrincipalAcegiUserToken)) {
176             fail("Should have returned PrincipalAcegiUserToken");
177         }
178 
179         PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
180         assertEquals("marissa", castResult.getPrincipal());
181         assertEquals("koala", castResult.getCredentials());
182         assertEquals("ROLE_TELLER",
183             castResult.getAuthorities()[0].getAuthority());
184         assertEquals("ROLE_SUPERVISOR",
185             castResult.getAuthorities()[1].getAuthority());
186         assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
187     }
188 
189     public void testAuthenticationSuccessUsingAlternateMethod()
190         throws Exception {
191         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
192         adapter.setAppContextLocation(
193             "org/acegisecurity/adapters/adaptertest-valid.xml");
194         adapter.setKey(ADAPTER_KEY);
195         adapter.init();
196 
197         Principal result = adapter.loginImpl(null, null, null, "marissa",
198                 "koala");
199 
200         if (!(result instanceof PrincipalAcegiUserToken)) {
201             fail("Should have returned PrincipalAcegiUserToken");
202         }
203 
204         PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
205         assertEquals("marissa", castResult.getPrincipal());
206         assertEquals("koala", castResult.getCredentials());
207         assertEquals("ROLE_TELLER",
208             castResult.getAuthorities()[0].getAuthority());
209         assertEquals("ROLE_SUPERVISOR",
210             castResult.getAuthorities()[1].getAuthority());
211         assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
212     }
213 
214     public void testAuthenticationWithNullPasswordHandledGracefully()
215         throws Exception {
216         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
217         adapter.setAppContextLocation(
218             "org/acegisecurity/adapters/adaptertest-valid.xml");
219         adapter.setKey(ADAPTER_KEY);
220         adapter.init();
221         assertEquals(null, adapter.loginImpl("marissa", null));
222     }
223 
224     public void testAuthenticationWithNullUserNameHandledGracefully()
225         throws Exception {
226         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
227         adapter.setAppContextLocation(
228             "org/acegisecurity/adapters/adaptertest-valid.xml");
229         adapter.setKey(ADAPTER_KEY);
230         adapter.init();
231         assertEquals(null, adapter.loginImpl(null, "koala"));
232     }
233 
234     public void testGetters() throws Exception {
235         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
236         adapter.setAppContextLocation(
237             "org/acegisecurity/adapters/adaptertest-valid.xml");
238         adapter.setKey(ADAPTER_KEY);
239         assertEquals(ADAPTER_KEY, adapter.getKey());
240         assertEquals("org/acegisecurity/adapters/adaptertest-valid.xml",
241             adapter.getAppContextLocation());
242     }
243 
244     public void testHasRoleWithANullPrincipalFails() throws Exception {
245         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
246         adapter.setAppContextLocation(
247             "org/acegisecurity/adapters/adaptertest-valid.xml");
248         adapter.setKey(ADAPTER_KEY);
249         adapter.init();
250         assertTrue(!adapter.isUserInRole(null, null, null, null, "ROLE_ONE"));
251     }
252 
253     public void testHasRoleWithAPrincipalTheAdapterDidNotCreateFails()
254         throws Exception {
255         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
256         adapter.setAppContextLocation(
257             "org/acegisecurity/adapters/adaptertest-valid.xml");
258         adapter.setKey(ADAPTER_KEY);
259         adapter.init();
260         assertTrue(!adapter.isUserInRole(null, null, null,
261                 new Principal() {
262                 public String getName() {
263                     return "MockPrincipal";
264                 }
265             }, "ROLE_ONE"));
266     }
267 
268     public void testHasRoleWithPrincipalAcegiUserToken()
269         throws Exception {
270         PrincipalAcegiUserToken token = new PrincipalAcegiUserToken("KEY",
271                 "Test", "Password",
272                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
273                         "ROLE_TWO")}, null);
274         ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
275         adapter.setAppContextLocation(
276             "org/acegisecurity/adapters/adaptertest-valid.xml");
277         adapter.setKey(ADAPTER_KEY);
278         adapter.init();
279         assertTrue(adapter.isUserInRole(null, null, null, token, "ROLE_ONE"));
280         assertTrue(adapter.isUserInRole(null, null, null, token, "ROLE_ONE"));
281         assertTrue(!adapter.isUserInRole(null, null, null, token,
282                 "ROLE_WE_DO_NOT_HAVE"));
283     }
284 }