    1   /* Copyright 2004, 2005 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  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      //~ Methods ================================================================
    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       * Test method for
    63       * 'org.acegisecurity.captcha.CaptchaValidationProcessingFilter.doFilter(ServletRequest,
    64       * ServletResponse, FilterChain)'
    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          // test with valid
    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       * Test method for
    91       * 'org.acegisecurity.captcha.CaptchaValidationProcessingFilter.doFilter(ServletRequest,
    92       * ServletResponse, FilterChain)'
    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         // test with valid
    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 }