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.providers.cas.ticketvalidator;
17  
18  import edu.yale.its.tp.cas.client.ProxyTicketValidator;
19  
20  import junit.framework.TestCase;
21  
22  import org.acegisecurity.AuthenticationServiceException;
23  import org.acegisecurity.BadCredentialsException;
24  
25  import org.acegisecurity.providers.cas.TicketResponse;
26  
27  import org.acegisecurity.ui.cas.ServiceProperties;
28  
29  import java.util.Vector;
30  
31  
32  /**
33   * Tests {@link CasProxyTicketValidator}.
34   *
35   * @author Ben Alex
36   * @version $Id: CasProxyTicketValidatorTests.java 1496 2006-05-23 13:38:33Z benalex $
37   */
38  public class CasProxyTicketValidatorTests extends TestCase {
39      //~ Constructors ===================================================================================================
40  
41      public CasProxyTicketValidatorTests() {
42          super();
43      }
44  
45      public CasProxyTicketValidatorTests(String arg0) {
46          super(arg0);
47      }
48  
49      //~ Methods ========================================================================================================
50  
51      public static void main(String[] args) {
52          junit.textui.TestRunner.run(CasProxyTicketValidatorTests.class);
53      }
54  
55      public final void setUp() throws Exception {
56          super.setUp();
57      }
58  
59      public void testGetters() {
60          CasProxyTicketValidator tv = new CasProxyTicketValidator();
61          tv.setProxyCallbackUrl("http://my.com/webapp/casProxy/someValidator");
62          assertEquals("http://my.com/webapp/casProxy/someValidator", tv.getProxyCallbackUrl());
63      }
64  
65      public void testNormalOperation() {
66          ServiceProperties sp = new ServiceProperties();
67          sp.setSendRenew(true);
68          sp.setService("https://my.com/webapp//j_acegi_cas_security_check");
69  
70          CasProxyTicketValidator tv = new MockCasProxyTicketValidator(true, false);
71          tv.setCasValidate("https://company.com/cas/proxyvalidate");
72          tv.setServiceProperties(sp);
73          tv.setProxyCallbackUrl("http://my.com/webapp/casProxy/someValidator");
74  
75          TicketResponse response = tv.confirmTicketValid("ST-0-ER94xMJmn6pha35CQRoZ");
76  
77          assertEquals("user", response.getUser());
78      }
79  
80      public void testProxyTicketValidatorInternalExceptionsGracefullyHandled() {
81          CasProxyTicketValidator tv = new MockCasProxyTicketValidator(false, true);
82          tv.setCasValidate("https://company.com/cas/proxyvalidate");
83          tv.setServiceProperties(new ServiceProperties());
84          tv.setProxyCallbackUrl("http://my.com/webapp/casProxy/someValidator");
85  
86          try {
87              tv.confirmTicketValid("ST-0-ER94xMJmn6pha35CQRoZ");
88              fail("Should have thrown AuthenticationServiceException");
89          } catch (AuthenticationServiceException expected) {
90              assertTrue(true);
91          }
92      }
93  
94      public void testValidationFailsOkAndOperationWithoutAProxyCallbackUrl() {
95          CasProxyTicketValidator tv = new MockCasProxyTicketValidator(false, false);
96          tv.setCasValidate("https://company.com/cas/proxyvalidate");
97          tv.setServiceProperties(new ServiceProperties());
98  
99          try {
100             tv.confirmTicketValid("ST-0-ER94xMJmn6pha35CQRoZ");
101             fail("Should have thrown BadCredentialsExpected");
102         } catch (BadCredentialsException expected) {
103             assertTrue(true);
104         }
105     }
106 
107     //~ Inner Classes ==================================================================================================
108 
109     private class MockCasProxyTicketValidator extends CasProxyTicketValidator {
110         private boolean returnTicket;
111         private boolean throwAuthenticationServiceException;
112 
113         public MockCasProxyTicketValidator(boolean returnTicket, boolean throwAuthenticationServiceException) {
114             this.returnTicket = returnTicket;
115             this.throwAuthenticationServiceException = throwAuthenticationServiceException;
116         }
117 
118         private MockCasProxyTicketValidator() {
119             super();
120         }
121 
122         protected TicketResponse validateNow(ProxyTicketValidator pv)
123             throws AuthenticationServiceException, BadCredentialsException {
124             if (returnTicket) {
125                 return new TicketResponse("user", new Vector(),
126                     "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
127             }
128 
129             if (throwAuthenticationServiceException) {
130                 throw new AuthenticationServiceException("As requested by mock");
131             }
132 
133             throw new BadCredentialsException("As requested by mock");
134         }
135     }
136 }