diff options
author | Ivan Kanakarakis <ivan.kanak@gmail.com> | 2019-01-10 22:53:56 +0200 |
---|---|---|
committer | Ivan Kanakarakis <ivan.kanak@gmail.com> | 2019-01-14 15:29:44 +0200 |
commit | cb760c81ce6739821782040b648ce42527b22b21 (patch) | |
tree | cadb18ca393332a9a6c0805b074f100bda7ebdc0 /tests | |
parent | 7f4e3e51027f2760240be7af058ab5329b73d14d (diff) | |
download | pysaml2-cb760c81ce6739821782040b648ce42527b22b21.tar.gz |
Convert exception expectation to with-raises idiom
Signed-off-by: Ivan Kanakarakis <ivan.kanak@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_40_sigver.py | 30 | ||||
-rw-r--r-- | tests/test_50_server.py | 63 |
2 files changed, 33 insertions, 60 deletions
diff --git a/tests/test_40_sigver.py b/tests/test_40_sigver.py index 898d1387..092fbc67 100644 --- a/tests/test_40_sigver.py +++ b/tests/test_40_sigver.py @@ -932,14 +932,13 @@ def test_xmlsec_err(): ("", "", "givenName"): ("Bar", ""), }) ) - try: - sec.sign_statement(assertion, class_name(assertion), - key_file=INVALID_KEY, - node_id=assertion.id) - except (XmlsecError, SigverError) as err: # should throw an exception - pass - else: - assert False + with raises(XmlsecError): + sec.sign_statement( + assertion, + class_name(assertion), + key_file=INVALID_KEY, + node_id=assertion.id, + ) def test_xmlsec_err_non_ascii_ava(): @@ -961,14 +960,13 @@ def test_xmlsec_err_non_ascii_ava(): ("", "", "givenName"): ("Bär", ""), }) ) - try: - sec.sign_statement(assertion, class_name(assertion), - key_file=INVALID_KEY, - node_id=assertion.id) - except (XmlsecError, SigverError) as err: # should throw an exception - pass - else: - assert False + with raises(XmlsecError): + sec.sign_statement( + assertion, + class_name(assertion), + key_file=INVALID_KEY, + node_id=assertion.id, + ) def test_sha256_signing(): diff --git a/tests/test_50_server.py b/tests/test_50_server.py index 11ace47b..dc6cbf42 100644 --- a/tests/test_50_server.py +++ b/tests/test_50_server.py @@ -8,7 +8,7 @@ from six.moves.urllib.parse import parse_qs import uuid from saml2.cert import OpenSSLWrapper -from saml2.sigver import make_temp, EncryptError, CertificateError +from saml2.sigver import make_temp, DecryptError, EncryptError, CertificateError from saml2.assertion import Policy from saml2.authn_context import INTERNETPROTOCOLPASSWORD from saml2.saml import NameID, NAMEID_FORMAT_TRANSIENT @@ -34,6 +34,7 @@ from py.test import raises from pathutils import full_path import saml2.xmldsig as ds + nid = NameID(name_qualifier="foo", format=NAMEID_FORMAT_TRANSIENT, text="123456") @@ -171,7 +172,7 @@ class TestServer1(): assert attr1.attribute_value[0].text == "Derek" assert attr0.friendly_name == "sn" assert attr0.attribute_value[0].text == "Jeter" - # + subject = assertion.subject assert _eq(subject.keyswv(), ["text", "name_id"]) assert subject.text == "_aaa" @@ -613,9 +614,11 @@ class TestServer1(): decr_text_old = copy.deepcopy("%s" % signed_resp) - decr_text = self.server.sec.decrypt(signed_resp, self.client.config.encryption_keypairs[0]["key_file"]) - - assert decr_text == decr_text_old + with raises(DecryptError): + decr_text = self.server.sec.decrypt( + signed_resp, + self.client.config.encryption_keypairs[0]["key_file"], + ) decr_text = self.server.sec.decrypt(signed_resp, self.client.config.encryption_keypairs[1]["key_file"]) @@ -958,7 +961,7 @@ class TestServer1(): self.verify_advice_assertion(resp, decr_text_2) def test_encrypted_response_8(self): - try: + with raises(EncryptError): _resp = self.server.create_authn_response( self.ava, "id12", # in_response_to @@ -973,13 +976,8 @@ class TestServer1(): encrypt_cert_advice="whatever", encrypt_cert_assertion="whatever" ) - assert False, "Must throw an exception" - except EncryptError as ex: - pass - except Exception as ex: - assert False, "Wrong exception!" - try: + with raises(EncryptError): _resp = self.server.create_authn_response( self.ava, "id12", # in_response_to @@ -993,13 +991,8 @@ class TestServer1(): pefim=True, encrypt_cert_advice="whatever", ) - assert False, "Must throw an exception" - except EncryptError as ex: - pass - except Exception as ex: - assert False, "Wrong exception!" - try: + with raises(EncryptError): _resp = self.server.create_authn_response( self.ava, "id12", # in_response_to @@ -1013,15 +1006,10 @@ class TestServer1(): encrypted_advice_attributes=False, encrypt_cert_assertion="whatever" ) - assert False, "Must throw an exception" - except EncryptError as ex: - pass - except Exception as ex: - assert False, "Wrong exception!" _server = Server("idp_conf_verify_cert") - try: + with raises(CertificateError): _resp = _server.create_authn_response( self.ava, "id12", # in_response_to @@ -1036,13 +1024,8 @@ class TestServer1(): encrypt_cert_advice="whatever", encrypt_cert_assertion="whatever" ) - assert False, "Must throw an exception" - except CertificateError as ex: - pass - except Exception as ex: - assert False, "Wrong exception!" - try: + with raises(CertificateError): _resp = _server.create_authn_response( self.ava, "id12", # in_response_to @@ -1056,13 +1039,8 @@ class TestServer1(): pefim=True, encrypt_cert_advice="whatever", ) - assert False, "Must throw an exception" - except CertificateError as ex: - pass - except Exception as ex: - assert False, "Wrong exception!" - try: + with raises(CertificateError): _resp = _server.create_authn_response( self.ava, "id12", # in_response_to @@ -1076,11 +1054,6 @@ class TestServer1(): encrypted_advice_attributes=False, encrypt_cert_assertion="whatever" ) - assert False, "Must throw an exception" - except CertificateError as ex: - pass - except Exception as ex: - assert False, "Wrong exception!" def test_encrypted_response_9(self): _server = Server("idp_conf_sp_no_encrypt") @@ -1715,9 +1688,11 @@ class TestServer1NonAsciiAva(): decr_text_old = copy.deepcopy("%s" % signed_resp) - decr_text = self.server.sec.decrypt(signed_resp, self.client.config.encryption_keypairs[0]["key_file"]) - - assert decr_text == decr_text_old + with raises(DecryptError): + decr_text = self.server.sec.decrypt( + signed_resp, + self.client.config.encryption_keypairs[0]["key_file"], + ) decr_text = self.server.sec.decrypt(signed_resp, self.client.config.encryption_keypairs[1]["key_file"]) |