-
Notifications
You must be signed in to change notification settings - Fork 108
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
Unit test on jwt exception type instead of string #3417
Merged
npalaska
merged 2 commits into
distributed-system-analysis:main
from
npalaska:jwt_exception_check_bug
May 10, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 exc.value.exc_type == jwt.ExpiredSignatureError, f"{exc.value.exc_type}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And in any case, the comparison should be |
||
|
||
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 exc.value.exc_type == jwt.InvalidAudienceError, f"{exc.value.exc_type}" | ||
|
||
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 exc.value.exc_type == jwt.InvalidSignatureError, f"{exc.value.exc_type}" | ||
|
||
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 exc.value.exc_type == jwt.InvalidAlgorithmError, f"{exc.value.exc_type}" | ||
|
||
|
||
@dataclass | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I like the idea of having "our exception" be a wrapper for the underlying exception (e.g., having the stringification of our exception include the stringification of the underlying exception), the argument to the c'tor should be an exception instance, not an exception type -- when we stringify the underlying exception, any arguments with which it was created should be reflected in the string, and that won't happen if we reference the class instead of the instance.
Also, in general, when you derive a subclass from a superclass, you should invoke the superclass c'tor in the subclass c'tor (this is implicit in many languages, but not in Python). Moreover, the
Exception
class already provides a__str__()
method which we can make use of, here. E.g.:That is,
str(OpenIDTokenInvalid(e))
will useException.__str__()
and it will returnToken invalid with exception:
followed by the stringification ofe
, and you don't need any more code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just reverted this change. If we use
isinstance
to check the type of the cause in the unit test, we don't need to touch this exception class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good sign that using
isinstance()
is the right path. 😁However, doing the
super()
call is "more correct" than the current code, and it enables direct (and useful) stringification of the resulting instance. (The current implementation will inherit the__str__()
method, but, since it doesn't call the superclass c'tor, the method will produce an empty string, which is quite unsatisfying.)