Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine repetitive encode/decode tests using parametrize #577

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
118 changes: 26 additions & 92 deletions tests/test_api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,66 +475,33 @@ def test_get_unverified_header_fails_on_bad_header_types(self, jws, payload):

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

@pytest.mark.parametrize(
"algo",
[
"RS256",
"RS384",
"RS512",
],
)
@crypto_required
def test_encode_decode_with_rsa_sha256(self, jws, payload):
# PEM-formatted RSA key
with open(key_path("testkey_rsa.priv"), "rb") as rsa_priv_file:
priv_rsakey = load_pem_private_key(rsa_priv_file.read(), password=None)
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS256")

with open(key_path("testkey_rsa.pub"), "rb") as rsa_pub_file:
pub_rsakey = load_ssh_public_key(rsa_pub_file.read())

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

# string-formatted key
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(key_path("testkey_rsa.pub")) as rsa_pub_file:
pub_rsakey = rsa_pub_file.read()
jws.decode(jws_message, pub_rsakey, algorithms=["RS256"])

@crypto_required
def test_encode_decode_with_rsa_sha384(self, jws, payload):
# PEM-formatted RSA key
with open(key_path("testkey_rsa.priv"), "rb") as rsa_priv_file:
priv_rsakey = load_pem_private_key(rsa_priv_file.read(), password=None)
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS384")

with open(key_path("testkey_rsa.pub"), "rb") as rsa_pub_file:
pub_rsakey = load_ssh_public_key(rsa_pub_file.read())
jws.decode(jws_message, pub_rsakey, algorithms=["RS384"])

# string-formatted key
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(key_path("testkey_rsa.pub")) as rsa_pub_file:
pub_rsakey = rsa_pub_file.read()
jws.decode(jws_message, pub_rsakey, algorithms=["RS384"])

@crypto_required
def test_encode_decode_with_rsa_sha512(self, jws, payload):
def test_encode_decode_rsa_related_algorithms(self, jws, payload, algo):
# PEM-formatted RSA key
with open(key_path("testkey_rsa.priv"), "rb") as rsa_priv_file:
priv_rsakey = load_pem_private_key(rsa_priv_file.read(), password=None)
jws_message = jws.encode(payload, priv_rsakey, algorithm="RS512")
jws_message = jws.encode(payload, priv_rsakey, algorithm=algo)

with open(key_path("testkey_rsa.pub"), "rb") as rsa_pub_file:
pub_rsakey = load_ssh_public_key(rsa_pub_file.read())
jws.decode(jws_message, pub_rsakey, algorithms=["RS512"])
jws.decode(jws_message, pub_rsakey, algorithms=[algo])

# string-formatted key
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")
jws_message = jws.encode(payload, priv_rsakey, algorithm=algo)

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"])
jws.decode(jws_message, pub_rsakey, algorithms=[algo])

def test_rsa_related_algorithms(self, jws):
jws = PyJWS()
Expand All @@ -556,66 +523,33 @@ def test_rsa_related_algorithms(self, jws):
assert "PS384" not in jws_algorithms
assert "PS512" not in jws_algorithms

@pytest.mark.parametrize(
"algo",
[
"ES256",
"ES384",
"ES512",
],
)
@crypto_required
def test_encode_decode_with_ecdsa_sha256(self, jws, payload):
# PEM-formatted EC key
with open(key_path("testkey_ec.priv"), "rb") as ec_priv_file:
priv_eckey = load_pem_private_key(ec_priv_file.read(), password=None)
jws_message = jws.encode(payload, priv_eckey, algorithm="ES256")

with open(key_path("testkey_ec.pub"), "rb") as ec_pub_file:
pub_eckey = load_pem_public_key(ec_pub_file.read())
jws.decode(jws_message, pub_eckey, algorithms=["ES256"])

# string-formatted key
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(key_path("testkey_ec.pub")) as ec_pub_file:
pub_eckey = ec_pub_file.read()
jws.decode(jws_message, pub_eckey, algorithms=["ES256"])

@crypto_required
def test_encode_decode_with_ecdsa_sha384(self, jws, payload):

# PEM-formatted EC key
with open(key_path("testkey_ec.priv"), "rb") as ec_priv_file:
priv_eckey = load_pem_private_key(ec_priv_file.read(), password=None)
jws_message = jws.encode(payload, priv_eckey, algorithm="ES384")

with open(key_path("testkey_ec.pub"), "rb") as ec_pub_file:
pub_eckey = load_pem_public_key(ec_pub_file.read())
jws.decode(jws_message, pub_eckey, algorithms=["ES384"])

# string-formatted key
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(key_path("testkey_ec.pub")) as ec_pub_file:
pub_eckey = ec_pub_file.read()
jws.decode(jws_message, pub_eckey, algorithms=["ES384"])

@crypto_required
def test_encode_decode_with_ecdsa_sha512(self, jws, payload):
def test_encode_decode_ecdsa_related_algorithms(self, jws, payload, algo):
# PEM-formatted EC key
with open(key_path("testkey_ec.priv"), "rb") as ec_priv_file:
priv_eckey = load_pem_private_key(ec_priv_file.read(), password=None)
jws_message = jws.encode(payload, priv_eckey, algorithm="ES512")
jws_message = jws.encode(payload, priv_eckey, algorithm=algo)

with open(key_path("testkey_ec.pub"), "rb") as ec_pub_file:
pub_eckey = load_pem_public_key(ec_pub_file.read())
jws.decode(jws_message, pub_eckey, algorithms=["ES512"])
jws.decode(jws_message, pub_eckey, algorithms=[algo])

# string-formatted key
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")
jws_message = jws.encode(payload, priv_eckey, algorithm=algo)

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"])
jws.decode(jws_message, pub_eckey, algorithms=[algo])

def test_ecdsa_related_algorithms(self, jws):
jws = PyJWS()
Expand Down