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.jetty;
017:
018: import junit.framework.TestCase;
019:
020: import org.mortbay.http.UserPrincipal;
021:
022: /**
023: * Tests {@link JettyAcegiUserRealm}.
024: *
025: * @author Ben Alex
026: * @version $Id: JettyAcegiUserRealmTests.java 1496 2006-05-23 13:38:33Z benalex $
027: */
028: public class JettyAcegiUserRealmTests extends TestCase {
029: //~ Instance fields ================================================================================================
030:
031: private final String ADAPTER_KEY = "my_key";
032: private final String REALM_NAME = "Acegi Powered Realm";
033:
034: //~ Constructors ===================================================================================================
035:
036: public JettyAcegiUserRealmTests() {
037: super ();
038: }
039:
040: public JettyAcegiUserRealmTests(String arg0) {
041: super (arg0);
042: }
043:
044: //~ Methods ========================================================================================================
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner.run(JettyAcegiUserRealmTests.class);
048: }
049:
050: private JettyAcegiUserRealm makeAdapter(String fileName)
051: throws Exception {
052: String useFile = "org/acegisecurity/adapters/" + fileName;
053:
054: return new JettyAcegiUserRealm(REALM_NAME, ADAPTER_KEY, useFile);
055: }
056:
057: public final void setUp() throws Exception {
058: super .setUp();
059: }
060:
061: public void testAdapterAbortsIfAppContextDoesNotContainAnAuthenticationBean()
062: throws Exception {
063: try {
064: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-invalid.xml");
065: fail("Should have thrown IllegalArgumentException");
066: } catch (IllegalArgumentException expected) {
067: assertEquals(
068: "Bean context must contain at least one bean of type AuthenticationManager",
069: expected.getMessage());
070: }
071: }
072:
073: public void testAdapterAbortsIfNoAppContextSpecified()
074: throws Exception {
075: try {
076: new JettyAcegiUserRealm(REALM_NAME, ADAPTER_KEY, null);
077: fail("Should have thrown IllegalArgumentException");
078: } catch (IllegalArgumentException expected) {
079: assertEquals("appContextLocation must be specified",
080: expected.getMessage());
081: }
082:
083: try {
084: new JettyAcegiUserRealm(REALM_NAME, ADAPTER_KEY, "");
085: fail("Should have thrown IllegalArgumentException");
086: } catch (IllegalArgumentException expected) {
087: assertEquals("appContextLocation must be specified",
088: expected.getMessage());
089: }
090: }
091:
092: public void testAdapterAbortsIfNoKeySpecified() throws Exception {
093: try {
094: new JettyAcegiUserRealm(REALM_NAME, null, "SOME_PATH");
095: fail("Should have thrown IllegalArgumentException");
096: } catch (IllegalArgumentException expected) {
097: assertEquals("key must be specified", expected.getMessage());
098: }
099:
100: try {
101: new JettyAcegiUserRealm(REALM_NAME, "", "SOME_PATH");
102: fail("Should have thrown IllegalArgumentException");
103: } catch (IllegalArgumentException expected) {
104: assertEquals("key must be specified", expected.getMessage());
105: }
106: }
107:
108: public void testAdapterAbortsIfNoRealmNameSpecified()
109: throws Exception {
110: try {
111: new JettyAcegiUserRealm(null, ADAPTER_KEY, "SOME_PATH");
112: fail("Should have thrown IllegalArgumentException");
113: } catch (IllegalArgumentException expected) {
114: assertEquals("realm must be specified", expected
115: .getMessage());
116: }
117:
118: try {
119: new JettyAcegiUserRealm(null, ADAPTER_KEY, "SOME_PATH");
120: fail("Should have thrown IllegalArgumentException");
121: } catch (IllegalArgumentException expected) {
122: assertEquals("realm must be specified", expected
123: .getMessage());
124: }
125: }
126:
127: public void testAdapterAbortsWithIncorrectApplicationContextLocation()
128: throws Exception {
129: try {
130: new JettyAcegiUserRealm(REALM_NAME, ADAPTER_KEY,
131: "SOME_INVALID_LOCATION");
132: fail("Should have thrown IllegalArgumentException");
133: } catch (IllegalArgumentException expected) {
134: assertTrue(expected.getMessage()
135: .startsWith("Cannot locate"));
136: }
137: }
138:
139: public void testAdapterIdentifiesTheRealmItManages()
140: throws Exception {
141: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
142: assertEquals(REALM_NAME, adapter.getName());
143: }
144:
145: public void testAdapterStartsUpSuccess() throws Exception {
146: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
147: assertTrue(true);
148: }
149:
150: public void testAuthenticationFailsForIncorrectPassword()
151: throws Exception {
152: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
153: assertEquals(null, adapter.authenticate("marissa", "kangaroo",
154: null));
155: }
156:
157: public void testAuthenticationFailsForIncorrectUserName()
158: throws Exception {
159: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
160: assertEquals(null, adapter.authenticate("melissa", "koala",
161: null));
162: }
163:
164: public void testAuthenticationSuccess() throws Exception {
165: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
166: UserPrincipal result = adapter.authenticate("marissa", "koala",
167: null);
168:
169: if (!(result instanceof JettyAcegiUserToken)) {
170: fail("Should have returned JettyAcegiUserToken");
171: }
172:
173: JettyAcegiUserToken castResult = (JettyAcegiUserToken) result;
174: assertEquals("marissa", castResult.getPrincipal());
175: assertEquals("koala", castResult.getCredentials());
176: assertEquals("ROLE_TELLER", castResult.getAuthorities()[0]
177: .getAuthority());
178: assertEquals("ROLE_SUPERVISOR", castResult.getAuthorities()[1]
179: .getAuthority());
180: assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
181: }
182:
183: public void testAuthenticationWithNullPasswordHandledGracefully()
184: throws Exception {
185: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
186: assertEquals(null, adapter.authenticate("marissa", null, null));
187: }
188:
189: public void testAuthenticationWithNullUserNameHandledGracefully()
190: throws Exception {
191: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
192: assertEquals(null, adapter.authenticate(null, "koala", null));
193: }
194:
195: public void testDisassociateImplemented() throws Exception {
196: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
197: adapter.disassociate(new MockUserPrincipal());
198: assertTrue(true);
199: }
200:
201: public void testGetAuthenticationManager() throws Exception {
202: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
203: assertTrue(adapter.getAuthenticationManager() != null);
204: }
205:
206: public void testLogoutImplemented() throws Exception {
207: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
208: adapter.logout(new MockUserPrincipal());
209: assertTrue(true);
210: }
211:
212: public void testNoArgsConstructor() {
213: try {
214: new JettyAcegiUserRealm();
215: fail("Should have thrown IllegalArgumentException");
216: } catch (IllegalArgumentException expected) {
217: assertTrue(true);
218: }
219: }
220:
221: public void testPopRoleImplemented() throws Exception {
222: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
223: MockUserPrincipal user = new MockUserPrincipal();
224: assertEquals(user, adapter.popRole(user));
225: }
226:
227: public void testPushRoleImplemented() throws Exception {
228: JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
229: MockUserPrincipal user = new MockUserPrincipal();
230: assertEquals(user, adapter.pushRole(user, "SOME_ROLE"));
231: }
232:
233: //~ Inner Classes ==================================================================================================
234:
235: private class MockUserPrincipal implements UserPrincipal {
236: public String getName() {
237: throw new UnsupportedOperationException(
238: "mock method not implemented");
239: }
240:
241: public boolean isAuthenticated() {
242: throw new UnsupportedOperationException(
243: "mock method not implemented");
244: }
245:
246: public boolean isUserInRole(String arg0) {
247: throw new UnsupportedOperationException(
248: "mock method not implemented");
249: }
250: }
251: }
|