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

Matthias Piepkorn
2017-01-29 352436410a73e70a8adffd8d5bfbd2bcdf97c139
commit | author | age
513246 1 package org.keycloak.protocol.cas;
MP 2
3 import org.junit.Test;
352436 4 import org.keycloak.protocol.cas.representations.CASErrorCode;
513246 5 import org.keycloak.protocol.cas.representations.CasServiceResponse;
MP 6 import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
7 import org.keycloak.protocol.cas.utils.ServiceResponseMarshaller;
8
9 import java.util.Arrays;
10 import java.util.HashMap;
11 import java.util.Map;
12
13 import static org.junit.Assert.assertEquals;
14
15 public class ServiceResponseTest {
16     private static final String EXPECTED_JSON_SUCCESS = "{\n" +
17             "  \"serviceResponse\" : {\n" +
18             "    \"authenticationSuccess\" : {\n" +
19             "      \"user\" : \"username\",\n" +
20             "      \"proxyGrantingTicket\" : \"PGTIOU-test\",\n" +
21             "      \"proxies\" : [ \"https://proxy1/pgtUrl\", \"https://proxy2/pgtUrl\" ],\n" +
22             "      \"attributes\" : {\n" +
23             "        \"string\" : \"abc\",\n" +
24             "        \"list\" : [ \"a\", \"b\" ],\n" +
25             "        \"int\" : 123\n" +
26             "      }\n" +
27             "    }\n" +
28             "  }\n" +
29             "}";
30     private static final String EXPECTED_XML_SUCCESS = "<cas:serviceResponse xmlns:cas=\"http://www.yale.edu/tp/cas\">\n" +
31             "    <cas:authenticationSuccess>\n" +
32             "        <cas:user>username</cas:user>\n" +
33             "        <cas:proxyGrantingTicket>PGTIOU-test</cas:proxyGrantingTicket>\n" +
34             "        <cas:proxies>\n" +
35             "            <cas:proxy>https://proxy1/pgtUrl</cas:proxy>\n" +
36             "            <cas:proxy>https://proxy2/pgtUrl</cas:proxy>\n" +
37             "        </cas:proxies>\n" +
38             "        <cas:attributes>\n" +
39             "            <cas:string>abc</cas:string>\n" +
40             "            <cas:list>a</cas:list>\n" +
41             "            <cas:list>b</cas:list>\n" +
42             "            <cas:int>123</cas:int>\n" +
43             "        </cas:attributes>\n" +
44             "    </cas:authenticationSuccess>\n" +
45             "</cas:serviceResponse>";
46     private static final String EXPECTED_JSON_FAILURE = "{\n" +
47             "  \"serviceResponse\" : {\n" +
48             "    \"authenticationFailure\" : {\n" +
352436 49             "      \"code\" : \"INVALID_REQUEST\",\n" +
513246 50             "      \"description\" : \"Error description\"\n" +
MP 51             "    }\n" +
52             "  }\n" +
53             "}";
54     private static final String EXPECTED_XML_FAILURE = "<cas:serviceResponse xmlns:cas=\"http://www.yale.edu/tp/cas\">\n" +
352436 55             "    <cas:authenticationFailure code=\"INVALID_REQUEST\">Error description</cas:authenticationFailure>\n" +
513246 56             "</cas:serviceResponse>";
MP 57
58     @Test
59     public void testSuccessResponse() throws Exception {
60         Map<String, Object> attributes = new HashMap<>();
61         attributes.put("list", Arrays.asList("a", "b"));
62         attributes.put("int", 123);
63         attributes.put("string", "abc");
64
65         CasServiceResponse response = ServiceResponseHelper.createSuccess("username", attributes, "PGTIOU-test",
66                 Arrays.asList("https://proxy1/pgtUrl", "https://proxy2/pgtUrl"));
67
68         assertEquals(EXPECTED_JSON_SUCCESS, ServiceResponseMarshaller.marshalJson(response));
69         assertEquals(EXPECTED_XML_SUCCESS, ServiceResponseMarshaller.marshalXml(response));
70     }
71
72     @Test
73     public void testErrorResponse() throws Exception {
352436 74         CasServiceResponse response = ServiceResponseHelper.createFailure(CASErrorCode.INVALID_REQUEST, "Error description");
513246 75
MP 76         assertEquals(EXPECTED_JSON_FAILURE, ServiceResponseMarshaller.marshalJson(response));
77         assertEquals(EXPECTED_XML_FAILURE, ServiceResponseMarshaller.marshalXml(response));
78     }
79 }