    1   package org.acegisecurity.ui.webapp;
    2
    3   import junit.framework.TestCase;
    4   import org.acegisecurity.Authentication;
    5   import org.acegisecurity.MockAuthenticationManager;
    6   import org.acegisecurity.ui.WebAuthenticationDetails;
    7
    8   import org.springframework.mock.web.MockHttpServletRequest;
    9
    10  /***
    11   * Tests SiteminderAuthenticationProcessingFilter.
    12   *
    13   * @author Ben Alex
    14   * @author <a href="mailto:scott@mccrory.us">Scott McCrory</a>
    15   * @version CVS $Id: SiteminderAuthenticationProcessingFilterTests.java,v 1.1
    16   *          2005/09/25 22:48:33 smccrory Exp $
    17   */
    18  public class SiteminderAuthenticationProcessingFilterTests extends TestCase {
    19
    20  	/***
    21  	 * Basic constructor.
    22  	 */
    23  	public SiteminderAuthenticationProcessingFilterTests() {
    24  		super();
    25  	}
    26
    27  	/***
    28  	 * Argument constructor.
    29  	 *
    30  	 * @param arg0
    31  	 */
    32  	public SiteminderAuthenticationProcessingFilterTests(String arg0) {
    33  		super(arg0);
    34  	}
    35
    36  	/***
    37  	 * @see junit.framework.TestCase#setUp()
    38  	 */
    39  	public final void setUp() throws Exception {
    40  		super.setUp();
    41  	}
    42
    43  	/***
    44  	 * Runs the tests as a command-line program.
    45  	 *
    46  	 * @param args
    47  	 */
    48  	public static void main(String[] args) {
    49  		junit.textui.TestRunner
    50  				.run(SiteminderAuthenticationProcessingFilterTests.class);
    51  	}
    52
    53  	/***
    54  	 * Tests the class' getters.
    55  	 */
    56  	public void testAccessors() {
    57
    58  		SiteminderAuthenticationProcessingFilter filter = new SiteminderAuthenticationProcessingFilter();
    59
    60  		filter.setAlwaysUseDefaultTargetUrl(true);
    61  		assertTrue(filter.isAlwaysUseDefaultTargetUrl());
    62
    63  		filter.setAuthenticationFailureUrl("foo");
    64  		assertEquals("foo", filter.getAuthenticationFailureUrl());
    65
    66  		filter.setContinueChainBeforeSuccessfulAuthentication(true);
    67  		assertTrue(filter.isContinueChainBeforeSuccessfulAuthentication());
    68
    69  		filter.setDefaultTargetUrl("bar");
    70  		assertEquals("bar", filter.getDefaultTargetUrl());
    71
    72  		filter.setFilterProcessesUrl("foobar");
    73  		assertEquals("foobar", filter.getFilterProcessesUrl());
    74
    75  		filter.setFormPasswordParameterKey("passwordParamKey");
    76  		assertEquals("passwordParamKey", filter.getFormPasswordParameterKey());
    77
    78  		filter.setFormUsernameParameterKey("usernameParamKey");
    79  		assertEquals("usernameParamKey", filter.getFormUsernameParameterKey());
    80
    81  		filter.setSiteminderPasswordHeaderKey("passwordHeaderKey");
    82  		assertEquals("passwordHeaderKey", filter.getSiteminderPasswordHeaderKey());
    83
    84  		filter.setSiteminderUsernameHeaderKey("usernameHeaderKey");
    85  		assertEquals("usernameHeaderKey", filter.getSiteminderUsernameHeaderKey());
    86
    87  	}
    88
    89  	/***
    90  	 * Tests normal form processing.
    91  	 *
    92  	 * @throws Exception
    93  	 */
    94  	public void testFormNormalOperation() throws Exception {
    95
    96  		MockHttpServletRequest request = new MockHttpServletRequest();
    97  		request
    98  				.addParameter(
    99  						SiteminderAuthenticationProcessingFilter.ACEGI_SECURITY_FORM_USERNAME_KEY,
    100 						"marissa");
    101 		request
    102 				.addParameter(
    103 						SiteminderAuthenticationProcessingFilter.ACEGI_SECURITY_FORM_PASSWORD_KEY,
    104 						"koala");
    105
    106 		MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
    107
    108 		SiteminderAuthenticationProcessingFilter filter = new SiteminderAuthenticationProcessingFilter();
    109 		filter.setAuthenticationManager(authMgr);
    110 		filter.init(null);
    111
    112 		Authentication result = filter.attemptAuthentication(request);
    113 		assertTrue(result != null);
    114 		assertEquals("127.0.0.1", ((WebAuthenticationDetails) result
    115 				.getDetails()).getRemoteAddress());
    116
    117 	}
    118
    119 	/***
    120 	 * Tests form null password handling.
    121 	 *
    122 	 * @throws Exception
    123 	 */
    124 	public void testFormNullPasswordHandledGracefully() throws Exception {
    125
    126 		MockHttpServletRequest request = new MockHttpServletRequest();
    127 		request
    128 				.addParameter(
    129 						SiteminderAuthenticationProcessingFilter.ACEGI_SECURITY_FORM_USERNAME_KEY,
    130 						"marissa");
    131
    132 		MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
    133
    134 		SiteminderAuthenticationProcessingFilter filter = new SiteminderAuthenticationProcessingFilter();
    135 		filter.setAuthenticationManager(authMgr);
    136 		filter.init(null);
    137
    138 		Authentication result = filter.attemptAuthentication(request);
    139 		assertTrue(result != null);
    140
    141 	}
    142
    143 	/***
    144 	 * Tests form null username handling.
    145 	 *
    146 	 * @throws Exception
    147 	 */
    148 	public void testFormNullUsernameHandledGracefully() throws Exception {
    149
    150 		MockHttpServletRequest request = new MockHttpServletRequest();
    151 		request
    152 				.addParameter(
    153 						SiteminderAuthenticationProcessingFilter.ACEGI_SECURITY_FORM_PASSWORD_KEY,
    154 						"koala");
    155
    156 		MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
    157
    158 		SiteminderAuthenticationProcessingFilter filter = new SiteminderAuthenticationProcessingFilter();
    159 		filter.setAuthenticationManager(authMgr);
    160 		filter.init(null);
    161
    162 		Authentication result = filter.attemptAuthentication(request);
    163 		assertTrue(result != null);
    164
    165 	}
    166
    167 	/***
    168 	 * Tests normal Siteminder header processing.
    169 	 *
    170 	 * @throws Exception
    171 	 */
    172 	public void testSiteminderNormalOperation() throws Exception {
    173
    174 		MockHttpServletRequest request = new MockHttpServletRequest();
    175 		request.addHeader("SM_USER", "E099544");
    176
    177 		MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
    178
    179 		SiteminderAuthenticationProcessingFilter filter = new SiteminderAuthenticationProcessingFilter();
    180 		filter.setAuthenticationManager(authMgr);
    181 		filter.setSiteminderUsernameHeaderKey("SM_USER");
    182 		filter.setSiteminderPasswordHeaderKey("SM_USER");
    183 		filter.init(null);
    184
    185 		Authentication result = filter.attemptAuthentication(request);
    186 		assertTrue(result != null);
    187 		assertEquals("127.0.0.1", ((WebAuthenticationDetails) result
    188 				.getDetails()).getRemoteAddress());
    189
    190 	}
    191
    192 }