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

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