1
2
3
4
5
6
7
8
9
10
11
12
13
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
34
35
36
37
38 public class CasProxyTicketValidatorTests extends TestCase {
39
40
41 public CasProxyTicketValidatorTests() {
42 super();
43 }
44
45 public CasProxyTicketValidatorTests(String arg0) {
46 super(arg0);
47 }
48
49
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
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 }