33from jwt .algorithms import Algorithm , HMACAlgorithm , NoneAlgorithm
44from jwt .exceptions import InvalidKeyError
55
6+ import pytest
7+
68from .compat import unittest
79from .utils import ensure_bytes , ensure_unicode , key_path
810
@@ -21,95 +23,95 @@ def setUp(self): # noqa
2123 def test_algorithm_should_throw_exception_if_prepare_key_not_impl (self ):
2224 algo = Algorithm ()
2325
24- with self . assertRaises (NotImplementedError ):
26+ with pytest . raises (NotImplementedError ):
2527 algo .prepare_key ('test' )
2628
2729 def test_algorithm_should_throw_exception_if_sign_not_impl (self ):
2830 algo = Algorithm ()
2931
30- with self . assertRaises (NotImplementedError ):
32+ with pytest . raises (NotImplementedError ):
3133 algo .sign ('message' , 'key' )
3234
3335 def test_algorithm_should_throw_exception_if_verify_not_impl (self ):
3436 algo = Algorithm ()
3537
36- with self . assertRaises (NotImplementedError ):
38+ with pytest . raises (NotImplementedError ):
3739 algo .verify ('message' , 'key' , 'signature' )
3840
3941 def test_none_algorithm_should_throw_exception_if_key_is_not_none (self ):
4042 algo = NoneAlgorithm ()
4143
42- with self . assertRaises (InvalidKeyError ):
44+ with pytest . raises (InvalidKeyError ):
4345 algo .prepare_key ('123' )
4446
4547 def test_hmac_should_reject_nonstring_key (self ):
4648 algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
4749
48- with self . assertRaises (TypeError ) as context :
50+ with pytest . raises (TypeError ) as context :
4951 algo .prepare_key (object ())
5052
51- exception = context .exception
52- self . assertEqual ( str (exception ), 'Expecting a string- or bytes-formatted key.' )
53+ exception = context .value
54+ assert str (exception ) == 'Expecting a string- or bytes-formatted key.'
5355
5456 def test_hmac_should_accept_unicode_key (self ):
5557 algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
5658
5759 algo .prepare_key (ensure_unicode ('awesome' ))
5860
59- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
61+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
6062 def test_hmac_should_throw_exception_if_key_is_pem_public_key (self ):
6163 algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
6264
63- with self . assertRaises (InvalidKeyError ):
65+ with pytest . raises (InvalidKeyError ):
6466 with open (key_path ('testkey2_rsa.pub.pem' ), 'r' ) as keyfile :
6567 algo .prepare_key (keyfile .read ())
6668
67- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
69+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
6870 def test_hmac_should_throw_exception_if_key_is_x509_certificate (self ):
6971 algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
7072
71- with self . assertRaises (InvalidKeyError ):
73+ with pytest . raises (InvalidKeyError ):
7274 with open (key_path ('testkey_rsa.cer' ), 'r' ) as keyfile :
7375 algo .prepare_key (keyfile .read ())
7476
75- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
77+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
7678 def test_hmac_should_throw_exception_if_key_is_ssh_public_key (self ):
7779 algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
7880
79- with self . assertRaises (InvalidKeyError ):
81+ with pytest . raises (InvalidKeyError ):
8082 with open (key_path ('testkey_rsa.pub' ), 'r' ) as keyfile :
8183 algo .prepare_key (keyfile .read ())
8284
83- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
85+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
8486 def test_hmac_should_throw_exception_if_key_is_x509_cert (self ):
8587 algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
8688
87- with self . assertRaises (InvalidKeyError ):
89+ with pytest . raises (InvalidKeyError ):
8890 with open (key_path ('testkey2_rsa.pub.pem' ), 'r' ) as keyfile :
8991 algo .prepare_key (keyfile .read ())
9092
91- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
93+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
9294 def test_rsa_should_parse_pem_public_key (self ):
9395 algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
9496
9597 with open (key_path ('testkey2_rsa.pub.pem' ), 'r' ) as pem_key :
9698 algo .prepare_key (pem_key .read ())
9799
98- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
100+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
99101 def test_rsa_should_accept_unicode_key (self ):
100102 algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
101103
102104 with open (key_path ('testkey_rsa' ), 'r' ) as rsa_key :
103105 algo .prepare_key (ensure_unicode (rsa_key .read ()))
104106
105- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
107+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
106108 def test_rsa_should_reject_non_string_key (self ):
107109 algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
108110
109- with self . assertRaises (TypeError ):
111+ with pytest . raises (TypeError ):
110112 algo .prepare_key (None )
111113
112- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
114+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
113115 def test_rsa_verify_should_return_false_if_signature_invalid (self ):
114116 algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
115117
@@ -129,9 +131,9 @@ def test_rsa_verify_should_return_false_if_signature_invalid(self):
129131 pub_key = algo .prepare_key (keyfile .read ())
130132
131133 result = algo .verify (message , pub_key , sig )
132- self . assertFalse ( result )
134+ assert not result
133135
134- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
136+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
135137 def test_rsa_verify_should_return_true_if_signature_valid (self ):
136138 algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
137139
@@ -151,21 +153,21 @@ def test_rsa_verify_should_return_true_if_signature_valid(self):
151153 result = algo .verify (message , pub_key , sig )
152154 self .assertTrue (result )
153155
154- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
156+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
155157 def test_ec_should_reject_non_string_key (self ):
156158 algo = ECAlgorithm (ECAlgorithm .SHA256 )
157159
158- with self . assertRaises (TypeError ):
160+ with pytest . raises (TypeError ):
159161 algo .prepare_key (None )
160162
161- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
163+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
162164 def test_ec_should_accept_unicode_key (self ):
163165 algo = ECAlgorithm (ECAlgorithm .SHA256 )
164166
165167 with open (key_path ('testkey_ec' ), 'r' ) as ec_key :
166168 algo .prepare_key (ensure_unicode (ec_key .read ()))
167169
168- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
170+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
169171 def test_ec_verify_should_return_false_if_signature_invalid (self ):
170172 algo = ECAlgorithm (ECAlgorithm .SHA256 )
171173
@@ -182,9 +184,9 @@ def test_ec_verify_should_return_false_if_signature_invalid(self):
182184 pub_key = algo .prepare_key (keyfile .read ())
183185
184186 result = algo .verify (message , pub_key , sig )
185- self . assertFalse ( result )
187+ assert not result
186188
187- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
189+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
188190 def test_ec_verify_should_return_true_if_signature_valid (self ):
189191 algo = ECAlgorithm (ECAlgorithm .SHA256 )
190192
@@ -200,9 +202,9 @@ def test_ec_verify_should_return_true_if_signature_valid(self):
200202 pub_key = algo .prepare_key (keyfile .read ())
201203
202204 result = algo .verify (message , pub_key , sig )
203- self . assertTrue ( result )
205+ assert result
204206
205- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
207+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
206208 def test_rsa_pss_sign_then_verify_should_return_true (self ):
207209 algo = RSAPSSAlgorithm (RSAPSSAlgorithm .SHA256 )
208210
@@ -216,9 +218,9 @@ def test_rsa_pss_sign_then_verify_should_return_true(self):
216218 pub_key = algo .prepare_key (keyfile .read ())
217219
218220 result = algo .verify (message , pub_key , sig )
219- self . assertTrue ( result )
221+ assert result
220222
221- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
223+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
222224 def test_rsa_pss_verify_should_return_false_if_signature_invalid (self ):
223225 algo = RSAPSSAlgorithm (RSAPSSAlgorithm .SHA256 )
224226
@@ -238,9 +240,9 @@ def test_rsa_pss_verify_should_return_false_if_signature_invalid(self):
238240 jwt_pub_key = algo .prepare_key (keyfile .read ())
239241
240242 result = algo .verify (jwt_message , jwt_pub_key , jwt_sig )
241- self . assertFalse ( result )
243+ assert not result
242244
243- @unittest . skipIf (not has_crypto , 'Not supported without cryptography library' )
245+ @pytest . mark . skipif (not has_crypto , reason = 'Not supported without cryptography library' )
244246 def test_rsa_pss_verify_should_return_true_if_signature_valid (self ):
245247 algo = RSAPSSAlgorithm (RSAPSSAlgorithm .SHA256 )
246248
@@ -258,4 +260,4 @@ def test_rsa_pss_verify_should_return_true_if_signature_valid(self):
258260 jwt_pub_key = algo .prepare_key (keyfile .read ())
259261
260262 result = algo .verify (jwt_message , jwt_pub_key , jwt_sig )
261- self . assertTrue ( result )
263+ assert result
0 commit comments