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  
21  
22  
23  
24  import java.net.URLEncoder;
25  
26  import org.springframework.mock.web.MockHttpServletRequest;
27  import org.springframework.mock.web.MockHttpServletResponse;
28  
29  
30  /***
31   * Tests {@link CasProcessingFilterEntryPoint}.
32   *
33   * @author Ben Alex
34   * @version $Id: CasProcessingFilterEntryPointTests.java,v 1.5 2005/11/17 00:55:48 benalex Exp $
35   */
36  public class CasProcessingFilterEntryPointTests extends TestCase {
37      //~ Constructors ===========================================================
38  
39      public CasProcessingFilterEntryPointTests() {
40          super();
41      }
42  
43      public CasProcessingFilterEntryPointTests(String arg0) {
44          super(arg0);
45      }
46  
47      //~ Methods ================================================================
48  
49      public final void setUp() throws Exception {
50          super.setUp();
51      }
52  
53      public static void main(String[] args) {
54          junit.textui.TestRunner.run(CasProcessingFilterEntryPointTests.class);
55      }
56  
57      public void testDetectsMissingLoginFormUrl() throws Exception {
58          CasProcessingFilterEntryPoint ep = new CasProcessingFilterEntryPoint();
59          ep.setServiceProperties(new ServiceProperties());
60  
61          try {
62              ep.afterPropertiesSet();
63              fail("Should have thrown IllegalArgumentException");
64          } catch (IllegalArgumentException expected) {
65              assertEquals("loginUrl must be specified", expected.getMessage());
66          }
67      }
68  
69      public void testDetectsMissingServiceProperties() throws Exception {
70          CasProcessingFilterEntryPoint ep = new CasProcessingFilterEntryPoint();
71          ep.setLoginUrl("https://cas/login");
72  
73          try {
74              ep.afterPropertiesSet();
75              fail("Should have thrown IllegalArgumentException");
76          } catch (IllegalArgumentException expected) {
77              assertEquals("serviceProperties must be specified",
78                  expected.getMessage());
79          }
80      }
81  
82      public void testGettersSetters() {
83          CasProcessingFilterEntryPoint ep = new CasProcessingFilterEntryPoint();
84          ep.setLoginUrl("https://cas/login");
85          assertEquals("https://cas/login", ep.getLoginUrl());
86  
87          ep.setServiceProperties(new ServiceProperties());
88          assertTrue(ep.getServiceProperties() != null);
89      }
90  
91      public void testNormalOperationWithRenewFalse() throws Exception {
92          ServiceProperties sp = new ServiceProperties();
93          sp.setSendRenew(false);
94          sp.setService(
95              "https://mycompany.com/bigWebApp/j_acegi_cas_security_check");
96  
97          CasProcessingFilterEntryPoint ep = new CasProcessingFilterEntryPoint();
98          ep.setLoginUrl("https://cas/login");
99          ep.setServiceProperties(sp);
100 
101         MockHttpServletRequest request = new MockHttpServletRequest();
102         request.setRequestURI("/some_path");
103 
104         MockHttpServletResponse response = new MockHttpServletResponse();
105 
106         ep.afterPropertiesSet();
107         ep.commence(request, response, null);
108 
109         assertEquals("https://cas/login?service="
110             + URLEncoder.encode(
111                 "https://mycompany.com/bigWebApp/j_acegi_cas_security_check",
112                 "UTF-8"), response.getRedirectedUrl());
113     }
114 
115     public void testNormalOperationWithRenewTrue() throws Exception {
116         ServiceProperties sp = new ServiceProperties();
117         sp.setSendRenew(true);
118         sp.setService(
119             "https://mycompany.com/bigWebApp/j_acegi_cas_security_check");
120 
121         CasProcessingFilterEntryPoint ep = new CasProcessingFilterEntryPoint();
122         ep.setLoginUrl("https://cas/login");
123         ep.setServiceProperties(sp);
124 
125         MockHttpServletRequest request = new MockHttpServletRequest();
126         request.setRequestURI("/some_path");
127 
128         MockHttpServletResponse response = new MockHttpServletResponse();
129 
130         ep.afterPropertiesSet();
131         ep.commence(request, response, null);
132         assertEquals("https://cas/login?renew=true&service=https://mycompany.com/bigWebApp/j_acegi_cas_security_check",
133             response.getRedirectedUrl());
134     }
135 }