Skip to content

Commit 6315f86

Browse files
committed
Converted all unittest constructs to pytest
1 parent cf8672d commit 6315f86

File tree

5 files changed

+199
-189
lines changed

5 files changed

+199
-189
lines changed

tests/contrib/test_algorithms.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import base64
22

3+
import pytest
4+
35
from ..compat import unittest
46
from ..utils import ensure_bytes, ensure_unicode, key_path
57

@@ -16,7 +18,7 @@
1618
has_ecdsa = False
1719

1820

19-
@unittest.skipIf(not has_pycrypto, 'Not supported without PyCrypto library')
21+
@pytest.mark.skipif(not has_pycrypto, reason='Not supported without PyCrypto library')
2022
class TestPycryptoAlgorithms(unittest.TestCase):
2123
def setUp(self): # noqa
2224
pass
@@ -36,7 +38,7 @@ def test_rsa_should_accept_unicode_key(self):
3638
def test_rsa_should_reject_non_string_key(self):
3739
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
3840

39-
with self.assertRaises(TypeError):
41+
with pytest.raises(TypeError):
4042
algo.prepare_key(None)
4143

4244
def test_rsa_sign_should_generate_correct_signature_value(self):
@@ -60,7 +62,7 @@ def test_rsa_sign_should_generate_correct_signature_value(self):
6062

6163
algo.sign(jwt_message, jwt_key)
6264
result = algo.verify(jwt_message, jwt_pub_key, expected_sig)
63-
self.assertTrue(result)
65+
assert result
6466

6567
def test_rsa_verify_should_return_false_if_signature_invalid(self):
6668
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
@@ -81,7 +83,7 @@ def test_rsa_verify_should_return_false_if_signature_invalid(self):
8183
jwt_pub_key = algo.prepare_key(keyfile.read())
8284

8385
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
84-
self.assertFalse(result)
86+
assert not result
8587

8688
def test_rsa_verify_should_return_true_if_signature_valid(self):
8789
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
@@ -100,7 +102,7 @@ def test_rsa_verify_should_return_true_if_signature_valid(self):
100102
jwt_pub_key = algo.prepare_key(keyfile.read())
101103

102104
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
103-
self.assertTrue(result)
105+
assert result
104106

105107
def test_rsa_prepare_key_should_be_idempotent(self):
106108
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
@@ -109,15 +111,15 @@ def test_rsa_prepare_key_should_be_idempotent(self):
109111
jwt_pub_key_first = algo.prepare_key(keyfile.read())
110112
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
111113

112-
self.assertEqual(jwt_pub_key_first, jwt_pub_key_second)
114+
assert jwt_pub_key_first == jwt_pub_key_second
113115

114116

115-
@unittest.skipIf(not has_ecdsa, 'Not supported without ecdsa library')
117+
@pytest.mark.skipif(not has_ecdsa, reason='Not supported without ecdsa library')
116118
class TestEcdsaAlgorithms(unittest.TestCase):
117119
def test_ec_should_reject_non_string_key(self):
118120
algo = ECAlgorithm(ECAlgorithm.SHA256)
119121

120-
with self.assertRaises(TypeError):
122+
with pytest.raises(TypeError):
121123
algo.prepare_key(None)
122124

123125
def test_ec_should_accept_unicode_key(self):
@@ -145,7 +147,7 @@ def test_ec_sign_should_generate_correct_signature_value(self):
145147

146148
algo.sign(jwt_message, jwt_key)
147149
result = algo.verify(jwt_message, jwt_pub_key, expected_sig)
148-
self.assertTrue(result)
150+
assert result
149151

150152
def test_ec_verify_should_return_false_if_signature_invalid(self):
151153
algo = ECAlgorithm(ECAlgorithm.SHA256)
@@ -164,7 +166,7 @@ def test_ec_verify_should_return_false_if_signature_invalid(self):
164166
jwt_pub_key = algo.prepare_key(keyfile.read())
165167

166168
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
167-
self.assertFalse(result)
169+
assert not result
168170

169171
def test_ec_verify_should_return_true_if_signature_valid(self):
170172
algo = ECAlgorithm(ECAlgorithm.SHA256)
@@ -181,7 +183,7 @@ def test_ec_verify_should_return_true_if_signature_valid(self):
181183
jwt_pub_key = algo.prepare_key(keyfile.read())
182184

183185
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
184-
self.assertTrue(result)
186+
assert result
185187

186188
def test_ec_prepare_key_should_be_idempotent(self):
187189
algo = ECAlgorithm(ECAlgorithm.SHA256)
@@ -190,4 +192,4 @@ def test_ec_prepare_key_should_be_idempotent(self):
190192
jwt_pub_key_first = algo.prepare_key(keyfile.read())
191193
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
192194

193-
self.assertEqual(jwt_pub_key_first, jwt_pub_key_second)
195+
assert jwt_pub_key_first == jwt_pub_key_second

tests/test_algorithms.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from jwt.algorithms import Algorithm, HMACAlgorithm, NoneAlgorithm
44
from jwt.exceptions import InvalidKeyError
55

6+
import pytest
7+
68
from .compat import unittest
79
from .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

Comments
 (0)