1 package org.collectionspace.services.common.config;
3 import java.io.ByteArrayInputStream;
4 import java.util.ArrayList;
7 import javax.xml.bind.JAXBException;
9 import org.collectionspace.services.config.AssertionAttributeProbeType;
10 import org.collectionspace.services.config.SAMLRelyingPartyType;
11 import org.collectionspace.services.config.SAMLType;
12 import org.collectionspace.services.config.ServiceConfig;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.testng.Assert;
16 import org.testng.annotations.BeforeSuite;
17 import org.testng.annotations.Test;
19 public class ServicesConfigReaderImplTest {
20 private static final Logger logger = LoggerFactory.getLogger(ServicesConfigReaderImplTest.class);
21 private static String BANNER = "-------------------------------------------------------";
22 // NOTE: adapted from https://collectionspace.atlassian.net/browse/DRYD-1702?focusedCommentId=60649
23 private static final String USERNAME_ATTRIBUTE = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress";
24 private static final String SSOID_ATTRIBUTE = "http://schemas.auth0.com/identifier";
25 private static final String SSO_CONFIG_STRING = new StringBuilder()
26 .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
27 .append("<svc:service-config xmlns:svc='http://collectionspace.org/services/config'>")
31 .append("<single-logout />")
32 .append("<relying-party-registrations>")
33 .append("<relying-party id=\"auth0\">")
34 .append("<name>Auth0 - Scenario 11</name>")
35 .append("<icon location=\"https://cdn.auth0.com/manhattan/versions/1.4478.0/assets/badge.png\" />")
36 .append("<metadata location=\"https://dev-cf0ltyyfory6gtqm.us.auth0.com/samlp/metadata/ZXtZfEN0mj96GP8LCmEUWcpuDO0OtqKY\" />")
37 .append("<assertion-username-probes>")
38 .append("<attribute name=\"" + USERNAME_ATTRIBUTE + "\" />")
39 .append("</assertion-username-probes>")
40 .append("<assertion-sso-id-probes>")
41 .append("<attribute name=\"" + SSOID_ATTRIBUTE + "\" />")
42 .append("</assertion-sso-id-probes>")
43 .append("</relying-party>")
44 .append("</relying-party-registrations>")
47 .append("</security>")
48 .append("</svc:service-config>")
50 private static final String MOCK_ROOT_DIR = "./";
51 private void testBanner(String msg) {
52 logger.info("\r" + BANNER + "\r\n" + this.getClass().getName() + "\r\n" + msg + "\r\n" + BANNER);
54 private List<AssertionAttributeProbeType> getUserNameProbesFromConfig() {
55 return getUserNameProbesFromConfig(serviceConfig);
57 private List<AssertionAttributeProbeType> getUserNameProbesFromConfig(ServiceConfig serviceConfig) {
58 SAMLType samlConfig = serviceConfig.getSecurity().getSso().getSaml();
59 List<SAMLRelyingPartyType> relyingParties = samlConfig.getRelyingPartyRegistrations().getRelyingParty();
60 SAMLRelyingPartyType relyingParty = relyingParties.get(0);
62 List<Object> usernameProbes = relyingParty.getAssertionUsernameProbes().getNameIdOrAttribute();
63 ArrayList<AssertionAttributeProbeType> up = new ArrayList<AssertionAttributeProbeType>();
64 for (Object obj : usernameProbes) {
65 AssertionAttributeProbeType a = (AssertionAttributeProbeType) obj;
71 private List<AssertionAttributeProbeType> getSsoIdProbesFromConfig() {
72 return getSsoIdProbesFromConfig(serviceConfig);
74 private List<AssertionAttributeProbeType> getSsoIdProbesFromConfig(ServiceConfig serviceConfig) {
75 SAMLType samlConfig = serviceConfig.getSecurity().getSso().getSaml();
76 List<SAMLRelyingPartyType> relyingParties = samlConfig.getRelyingPartyRegistrations().getRelyingParty();
77 SAMLRelyingPartyType relyingParty = relyingParties.get(0);
79 List<Object> ssoIdProbes = relyingParty.getAssertionSsoIdProbes().getNameIdOrAttribute();
80 ArrayList<AssertionAttributeProbeType> up = new ArrayList<AssertionAttributeProbeType>();
81 for (Object obj : ssoIdProbes) {
82 AssertionAttributeProbeType a = (AssertionAttributeProbeType) obj;
88 // the tests are below
89 private ServiceConfig serviceConfig = null;
91 public void setup() throws JAXBException {
92 ServicesConfigReaderImpl rdr = new ServicesConfigReaderImpl(MOCK_ROOT_DIR);
93 ByteArrayInputStream in = new ByteArrayInputStream(SSO_CONFIG_STRING.getBytes());
95 serviceConfig = (ServiceConfig) rdr.parse(in, ServiceConfig.class);
96 } catch (JAXBException e) {
97 logger.warn("Could not create test service config: " + e.getLocalizedMessage());
102 public void usernameProbesNotNullOrEmpty() {
103 testBanner("the username probes list is not null or empty");
105 List<AssertionAttributeProbeType> usernameProbes = getUserNameProbesFromConfig();
106 Assert.assertNotNull(usernameProbes);
107 if(null != usernameProbes) {
108 Assert.assertFalse(usernameProbes.isEmpty());
111 @Test(dependsOnMethods = {"usernameProbesNotNullOrEmpty"})
112 public void usernameProbesCorrectlyParsedFromConfig() {
113 testBanner("the username probes list has expected contents");
115 List<AssertionAttributeProbeType> usernameProbes = getUserNameProbesFromConfig();
116 Assert.assertEquals(usernameProbes.size(), 1);
117 AssertionAttributeProbeType probe = usernameProbes.get(0);
118 Assert.assertEquals(probe.getName(), USERNAME_ATTRIBUTE);
121 public void ssoIdProbesNotNullOrEmpty() {
122 testBanner("the SSO ID probes list is not null or empty");
124 List<AssertionAttributeProbeType> ssoIdProbes = getSsoIdProbesFromConfig();
125 Assert.assertNotNull(ssoIdProbes);
126 if(null != ssoIdProbes) {
127 Assert.assertFalse(ssoIdProbes.isEmpty());
130 @Test(dependsOnMethods = {"ssoIdProbesNotNullOrEmpty"})
131 public void ssoIdProbesCorrectlyParsedFromConfig() {
132 testBanner("the SSO ID probes list has expected contents");
134 List<AssertionAttributeProbeType> ssoIdProbes = getSsoIdProbesFromConfig();
135 Assert.assertEquals(ssoIdProbes.size(), 1);
136 AssertionAttributeProbeType probe = ssoIdProbes.get(0);
137 Assert.assertEquals(probe.getName(), SSOID_ATTRIBUTE);