Skip to content

Commit 6ce4e35

Browse files
authored
Use existing key_path() utility function throughout tests (jpadilla#540)
1 parent 696f65d commit 6ce4e35

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

tests/test_api_jws.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
)
1414
from jwt.utils import base64url_decode, force_bytes, force_unicode
1515

16+
from .utils import key_path
17+
1618
try:
1719
from cryptography.hazmat.primitives.serialization import (
1820
load_pem_private_key,
@@ -222,7 +224,7 @@ def test_decodes_valid_jws(self, jws, payload):
222224
)
223225
def test_decodes_valid_es384_jws(self, jws):
224226
example_payload = {"hello": "world"}
225-
with open("tests/keys/testkey_ec.pub") as fp:
227+
with open(key_path("testkey_ec.pub")) as fp:
226228
example_pubkey = fp.read()
227229
example_jws = (
228230
b"eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9."
@@ -245,7 +247,7 @@ def test_decodes_valid_es384_jws(self, jws):
245247
)
246248
def test_decodes_valid_rs384_jws(self, jws):
247249
example_payload = {"hello": "world"}
248-
with open("tests/keys/testkey_rsa.pub") as fp:
250+
with open(key_path("testkey_rsa.pub")) as fp:
249251
example_pubkey = fp.read()
250252
example_jws = (
251253
b"eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9"
@@ -491,23 +493,23 @@ def test_get_unverified_header_fails_on_bad_header_types(
491493
)
492494
def test_encode_decode_with_rsa_sha256(self, jws, payload):
493495
# PEM-formatted RSA key
494-
with open("tests/keys/testkey_rsa.priv") as rsa_priv_file:
496+
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
495497
priv_rsakey = load_pem_private_key(
496498
force_bytes(rsa_priv_file.read()), password=None
497499
)
498500
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS256")
499501

500-
with open("tests/keys/testkey_rsa.pub") as rsa_pub_file:
502+
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
501503
pub_rsakey = load_ssh_public_key(force_bytes(rsa_pub_file.read()))
502504

503505
jws.decode(jws_message, pub_rsakey, algorithms=["RS256"])
504506

505507
# string-formatted key
506-
with open("tests/keys/testkey_rsa.priv") as rsa_priv_file:
508+
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
507509
priv_rsakey = rsa_priv_file.read()
508510
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS256")
509511

510-
with open("tests/keys/testkey_rsa.pub") as rsa_pub_file:
512+
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
511513
pub_rsakey = rsa_pub_file.read()
512514
jws.decode(jws_message, pub_rsakey, algorithms=["RS256"])
513515

@@ -516,22 +518,22 @@ def test_encode_decode_with_rsa_sha256(self, jws, payload):
516518
)
517519
def test_encode_decode_with_rsa_sha384(self, jws, payload):
518520
# PEM-formatted RSA key
519-
with open("tests/keys/testkey_rsa.priv") as rsa_priv_file:
521+
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
520522
priv_rsakey = load_pem_private_key(
521523
force_bytes(rsa_priv_file.read()), password=None
522524
)
523525
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS384")
524526

525-
with open("tests/keys/testkey_rsa.pub") as rsa_pub_file:
527+
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
526528
pub_rsakey = load_ssh_public_key(force_bytes(rsa_pub_file.read()))
527529
jws.decode(jws_message, pub_rsakey, algorithms=["RS384"])
528530

529531
# string-formatted key
530-
with open("tests/keys/testkey_rsa.priv") as rsa_priv_file:
532+
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
531533
priv_rsakey = rsa_priv_file.read()
532534
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS384")
533535

534-
with open("tests/keys/testkey_rsa.pub") as rsa_pub_file:
536+
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
535537
pub_rsakey = rsa_pub_file.read()
536538
jws.decode(jws_message, pub_rsakey, algorithms=["RS384"])
537539

@@ -540,22 +542,22 @@ def test_encode_decode_with_rsa_sha384(self, jws, payload):
540542
)
541543
def test_encode_decode_with_rsa_sha512(self, jws, payload):
542544
# PEM-formatted RSA key
543-
with open("tests/keys/testkey_rsa.priv") as rsa_priv_file:
545+
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
544546
priv_rsakey = load_pem_private_key(
545547
force_bytes(rsa_priv_file.read()), password=None
546548
)
547549
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS512")
548550

549-
with open("tests/keys/testkey_rsa.pub") as rsa_pub_file:
551+
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
550552
pub_rsakey = load_ssh_public_key(force_bytes(rsa_pub_file.read()))
551553
jws.decode(jws_message, pub_rsakey, algorithms=["RS512"])
552554

553555
# string-formatted key
554-
with open("tests/keys/testkey_rsa.priv") as rsa_priv_file:
556+
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
555557
priv_rsakey = rsa_priv_file.read()
556558
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS512")
557559

558-
with open("tests/keys/testkey_rsa.pub") as rsa_pub_file:
560+
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
559561
pub_rsakey = rsa_pub_file.read()
560562
jws.decode(jws_message, pub_rsakey, algorithms=["RS512"])
561563

@@ -584,22 +586,22 @@ def test_rsa_related_algorithms(self, jws):
584586
)
585587
def test_encode_decode_with_ecdsa_sha256(self, jws, payload):
586588
# PEM-formatted EC key
587-
with open("tests/keys/testkey_ec.priv") as ec_priv_file:
589+
with open(key_path("testkey_ec.priv")) as ec_priv_file:
588590
priv_eckey = load_pem_private_key(
589591
force_bytes(ec_priv_file.read()), password=None
590592
)
591593
jws_message = jws.encode(payload, priv_eckey, algorithm="ES256")
592594

593-
with open("tests/keys/testkey_ec.pub") as ec_pub_file:
595+
with open(key_path("testkey_ec.pub")) as ec_pub_file:
594596
pub_eckey = load_pem_public_key(force_bytes(ec_pub_file.read()))
595597
jws.decode(jws_message, pub_eckey, algorithms=["ES256"])
596598

597599
# string-formatted key
598-
with open("tests/keys/testkey_ec.priv") as ec_priv_file:
600+
with open(key_path("testkey_ec.priv")) as ec_priv_file:
599601
priv_eckey = ec_priv_file.read()
600602
jws_message = jws.encode(payload, priv_eckey, algorithm="ES256")
601603

602-
with open("tests/keys/testkey_ec.pub") as ec_pub_file:
604+
with open(key_path("testkey_ec.pub")) as ec_pub_file:
603605
pub_eckey = ec_pub_file.read()
604606
jws.decode(jws_message, pub_eckey, algorithms=["ES256"])
605607

@@ -609,22 +611,22 @@ def test_encode_decode_with_ecdsa_sha256(self, jws, payload):
609611
def test_encode_decode_with_ecdsa_sha384(self, jws, payload):
610612

611613
# PEM-formatted EC key
612-
with open("tests/keys/testkey_ec.priv") as ec_priv_file:
614+
with open(key_path("testkey_ec.priv")) as ec_priv_file:
613615
priv_eckey = load_pem_private_key(
614616
force_bytes(ec_priv_file.read()), password=None
615617
)
616618
jws_message = jws.encode(payload, priv_eckey, algorithm="ES384")
617619

618-
with open("tests/keys/testkey_ec.pub") as ec_pub_file:
620+
with open(key_path("testkey_ec.pub")) as ec_pub_file:
619621
pub_eckey = load_pem_public_key(force_bytes(ec_pub_file.read()))
620622
jws.decode(jws_message, pub_eckey, algorithms=["ES384"])
621623

622624
# string-formatted key
623-
with open("tests/keys/testkey_ec.priv") as ec_priv_file:
625+
with open(key_path("testkey_ec.priv")) as ec_priv_file:
624626
priv_eckey = ec_priv_file.read()
625627
jws_message = jws.encode(payload, priv_eckey, algorithm="ES384")
626628

627-
with open("tests/keys/testkey_ec.pub") as ec_pub_file:
629+
with open(key_path("testkey_ec.pub")) as ec_pub_file:
628630
pub_eckey = ec_pub_file.read()
629631
jws.decode(jws_message, pub_eckey, algorithms=["ES384"])
630632

@@ -633,22 +635,22 @@ def test_encode_decode_with_ecdsa_sha384(self, jws, payload):
633635
)
634636
def test_encode_decode_with_ecdsa_sha512(self, jws, payload):
635637
# PEM-formatted EC key
636-
with open("tests/keys/testkey_ec.priv") as ec_priv_file:
638+
with open(key_path("testkey_ec.priv")) as ec_priv_file:
637639
priv_eckey = load_pem_private_key(
638640
force_bytes(ec_priv_file.read()), password=None
639641
)
640642
jws_message = jws.encode(payload, priv_eckey, algorithm="ES512")
641643

642-
with open("tests/keys/testkey_ec.pub") as ec_pub_file:
644+
with open(key_path("testkey_ec.pub")) as ec_pub_file:
643645
pub_eckey = load_pem_public_key(force_bytes(ec_pub_file.read()))
644646
jws.decode(jws_message, pub_eckey, algorithms=["ES512"])
645647

646648
# string-formatted key
647-
with open("tests/keys/testkey_ec.priv") as ec_priv_file:
649+
with open(key_path("testkey_ec.priv")) as ec_priv_file:
648650
priv_eckey = ec_priv_file.read()
649651
jws_message = jws.encode(payload, priv_eckey, algorithm="ES512")
650652

651-
with open("tests/keys/testkey_ec.pub") as ec_pub_file:
653+
with open(key_path("testkey_ec.pub")) as ec_pub_file:
652654
pub_eckey = ec_pub_file.read()
653655
jws.decode(jws_message, pub_eckey, algorithms=["ES512"])
654656

tests/test_api_jwt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919

2020
from .test_api_jws import has_crypto
21-
from .utils import utc_timestamp
21+
from .utils import key_path, utc_timestamp
2222

2323

2424
@pytest.fixture
@@ -220,7 +220,7 @@ def test_encode_datetime(self, jwt):
220220
)
221221
def test_decodes_valid_es256_jwt(self, jwt):
222222
example_payload = {"hello": "world"}
223-
with open("tests/keys/testkey_ec.pub") as fp:
223+
with open(key_path("testkey_ec.pub")) as fp:
224224
example_pubkey = fp.read()
225225
example_jwt = (
226226
b"eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9."
@@ -242,7 +242,7 @@ def test_decodes_valid_es256_jwt(self, jwt):
242242
)
243243
def test_decodes_valid_rs384_jwt(self, jwt):
244244
example_payload = {"hello": "world"}
245-
with open("tests/keys/testkey_rsa.pub") as fp:
245+
with open(key_path("testkey_rsa.pub")) as fp:
246246
example_pubkey = fp.read()
247247
example_jwt = (
248248
b"eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9"

0 commit comments

Comments
 (0)