From 3a29880bf3c1ae95cf96505366be6678e91e8840 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 (b0.72 backport of #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 | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/pbench/test/unit/server/auth/test_auth.py b/lib/pbench/test/unit/server/auth/test_auth.py index 173f27ae72..9f0fe0beb3 100644 --- a/lib/pbench/test/unit/server/auth/test_auth.py +++ b/lib/pbench/test/unit/server/auth/test_auth.py @@ -532,9 +532,7 @@ def test_token_introspect_offline_exp(self, monkeypatch, rsa_keys): with pytest.raises(OpenIDTokenInvalid) as exc: oidc_client.token_introspect_offline(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_offline_aud(self, monkeypatch, rsa_keys): """Verify .token_introspect_offline() failure via audience error""" @@ -548,7 +546,7 @@ def test_token_introspect_offline_aud(self, monkeypatch, rsa_keys): with pytest.raises(OpenIDTokenInvalid) as exc: oidc_client.token_introspect_offline(token) - assert str(exc.value.__cause__) == "Invalid audience", f"{exc.value.__cause__}" + assert isinstance(exc.value.__cause__, jwt.exceptions.InvalidAudienceError) def test_token_introspect_offline_sig(self, monkeypatch, rsa_keys): """Verify .token_introspect_offline() failure via signature error""" @@ -565,9 +563,7 @@ def test_token_introspect_offline_sig(self, monkeypatch, rsa_keys): with pytest.raises(OpenIDTokenInvalid) as exc: # Make the signature invalid. oidc_client.token_introspect_offline(token + "1") - assert ( - str(exc.value.__cause__) == "Signature verification failed" - ), f"{exc.value.__cause__}" + assert isinstance(exc.value.__cause__, jwt.exceptions.InvalidSignatureError) def test_get_userinfo(self, monkeypatch): """Verify .get_userinfo() properly invokes Connection.get()"""