Skip to content

Commit

Permalink
Upgrade tests to use python 3.6+ syntax (mpdavis#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
asherf authored May 6, 2021
1 parent 030e8f6 commit 2fad8a0
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 56 deletions.
18 changes: 9 additions & 9 deletions tests/algorithms/test_AES.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
# https://tools.ietf.org/html/rfc3394#section-2.2.3.1
VECTORS = (
(ALGORITHMS.A128KW,
six.ensure_binary("00112233445566778899AABBCCDDEEFF"),
six.ensure_binary("000102030405060708090A0B0C0D0E0F"),
six.ensure_binary("1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5")),
b"00112233445566778899AABBCCDDEEFF",
b"000102030405060708090A0B0C0D0E0F",
b"1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5"),
(ALGORITHMS.A192KW,
six.ensure_binary("00112233445566778899AABBCCDDEEFF0001020304050607"),
six.ensure_binary("000102030405060708090A0B0C0D0E0F1011121314151617"),
six.ensure_binary("031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2")),
b"00112233445566778899AABBCCDDEEFF0001020304050607",
b"000102030405060708090A0B0C0D0E0F1011121314151617",
b"031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2"),
(ALGORITHMS.A256KW,
six.ensure_binary("00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F"),
six.ensure_binary("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"),
six.ensure_binary("28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21")),
b"00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F",
b"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
b"28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21"),
)


Expand Down
2 changes: 1 addition & 1 deletion tests/algorithms/test_AES_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@pytest.mark.skipif(CryptographyAESKey is None,
reason="Multiple crypto backends not available for backend compatibility tests"
)
class TestBackendAesCompatibility(object):
class TestBackendAesCompatibility:
@pytest.mark.parametrize("backend_decrypt", CRYPTO_BACKENDS)
@pytest.mark.parametrize("backend_encrypt", CRYPTO_BACKENDS)
@pytest.mark.parametrize("algorithm", ALGORITHMS.AES_PSEUDO)
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithms/test_EC_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
None in (ECDSAECKey, CryptographyECKey),
reason="Multiple crypto backends not available for backend compatibility tests"
)
class TestBackendEcdsaCompatibility(object):
class TestBackendEcdsaCompatibility:

@pytest.mark.parametrize("BackendSign", [ECDSAECKey, CryptographyECKey])
@pytest.mark.parametrize("BackendVerify", [ECDSAECKey, CryptographyECKey])
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithms/test_HMAC_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
CryptographyHMACKey is None,
reason="Multiple crypto backends not available for backend compatibility tests"
)
class TestBackendAesCompatibility(object):
class TestBackendAesCompatibility:
@pytest.mark.parametrize("backend_sign", CRYPTO_BACKENDS)
@pytest.mark.parametrize("backend_verify", CRYPTO_BACKENDS)
@pytest.mark.parametrize("algorithm", SUPPORTED_ALGORITHMS)
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithms/test_RSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _actually_invalid_private_key_pkcs8_pem():


@pytest.mark.skipif(PurePythonRSAKey is None, reason="python-rsa backend not available")
class TestPurePythonRsa(object):
class TestPurePythonRsa:

def test_python_rsa_legacy_pem_read(self):
key = PurePythonRSAKey(LEGACY_INVALID_PRIVATE_KEY_PKCS8_PEM, ALGORITHMS.RS256)
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithms/test_RSA_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
None in (PurePythonRSAKey, CryptographyRSAKey),
reason="Multiple crypto backends not available for backend compatibility tests"
)
class TestBackendRsaCompatibility(object):
class TestBackendRsaCompatibility:

@pytest.mark.parametrize("BackendSign", CRYPTO_BACKENDS)
@pytest.mark.parametrize("BackendVerify", CRYPTO_BACKENDS)
Expand Down
1 change: 0 additions & 1 deletion tests/rfc/test_rfc7520.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Disable flake8 reporting
# flake8: noqa

Expand Down
1 change: 0 additions & 1 deletion tests/test_firebase.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import pytest
Expand Down
14 changes: 7 additions & 7 deletions tests/test_jwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@
OCT_512_BIT_KEY = b"\x04\xd3\x1f\xc5T\x9d\xfc\xfe\x0bd\x9d\xfa?\xaaj\xcek|\xd4-ok\t\xdb\xc8\xb1\x00\xf0\x8f\x9c,\xcf\x04\xd3\x1f\xc5T\x9d\xfc\xfe\x0bd\x9d\xfa?\xaaj\xcek|\xd4-ok\t\xdb\xc8\xb1\x00\xf0\x8f\x9c,\xcf"


class TestGetUnverifiedHeader(object):
class TestGetUnverifiedHeader:

def test_valid_header_and_auth_tag(self):
expected_header = {u"alg": u"RSA1_5", u"enc": u"A128CBC-HS256"}
expected_header = {"alg": "RSA1_5", "enc": "A128CBC-HS256"}
jwe_str = "eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0." \
"UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7" \
"Zx0-kFm1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgN" \
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_non_json_header_section_raises_jwe_parse_error(self):
jwe.get_unverified_header(jwe_str)

def test_wrong_auth_tag_is_ignored(self):
expected_header = {u"alg": u"RSA1_5", u"enc": u"A128CBC-HS256"}
expected_header = {"alg": "RSA1_5", "enc": "A128CBC-HS256"}
jwe_str = "eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0." \
"UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7" \
"Zx0-kFm1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgN" \
Expand All @@ -131,7 +131,7 @@ def test_wrong_auth_tag_is_ignored(self):

@pytest.mark.skipif(AESKey is None, reason="Test requires AES Backend")
@pytest.mark.skipif(RSAKey is RSABackendRSAKey, reason="RSA Backend does not support all modes")
class TestDecrypt(object):
class TestDecrypt:

JWE_RSA_PACKAGES = (
pytest.param(
Expand Down Expand Up @@ -353,7 +353,7 @@ def test_non_json_header_is_parse_error(self):
jwe.decrypt(jwe_str, "key")


class TestEncrypt(object):
class TestEncrypt:

@pytest.mark.skipif(AESKey is None, reason="No AES backend")
def test_rfc7516_appendix_b_direct(self, monkeypatch):
Expand Down Expand Up @@ -398,7 +398,7 @@ def test_encrypt_decrypt_aes_kw(self, alg, enc, zip):
elif alg == ALGORITHMS.A256KW:
key = OCT_256_BIT_KEY
else:
pytest.fail("I don't know how to handle enc {}".format(alg))
pytest.fail(f"I don't know how to handle enc {alg}")
expected = b"Live long and prosper."
jwe_value = jwe.encrypt(expected[:], key, enc, alg, zip)
actual = jwe.decrypt(jwe_value, key)
Expand All @@ -419,7 +419,7 @@ def test_encrypt_decrypt_dir_kw(self, enc, zip):
elif enc == ALGORITHMS.A256CBC_HS512:
key = OCT_512_BIT_KEY
else:
pytest.fail("I don't know how to handle enc {}".format(enc))
pytest.fail(f"I don't know how to handle enc {enc}")
expected = b"Live long and prosper."
jwe_value = jwe.encrypt(expected[:], key, enc, ALGORITHMS.DIR, zip)
actual = jwe.decrypt(jwe_value, key)
Expand Down
54 changes: 27 additions & 27 deletions tests/test_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def payload():
return payload


class TestJWS(object):
class TestJWS:

def test_unicode_token(self):
token = u'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'
jws.verify(token, 'secret', ['HS256'])

def test_multiple_keys(self):
Expand All @@ -44,7 +44,7 @@ def raise_exception(self, msg, sig):
jwk.HMACKey.verify = old_jwk_verify

def test_invalid_algorithm(self):
token = u'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'
with pytest.raises(JWSError):
jws.verify(token, 'secret', [None])

Expand Down Expand Up @@ -89,15 +89,15 @@ def test_round_trip_with_different_key_types(self, key):
assert verified_data['testkey'] == 'testvalue'


class TestJWK(object):
class TestJWK:
def test_jwk(self, payload):
key_data = 'key'
key = jwk.construct(key_data, algorithm='HS256')
token = jws.sign(payload, key, algorithm=ALGORITHMS.HS256)
assert jws.verify(token, key_data, ALGORITHMS.HS256) == payload


class TestHMAC(object):
class TestHMAC:

def testHMAC256(self, payload):
token = jws.sign(payload, 'secret', algorithm=ALGORITHMS.HS256)
Expand Down Expand Up @@ -212,24 +212,24 @@ def test_add_headers(self, payload):

@pytest.fixture
def jwk_set():
return {u'keys': [{u'alg': u'RS256',
u'e': u'AQAB',
u'kid': u'40aa42edac0614d7ca3f57f97ee866cdfba3b61a',
u'kty': u'RSA',
u'n': u'6lm9AEGLPFpVqnfeVFuTIZsj7vz_kxla6uW1WWtosM_MtIjXkyyiSolxiSOs3bzG66iVm71023QyOzKYFbio0hI-yZauG3g9nH-zb_AHScsjAKagHtrHmTdtq0JcNkQnAaaUwxVbjwMlYAcOh87W5jWj_MAcPvc-qjy8-WJ81UgoOUZNiKByuF4-9igxKZeskGRXuTPX64kWGBmKl-tM7VnCGMKoK3m92NPrktfBoNN_EGGthNfQsKFUdQFJFtpMuiXp9Gib7dcMGabxcG2GUl-PU086kPUyUdUYiMN2auKSOxSUZgDjT7DcI8Sn8kdQ0-tImaHi54JNa1PNNdKRpw',
u'use': u'sig'},
{u'alg': u'RS256',
u'e': u'AQAB',
u'kid': u'8fbbeea40332d2c0d27e37e1904af29b64594e57',
u'kty': u'RSA',
u'n': u'z7h6_rt35-j6NV2iQvYIuR3xvsxmEImgMl8dc8CFl4SzEWrry3QILajKxQZA9YYYfXIcZUG_6R6AghVMJetNIl2AhCoEr3RQjjNsm9PE6h5p2kQ-zIveFeb__4oIkVihYtxtoYBSdVj69nXLUAJP2bxPfU8RDp5X7hT62pKR05H8QLxH8siIQ5qR2LGFw_dJcitAVRRQofuaj_9u0CLZBfinqyRkBc7a0zi7pBxtEiIbn9sRr8Kkb_Boap6BHbnLS-YFBVarcgFBbifRf7NlK5dqE9z4OUb-dx8wCMRIPVAx_hV4Qx2anTgp1sDA6V4vd4NaCOZX-mSctNZqQmKtNw',
u'use': u'sig'},
{u'alg': u'RS256',
u'e': u'AQAB',
u'kid': u'6758b0b8eb341e90454860432d6a1648bf4de03b',
u'kty': u'RSA',
u'n': u'5K0rYaA7xtqSe1nFn_nCA10uUXY81NcohMeFsYLbBlx_NdpsmbpgtXJ6ektYR7rUdtMMLu2IONlNhkWlx-lge91okyacUrWHP88PycilUE-RnyVjbPEm3seR0VefgALfN4y_e77ljq2F7W2_kbUkTvDzriDIWvQT0WwVF5FIOBydfDDs92S-queaKgLBwt50SXJCZryLew5ODrwVsFGI4Et6MLqjS-cgWpCNwzcRqjBRsse6DXnex_zSRII4ODzKIfX4qdFBKZHO_BkTsK9DNkUayrr9cz8rFRK6TEH6XTVabgsyd6LP6PTxhpiII_pTYRSWk7CGMnm2nO0dKxzaFQ',
u'use': u'sig'}]}
return {'keys': [{'alg': 'RS256',
'e': 'AQAB',
'kid': '40aa42edac0614d7ca3f57f97ee866cdfba3b61a',
'kty': 'RSA',
'n': '6lm9AEGLPFpVqnfeVFuTIZsj7vz_kxla6uW1WWtosM_MtIjXkyyiSolxiSOs3bzG66iVm71023QyOzKYFbio0hI-yZauG3g9nH-zb_AHScsjAKagHtrHmTdtq0JcNkQnAaaUwxVbjwMlYAcOh87W5jWj_MAcPvc-qjy8-WJ81UgoOUZNiKByuF4-9igxKZeskGRXuTPX64kWGBmKl-tM7VnCGMKoK3m92NPrktfBoNN_EGGthNfQsKFUdQFJFtpMuiXp9Gib7dcMGabxcG2GUl-PU086kPUyUdUYiMN2auKSOxSUZgDjT7DcI8Sn8kdQ0-tImaHi54JNa1PNNdKRpw',
'use': 'sig'},
{'alg': 'RS256',
'e': 'AQAB',
'kid': '8fbbeea40332d2c0d27e37e1904af29b64594e57',
'kty': 'RSA',
'n': 'z7h6_rt35-j6NV2iQvYIuR3xvsxmEImgMl8dc8CFl4SzEWrry3QILajKxQZA9YYYfXIcZUG_6R6AghVMJetNIl2AhCoEr3RQjjNsm9PE6h5p2kQ-zIveFeb__4oIkVihYtxtoYBSdVj69nXLUAJP2bxPfU8RDp5X7hT62pKR05H8QLxH8siIQ5qR2LGFw_dJcitAVRRQofuaj_9u0CLZBfinqyRkBc7a0zi7pBxtEiIbn9sRr8Kkb_Boap6BHbnLS-YFBVarcgFBbifRf7NlK5dqE9z4OUb-dx8wCMRIPVAx_hV4Qx2anTgp1sDA6V4vd4NaCOZX-mSctNZqQmKtNw',
'use': 'sig'},
{'alg': 'RS256',
'e': 'AQAB',
'kid': '6758b0b8eb341e90454860432d6a1648bf4de03b',
'kty': 'RSA',
'n': '5K0rYaA7xtqSe1nFn_nCA10uUXY81NcohMeFsYLbBlx_NdpsmbpgtXJ6ektYR7rUdtMMLu2IONlNhkWlx-lge91okyacUrWHP88PycilUE-RnyVjbPEm3seR0VefgALfN4y_e77ljq2F7W2_kbUkTvDzriDIWvQT0WwVF5FIOBydfDDs92S-queaKgLBwt50SXJCZryLew5ODrwVsFGI4Et6MLqjS-cgWpCNwzcRqjBRsse6DXnex_zSRII4ODzKIfX4qdFBKZHO_BkTsK9DNkUayrr9cz8rFRK6TEH6XTVabgsyd6LP6PTxhpiII_pTYRSWk7CGMnm2nO0dKxzaFQ',
'use': 'sig'}]}


google_id_token = (
Expand All @@ -248,7 +248,7 @@ def jwk_set():
)


class TestGetKeys(object):
class TestGetKeys:

def test_dict(self):
assert ({},) == jws._get_keys({})
Expand Down Expand Up @@ -286,7 +286,7 @@ def test_jwk(self):


@pytest.mark.skipif(RSAKey is None, reason="RSA is not available")
class TestRSA(object):
class TestRSA:

def test_jwk_set(self, jwk_set):
# Would raise a JWSError if validation failed.
Expand Down Expand Up @@ -352,7 +352,7 @@ def test_private_verify_raises_warning(self, payload):
-----END PUBLIC KEY-----"""


class TestEC(object):
class TestEC:

def test_EC256(self, payload):
token = jws.sign(payload, ec_private_key, algorithm=ALGORITHMS.ES256)
Expand All @@ -372,7 +372,7 @@ def test_wrong_alg(self, payload):
jws.verify(token, rsa_public_key, ALGORITHMS.ES384)


class TestLoad(object):
class TestLoad:

def test_header_not_mapping(self):
token = 'WyJ0ZXN0Il0.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'
Expand Down
9 changes: 4 additions & 5 deletions tests/test_jwt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import base64
import json

Expand Down Expand Up @@ -53,16 +52,16 @@ def test_no_alg(self, claims, key):
algorithms=[])

@pytest.mark.parametrize("key, token",
[('1234567890', u'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCJ9.aNBlulVhiYSCzvsh1rTzXZC2eWJmNrMBjINT-0wQz4k'),
('123456789.0',u'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCJ9.D8WLFPMi3yKgua2jm3BKThFsParXpgxhIbsUc39zJDw')])
[('1234567890', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCJ9.aNBlulVhiYSCzvsh1rTzXZC2eWJmNrMBjINT-0wQz4k'),
('123456789.0','eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCJ9.D8WLFPMi3yKgua2jm3BKThFsParXpgxhIbsUc39zJDw')])
def test_numeric_key(self, key, token):
token_info = jwt.decode(token, key)
assert token_info == {"name": "test"}

def test_invalid_claims_json(self):
old_jws_verify = jws.verify
try:
token = u'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'

def return_invalid_json(token, key, algorithms, verify=True):
return b'["a", "b"}'
Expand All @@ -77,7 +76,7 @@ def return_invalid_json(token, key, algorithms, verify=True):
def test_invalid_claims(self):
old_jws_verify = jws.verify
try:
token = u'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'

def return_encoded_array(token, key, algorithms, verify=True):
return b'["a","b"]'
Expand Down
1 change: 0 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from datetime import timedelta

from jose import utils
Expand Down

0 comments on commit 2fad8a0

Please sign in to comment.