    1   /* Copyright 2004 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.ui.cas;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.Authentication;
    21  import org.acegisecurity.AuthenticationException;
    22  import org.acegisecurity.MockAuthenticationManager;
    23
    24  import org.springframework.mock.web.MockHttpServletRequest;
    25
    26
    27  /***
    28   * Tests {@link CasProcessingFilter}.
    29   *
    30   * @author Ben Alex
    31   * @version $Id: CasProcessingFilterTests.java,v 1.3 2005/11/17 00:55:48 benalex Exp $
    32   */
    33  public class CasProcessingFilterTests extends TestCase {
    34      //~ Constructors ===========================================================
    35
    36      public CasProcessingFilterTests() {
    37          super();
    38      }
    39
    40      public CasProcessingFilterTests(String arg0) {
    41          super(arg0);
    42      }
    43
    44      //~ Methods ================================================================
    45
    46      public final void setUp() throws Exception {
    47          super.setUp();
    48      }
    49
    50      public static void main(String[] args) {
    51          junit.textui.TestRunner.run(CasProcessingFilterTests.class);
    52      }
    53
    54      public void testGetters() {
    55          CasProcessingFilter filter = new CasProcessingFilter();
    56          assertEquals("/j_acegi_cas_security_check",
    57              filter.getDefaultFilterProcessesUrl());
    58      }
    59
    60      public void testNormalOperation() throws Exception {
    61          MockHttpServletRequest request = new MockHttpServletRequest();
    62          request.addParameter("ticket", "ST-0-ER94xMJmn6pha35CQRoZ");
    63
    64          MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
    65
    66          CasProcessingFilter filter = new CasProcessingFilter();
    67          filter.setAuthenticationManager(authMgr);
    68          filter.init(null);
    69
    70          Authentication result = filter.attemptAuthentication(request);
    71          assertTrue(result != null);
    72      }
    73
    74      public void testNullServiceTicketHandledGracefully()
    75          throws Exception {
    76          MockHttpServletRequest request = new MockHttpServletRequest();
    77
    78          MockAuthenticationManager authMgr = new MockAuthenticationManager(false);
    79
    80          CasProcessingFilter filter = new CasProcessingFilter();
    81          filter.setAuthenticationManager(authMgr);
    82          filter.init(null);
    83
    84          try {
    85              filter.attemptAuthentication(request);
    86              fail("Should have thrown AuthenticationException");
    87          } catch (AuthenticationException expected) {
    88              assertTrue(true);
    89          }
    90      }
    91  }