diff options
author | Mark Adams <mark@markadams.me> | 2017-03-14 08:04:03 -0500 |
---|---|---|
committer | Mark Adams <mark@markadams.me> | 2017-03-14 10:43:58 -0500 |
commit | d04339de54fca44c09ae9105627ab4ae7e0abdb1 (patch) | |
tree | dcfdd94edb8d89b2222949ead86694a9e1720be8 /tests | |
parent | 1710c1524c69c39dfece7a24b87179be5eeff217 (diff) | |
download | pyjwt-fix-key-errors.tar.gz |
Refactor error handling in Algorithm.prepare_key() methodsfix-key-errors
Our error handling in Algorithm.prepare_key() was previously weird and
kind of inconsistent. This change makes a number of improvements:
* Refactors RSA and ECDSA prepare_key() methods to reduce nesting and
make the code simpler to understand
* All calls to Algorithm.prepare_key() return InvalidKeyError (or a
subclass) or a valid key instance.
* Created a new InvalidAsymmetricKeyError class that is used to provide
a standard message when an invalid RSA or ECDSA key is used.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/contrib/test_algorithms.py | 5 | ||||
-rw-r--r-- | tests/test_algorithms.py | 8 |
2 files changed, 7 insertions, 6 deletions
diff --git a/tests/contrib/test_algorithms.py b/tests/contrib/test_algorithms.py index 6d5ca75..b774ad1 100644 --- a/tests/contrib/test_algorithms.py +++ b/tests/contrib/test_algorithms.py @@ -1,5 +1,6 @@ import base64 +from jwt.exceptions import InvalidAsymmetricKeyError from jwt.utils import force_bytes, force_unicode import pytest @@ -36,7 +37,7 @@ class TestPycryptoAlgorithms: def test_rsa_should_reject_non_string_key(self): algo = RSAAlgorithm(RSAAlgorithm.SHA256) - with pytest.raises(TypeError): + with pytest.raises(InvalidAsymmetricKeyError): algo.prepare_key(None) def test_rsa_sign_should_generate_correct_signature_value(self): @@ -117,7 +118,7 @@ class TestEcdsaAlgorithms: def test_ec_should_reject_non_string_key(self): algo = ECAlgorithm(ECAlgorithm.SHA256) - with pytest.raises(TypeError): + with pytest.raises(InvalidAsymmetricKeyError): algo.prepare_key(None) def test_ec_should_accept_unicode_key(self): diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index 11d8cd0..69501ee 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -58,11 +58,11 @@ class TestAlgorithms: def test_hmac_should_reject_nonstring_key(self): algo = HMACAlgorithm(HMACAlgorithm.SHA256) - with pytest.raises(TypeError) as context: + with pytest.raises(InvalidKeyError) as context: algo.prepare_key(object()) exception = context.value - assert str(exception) == 'Expected a string value' + assert str(exception) == 'HMAC secret key must be a string type.' def test_hmac_should_accept_unicode_key(self): algo = HMACAlgorithm(HMACAlgorithm.SHA256) @@ -144,7 +144,7 @@ class TestAlgorithms: def test_rsa_should_reject_non_string_key(self): algo = RSAAlgorithm(RSAAlgorithm.SHA256) - with pytest.raises(TypeError): + with pytest.raises(InvalidKeyError): algo.prepare_key(None) @pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library') @@ -358,7 +358,7 @@ class TestAlgorithms: def test_ec_should_reject_non_string_key(self): algo = ECAlgorithm(ECAlgorithm.SHA256) - with pytest.raises(TypeError): + with pytest.raises(InvalidKeyError): algo.prepare_key(None) @pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library') |