001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers.cas.ticketvalidator;
017:
018: import edu.yale.its.tp.cas.client.ProxyTicketValidator;
019:
020: import junit.framework.TestCase;
021:
022: import org.acegisecurity.AuthenticationServiceException;
023: import org.acegisecurity.BadCredentialsException;
024:
025: import org.acegisecurity.providers.cas.TicketResponse;
026:
027: import org.acegisecurity.ui.cas.ServiceProperties;
028:
029: import java.util.Vector;
030:
031: /**
032: * Tests {@link CasProxyTicketValidator}.
033: *
034: * @author Ben Alex
035: * @version $Id: CasProxyTicketValidatorTests.java 1496 2006-05-23 13:38:33Z benalex $
036: */
037: public class CasProxyTicketValidatorTests extends TestCase {
038: //~ Constructors ===================================================================================================
039:
040: public CasProxyTicketValidatorTests() {
041: super ();
042: }
043:
044: public CasProxyTicketValidatorTests(String arg0) {
045: super (arg0);
046: }
047:
048: //~ Methods ========================================================================================================
049:
050: public static void main(String[] args) {
051: junit.textui.TestRunner.run(CasProxyTicketValidatorTests.class);
052: }
053:
054: public final void setUp() throws Exception {
055: super .setUp();
056: }
057:
058: public void testGetters() {
059: CasProxyTicketValidator tv = new CasProxyTicketValidator();
060: tv
061: .setProxyCallbackUrl("http://my.com/webapp/casProxy/someValidator");
062: assertEquals("http://my.com/webapp/casProxy/someValidator", tv
063: .getProxyCallbackUrl());
064: }
065:
066: public void testNormalOperation() {
067: ServiceProperties sp = new ServiceProperties();
068: sp.setSendRenew(true);
069: sp
070: .setService("https://my.com/webapp//j_acegi_cas_security_check");
071:
072: CasProxyTicketValidator tv = new MockCasProxyTicketValidator(
073: true, false);
074: tv.setCasValidate("https://company.com/cas/proxyvalidate");
075: tv.setServiceProperties(sp);
076: tv
077: .setProxyCallbackUrl("http://my.com/webapp/casProxy/someValidator");
078:
079: TicketResponse response = tv
080: .confirmTicketValid("ST-0-ER94xMJmn6pha35CQRoZ");
081:
082: assertEquals("user", response.getUser());
083: }
084:
085: public void testProxyTicketValidatorInternalExceptionsGracefullyHandled() {
086: CasProxyTicketValidator tv = new MockCasProxyTicketValidator(
087: false, true);
088: tv.setCasValidate("https://company.com/cas/proxyvalidate");
089: tv.setServiceProperties(new ServiceProperties());
090: tv
091: .setProxyCallbackUrl("http://my.com/webapp/casProxy/someValidator");
092:
093: try {
094: tv.confirmTicketValid("ST-0-ER94xMJmn6pha35CQRoZ");
095: fail("Should have thrown AuthenticationServiceException");
096: } catch (AuthenticationServiceException expected) {
097: assertTrue(true);
098: }
099: }
100:
101: public void testValidationFailsOkAndOperationWithoutAProxyCallbackUrl() {
102: CasProxyTicketValidator tv = new MockCasProxyTicketValidator(
103: false, false);
104: tv.setCasValidate("https://company.com/cas/proxyvalidate");
105: tv.setServiceProperties(new ServiceProperties());
106:
107: try {
108: tv.confirmTicketValid("ST-0-ER94xMJmn6pha35CQRoZ");
109: fail("Should have thrown BadCredentialsExpected");
110: } catch (BadCredentialsException expected) {
111: assertTrue(true);
112: }
113: }
114:
115: //~ Inner Classes ==================================================================================================
116:
117: private class MockCasProxyTicketValidator extends
118: CasProxyTicketValidator {
119: private boolean returnTicket;
120: private boolean throwAuthenticationServiceException;
121:
122: public MockCasProxyTicketValidator(boolean returnTicket,
123: boolean throwAuthenticationServiceException) {
124: this .returnTicket = returnTicket;
125: this .throwAuthenticationServiceException = throwAuthenticationServiceException;
126: }
127:
128: private MockCasProxyTicketValidator() {
129: super ();
130: }
131:
132: protected TicketResponse validateNow(ProxyTicketValidator pv)
133: throws AuthenticationServiceException,
134: BadCredentialsException {
135: if (returnTicket) {
136: return new TicketResponse("user", new Vector(),
137: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
138: }
139:
140: if (throwAuthenticationServiceException) {
141: throw new AuthenticationServiceException(
142: "As requested by mock");
143: }
144:
145: throw new BadCredentialsException("As requested by mock");
146: }
147: }
148: }
|