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

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