    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.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  import org.acegisecurity.providers.cas.TicketResponse;
    25  import org.acegisecurity.ui.cas.ServiceProperties;
    26
    27  import java.util.Vector;
    28
    29
    30  /***
    31   * Tests {@link CasProxyTicketValidator}.
    32   *
    33   * @author Ben Alex
    34   * @version $Id: CasProxyTicketValidatorTests.java,v 1.2 2005/11/17 00:55:50 benalex Exp $
    35   */
    36  public class CasProxyTicketValidatorTests extends TestCase {
    37      //~ Constructors ===========================================================
    38
    39      public CasProxyTicketValidatorTests() {
    40          super();
    41      }
    42
    43      public CasProxyTicketValidatorTests(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(CasProxyTicketValidatorTests.class);
    55      }
    56
    57      public void testGetters() {
    58          CasProxyTicketValidator tv = new CasProxyTicketValidator();
    59          tv.setProxyCallbackUrl("http://my.com/webapp/casProxy/someValidator");
    60          assertEquals("http://my.com/webapp/casProxy/someValidator",
    61              tv.getProxyCallbackUrl());
    62      }
    63
    64      public void testNormalOperation() {
    65          ServiceProperties sp = new ServiceProperties();
    66          sp.setSendRenew(true);
    67          sp.setService("https://my.com/webapp//j_acegi_cas_security_check");
    68
    69          CasProxyTicketValidator tv = new MockCasProxyTicketValidator(true, false);
    70          tv.setCasValidate("https://company.com/cas/proxyvalidate");
    71          tv.setServiceProperties(sp);
    72          tv.setProxyCallbackUrl("http://my.com/webapp/casProxy/someValidator");
    73
    74          TicketResponse response = tv.confirmTicketValid(
    75                  "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,
    96                  false);
    97          tv.setCasValidate("https://company.com/cas/proxyvalidate");
    98          tv.setServiceProperties(new ServiceProperties());
    99
    100         try {
    101             tv.confirmTicketValid("ST-0-ER94xMJmn6pha35CQRoZ");
    102             fail("Should have thrown BadCredentialsExpected");
    103         } catch (BadCredentialsException expected) {
    104             assertTrue(true);
    105         }
    106     }
    107
    108     //~ Inner Classes ==========================================================
    109
    110     private class MockCasProxyTicketValidator extends CasProxyTicketValidator {
    111         private boolean returnTicket;
    112         private boolean throwAuthenticationServiceException;
    113
    114         public MockCasProxyTicketValidator(boolean returnTicket,
    115             boolean throwAuthenticationServiceException) {
    116             this.returnTicket = returnTicket;
    117             this.throwAuthenticationServiceException = throwAuthenticationServiceException;
    118         }
    119
    120         private MockCasProxyTicketValidator() {
    121             super();
    122         }
    123
    124         protected TicketResponse validateNow(ProxyTicketValidator pv)
    125             throws AuthenticationServiceException, BadCredentialsException {
    126             if (returnTicket) {
    127                 return new TicketResponse("user", new Vector(),
    128                     "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
    129             }
    130
    131             if (throwAuthenticationServiceException) {
    132                 throw new AuthenticationServiceException("As requested by mock");
    133             }
    134
    135             throw new BadCredentialsException("As requested by mock");
    136         }
    137     }
    138 }