mirror of https://github.com/jacekkow/keycloak-protocol-cas

Jacek Kowalski
2023-11-24 f3460d82f87591ebf1260e02ef7565f5e8eb00f3
commit | author | age
513246 1 package org.keycloak.protocol.cas;
MP 2
bce810 3 import com.jayway.jsonpath.JsonPath;
513246 4 import org.junit.Test;
352436 5 import org.keycloak.protocol.cas.representations.CASErrorCode;
8a5518 6 import org.keycloak.protocol.cas.representations.CASServiceResponse;
513246 7 import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
MP 8 import org.keycloak.protocol.cas.utils.ServiceResponseMarshaller;
bce810 9 import org.w3c.dom.Document;
MP 10 import org.w3c.dom.Node;
11 import org.xmlunit.xpath.JAXPXPathEngine;
12 import org.xmlunit.xpath.XPathEngine;
513246 13
bce810 14 import java.util.*;
513246 15
MP 16 import static org.junit.Assert.assertEquals;
74023a 17 import static org.keycloak.protocol.cas.XMLValidator.parseAndValidate;
EH 18 import static org.keycloak.protocol.cas.XMLValidator.schemaFromClassPath;
513246 19
MP 20 public class ServiceResponseTest {
bce810 21     private final XPathEngine xpath = new JAXPXPathEngine();
MP 22
23     public ServiceResponseTest() {
24         xpath.setNamespaceContext(Collections.singletonMap("cas", "http://www.yale.edu/tp/cas"));
25     }
513246 26
MP 27     @Test
28     public void testSuccessResponse() throws Exception {
29         Map<String, Object> attributes = new HashMap<>();
30         attributes.put("list", Arrays.asList("a", "b"));
31         attributes.put("int", 123);
32         attributes.put("string", "abc");
33
bce810 34         List<String> proxies = Arrays.asList("https://proxy1/pgtUrl", "https://proxy2/pgtUrl");
8a5518 35         CASServiceResponse response = ServiceResponseHelper.createSuccess("username", attributes, "PGTIOU-test",
bce810 36                 proxies);
513246 37
bce810 38         // Build and validate JSON response
MP 39
40         String json = ServiceResponseMarshaller.marshalJson(response);
41         assertEquals("username", JsonPath.read(json, "$.serviceResponse.authenticationSuccess.user"));
42         assertEquals(attributes.get("list"), JsonPath.read(json, "$.serviceResponse.authenticationSuccess.attributes.list"));
43         assertEquals(attributes.get("int"), JsonPath.read(json, "$.serviceResponse.authenticationSuccess.attributes.int"));
44         assertEquals(attributes.get("string"), JsonPath.read(json, "$.serviceResponse.authenticationSuccess.attributes.string"));
45         assertEquals("PGTIOU-test", JsonPath.read(json, "$.serviceResponse.authenticationSuccess.proxyGrantingTicket"));
46         assertEquals(proxies, JsonPath.read(json, "$.serviceResponse.authenticationSuccess.proxies"));
47
48         // Build and validate XML response
49
50         String xml = ServiceResponseMarshaller.marshalXml(response);
74023a 51         Document doc = parseAndValidate(xml, schemaFromClassPath("cas-response-schema.xsd"));
bce810 52         assertEquals("username", xpath.evaluate("/cas:serviceResponse/cas:authenticationSuccess/cas:user", doc));
MP 53         int idx = 0;
54         for (Node node : xpath.selectNodes("/cas:serviceResponse/cas:authenticationSuccess/cas:attributes/cas:list", doc)) {
55             assertEquals(((List)attributes.get("list")).get(idx), node.getTextContent());
56             idx++;
57         }
58         assertEquals(((List)attributes.get("list")).size(), idx);
59         assertEquals(attributes.get("int").toString(), xpath.evaluate("/cas:serviceResponse/cas:authenticationSuccess/cas:attributes/cas:int", doc));
60         assertEquals(attributes.get("string").toString(), xpath.evaluate("/cas:serviceResponse/cas:authenticationSuccess/cas:attributes/cas:string", doc));
61
62         assertEquals("PGTIOU-test", xpath.evaluate("/cas:serviceResponse/cas:authenticationSuccess/cas:proxyGrantingTicket", doc));
63         idx = 0;
64         for (Node node : xpath.selectNodes("/cas:serviceResponse/cas:authenticationSuccess/cas:proxies/cas:proxy", doc)) {
65             assertEquals(proxies.get(idx), node.getTextContent());
66             idx++;
67         }
68         assertEquals(proxies.size(), idx);
513246 69     }
MP 70
71     @Test
72     public void testErrorResponse() throws Exception {
8a5518 73         CASServiceResponse response = ServiceResponseHelper.createFailure(CASErrorCode.INVALID_REQUEST, "Error description");
513246 74
bce810 75         // Build and validate JSON response
MP 76
77         String json = ServiceResponseMarshaller.marshalJson(response);
78         assertEquals(CASErrorCode.INVALID_REQUEST.name(), JsonPath.read(json, "$.serviceResponse.authenticationFailure.code"));
79         assertEquals("Error description", JsonPath.read(json, "$.serviceResponse.authenticationFailure.description"));
80
81         // Build and validate XML response
82
83         String xml = ServiceResponseMarshaller.marshalXml(response);
74023a 84         Document doc = parseAndValidate(xml, schemaFromClassPath("cas-response-schema.xsd"));
bce810 85         assertEquals(CASErrorCode.INVALID_REQUEST.name(), xpath.evaluate("/cas:serviceResponse/cas:authenticationFailure/@code", doc));
MP 86         assertEquals("Error description", xpath.evaluate("/cas:serviceResponse/cas:authenticationFailure", doc));
513246 87     }
MP 88 }