Skip to content

Commit 09d24fc

Browse files
authored
Combine repetitive encode/decode tests using parametrize (jpadilla#577)
1 parent 06523a0 commit 09d24fc

File tree

1 file changed

+26
-92
lines changed

1 file changed

+26
-92
lines changed

tests/test_api_jws.py

Lines changed: 26 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -475,66 +475,33 @@ def test_get_unverified_header_fails_on_bad_header_types(self, jws, payload):
475475

476476
assert "Key ID header parameter must be a string" == str(exc.value)
477477

478+
@pytest.mark.parametrize(
479+
"algo",
480+
[
481+
"RS256",
482+
"RS384",
483+
"RS512",
484+
],
485+
)
478486
@crypto_required
479-
def test_encode_decode_with_rsa_sha256(self, jws, payload):
480-
# PEM-formatted RSA key
481-
with open(key_path("testkey_rsa.priv"), "rb") as rsa_priv_file:
482-
priv_rsakey = load_pem_private_key(rsa_priv_file.read(), password=None)
483-
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS256")
484-
485-
with open(key_path("testkey_rsa.pub"), "rb") as rsa_pub_file:
486-
pub_rsakey = load_ssh_public_key(rsa_pub_file.read())
487-
488-
jws.decode(jws_message, pub_rsakey, algorithms=["RS256"])
489-
490-
# string-formatted key
491-
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
492-
priv_rsakey = rsa_priv_file.read()
493-
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS256")
494-
495-
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
496-
pub_rsakey = rsa_pub_file.read()
497-
jws.decode(jws_message, pub_rsakey, algorithms=["RS256"])
498-
499-
@crypto_required
500-
def test_encode_decode_with_rsa_sha384(self, jws, payload):
501-
# PEM-formatted RSA key
502-
with open(key_path("testkey_rsa.priv"), "rb") as rsa_priv_file:
503-
priv_rsakey = load_pem_private_key(rsa_priv_file.read(), password=None)
504-
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS384")
505-
506-
with open(key_path("testkey_rsa.pub"), "rb") as rsa_pub_file:
507-
pub_rsakey = load_ssh_public_key(rsa_pub_file.read())
508-
jws.decode(jws_message, pub_rsakey, algorithms=["RS384"])
509-
510-
# string-formatted key
511-
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
512-
priv_rsakey = rsa_priv_file.read()
513-
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS384")
514-
515-
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
516-
pub_rsakey = rsa_pub_file.read()
517-
jws.decode(jws_message, pub_rsakey, algorithms=["RS384"])
518-
519-
@crypto_required
520-
def test_encode_decode_with_rsa_sha512(self, jws, payload):
487+
def test_encode_decode_rsa_related_algorithms(self, jws, payload, algo):
521488
# PEM-formatted RSA key
522489
with open(key_path("testkey_rsa.priv"), "rb") as rsa_priv_file:
523490
priv_rsakey = load_pem_private_key(rsa_priv_file.read(), password=None)
524-
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS512")
491+
jws_message = jws.encode(payload, priv_rsakey, algorithm=algo)
525492

526493
with open(key_path("testkey_rsa.pub"), "rb") as rsa_pub_file:
527494
pub_rsakey = load_ssh_public_key(rsa_pub_file.read())
528-
jws.decode(jws_message, pub_rsakey, algorithms=["RS512"])
495+
jws.decode(jws_message, pub_rsakey, algorithms=[algo])
529496

530497
# string-formatted key
531498
with open(key_path("testkey_rsa.priv")) as rsa_priv_file:
532499
priv_rsakey = rsa_priv_file.read()
533-
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS512")
500+
jws_message = jws.encode(payload, priv_rsakey, algorithm=algo)
534501

535502
with open(key_path("testkey_rsa.pub")) as rsa_pub_file:
536503
pub_rsakey = rsa_pub_file.read()
537-
jws.decode(jws_message, pub_rsakey, algorithms=["RS512"])
504+
jws.decode(jws_message, pub_rsakey, algorithms=[algo])
538505

539506
def test_rsa_related_algorithms(self, jws):
540507
jws = PyJWS()
@@ -556,66 +523,33 @@ def test_rsa_related_algorithms(self, jws):
556523
assert "PS384" not in jws_algorithms
557524
assert "PS512" not in jws_algorithms
558525

526+
@pytest.mark.parametrize(
527+
"algo",
528+
[
529+
"ES256",
530+
"ES384",
531+
"ES512",
532+
],
533+
)
559534
@crypto_required
560-
def test_encode_decode_with_ecdsa_sha256(self, jws, payload):
561-
# PEM-formatted EC key
562-
with open(key_path("testkey_ec.priv"), "rb") as ec_priv_file:
563-
priv_eckey = load_pem_private_key(ec_priv_file.read(), password=None)
564-
jws_message = jws.encode(payload, priv_eckey, algorithm="ES256")
565-
566-
with open(key_path("testkey_ec.pub"), "rb") as ec_pub_file:
567-
pub_eckey = load_pem_public_key(ec_pub_file.read())
568-
jws.decode(jws_message, pub_eckey, algorithms=["ES256"])
569-
570-
# string-formatted key
571-
with open(key_path("testkey_ec.priv")) as ec_priv_file:
572-
priv_eckey = ec_priv_file.read()
573-
jws_message = jws.encode(payload, priv_eckey, algorithm="ES256")
574-
575-
with open(key_path("testkey_ec.pub")) as ec_pub_file:
576-
pub_eckey = ec_pub_file.read()
577-
jws.decode(jws_message, pub_eckey, algorithms=["ES256"])
578-
579-
@crypto_required
580-
def test_encode_decode_with_ecdsa_sha384(self, jws, payload):
581-
582-
# PEM-formatted EC key
583-
with open(key_path("testkey_ec.priv"), "rb") as ec_priv_file:
584-
priv_eckey = load_pem_private_key(ec_priv_file.read(), password=None)
585-
jws_message = jws.encode(payload, priv_eckey, algorithm="ES384")
586-
587-
with open(key_path("testkey_ec.pub"), "rb") as ec_pub_file:
588-
pub_eckey = load_pem_public_key(ec_pub_file.read())
589-
jws.decode(jws_message, pub_eckey, algorithms=["ES384"])
590-
591-
# string-formatted key
592-
with open(key_path("testkey_ec.priv")) as ec_priv_file:
593-
priv_eckey = ec_priv_file.read()
594-
jws_message = jws.encode(payload, priv_eckey, algorithm="ES384")
595-
596-
with open(key_path("testkey_ec.pub")) as ec_pub_file:
597-
pub_eckey = ec_pub_file.read()
598-
jws.decode(jws_message, pub_eckey, algorithms=["ES384"])
599-
600-
@crypto_required
601-
def test_encode_decode_with_ecdsa_sha512(self, jws, payload):
535+
def test_encode_decode_ecdsa_related_algorithms(self, jws, payload, algo):
602536
# PEM-formatted EC key
603537
with open(key_path("testkey_ec.priv"), "rb") as ec_priv_file:
604538
priv_eckey = load_pem_private_key(ec_priv_file.read(), password=None)
605-
jws_message = jws.encode(payload, priv_eckey, algorithm="ES512")
539+
jws_message = jws.encode(payload, priv_eckey, algorithm=algo)
606540

607541
with open(key_path("testkey_ec.pub"), "rb") as ec_pub_file:
608542
pub_eckey = load_pem_public_key(ec_pub_file.read())
609-
jws.decode(jws_message, pub_eckey, algorithms=["ES512"])
543+
jws.decode(jws_message, pub_eckey, algorithms=[algo])
610544

611545
# string-formatted key
612546
with open(key_path("testkey_ec.priv")) as ec_priv_file:
613547
priv_eckey = ec_priv_file.read()
614-
jws_message = jws.encode(payload, priv_eckey, algorithm="ES512")
548+
jws_message = jws.encode(payload, priv_eckey, algorithm=algo)
615549

616550
with open(key_path("testkey_ec.pub")) as ec_pub_file:
617551
pub_eckey = ec_pub_file.read()
618-
jws.decode(jws_message, pub_eckey, algorithms=["ES512"])
552+
jws.decode(jws_message, pub_eckey, algorithms=[algo])
619553

620554
def test_ecdsa_related_algorithms(self, jws):
621555
jws = PyJWS()

0 commit comments

Comments
 (0)