From e3095f7e1539325cc8a2bd0dbabea90ab146c3f7 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 14 Jan 2020 16:55:33 +0000 Subject: [PATCH] add tests for get_remote_user_id (Featuring a real SAML response to parse) --- tests/__init__.py | 10 ++-- tests/test_attributes.py | 35 ++++++++++- tests/test_saml_response.xml | 110 +++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 tests/test_saml_response.xml diff --git a/tests/__init__.py b/tests/__init__.py index 4bd8d25..26f9c96 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,15 +1,15 @@ -from typing import Tuple +from typing import Optional, Tuple from matrix_synapse_saml_mozilla import SamlMappingProvider -def create_mapping_provider() -> Tuple[SamlMappingProvider, dict]: +def create_mapping_provider(config_dict: Optional[dict] = None) -> SamlMappingProvider: # Default configuration - config_dict = {} + if config_dict is None: + config_dict = {} # Convert the config dictionary to a SamlMappingProvider.SamlConfig object config = SamlMappingProvider.parse_config(config_dict) # Create a new instance of the provider with the specified config - # Return the config dict as well for other test methods to use - return SamlMappingProvider(config, None), config_dict + return SamlMappingProvider(config, None) diff --git a/tests/test_attributes.py b/tests/test_attributes.py index a89af77..62a8da0 100644 --- a/tests/test_attributes.py +++ b/tests/test_attributes.py @@ -18,6 +18,11 @@ import re import time import unittest +import pkg_resources +from saml2.config import SPConfig +from saml2.response import AuthnResponse +from saml2.sigver import security_context + from synapse.api.errors import RedirectException from matrix_synapse_saml_mozilla._sessions import username_mapping_sessions @@ -37,12 +42,40 @@ class FakeResponse: self.ava["displayName"] = [display_name] +def _load_test_response() -> AuthnResponse: + response_xml = pkg_resources.resource_string( + "tests", "test_saml_response.xml" + ).decode("utf-8") + + config = SPConfig() + config.load({}) + assert config.attribute_converters is not None + + response = AuthnResponse( + sec_context=security_context(config), + attribute_converters=config.attribute_converters, + entity_id="https://host/_matrix/saml2/metadata.xml", + allow_unsolicited=True, + # tell it not to check the `destination` + asynchop=False, + ) + response.loads(response_xml, decode=False, origxml=response_xml) + response.verify() + return response + + class SamlUserAttributeTestCase(unittest.TestCase): + def test_get_remote_user_id_from_name_id(self): + resp = _load_test_response() + provider = create_mapping_provider({"use_name_id_for_remote_uid": True,}) + remote_user_id = provider.get_remote_user_id(resp, "",) + self.assertEqual(remote_user_id, "test@domain.com") + def test_redirect(self): """Creates a dummy response, feeds it to the provider and checks that it redirects to the username picker. """ - provider, config = create_mapping_provider() + provider = create_mapping_provider() response = FakeResponse(123435, "Jonny") # we expect this to redirect to the username picker diff --git a/tests/test_saml_response.xml b/tests/test_saml_response.xml new file mode 100644 index 0000000..d733056 --- /dev/null +++ b/tests/test_saml_response.xml @@ -0,0 +1,110 @@ + + + + urn:auth.issuer.com + + test@domain.com + + + + + + + https://host/_matrix/saml2/metadata.xml + + + + + urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified + + + + + ad|domain-LDAP|testuser + + + testuser@domain.com + + + Jan de + Mooij + + + Jan + + + de + Mooij + + + everyone + + group1 + group2 + + + testuser@domain.com + + + ad + + + domain-LDAP + + + false + + + Jan de + Mooij + + + testuser@domain.com + other@domain.com + + + mail=testuser@domain.com,o=com,dc=domain + + + mail=testuser@domain.com,o=com,dc=domain + + + other@domain.com + + + [object Object] + + + http://avatar_url + + + group1 + group2 + + + 2FA + + + MEDIUM + + + false + + + true + + + clientID + + + Tue Jan 14 2020 13:35:34 GMT+0000 (Coordinated Universal Time) + + + Fri Jun 16 2017 13:24:53 GMT+0000 (Coordinated Universal Time) + + + duo + + + +