From 2fad8a0d0896171c7f6ab503cdb13db49f2fd81b Mon Sep 17 00:00:00 2001 From: Asher Foa Date: Wed, 5 May 2021 18:40:42 -0700 Subject: [PATCH] Upgrade tests to use python 3.6+ syntax (#249) --- tests/algorithms/test_AES.py | 18 +++++----- tests/algorithms/test_AES_compat.py | 2 +- tests/algorithms/test_EC_compat.py | 2 +- tests/algorithms/test_HMAC_compat.py | 2 +- tests/algorithms/test_RSA.py | 2 +- tests/algorithms/test_RSA_compat.py | 2 +- tests/rfc/test_rfc7520.py | 1 - tests/test_firebase.py | 1 - tests/test_jwe.py | 14 ++++---- tests/test_jws.py | 54 ++++++++++++++-------------- tests/test_jwt.py | 9 +++-- tests/test_utils.py | 1 - 12 files changed, 52 insertions(+), 56 deletions(-) diff --git a/tests/algorithms/test_AES.py b/tests/algorithms/test_AES.py index 8acf90e4..e66b1050 100644 --- a/tests/algorithms/test_AES.py +++ b/tests/algorithms/test_AES.py @@ -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"), ) diff --git a/tests/algorithms/test_AES_compat.py b/tests/algorithms/test_AES_compat.py index 6750e9a5..6c1cd945 100644 --- a/tests/algorithms/test_AES_compat.py +++ b/tests/algorithms/test_AES_compat.py @@ -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) diff --git a/tests/algorithms/test_EC_compat.py b/tests/algorithms/test_EC_compat.py index 1fc6dab3..9c0ee20c 100644 --- a/tests/algorithms/test_EC_compat.py +++ b/tests/algorithms/test_EC_compat.py @@ -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]) diff --git a/tests/algorithms/test_HMAC_compat.py b/tests/algorithms/test_HMAC_compat.py index 7ffcef0d..44d41098 100644 --- a/tests/algorithms/test_HMAC_compat.py +++ b/tests/algorithms/test_HMAC_compat.py @@ -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) diff --git a/tests/algorithms/test_RSA.py b/tests/algorithms/test_RSA.py index 8d870aa7..1d19475e 100644 --- a/tests/algorithms/test_RSA.py +++ b/tests/algorithms/test_RSA.py @@ -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) diff --git a/tests/algorithms/test_RSA_compat.py b/tests/algorithms/test_RSA_compat.py index 77eecf0a..ea760420 100644 --- a/tests/algorithms/test_RSA_compat.py +++ b/tests/algorithms/test_RSA_compat.py @@ -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) diff --git a/tests/rfc/test_rfc7520.py b/tests/rfc/test_rfc7520.py index fb381d13..d48a4e87 100644 --- a/tests/rfc/test_rfc7520.py +++ b/tests/rfc/test_rfc7520.py @@ -1,4 +1,3 @@ - # Disable flake8 reporting # flake8: noqa diff --git a/tests/test_firebase.py b/tests/test_firebase.py index b31ac8fe..a7415496 100644 --- a/tests/test_firebase.py +++ b/tests/test_firebase.py @@ -1,4 +1,3 @@ - import json import pytest diff --git a/tests/test_jwe.py b/tests/test_jwe.py index e0f79d7f..7708a9e5 100644 --- a/tests/test_jwe.py +++ b/tests/test_jwe.py @@ -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" \ @@ -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" \ @@ -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( @@ -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): @@ -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) @@ -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) diff --git a/tests/test_jws.py b/tests/test_jws.py index b0652554..c0d93ee3 100644 --- a/tests/test_jws.py +++ b/tests/test_jws.py @@ -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): @@ -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]) @@ -89,7 +89,7 @@ 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') @@ -97,7 +97,7 @@ def test_jwk(self, payload): 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) @@ -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 = ( @@ -248,7 +248,7 @@ def jwk_set(): ) -class TestGetKeys(object): +class TestGetKeys: def test_dict(self): assert ({},) == jws._get_keys({}) @@ -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. @@ -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) @@ -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' diff --git a/tests/test_jwt.py b/tests/test_jwt.py index 03783ef7..5e5359a6 100644 --- a/tests/test_jwt.py +++ b/tests/test_jwt.py @@ -1,4 +1,3 @@ - import base64 import json @@ -53,8 +52,8 @@ 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"} @@ -62,7 +61,7 @@ def test_numeric_key(self, key, token): 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"}' @@ -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"]' diff --git a/tests/test_utils.py b/tests/test_utils.py index 71ced1f0..86dcfb86 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,3 @@ - from datetime import timedelta from jose import utils