Skip to content

Commit 975fa0f

Browse files
committed
Added some more tests to improve coverage for jwt.contrib.algorithms
1 parent 3519665 commit 975fa0f

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/contrib/test_algorithms.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ def test_rsa_should_reject_non_string_key(self):
3939
with self.assertRaises(TypeError):
4040
algo.prepare_key(None)
4141

42+
def test_rsa_sign_should_generate_correct_signature_value(self):
43+
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
44+
45+
jwt_message = ensure_bytes('Hello World!')
46+
47+
expected_sig = base64.b64decode(ensure_bytes(
48+
'yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp'
49+
'10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl'
50+
'2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix'
51+
'sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX'
52+
'fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA'
53+
'APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA=='))
54+
55+
with open(key_path('testkey_rsa'), 'r') as keyfile:
56+
jwt_key = algo.prepare_key(keyfile.read())
57+
58+
with open(key_path('testkey_rsa.pub'), 'r') as keyfile:
59+
jwt_pub_key = algo.prepare_key(keyfile.read())
60+
61+
algo.sign(jwt_message, jwt_key)
62+
result = algo.verify(jwt_message, jwt_pub_key, expected_sig)
63+
self.assertTrue(result)
64+
4265
def test_rsa_verify_should_return_false_if_signature_invalid(self):
4366
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
4467

@@ -79,6 +102,15 @@ def test_rsa_verify_should_return_true_if_signature_valid(self):
79102
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
80103
self.assertTrue(result)
81104

105+
def test_rsa_prepare_key_should_be_idempotent(self):
106+
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
107+
108+
with open(key_path('testkey_rsa.pub'), 'r') as keyfile:
109+
jwt_pub_key_first = algo.prepare_key(keyfile.read())
110+
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
111+
112+
self.assertEqual(jwt_pub_key_first, jwt_pub_key_second)
113+
82114

83115
@unittest.skipIf(not has_ecdsa, 'Not supported without ecdsa library')
84116
class TestEcdsaAlgorithms(unittest.TestCase):
@@ -94,6 +126,27 @@ def test_ec_should_accept_unicode_key(self):
94126
with open(key_path('testkey_ec'), 'r') as ec_key:
95127
algo.prepare_key(ensure_unicode(ec_key.read()))
96128

129+
def test_ec_sign_should_generate_correct_signature_value(self):
130+
algo = ECAlgorithm(ECAlgorithm.SHA256)
131+
132+
jwt_message = ensure_bytes('Hello World!')
133+
134+
expected_sig = base64.b64decode(ensure_bytes(
135+
'MIGIAkIB9vYz+inBL8aOTA4auYz/zVuig7TT1bQgKROIQX9YpViHkFa4DT5'
136+
'5FuFKn9XzVlk90p6ldEj42DC9YecXHbC2t+cCQgCicY+8f3f/KCNtWK7cif'
137+
'6vdsVwm6Lrjs0Ag6ZqCf+olN11hVt1qKBC4lXppqB1gNWEmNQaiz1z2QRyc'
138+
'zJ8hSJmbw=='))
139+
140+
with open(key_path('testkey_ec'), 'r') as keyfile:
141+
jwt_key = algo.prepare_key(keyfile.read())
142+
143+
with open(key_path('testkey_ec.pub'), 'r') as keyfile:
144+
jwt_pub_key = algo.prepare_key(keyfile.read())
145+
146+
algo.sign(jwt_message, jwt_key)
147+
result = algo.verify(jwt_message, jwt_pub_key, expected_sig)
148+
self.assertTrue(result)
149+
97150
def test_ec_verify_should_return_false_if_signature_invalid(self):
98151
algo = ECAlgorithm(ECAlgorithm.SHA256)
99152

@@ -129,3 +182,12 @@ def test_ec_verify_should_return_true_if_signature_valid(self):
129182

130183
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
131184
self.assertTrue(result)
185+
186+
def test_ec_prepare_key_should_be_idempotent(self):
187+
algo = ECAlgorithm(ECAlgorithm.SHA256)
188+
189+
with open(key_path('testkey_ec.pub'), 'r') as keyfile:
190+
jwt_pub_key_first = algo.prepare_key(keyfile.read())
191+
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
192+
193+
self.assertEqual(jwt_pub_key_first, jwt_pub_key_second)

0 commit comments

Comments
 (0)