From 9e1855b5b1b1688233d65c642827a7c48c3c717b Mon Sep 17 00:00:00 2001 From: Eric Barry Date: Sat, 27 Jul 2019 16:58:38 -0400 Subject: [PATCH 1/2] #35 Adding in tests to cover bugs --- tests/unit/test_saml.py | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/unit/test_saml.py b/tests/unit/test_saml.py index db2e218..89a63b6 100644 --- a/tests/unit/test_saml.py +++ b/tests/unit/test_saml.py @@ -258,6 +258,73 @@ def tests_uses_default_form_values(self, generic_auth, generic_config, } ) + def test_input_missing_name_attribute(self, generic_auth, + generic_config, + mock_requests_session): + saml_form = ( + '' + '
' + '' + '' + '' + '' + '
' + '' + ) + mock_requests_session.get.return_value = mock.Mock( + spec=requests.Response, status_code=200, text=saml_form + ) + mock_requests_session.post.return_value = mock.Mock( + spec=requests.Response, status_code=200, text=( + '
' + ) + ) + saml_assertion = generic_auth.retrieve_saml_assertion(generic_config) + assert saml_assertion == 'fakeassertion' + + mock_requests_session.post.assert_called_with( + "https://example.com/path/login/", verify=True, + data={ + 'username': 'monty', + 'password': 'mypassword', + 'spam': 'eggs' + } + ) + + def test_boolean_presence_attribute(self, generic_auth, + generic_config, + mock_requests_session): + saml_form = ( + '' + '
' + '' + '' + '' + '
' + '' + ) + mock_requests_session.get.return_value = mock.Mock( + spec=requests.Response, status_code=200, text=saml_form + ) + mock_requests_session.post.return_value = mock.Mock( + spec=requests.Response, status_code=200, text=( + '
' + ) + ) + saml_assertion = generic_auth.retrieve_saml_assertion(generic_config) + assert saml_assertion == 'fakeassertion' + + mock_requests_session.post.assert_called_with( + "https://example.com/path/login/", verify=True, + data={ + 'username': 'monty', + 'password': 'mypassword', + 'spam': 'eggs' + } + ) + def test_error_getting_form(self, generic_auth, mock_requests_session, generic_config): mock_requests_session.get.return_value = mock.Mock( From b864ab85041f5b90b72c7ba5b3d370d82cb49490 Mon Sep 17 00:00:00 2001 From: Eric Barry Date: Sat, 27 Jul 2019 17:19:27 -0400 Subject: [PATCH 2/2] #35 Adding in fixes to broken tests, renaming test and data to be more readable --- awsprocesscreds/saml.py | 6 ++++-- tests/unit/test_saml.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/awsprocesscreds/saml.py b/awsprocesscreds/saml.py index 16ed432..81cb60e 100644 --- a/awsprocesscreds/saml.py +++ b/awsprocesscreds/saml.py @@ -157,7 +157,8 @@ def _retrieve_login_form_from_endpoint(self, endpoint): if not form_action.lower().startswith('https://'): raise SAMLError('Your SAML IdP must use HTTPS connection') payload = dict((tag.attrib['name'], tag.attrib.get('value', '')) - for tag in login_form_html_node.findall(".//input")) + for tag in login_form_html_node.findall( + ".//input[@name]")) return form_action, payload def _assert_non_error_response(self, response): @@ -287,7 +288,8 @@ def _dict2str(self, d): # so that the output will be suitable to be fed into an ET later. parts = [] for k, v in d.items(): - escaped_value = escape(v) # pylint: disable=deprecated-method + escaped_value = escape( # pylint: disable=deprecated-method + v) if v is not None else None parts.append('%s="%s"' % (k, escaped_value)) return ' '.join(sorted(parts)) diff --git a/tests/unit/test_saml.py b/tests/unit/test_saml.py index 89a63b6..00888af 100644 --- a/tests/unit/test_saml.py +++ b/tests/unit/test_saml.py @@ -292,13 +292,13 @@ def test_input_missing_name_attribute(self, generic_auth, } ) - def test_boolean_presence_attribute(self, generic_auth, + def test_boolean_attribute_handling(self, generic_auth, generic_config, mock_requests_session): saml_form = ( '' '
' - '' + '' '' '' '
'