From 10d570415cbf33de13b63a8bbad722993008b55f Mon Sep 17 00:00:00 2001 From: Nikhil Palaskar Date: Wed, 10 May 2023 11:58:11 -0400 Subject: [PATCH] Unit test on jwt exception type instead of string (#3417) * Check jwt exception type instead of string in the unit tests Jwt exception (specifically jwt.exception.InvalidAudienceError) has changed its string message from `Invalid Audience` to `Audience doesn't match`. In the unit test instead of checking the string message check the type of the exception. --- lib/pbench/test/unit/server/auth/test_auth.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/pbench/test/unit/server/auth/test_auth.py b/lib/pbench/test/unit/server/auth/test_auth.py index 413ddfb5da..7f406df8a5 100644 --- a/lib/pbench/test/unit/server/auth/test_auth.py +++ b/lib/pbench/test/unit/server/auth/test_auth.py @@ -378,9 +378,7 @@ def test_token_introspect_exp(self, monkeypatch, rsa_keys): with pytest.raises(OpenIDTokenInvalid) as exc: oidc_client.token_introspect(token) - assert ( - str(exc.value.__cause__) == "Signature has expired" - ), f"{exc.value.__cause__}" + assert isinstance(exc.value.__cause__, jwt.exceptions.ExpiredSignatureError) def test_token_introspect_aud(self, monkeypatch, rsa_keys): """Verify .token_introspect() failure via audience error""" @@ -394,7 +392,7 @@ def test_token_introspect_aud(self, monkeypatch, rsa_keys): with pytest.raises(OpenIDTokenInvalid) as exc: oidc_client.token_introspect(token) - assert str(exc.value.__cause__) == "Invalid audience", f"{exc.value.__cause__}" + assert isinstance(exc.value.__cause__, jwt.exceptions.InvalidAudienceError) def test_token_introspect_sig(self, monkeypatch, rsa_keys): """Verify .token_introspect() failure via signature error""" @@ -411,9 +409,7 @@ def test_token_introspect_sig(self, monkeypatch, rsa_keys): with pytest.raises(OpenIDTokenInvalid) as exc: # Make the signature invalid. oidc_client.token_introspect(token + "1") - assert ( - str(exc.value.__cause__) == "Signature verification failed" - ), f"{exc.value.__cause__}" + assert isinstance(exc.value.__cause__, jwt.exceptions.InvalidSignatureError) def test_token_introspect_alg(self, monkeypatch, rsa_keys): """Verify .token_introspect() failure via algorithm error""" @@ -430,9 +426,7 @@ def test_token_introspect_alg(self, monkeypatch, rsa_keys): with pytest.raises(OpenIDTokenInvalid) as exc: oidc_client.token_introspect(generated_api_key) - assert ( - str(exc.value.__cause__) == "The specified alg value is not allowed" - ), f"{exc.value.__cause__}" + assert isinstance(exc.value.__cause__, jwt.exceptions.InvalidAlgorithmError) @dataclass