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.providers.cas.proxy;
17  
18  import java.util.List;
19  import java.util.Vector;
20  
21  import junit.framework.TestCase;
22  
23  import org.acegisecurity.providers.cas.ProxyUntrustedException;
24  
25  
26  /***
27   * Tests {@link NamedCasProxyDecider}.
28   */
29  public class NamedCasProxyDeciderTests extends TestCase {
30      //~ Constructors ===========================================================
31  
32      public NamedCasProxyDeciderTests() {
33          super();
34      }
35  
36      public NamedCasProxyDeciderTests(String arg0) {
37          super(arg0);
38      }
39  
40      //~ Methods ================================================================
41  
42      public static void main(String[] args) {
43          junit.textui.TestRunner.run(NamedCasProxyDeciderTests.class);
44      }
45  
46      public final void setUp() throws Exception {
47          super.setUp();
48      }
49  
50      public void testAcceptsIfNearestProxyIsAuthorized()
51          throws Exception {
52          NamedCasProxyDecider proxyDecider = new NamedCasProxyDecider();
53  
54          // Build the ticket returned from CAS
55          List proxyList = new Vector();
56          proxyList.add("https://localhost/newPortal/j_acegi_cas_security_check");
57  
58          // Build the list of valid nearest proxies
59          List validProxies = new Vector();
60          validProxies.add("https://localhost/portal/j_acegi_cas_security_check");
61          validProxies.add(
62              "https://localhost/newPortal/j_acegi_cas_security_check");
63          proxyDecider.setValidProxies(validProxies);
64          proxyDecider.afterPropertiesSet();
65  
66          proxyDecider.confirmProxyListTrusted(proxyList);
67          assertTrue(true);
68      }
69  
70      public void testAcceptsIfNoProxiesInTicket() {
71          NamedCasProxyDecider proxyDecider = new NamedCasProxyDecider();
72  
73          List proxyList = new Vector(); // no proxies in list
74  
75          proxyDecider.confirmProxyListTrusted(proxyList);
76          assertTrue(true);
77      }
78  
79      public void testDetectsMissingValidProxiesList() throws Exception {
80          NamedCasProxyDecider proxyDecider = new NamedCasProxyDecider();
81  
82          try {
83              proxyDecider.afterPropertiesSet();
84              fail("Should have thrown IllegalArgumentException");
85          } catch (IllegalArgumentException expected) {
86              assertEquals("A validProxies list must be set",
87                  expected.getMessage());
88          }
89      }
90  
91      public void testDoesNotAcceptNull() {
92          NamedCasProxyDecider proxyDecider = new NamedCasProxyDecider();
93  
94          try {
95              proxyDecider.confirmProxyListTrusted(null);
96              fail("Should have thrown IllegalArgumentException");
97          } catch (IllegalArgumentException expected) {
98              assertEquals("proxyList cannot be null", expected.getMessage());
99          }
100     }
101 
102     public void testGettersSetters() {
103         NamedCasProxyDecider proxyDecider = new NamedCasProxyDecider();
104 
105         // Build the list of valid nearest proxies
106         List validProxies = new Vector();
107         validProxies.add("https://localhost/portal/j_acegi_cas_security_check");
108         validProxies.add(
109             "https://localhost/newPortal/j_acegi_cas_security_check");
110         proxyDecider.setValidProxies(validProxies);
111 
112         assertEquals(validProxies, proxyDecider.getValidProxies());
113     }
114 
115     public void testRejectsIfNearestProxyIsNotAuthorized()
116         throws Exception {
117         NamedCasProxyDecider proxyDecider = new NamedCasProxyDecider();
118 
119         // Build the ticket returned from CAS
120         List proxyList = new Vector();
121         proxyList.add(
122             "https://localhost/untrustedWebApp/j_acegi_cas_security_check");
123 
124         // Build the list of valid nearest proxies
125         List validProxies = new Vector();
126         validProxies.add("https://localhost/portal/j_acegi_cas_security_check");
127         validProxies.add(
128             "https://localhost/newPortal/j_acegi_cas_security_check");
129         proxyDecider.setValidProxies(validProxies);
130         proxyDecider.afterPropertiesSet();
131 
132         try {
133             proxyDecider.confirmProxyListTrusted(proxyList);
134             fail("Should have thrown ProxyUntrustedException");
135         } catch (ProxyUntrustedException expected) {
136             assertTrue(true);
137         }
138     }
139 }