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
22 import org.acegisecurity.util.MockFilterChain;
23
24 import org.springframework.mock.web.MockHttpServletRequest;
25
26
27
28
29
30
31
32
33 public class CaptchaValidationProcessingFilterTests extends TestCase {
34
35
36
37
38 public void testAfterPropertiesSet() throws Exception {
39 CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
40
41 try {
42 filter.afterPropertiesSet();
43 fail("should have thrown an invalid argument exception");
44 } catch (Exception e) {
45 assertTrue("should be an InvalidArgumentException",
46 IllegalArgumentException.class.isAssignableFrom(e.getClass()));
47 }
48
49 filter.setCaptchaService(new MockCaptchaServiceProxy());
50 filter.afterPropertiesSet();
51 filter.setCaptchaValidationParameter(null);
52
53 try {
54 filter.afterPropertiesSet();
55 fail("should have thrown an invalid argument exception");
56 } catch (Exception e) {
57 assertTrue("should be an InvalidArgumentException",
58 IllegalArgumentException.class.isAssignableFrom(e.getClass()));
59 }
60 }
61
62
63
64
65
66
67 public void testDoFilterWithRequestParameter() throws Exception {
68 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
69 SecurityContextHolder.setContext(context);
70
71 MockHttpServletRequest request = new MockHttpServletRequest();
72
73 CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
74 request.addParameter(filter.getCaptchaValidationParameter(), "");
75
76 MockCaptchaServiceProxy service = new MockCaptchaServiceProxy();
77 MockFilterChain chain = new MockFilterChain(true);
78 filter.setCaptchaService(service);
79 filter.doFilter(request, null, chain);
80 assertTrue("should have been called", service.hasBeenCalled);
81 assertFalse("context should not have been updated", context.isHuman());
82
83
84 service.valid = true;
85 filter.doFilter(request, null, chain);
86 assertTrue("should have been called", service.hasBeenCalled);
87 assertTrue("context should have been updated", context.isHuman());
88 }
89
90
91
92
93
94
95 public void testDoFilterWithoutRequestParameter() throws Exception {
96 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
97 SecurityContextHolder.setContext(context);
98
99 MockHttpServletRequest request = new MockHttpServletRequest();
100 CaptchaValidationProcessingFilter filter = new CaptchaValidationProcessingFilter();
101 MockCaptchaServiceProxy service = new MockCaptchaServiceProxy();
102 MockFilterChain chain = new MockFilterChain(true);
103 filter.setCaptchaService(service);
104 filter.doFilter(request, null, chain);
105 assertFalse("proxy should not have been called", service.hasBeenCalled);
106 assertFalse("context should not have been updated", context.isHuman());
107
108
109 service.valid = true;
110 filter.doFilter(request, null, chain);
111 assertFalse("proxy should not have been called", service.hasBeenCalled);
112 assertFalse("context should not have been updated", context.isHuman());
113 }
114 }