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.captcha;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.context.SecurityContextHolder;
021:
022: import org.acegisecurity.util.MockFilterChain;
023:
024: import org.springframework.mock.web.MockHttpServletRequest;
025:
026: /**
027: * Tests {@link CaptchaValidationProcessingFilter}.
028: *
029: * @author marc antoine Garrigue
030: * @version $Id: CaptchaValidationProcessingFilterTests.java 1496 2006-05-23 13:38:33Z benalex $
031: */
032: public class CaptchaValidationProcessingFilterTests extends TestCase {
033: //~ Methods ========================================================================================================
034:
035: /*
036: */
037: public void testAfterPropertiesSet() throws Exception {
038: CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
039:
040: try {
041: filter.afterPropertiesSet();
042: fail("should have thrown an invalid argument exception");
043: } catch (Exception e) {
044: assertTrue("should be an InvalidArgumentException",
045: IllegalArgumentException.class.isAssignableFrom(e
046: .getClass()));
047: }
048:
049: filter.setCaptchaService(new MockCaptchaServiceProxy());
050: filter.afterPropertiesSet();
051: filter.setCaptchaValidationParameter(null);
052:
053: try {
054: filter.afterPropertiesSet();
055: fail("should have thrown an invalid argument exception");
056: } catch (Exception e) {
057: assertTrue("should be an InvalidArgumentException",
058: IllegalArgumentException.class.isAssignableFrom(e
059: .getClass()));
060: }
061: }
062:
063: /*
064: * Test method for
065: * 'org.acegisecurity.captcha.CaptchaValidationProcessingFilter.doFilter(ServletRequest,
066: * ServletResponse, FilterChain)'
067: */
068: public void testDoFilterWithRequestParameter() throws Exception {
069: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
070: SecurityContextHolder.setContext(context);
071:
072: MockHttpServletRequest request = new MockHttpServletRequest();
073:
074: CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
075: request
076: .addParameter(filter.getCaptchaValidationParameter(),
077: "");
078:
079: MockCaptchaServiceProxy service = new MockCaptchaServiceProxy();
080: MockFilterChain chain = new MockFilterChain(true);
081: filter.setCaptchaService(service);
082: filter.doFilter(request, null, chain);
083: assertTrue("should have been called", service.hasBeenCalled);
084: assertFalse("context should not have been updated", context
085: .isHuman());
086:
087: // test with valid
088: service.valid = true;
089: filter.doFilter(request, null, chain);
090: assertTrue("should have been called", service.hasBeenCalled);
091: assertTrue("context should have been updated", context
092: .isHuman());
093: }
094:
095: /*
096: * Test method for
097: * 'org.acegisecurity.captcha.CaptchaValidationProcessingFilter.doFilter(ServletRequest,
098: * ServletResponse, FilterChain)'
099: */
100: public void testDoFilterWithoutRequestParameter() throws Exception {
101: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
102: SecurityContextHolder.setContext(context);
103:
104: MockHttpServletRequest request = new MockHttpServletRequest();
105: CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
106: MockCaptchaServiceProxy service = new MockCaptchaServiceProxy();
107: MockFilterChain chain = new MockFilterChain(true);
108: filter.setCaptchaService(service);
109: filter.doFilter(request, null, chain);
110: assertFalse("proxy should not have been called",
111: service.hasBeenCalled);
112: assertFalse("context should not have been updated", context
113: .isHuman());
114:
115: // test with valid
116: service.valid = true;
117: filter.doFilter(request, null, chain);
118: assertFalse("proxy should not have been called",
119: service.hasBeenCalled);
120: assertFalse("context should not have been updated", context
121: .isHuman());
122: }
123: }
|