001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.adapters.resin;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: import org.acegisecurity.adapters.PrincipalAcegiUserToken;
024:
025: import java.security.Principal;
026:
027: import javax.servlet.ServletException;
028:
029: /**
030: * Tests {@link ResinAcegiAuthenticator}.
031: *
032: * @author Ben Alex
033: * @version $Id: ResinAcegiAuthenticatorTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class ResinAcegiAuthenticatorTests extends TestCase {
036: //~ Instance fields ================================================================================================
037:
038: private final String ADAPTER_KEY = "my_key";
039:
040: //~ Constructors ===================================================================================================
041:
042: public ResinAcegiAuthenticatorTests() {
043: super ();
044: }
045:
046: public ResinAcegiAuthenticatorTests(String arg0) {
047: super (arg0);
048: }
049:
050: //~ Methods ========================================================================================================
051:
052: public static void main(String[] args) {
053: junit.textui.TestRunner.run(ResinAcegiAuthenticatorTests.class);
054: }
055:
056: public final void setUp() throws Exception {
057: super .setUp();
058: }
059:
060: public void testAdapterAbortsIfAppContextDoesNotContainAnAuthenticationBean()
061: throws Exception {
062: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
063: adapter
064: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-invalid.xml");
065: adapter.setKey(ADAPTER_KEY);
066:
067: try {
068: adapter.init();
069: fail("Should have thrown ServletException");
070: } catch (ServletException expected) {
071: assertEquals(
072: "Bean context must contain at least one bean of type AuthenticationManager",
073: expected.getMessage());
074: }
075: }
076:
077: public void testAdapterAbortsIfNoAppContextSpecified()
078: throws Exception {
079: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
080: adapter.setKey(ADAPTER_KEY);
081:
082: try {
083: adapter.init();
084: fail("Should have thrown ServletException");
085: } catch (ServletException expected) {
086: assertEquals("appContextLocation must be defined", expected
087: .getMessage());
088: }
089:
090: adapter.setAppContextLocation("");
091:
092: try {
093: adapter.init();
094: fail("Should have thrown ServletException");
095: } catch (ServletException expected) {
096: assertEquals("appContextLocation must be defined", expected
097: .getMessage());
098: }
099: }
100:
101: public void testAdapterAbortsIfNoKeySpecified() throws Exception {
102: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
103: adapter
104: .setAppContextLocation("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()
134: .startsWith("Cannot locate"));
135: }
136: }
137:
138: public void testAdapterStartsUpSuccess() throws Exception {
139: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
140: adapter
141: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
142: adapter.setKey(ADAPTER_KEY);
143: adapter.init();
144: assertTrue(true);
145: }
146:
147: public void testAuthenticationFailsForIncorrectPassword()
148: throws Exception {
149: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
150: adapter
151: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
152: adapter.setKey(ADAPTER_KEY);
153: adapter.init();
154: assertEquals(null, adapter.loginImpl("marissa", "kangaroo"));
155: }
156:
157: public void testAuthenticationFailsForIncorrectUserName()
158: throws Exception {
159: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
160: adapter
161: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
162: adapter.setKey(ADAPTER_KEY);
163: adapter.init();
164: assertEquals(null, adapter.loginImpl("melissa", "koala"));
165: }
166:
167: public void testAuthenticationSuccess() throws Exception {
168: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
169: adapter
170: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
171: adapter.setKey(ADAPTER_KEY);
172: adapter.init();
173:
174: Principal result = adapter.loginImpl("marissa", "koala");
175:
176: if (!(result instanceof PrincipalAcegiUserToken)) {
177: fail("Should have returned PrincipalAcegiUserToken");
178: }
179:
180: PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
181: assertEquals("marissa", castResult.getPrincipal());
182: assertEquals("koala", castResult.getCredentials());
183: assertEquals("ROLE_TELLER", castResult.getAuthorities()[0]
184: .getAuthority());
185: assertEquals("ROLE_SUPERVISOR", castResult.getAuthorities()[1]
186: .getAuthority());
187: assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
188: }
189:
190: public void testAuthenticationSuccessUsingAlternateMethod()
191: throws Exception {
192: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
193: adapter
194: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
195: adapter.setKey(ADAPTER_KEY);
196: adapter.init();
197:
198: Principal result = adapter.loginImpl(null, null, null,
199: "marissa", "koala");
200:
201: if (!(result instanceof PrincipalAcegiUserToken)) {
202: fail("Should have returned PrincipalAcegiUserToken");
203: }
204:
205: PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
206: assertEquals("marissa", castResult.getPrincipal());
207: assertEquals("koala", castResult.getCredentials());
208: assertEquals("ROLE_TELLER", castResult.getAuthorities()[0]
209: .getAuthority());
210: assertEquals("ROLE_SUPERVISOR", castResult.getAuthorities()[1]
211: .getAuthority());
212: assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
213: }
214:
215: public void testAuthenticationWithNullPasswordHandledGracefully()
216: throws Exception {
217: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
218: adapter
219: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
220: adapter.setKey(ADAPTER_KEY);
221: adapter.init();
222: assertEquals(null, adapter.loginImpl("marissa", null));
223: }
224:
225: public void testAuthenticationWithNullUserNameHandledGracefully()
226: throws Exception {
227: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
228: adapter
229: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
230: adapter.setKey(ADAPTER_KEY);
231: adapter.init();
232: assertEquals(null, adapter.loginImpl(null, "koala"));
233: }
234:
235: public void testGetters() throws Exception {
236: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
237: adapter
238: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
239: adapter.setKey(ADAPTER_KEY);
240: assertEquals(ADAPTER_KEY, adapter.getKey());
241: assertEquals(
242: "org/acegisecurity/adapters/adaptertest-valid.xml",
243: adapter.getAppContextLocation());
244: }
245:
246: public void testHasRoleWithANullPrincipalFails() throws Exception {
247: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
248: adapter
249: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
250: adapter.setKey(ADAPTER_KEY);
251: adapter.init();
252: assertTrue(!adapter.isUserInRole(null, null, null, null,
253: "ROLE_ONE"));
254: }
255:
256: public void testHasRoleWithAPrincipalTheAdapterDidNotCreateFails()
257: throws Exception {
258: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
259: adapter
260: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
261: adapter.setKey(ADAPTER_KEY);
262: adapter.init();
263: assertTrue(!adapter.isUserInRole(null, null, null,
264: new Principal() {
265: public String getName() {
266: return "MockPrincipal";
267: }
268: }, "ROLE_ONE"));
269: }
270:
271: public void testHasRoleWithPrincipalAcegiUserToken()
272: throws Exception {
273: PrincipalAcegiUserToken token = new PrincipalAcegiUserToken(
274: "KEY", "Test", "Password", new GrantedAuthority[] {
275: new GrantedAuthorityImpl("ROLE_ONE"),
276: new GrantedAuthorityImpl("ROLE_TWO") }, null);
277: ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
278: adapter
279: .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
280: adapter.setKey(ADAPTER_KEY);
281: adapter.init();
282: assertTrue(adapter.isUserInRole(null, null, null, token,
283: "ROLE_ONE"));
284: assertTrue(adapter.isUserInRole(null, null, null, token,
285: "ROLE_ONE"));
286: assertTrue(!adapter.isUserInRole(null, null, null, token,
287: "ROLE_WE_DO_NOT_HAVE"));
288: }
289: }
|