From 6c66418512f769f4f290f6c56e8c7d5412eabb3f Mon Sep 17 00:00:00 2001 From: Scott Battaglia Date: Tue, 14 Mar 2006 17:14:03 +0000 Subject: [PATCH] CAS-195 test cases for CasAuthenticationHandler --- .../cas3/CasAuthenticationHandlerTests.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 adapters/cas/src/test/java/org/acegisecurity/adapters/cas3/CasAuthenticationHandlerTests.java diff --git a/adapters/cas/src/test/java/org/acegisecurity/adapters/cas3/CasAuthenticationHandlerTests.java b/adapters/cas/src/test/java/org/acegisecurity/adapters/cas3/CasAuthenticationHandlerTests.java new file mode 100644 index 0000000000..b58c4867ba --- /dev/null +++ b/adapters/cas/src/test/java/org/acegisecurity/adapters/cas3/CasAuthenticationHandlerTests.java @@ -0,0 +1,77 @@ +package org.acegisecurity.adapters.cas3; + +import org.acegisecurity.AuthenticationManager; +import org.jasig.cas.authentication.handler.AuthenticationException; +import org.jasig.cas.authentication.principal.UsernamePasswordCredentials; +import org.springframework.test.AbstractDependencyInjectionSpringContextTests; + +/** + * Tests {@link CasAuthenticationHandler} + * @author Scott Battaglia + * @version $Id$ + * + */ +public class CasAuthenticationHandlerTests extends AbstractDependencyInjectionSpringContextTests { + + private AuthenticationManager authenticationManager; + + private CasAuthenticationHandler casAuthenticationHandler; + + public void setAuthenticationManager(final AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + protected String[] getConfigLocations() { + return new String[] {"/org/acegisecurity/adapters/cas/applicationContext-valid.xml"}; + } + + protected void onSetUp() throws Exception { + this.casAuthenticationHandler = new CasAuthenticationHandler(); + this.casAuthenticationHandler.setAuthenticationManager(authenticationManager); + this.casAuthenticationHandler.afterPropertiesSet(); + } + + public void testAfterPropertiesSet() throws Exception { + this.casAuthenticationHandler.setAuthenticationManager(null); + try { + this.casAuthenticationHandler.afterPropertiesSet(); + fail("IllegalArgumenException expected when no AuthenticationManager is set."); + } catch (final IllegalArgumentException e) { + // this is okay + } + } + + public void testValidUsernamePasswordCombination() { + try { + assertTrue(this.casAuthenticationHandler.authenticate(getCredentialsFor("scott", "wombat"))); + } catch (final AuthenticationException e) { + fail("AuthenticationException not expected."); + } + } + + public void testInvalidUsernamePasswordCombination() { + try { + assertFalse(this.casAuthenticationHandler.authenticate(getCredentialsFor("scott", "scott"))); + } catch (final AuthenticationException e) { + fail("AuthenticationException not expected."); + } + } + + public void testGracefullyHandlesInvalidInput() { + + try { + assertFalse(this.casAuthenticationHandler.authenticate(getCredentialsFor("", ""))); + assertFalse(this.casAuthenticationHandler.authenticate(getCredentialsFor(null, null))); + } catch (final AuthenticationException e) { + fail("AuthenticationException not expected."); + } + } + + private UsernamePasswordCredentials getCredentialsFor(final String username, final String password) { + final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(); + credentials.setUsername(username); + credentials.setPassword(password); + + return credentials; + } +}