diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2020-12-16 17:26:47 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-16 20:26:47 -0500 |
commit | 6ce4e35a9d373bb969235d99bdb42965e7bba38a (patch) | |
tree | 04a13e5d67ddf434c7e47da3203a3709a89d89c3 | |
parent | 696f65d654d13c1581f396c00f09dd48dfef59b4 (diff) | |
download | pyjwt-6ce4e35a9d373bb969235d99bdb42965e7bba38a.tar.gz |
Use existing key_path() utility function throughout tests (#540)
-rw-r--r-- | tests/test_api_jws.py | 54 | ||||
-rw-r--r-- | tests/test_api_jwt.py | 6 |
2 files changed, 31 insertions, 29 deletions
diff --git a/tests/test_api_jws.py b/tests/test_api_jws.py index 7da3b0c..ad96d92 100644 --- a/tests/test_api_jws.py +++ b/tests/test_api_jws.py @@ -13,6 +13,8 @@ from jwt.exceptions import ( ) from jwt.utils import base64url_decode, force_bytes, force_unicode +from .utils import key_path + try: from cryptography.hazmat.primitives.serialization import ( load_pem_private_key, @@ -222,7 +224,7 @@ class TestJWS: ) def test_decodes_valid_es384_jws(self, jws): example_payload = {"hello": "world"} - with open("tests/keys/testkey_ec.pub") as fp: + with open(key_path("testkey_ec.pub")) as fp: example_pubkey = fp.read() example_jws = ( b"eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9." @@ -245,7 +247,7 @@ class TestJWS: ) def test_decodes_valid_rs384_jws(self, jws): example_payload = {"hello": "world"} - with open("tests/keys/testkey_rsa.pub") as fp: + with open(key_path("testkey_rsa.pub")) as fp: example_pubkey = fp.read() example_jws = ( b"eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9" @@ -491,23 +493,23 @@ class TestJWS: ) def test_encode_decode_with_rsa_sha256(self, jws, payload): # PEM-formatted RSA key - with open("tests/keys/testkey_rsa.priv") as rsa_priv_file: + with open(key_path("testkey_rsa.priv")) as rsa_priv_file: priv_rsakey = load_pem_private_key( force_bytes(rsa_priv_file.read()), password=None ) jws_message = jws.encode(payload, priv_rsakey, algorithm="RS256") - with open("tests/keys/testkey_rsa.pub") as rsa_pub_file: + with open(key_path("testkey_rsa.pub")) as rsa_pub_file: pub_rsakey = load_ssh_public_key(force_bytes(rsa_pub_file.read())) jws.decode(jws_message, pub_rsakey, algorithms=["RS256"]) # string-formatted key - with open("tests/keys/testkey_rsa.priv") as rsa_priv_file: + with open(key_path("testkey_rsa.priv")) as rsa_priv_file: priv_rsakey = rsa_priv_file.read() jws_message = jws.encode(payload, priv_rsakey, algorithm="RS256") - with open("tests/keys/testkey_rsa.pub") as rsa_pub_file: + with open(key_path("testkey_rsa.pub")) as rsa_pub_file: pub_rsakey = rsa_pub_file.read() jws.decode(jws_message, pub_rsakey, algorithms=["RS256"]) @@ -516,22 +518,22 @@ class TestJWS: ) def test_encode_decode_with_rsa_sha384(self, jws, payload): # PEM-formatted RSA key - with open("tests/keys/testkey_rsa.priv") as rsa_priv_file: + with open(key_path("testkey_rsa.priv")) as rsa_priv_file: priv_rsakey = load_pem_private_key( force_bytes(rsa_priv_file.read()), password=None ) jws_message = jws.encode(payload, priv_rsakey, algorithm="RS384") - with open("tests/keys/testkey_rsa.pub") as rsa_pub_file: + with open(key_path("testkey_rsa.pub")) as rsa_pub_file: pub_rsakey = load_ssh_public_key(force_bytes(rsa_pub_file.read())) jws.decode(jws_message, pub_rsakey, algorithms=["RS384"]) # string-formatted key - with open("tests/keys/testkey_rsa.priv") as rsa_priv_file: + with open(key_path("testkey_rsa.priv")) as rsa_priv_file: priv_rsakey = rsa_priv_file.read() jws_message = jws.encode(payload, priv_rsakey, algorithm="RS384") - with open("tests/keys/testkey_rsa.pub") as rsa_pub_file: + with open(key_path("testkey_rsa.pub")) as rsa_pub_file: pub_rsakey = rsa_pub_file.read() jws.decode(jws_message, pub_rsakey, algorithms=["RS384"]) @@ -540,22 +542,22 @@ class TestJWS: ) def test_encode_decode_with_rsa_sha512(self, jws, payload): # PEM-formatted RSA key - with open("tests/keys/testkey_rsa.priv") as rsa_priv_file: + with open(key_path("testkey_rsa.priv")) as rsa_priv_file: priv_rsakey = load_pem_private_key( force_bytes(rsa_priv_file.read()), password=None ) jws_message = jws.encode(payload, priv_rsakey, algorithm="RS512") - with open("tests/keys/testkey_rsa.pub") as rsa_pub_file: + with open(key_path("testkey_rsa.pub")) as rsa_pub_file: pub_rsakey = load_ssh_public_key(force_bytes(rsa_pub_file.read())) jws.decode(jws_message, pub_rsakey, algorithms=["RS512"]) # string-formatted key - with open("tests/keys/testkey_rsa.priv") as rsa_priv_file: + with open(key_path("testkey_rsa.priv")) as rsa_priv_file: priv_rsakey = rsa_priv_file.read() jws_message = jws.encode(payload, priv_rsakey, algorithm="RS512") - with open("tests/keys/testkey_rsa.pub") as rsa_pub_file: + with open(key_path("testkey_rsa.pub")) as rsa_pub_file: pub_rsakey = rsa_pub_file.read() jws.decode(jws_message, pub_rsakey, algorithms=["RS512"]) @@ -584,22 +586,22 @@ class TestJWS: ) def test_encode_decode_with_ecdsa_sha256(self, jws, payload): # PEM-formatted EC key - with open("tests/keys/testkey_ec.priv") as ec_priv_file: + with open(key_path("testkey_ec.priv")) as ec_priv_file: priv_eckey = load_pem_private_key( force_bytes(ec_priv_file.read()), password=None ) jws_message = jws.encode(payload, priv_eckey, algorithm="ES256") - with open("tests/keys/testkey_ec.pub") as ec_pub_file: + with open(key_path("testkey_ec.pub")) as ec_pub_file: pub_eckey = load_pem_public_key(force_bytes(ec_pub_file.read())) jws.decode(jws_message, pub_eckey, algorithms=["ES256"]) # string-formatted key - with open("tests/keys/testkey_ec.priv") as ec_priv_file: + with open(key_path("testkey_ec.priv")) as ec_priv_file: priv_eckey = ec_priv_file.read() jws_message = jws.encode(payload, priv_eckey, algorithm="ES256") - with open("tests/keys/testkey_ec.pub") as ec_pub_file: + with open(key_path("testkey_ec.pub")) as ec_pub_file: pub_eckey = ec_pub_file.read() jws.decode(jws_message, pub_eckey, algorithms=["ES256"]) @@ -609,22 +611,22 @@ class TestJWS: def test_encode_decode_with_ecdsa_sha384(self, jws, payload): # PEM-formatted EC key - with open("tests/keys/testkey_ec.priv") as ec_priv_file: + with open(key_path("testkey_ec.priv")) as ec_priv_file: priv_eckey = load_pem_private_key( force_bytes(ec_priv_file.read()), password=None ) jws_message = jws.encode(payload, priv_eckey, algorithm="ES384") - with open("tests/keys/testkey_ec.pub") as ec_pub_file: + with open(key_path("testkey_ec.pub")) as ec_pub_file: pub_eckey = load_pem_public_key(force_bytes(ec_pub_file.read())) jws.decode(jws_message, pub_eckey, algorithms=["ES384"]) # string-formatted key - with open("tests/keys/testkey_ec.priv") as ec_priv_file: + with open(key_path("testkey_ec.priv")) as ec_priv_file: priv_eckey = ec_priv_file.read() jws_message = jws.encode(payload, priv_eckey, algorithm="ES384") - with open("tests/keys/testkey_ec.pub") as ec_pub_file: + with open(key_path("testkey_ec.pub")) as ec_pub_file: pub_eckey = ec_pub_file.read() jws.decode(jws_message, pub_eckey, algorithms=["ES384"]) @@ -633,22 +635,22 @@ class TestJWS: ) def test_encode_decode_with_ecdsa_sha512(self, jws, payload): # PEM-formatted EC key - with open("tests/keys/testkey_ec.priv") as ec_priv_file: + with open(key_path("testkey_ec.priv")) as ec_priv_file: priv_eckey = load_pem_private_key( force_bytes(ec_priv_file.read()), password=None ) jws_message = jws.encode(payload, priv_eckey, algorithm="ES512") - with open("tests/keys/testkey_ec.pub") as ec_pub_file: + with open(key_path("testkey_ec.pub")) as ec_pub_file: pub_eckey = load_pem_public_key(force_bytes(ec_pub_file.read())) jws.decode(jws_message, pub_eckey, algorithms=["ES512"]) # string-formatted key - with open("tests/keys/testkey_ec.priv") as ec_priv_file: + with open(key_path("testkey_ec.priv")) as ec_priv_file: priv_eckey = ec_priv_file.read() jws_message = jws.encode(payload, priv_eckey, algorithm="ES512") - with open("tests/keys/testkey_ec.pub") as ec_pub_file: + with open(key_path("testkey_ec.pub")) as ec_pub_file: pub_eckey = ec_pub_file.read() jws.decode(jws_message, pub_eckey, algorithms=["ES512"]) diff --git a/tests/test_api_jwt.py b/tests/test_api_jwt.py index d4e651f..83177bf 100644 --- a/tests/test_api_jwt.py +++ b/tests/test_api_jwt.py @@ -18,7 +18,7 @@ from jwt.exceptions import ( ) from .test_api_jws import has_crypto -from .utils import utc_timestamp +from .utils import key_path, utc_timestamp @pytest.fixture @@ -220,7 +220,7 @@ class TestJWT: ) def test_decodes_valid_es256_jwt(self, jwt): example_payload = {"hello": "world"} - with open("tests/keys/testkey_ec.pub") as fp: + with open(key_path("testkey_ec.pub")) as fp: example_pubkey = fp.read() example_jwt = ( b"eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9." @@ -242,7 +242,7 @@ class TestJWT: ) def test_decodes_valid_rs384_jwt(self, jwt): example_payload = {"hello": "world"} - with open("tests/keys/testkey_rsa.pub") as fp: + with open(key_path("testkey_rsa.pub")) as fp: example_pubkey = fp.read() example_jwt = ( b"eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9" |