|
| 1 | +/* |
| 2 | + * Licensed to Jasig under one or more contributor license |
| 3 | + * agreements. See the NOTICE file distributed with this work |
| 4 | + * for additional information regarding copyright ownership. |
| 5 | + * Jasig licenses this file to you under the Apache License, |
| 6 | + * Version 2.0 (the "License"); you may not use this file |
| 7 | + * except in compliance with the License. You may obtain a |
| 8 | + * copy of the License at the following location: |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.jasig.cas.client.validation; |
| 20 | + |
| 21 | +import org.jasig.cas.client.PublicTestHttpServer; |
| 22 | +import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage; |
| 23 | +import org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl; |
| 24 | +import org.jasig.cas.client.proxy.ProxyRetriever; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.io.UnsupportedEncodingException; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.junit.Assert.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * Test cases for the {@link Cas20ServiceTicketValidator}. |
| 35 | + * |
| 36 | + * @author Scott Battaglia |
| 37 | + * @version $Revision: 11737 $ $Date: 2007-10-03 09:14:02 -0400 (Tue, 03 Oct 2007) $ |
| 38 | + * @since 3.0 |
| 39 | + */ |
| 40 | +public final class Cas30ServiceTicketValidatorTests extends AbstractTicketValidatorTests { |
| 41 | + |
| 42 | + private static final PublicTestHttpServer server = PublicTestHttpServer.instance(8088); |
| 43 | + |
| 44 | + private Cas30ServiceTicketValidator ticketValidator; |
| 45 | + |
| 46 | + private ProxyGrantingTicketStorage proxyGrantingTicketStorage; |
| 47 | + |
| 48 | + public Cas30ServiceTicketValidatorTests() { |
| 49 | + super(); |
| 50 | + } |
| 51 | + |
| 52 | + /*@AfterClass |
| 53 | + public static void classCleanUp() { |
| 54 | + server.shutdown(); |
| 55 | + } */ |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() throws Exception { |
| 59 | + this.proxyGrantingTicketStorage = getProxyGrantingTicketStorage(); |
| 60 | + this.ticketValidator = new Cas30ServiceTicketValidator(CONST_CAS_SERVER_URL_PREFIX + "8088"); |
| 61 | + this.ticketValidator.setProxyCallbackUrl("test"); |
| 62 | + this.ticketValidator.setProxyGrantingTicketStorage(getProxyGrantingTicketStorage()); |
| 63 | + this.ticketValidator.setProxyRetriever(getProxyRetriever()); |
| 64 | + this.ticketValidator.setRenew(true); |
| 65 | + } |
| 66 | + |
| 67 | + private ProxyGrantingTicketStorage getProxyGrantingTicketStorage() { |
| 68 | + return new ProxyGrantingTicketStorageImpl(); |
| 69 | + } |
| 70 | + |
| 71 | + private ProxyRetriever getProxyRetriever() { |
| 72 | + return new ProxyRetriever() { |
| 73 | + |
| 74 | + /** Unique Id for serialization. */ |
| 75 | + private static final long serialVersionUID = 1L; |
| 76 | + |
| 77 | + public String getProxyTicketIdFor(String proxyGrantingTicketId, String targetService) { |
| 78 | + return "test"; |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testNoResponse() throws UnsupportedEncodingException { |
| 85 | + final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationFailure code=\"INVALID_TICKET\">Ticket ST-1856339-aA5Yuvrxzpv8Tau1cYQ7 not recognized</cas:authenticationFailure></cas:serviceResponse>"; |
| 86 | + server.content = RESPONSE.getBytes(server.encoding); |
| 87 | + try { |
| 88 | + this.ticketValidator.validate("test", "test"); |
| 89 | + fail("ValidationException expected due to 'no' response"); |
| 90 | + } catch (final TicketValidationException e) { |
| 91 | + // expected |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testYesResponseButNoPgt() throws TicketValidationException, UnsupportedEncodingException { |
| 97 | + final String USERNAME = "username"; |
| 98 | + final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>" |
| 99 | + + USERNAME + "</cas:user></cas:authenticationSuccess></cas:serviceResponse>"; |
| 100 | + server.content = RESPONSE.getBytes(server.encoding); |
| 101 | + |
| 102 | + final Assertion assertion = this.ticketValidator.validate("test", "test"); |
| 103 | + assertEquals(USERNAME, assertion.getPrincipal().getName()); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testYesResponseWithPgt() throws TicketValidationException, UnsupportedEncodingException { |
| 109 | + final String USERNAME = "username"; |
| 110 | + final String PGTIOU = "testPgtIou"; |
| 111 | + final String PGT = "test"; |
| 112 | + final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>" |
| 113 | + + USERNAME |
| 114 | + + "</cas:user><cas:proxyGrantingTicket>" |
| 115 | + + PGTIOU |
| 116 | + + "</cas:proxyGrantingTicket></cas:authenticationSuccess></cas:serviceResponse>"; |
| 117 | + |
| 118 | + server.content = RESPONSE.getBytes(server.encoding); |
| 119 | + this.proxyGrantingTicketStorage.save(PGTIOU, PGT); |
| 120 | + |
| 121 | + final Assertion assertion = this.ticketValidator.validate("test", "test"); |
| 122 | + assertEquals(USERNAME, assertion.getPrincipal().getName()); |
| 123 | + // assertEquals(PGT, assertion.getProxyGrantingTicketId()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testGetAttributes() throws TicketValidationException, UnsupportedEncodingException { |
| 128 | + final String USERNAME = "username"; |
| 129 | + final String PGTIOU = "testPgtIou"; |
| 130 | + final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>" |
| 131 | + + USERNAME |
| 132 | + + "</cas:user><cas:proxyGrantingTicket>" |
| 133 | + + PGTIOU |
| 134 | + + "</cas:proxyGrantingTicket><cas:attributes><cas:password>test</cas:password><cas:eduPersonId>id</cas:eduPersonId><cas:longAttribute>test1\n\ntest</cas:longAttribute><cas:multivaluedAttribute>value1</cas:multivaluedAttribute><cas:multivaluedAttribute>value2</cas:multivaluedAttribute></cas:attributes></cas:authenticationSuccess></cas:serviceResponse>"; |
| 135 | + |
| 136 | + server.content = RESPONSE.getBytes(server.encoding); |
| 137 | + final Assertion assertion = this.ticketValidator.validate("test", "test"); |
| 138 | + assertEquals(USERNAME, assertion.getPrincipal().getName()); |
| 139 | + assertEquals("test", assertion.getPrincipal().getAttributes().get("password")); |
| 140 | + assertEquals("id", assertion.getPrincipal().getAttributes().get("eduPersonId")); |
| 141 | + assertEquals("test1\n\ntest", assertion.getPrincipal().getAttributes().get("longAttribute")); |
| 142 | + try { |
| 143 | + List<?> multivalued = (List<?>) assertion.getPrincipal().getAttributes().get("multivaluedAttribute"); |
| 144 | + assertArrayEquals(new String[] { "value1", "value2" }, multivalued.toArray()); |
| 145 | + } catch (Exception e) { |
| 146 | + fail("'multivaluedAttribute' attribute expected as List<Object> object."); |
| 147 | + } |
| 148 | + //assertEquals(PGT, assertion.getProxyGrantingTicketId()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testGetInlinedAttributes() throws TicketValidationException, UnsupportedEncodingException { |
| 153 | + final String USERNAME = "username"; |
| 154 | + final String PGTIOU = "testPgtIou"; |
| 155 | + final String RESPONSE = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><cas:authenticationSuccess><cas:user>" |
| 156 | + + USERNAME |
| 157 | + + "</cas:user><cas:proxyGrantingTicket>" |
| 158 | + + PGTIOU |
| 159 | + + "</cas:proxyGrantingTicket><cas:attributes><cas:attribute name=\"password\" value=\"test\"/><cas:attribute name=\"eduPersonId\" value=\"id\"/><cas:attribute name=\"longAttribute\" value=\"test1 test\"/><cas:attribute name=\"multivaluedAttribute\" value=\"value1\"/><cas:attribute name=\"multivaluedAttribute\" value=\"value2\"/></cas:attributes></cas:authenticationSuccess></cas:serviceResponse>"; |
| 160 | + |
| 161 | + server.content = RESPONSE.getBytes(server.encoding); |
| 162 | + final Assertion assertion = this.ticketValidator.validate("test", "test"); |
| 163 | + assertEquals(USERNAME, assertion.getPrincipal().getName()); |
| 164 | + assertEquals("test", assertion.getPrincipal().getAttributes().get("password")); |
| 165 | + assertEquals("id", assertion.getPrincipal().getAttributes().get("eduPersonId")); |
| 166 | + assertEquals("test1\n\ntest", assertion.getPrincipal().getAttributes().get("longAttribute")); |
| 167 | + try { |
| 168 | + List<?> multivalued = (List<?>) assertion.getPrincipal().getAttributes().get("multivaluedAttribute"); |
| 169 | + assertArrayEquals(new String[] { "value1", "value2" }, multivalued.toArray()); |
| 170 | + } catch (Exception e) { |
| 171 | + fail("'multivaluedAttribute' attribute expected as List<Object> object."); |
| 172 | + } |
| 173 | + //assertEquals(PGT, assertion.getProxyGrantingTicketId()); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void testInvalidResponse() throws Exception { |
| 178 | + final String RESPONSE = "<root />"; |
| 179 | + server.content = RESPONSE.getBytes(server.encoding); |
| 180 | + try { |
| 181 | + this.ticketValidator.validate("test", "test"); |
| 182 | + fail("ValidationException expected due to invalid response."); |
| 183 | + } catch (final TicketValidationException e) { |
| 184 | + // expected |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments