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