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.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 1496 2006-05-23 13:38:33Z benalex $
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 static void main(String[] args) {
47          junit.textui.TestRunner.run(CasProcessingFilterTests.class);
48      }
49  
50      public final void setUp() throws Exception {
51          super.setUp();
52      }
53  
54      public void testGetters() {
55          CasProcessingFilter filter = new CasProcessingFilter();
56          assertEquals("/j_acegi_cas_security_check", filter.getDefaultFilterProcessesUrl());
57      }
58  
59      public void testNormalOperation() throws Exception {
60          MockHttpServletRequest request = new MockHttpServletRequest();
61          request.addParameter("ticket", "ST-0-ER94xMJmn6pha35CQRoZ");
62  
63          MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
64  
65          CasProcessingFilter filter = new CasProcessingFilter();
66          filter.setAuthenticationManager(authMgr);
67          filter.init(null);
68  
69          Authentication result = filter.attemptAuthentication(request);
70          assertTrue(result != null);
71      }
72  
73      public void testNullServiceTicketHandledGracefully()
74          throws Exception {
75          MockHttpServletRequest request = new MockHttpServletRequest();
76  
77          MockAuthenticationManager authMgr = new MockAuthenticationManager(false);
78  
79          CasProcessingFilter filter = new CasProcessingFilter();
80          filter.setAuthenticationManager(authMgr);
81          filter.init(null);
82  
83          try {
84              filter.attemptAuthentication(request);
85              fail("Should have thrown AuthenticationException");
86          } catch (AuthenticationException expected) {
87              assertTrue(true);
88          }
89      }
90  }