1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.captcha;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.context.SecurityContextHolder;
21 import org.acegisecurity.util.MockFilterChain;
22
23 import org.springframework.mock.web.MockHttpServletRequest;
24
25
26 /***
27 * Tests {@link CaptchaValidationProcessingFilter}.
28 *
29 * @author marc antoine Garrigue
30 * @version $Id: CaptchaValidationProcessingFilterTests.java,v 1.4 2005/11/17 00:56:08 benalex Exp $
31 */
32 public class CaptchaValidationProcessingFilterTests extends TestCase {
33
34
35
36
37 public void testAfterPropertiesSet() throws Exception {
38 CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
39
40 try {
41 filter.afterPropertiesSet();
42 fail("should have thrown an invalid argument exception");
43 } catch (Exception e) {
44 assertTrue("should be an InvalidArgumentException",
45 IllegalArgumentException.class.isAssignableFrom(e.getClass()));
46 }
47
48 filter.setCaptchaService(new MockCaptchaServiceProxy());
49 filter.afterPropertiesSet();
50 filter.setCaptchaValidationParameter(null);
51
52 try {
53 filter.afterPropertiesSet();
54 fail("should have thrown an invalid argument exception");
55 } catch (Exception e) {
56 assertTrue("should be an InvalidArgumentException",
57 IllegalArgumentException.class.isAssignableFrom(e.getClass()));
58 }
59 }
60
61
62
63
64
65
66 public void testDoFilterWithRequestParameter() throws Exception {
67 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
68 SecurityContextHolder.setContext(context);
69
70 MockHttpServletRequest request = new MockHttpServletRequest();
71
72 CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
73 request.addParameter(filter.getCaptchaValidationParameter(), "");
74
75 MockCaptchaServiceProxy service = new MockCaptchaServiceProxy();
76 MockFilterChain chain = new MockFilterChain(true);
77 filter.setCaptchaService(service);
78 filter.doFilter(request, null, chain);
79 assertTrue("should have been called", service.hasBeenCalled);
80 assertFalse("context should not have been updated", context.isHuman());
81
82
83 service.valid = true;
84 filter.doFilter(request, null, chain);
85 assertTrue("should have been called", service.hasBeenCalled);
86 assertTrue("context should have been updated", context.isHuman());
87 }
88
89
90
91
92
93
94 public void testDoFilterWithoutRequestParameter() throws Exception {
95 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
96 SecurityContextHolder.setContext(context);
97
98 MockHttpServletRequest request = new MockHttpServletRequest();
99 CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
100 MockCaptchaServiceProxy service = new MockCaptchaServiceProxy();
101 MockFilterChain chain = new MockFilterChain(true);
102 filter.setCaptchaService(service);
103 filter.doFilter(request, null, chain);
104 assertFalse("proxy should not have been called", service.hasBeenCalled);
105 assertFalse("context should not have been updated", context.isHuman());
106
107
108 service.valid = true;
109 filter.doFilter(request, null, chain);
110 assertFalse("proxy should not have been called", service.hasBeenCalled);
111 assertFalse("context should not have been updated", context.isHuman());
112 }
113 }