1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests {@link CaptchaValidationProcessingFilter}.
29   *
30   * @author marc antoine Garrigue
31   * @version $Id: CaptchaValidationProcessingFilterTests.java 1496 2006-05-23 13:38:33Z benalex $
32   */
33  public class CaptchaValidationProcessingFilterTests extends TestCase {
34      //~ Methods ========================================================================================================
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       * Test method for
64       * 'org.acegisecurity.captcha.CaptchaValidationProcessingFilter.doFilter(ServletRequest,
65       * ServletResponse, FilterChain)'
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          // test with valid
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       * Test method for
92       * 'org.acegisecurity.captcha.CaptchaValidationProcessingFilter.doFilter(ServletRequest,
93       * ServletResponse, FilterChain)'
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         // test with valid
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 }