From b04147b76075c19f361273d4a192327d34fde926 Mon Sep 17 00:00:00 2001 From: Tom Kelley Date: Fri, 28 Feb 2020 15:04:33 -0800 Subject: [PATCH] Cover some branches that were created. --- tests/test_authenticator.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_authenticator.py b/tests/test_authenticator.py index 554e683..ab9e934 100644 --- a/tests/test_authenticator.py +++ b/tests/test_authenticator.py @@ -768,6 +768,42 @@ def test_add_user_fail(self): mock_datetime.now.assert_called_once_with(timezone.utc) mock_subprocess.call.assert_called_once_with(['useradd', 'bluedata']) + def test_get_username_fail(self): + # Let's use a trivial example for breaking things. + saml_data = test_constants.metadata_encoded_xml_dict['low_strength_cert']['SHA-1'] + with patch('samlauthenticator.samlauthenticator.datetime') as mock_datetime: + + mock_datetime.now.return_value = saml_data.datetime_stamp + mock_datetime.strptime = datetime.strptime + a = SAMLAuthenticator() + a.metadata_content = saml_data.metadata_xml + a.normalize_username = MagicMock() + a.xpath_username_location = '//saml:Invalid/saml:Username/saml:Path/text()' + + assert a._authenticate(None, {a.login_post_field: saml_data.b64encoded_response}) is None + mock_datetime.now.assert_called_once_with(timezone.utc) + a.normalize_username.assert_not_called() + + def test_normalize_username_fail(self): + # Let's use a trivial example for breaking things. + saml_data = test_constants.metadata_encoded_xml_dict['low_strength_cert']['SHA-1'] + with patch('samlauthenticator.samlauthenticator.datetime') as mock_datetime: + + mock_datetime.now.return_value = saml_data.datetime_stamp + mock_datetime.strptime = datetime.strptime + a = SAMLAuthenticator() + a.metadata_content = saml_data.metadata_xml + a._check_username_and_add_user = MagicMock() + + def bad_normalize(username): + return None + + a.normalize_username = bad_normalize + + assert a._authenticate(None, {a.login_post_field: saml_data.b64encoded_response}) is None + mock_datetime.now.assert_called_once_with(timezone.utc) + a._check_username_and_add_user.assert_not_called() + class TestGetRedirect(unittest.TestCase):