Update tests

master
Andrew Morgan 5 years ago
parent df8b731edd
commit ce2a6b977f

@ -48,56 +48,69 @@ def _make_test_saml_response(
} }
if display_name: if display_name:
self.ava["displayName"] = display_name self.ava["displayName"] = [display_name]
return FakeResponse() return FakeResponse()
class SamlUsernameTestCase(unittest.TestCase): class SamlUserAttributeTestCase(unittest.TestCase):
def test_normal_user(self): def _attribute_test(
self,
input_uid: str,
input_displayname: Optional[str],
output_localpart: str,
output_displayname: Optional[str],
):
"""Creates a dummy response, feeds it to the provider and checks the output
Args:
input_uid: The value of the mxid_source_attribute that the provider will
base the generated localpart off of.
input_displayname: The saml auth response displayName value that the
provider will generate a Matrix user displayname from.
output_localpart: The expected mxid localpart.
output_displayname: The expected matrix displayname.
"""
provider, config = create_mapping_provider() provider, config = create_mapping_provider()
response = _make_test_saml_response(config, "john*doe2000#@example.com", None) response = _make_test_saml_response(config, input_uid, input_displayname)
attribute_dict = provider.saml_response_to_user_attributes(response) attribute_dict = provider.saml_response_to_user_attributes(response)
self.assertEqual(attribute_dict["mxid_localpart"], "john.doe2000") self.assertEqual(attribute_dict["mxid_localpart"], output_localpart)
self.assertEqual(attribute_dict["displayname"], "john.doe2000") self.assertEqual(attribute_dict["displayname"], output_displayname)
def test_multiple_adjacent_symbols(self): def test_normal_user(self):
provider = create_mapping_provider() self._attribute_test("john*doe2000#@example.com", None, "john.doe2000", None)
def test_normal_user_displayname(self):
self._attribute_test(
"john*doe2000#@example.com", "Jonny", "john.doe2000", "Jonny"
)
username = "bob%^$&#!bobby@example.com" def test_multiple_adjacent_symbols(self):
localpart = provider.saml_response_to_user_attributes(username) self._attribute_test("bob%^$&#!bobby@example.com", None, "bob.bobby", None)
self.assertEqual(localpart, "bob.bobby")
def test_username_does_not_end_with_dot(self): def test_username_does_not_end_with_dot(self):
"""This is allowed in mxid syntax, but is not aesthetically pleasing""" """This is allowed in mxid syntax, but is not aesthetically pleasing"""
provider = create_mapping_provider() self._attribute_test("bob.bobby$@example.com", None, "bob.bobby", None)
username = "bob.bobby$@example.com"
localpart = provider.saml_response_to_user_attributes(username)
self.assertEqual(localpart, "bob.bobby")
def test_username_no_email(self): def test_username_no_email(self):
provider = create_mapping_provider() self._attribute_test("bob.bobby", None, "bob.bobby", None)
username = "bob.bobby"
localpart = provider.saml_response_to_user_attributes(username)
self.assertEqual(localpart, "bob.bobby")
def test_username_starting_with_underscore(self): def test_username_starting_with_underscore(self):
provider = create_mapping_provider() self._attribute_test(
"_twilight (sparkle)@somewhere.com", None, "twilight.sparkle", None
username = "_twilight (sparkle)@somewhere.com" )
localpart = provider.saml_response_to_user_attributes(username)
self.assertEqual(localpart, "twilight.sparkle")
def test_existing_user(self): def test_existing_user(self):
provider = create_mapping_provider() provider, config = create_mapping_provider()
response = _make_test_saml_response(config, "wibble%@wobble.com", None)
username = "wibble%@wobble.com" attribute_dict = provider.saml_response_to_user_attributes(response)
localpart = provider.saml_response_to_user_attributes(username)
# Simulate a failure on the first attempt # Simulate a failure on the first attempt
localpart = provider.saml_response_to_user_attributes(username, failures=1) attribute_dict = provider.saml_response_to_user_attributes(response, failures=1)
self.assertEqual(localpart, "wibble1")
self.assertEqual(attribute_dict["mxid_localpart"], "wibble1")
self.assertEqual(attribute_dict["displayname"], None)
Loading…
Cancel
Save