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.cas3;
017:
018: import org.acegisecurity.AuthenticationManager;
019:
020: import org.jasig.cas.authentication.handler.AuthenticationException;
021: import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
022:
023: import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
024:
025: /**
026: * Tests {@link CasAuthenticationHandler}
027: *
028: * @author Scott Battaglia
029: * @version $Id: CasAuthenticationHandlerTests.java 1496 2006-05-23 13:38:33Z benalex $
030: */
031: public class CasAuthenticationHandlerTests extends
032: AbstractDependencyInjectionSpringContextTests {
033: //~ Instance fields ================================================================================================
034:
035: private AuthenticationManager authenticationManager;
036: private CasAuthenticationHandler casAuthenticationHandler;
037:
038: //~ Methods ========================================================================================================
039:
040: protected String[] getConfigLocations() {
041: return new String[] { "/org/acegisecurity/adapters/cas/applicationContext-valid.xml" };
042: }
043:
044: private UsernamePasswordCredentials getCredentialsFor(
045: final String username, final String password) {
046: final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials();
047: credentials.setUsername(username);
048: credentials.setPassword(password);
049:
050: return credentials;
051: }
052:
053: protected void onSetUp() throws Exception {
054: this .casAuthenticationHandler = new CasAuthenticationHandler();
055: this .casAuthenticationHandler
056: .setAuthenticationManager(authenticationManager);
057: this .casAuthenticationHandler.afterPropertiesSet();
058: }
059:
060: public void setAuthenticationManager(
061: final AuthenticationManager authenticationManager) {
062: this .authenticationManager = authenticationManager;
063: }
064:
065: public void testAfterPropertiesSet() throws Exception {
066: this .casAuthenticationHandler.setAuthenticationManager(null);
067:
068: try {
069: this .casAuthenticationHandler.afterPropertiesSet();
070: fail("IllegalArgumenException expected when no AuthenticationManager is set.");
071: } catch (final IllegalArgumentException e) {
072: // this is okay
073: }
074: }
075:
076: public void testGracefullyHandlesInvalidInput() {
077: try {
078: assertFalse(this .casAuthenticationHandler
079: .authenticate(getCredentialsFor("", "")));
080: assertFalse(this .casAuthenticationHandler
081: .authenticate(getCredentialsFor(null, null)));
082: } catch (final AuthenticationException e) {
083: fail("AuthenticationException not expected.");
084: }
085: }
086:
087: public void testInvalidUsernamePasswordCombination() {
088: try {
089: assertFalse(this .casAuthenticationHandler
090: .authenticate(getCredentialsFor("scott", "scott")));
091: } catch (final AuthenticationException e) {
092: fail("AuthenticationException not expected.");
093: }
094: }
095:
096: public void testValidUsernamePasswordCombination() {
097: try {
098: assertTrue(this .casAuthenticationHandler
099: .authenticate(getCredentialsFor("scott", "wombat")));
100: } catch (final AuthenticationException e) {
101: fail("AuthenticationException not expected.");
102: }
103: }
104: }
|