Skip to content

Commit

Permalink
Unit test on jwt exception type instead of string (#3417)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
npalaska committed May 10, 2023
1 parent 918d435 commit 10d5704
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions lib/pbench/test/unit/server/auth/test_auth.py
Expand Up @@ -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"""
Expand All @@ -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"""
Expand All @@ -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"""
Expand All @@ -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
Expand Down

0 comments on commit 10d5704

Please sign in to comment.