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.cas;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.MockAuthenticationManager;
021:
022: import org.springframework.mock.web.MockHttpServletRequest;
023:
024: /**
025: * Tests {@link CasPasswordHandler}.
026: *
027: * @author Ben Alex
028: * @version $Id: CasPasswordHandlerTests.java 1496 2006-05-23 13:38:33Z benalex $
029: */
030: public class CasPasswordHandlerTests extends TestCase {
031: //~ Constructors ===================================================================================================
032:
033: public CasPasswordHandlerTests() {
034: super ();
035: }
036:
037: public CasPasswordHandlerTests(String arg0) {
038: super (arg0);
039: }
040:
041: //~ Methods ========================================================================================================
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner.run(CasPasswordHandlerTests.class);
045: }
046:
047: public final void setUp() throws Exception {
048: super .setUp();
049: }
050:
051: public void testDeniesAccessWhenAuthenticationManagerThrowsException()
052: throws Exception {
053: CasPasswordHandler handler = new CasPasswordHandler();
054: handler.setAuthenticationManager(new MockAuthenticationManager(
055: false));
056: handler.afterPropertiesSet();
057:
058: assertFalse(handler.authenticate(new MockHttpServletRequest(),
059: "username", "password"));
060: }
061:
062: public void testDetectsEmptyAuthenticationManager()
063: throws Exception {
064: CasPasswordHandler handler = new CasPasswordHandler();
065:
066: try {
067: handler.afterPropertiesSet();
068: fail("Should have thrown IllegalArgumentException");
069: } catch (IllegalArgumentException expected) {
070: assertEquals("An AuthenticationManager is required",
071: expected.getMessage());
072: }
073: }
074:
075: public void testGettersSetters() {
076: CasPasswordHandler handler = new CasPasswordHandler();
077: handler.setAuthenticationManager(new MockAuthenticationManager(
078: false));
079: assertTrue(handler.getAuthenticationManager() != null);
080: }
081:
082: public void testGracefullyHandlesEmptyUsernamesAndPassword()
083: throws Exception {
084: CasPasswordHandler handler = new CasPasswordHandler();
085: handler.setAuthenticationManager(new MockAuthenticationManager(
086: true));
087: handler.afterPropertiesSet();
088:
089: // If empty or null username we return false
090: assertFalse(handler.authenticate(new MockHttpServletRequest(),
091: "", "password"));
092: assertFalse(handler.authenticate(new MockHttpServletRequest(),
093: null, "password"));
094:
095: // We authenticate with null passwords (they might not have one)
096: assertTrue(handler.authenticate(new MockHttpServletRequest(),
097: "user", null));
098: assertTrue(handler.authenticate(new MockHttpServletRequest(),
099: "user", ""));
100: }
101:
102: public void testNormalOperation() throws Exception {
103: CasPasswordHandler handler = new CasPasswordHandler();
104: handler.setAuthenticationManager(new MockAuthenticationManager(
105: true));
106: handler.afterPropertiesSet();
107:
108: assertTrue(handler.authenticate(new MockHttpServletRequest(),
109: "username", "password"));
110: }
111: }
|