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

github-actions
2024-12-03 0d71e01978a7871823e81ba1430777c3e6eca853
commit | author | age
74023a 1 package org.keycloak.protocol.cas;
EH 2
fdb9f6 3 import jakarta.ws.rs.core.Response;
74023a 4 import org.junit.Test;
EH 5 import org.keycloak.dom.saml.v1.protocol.SAML11ResponseType;
6 import org.keycloak.protocol.cas.representations.CASErrorCode;
7 import org.keycloak.protocol.cas.representations.SamlResponseHelper;
8 import org.keycloak.protocol.cas.utils.CASValidationException;
9 import org.w3c.dom.Document;
10
11 import java.util.Collections;
12
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 public class SamlResponseTest {
17     @Test
18     public void successResponseIsWrappedInSOAP() {
19         SAML11ResponseType response = SamlResponseHelper.successResponse("keycloak", "test@example.com", Collections.emptyMap());
20         String soapResult = SamlResponseHelper.soap(response);
21         assertTrue(soapResult.contains("samlp:Success"));
22         assertTrue(soapResult.contains("test@example.com"));
23         assertTrue(soapResult.contains("keycloak"));
24     }
25
26     @Test
27     public void failureResponseIsWrappedInSOAP() {
28         SAML11ResponseType response = SamlResponseHelper.errorResponse(new CASValidationException(CASErrorCode.INVALID_TICKET, "Nope", Response.Status.BAD_REQUEST));
29         String nope = SamlResponseHelper.soap(response);
30         assertTrue(nope.contains("Nope"));
31     }
32
33     @Test
34     public void validateSchemaResponseFailure() throws Exception {
35         SAML11ResponseType response = SamlResponseHelper.errorResponse(new CASValidationException(CASErrorCode.INVALID_TICKET, "Nope", Response.Status.BAD_REQUEST));
36         String output = SamlResponseHelper.toString(SamlResponseHelper.toDOM(response));
37         Document doc = XMLValidator.parseAndValidate(output, XMLValidator.schemaFromClassPath("oasis-sstc-saml-schema-protocol-1.1.xsd"));
38         assertNotNull(doc);
39     }
40
41     @Test
42     public void validateSchemaResponseSuccess() throws Exception {
43         SAML11ResponseType response = SamlResponseHelper.successResponse("keycloak", "test@example.com", Collections.emptyMap());
44         String output = SamlResponseHelper.toString(SamlResponseHelper.toDOM(response));
45         Document doc = XMLValidator.parseAndValidate(output, XMLValidator.schemaFromClassPath("oasis-sstc-saml-schema-protocol-1.1.xsd"));
46         assertNotNull(doc);
47     }
48 }