-
Notifications
You must be signed in to change notification settings - Fork 169
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
Add OIDCClient for django tests #318
base: main
Are you sure you want to change the base?
Conversation
Add an OIDCClient for django tests. This can be useful when you need to use the oidc_id_token in your views. The default Django test client does not provide this. By making use of this client your requests will contain a `oidc_id_token` key with a random value (which you can specify). Because there is no easy way to do this yourself for the Django test client (you can either fix it by writing a custom client or by using a `RequestFactory`, both are not very ideal) I thought it would be useful to provide a default client that comes with the mozilla library.
|
||
def test_sets_specified_id_token_for_session(self): | ||
class StubOIDCClient(OIDCClient): | ||
oidc_id_token = 'some_other_oidc_token' |
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.
Note that I tested it like this because the session property of the django test client is a little weird to wrap your head around. So instead of instantiating and specifying a new oidc token and refreshing the session, I thought a test like this would be better.
Codecov Report
@@ Coverage Diff @@
## master #318 +/- ##
=========================================
+ Coverage 88.36% 88.57% +0.2%
=========================================
Files 7 8 +1
Lines 447 455 +8
=========================================
+ Hits 395 403 +8
Misses 52 52
Continue to review full report at Codecov.
|
Usage when specifying this class remains the same for the test client, you just have to specify it in the test case:
|
Hi there, could someone have a look at this? |
Could someone have a look at this? |
Bump :) |
tests/test_oidc_client.py
Outdated
from mozilla_django_oidc.test import OIDCClient | ||
|
||
|
||
class TestOIDCClient(SimpleTestCase): |
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 think deriving from TestCase
may work better since none of the things added by SimpleTestCase
seem needed here — are they?
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've changed it to make use of unittest.Testcase
. I assume that's what you mean, since the Django TestCase
class inherits from SimpleTestCase
:)
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.
It's the other way around — see https://github.com/django/django/blob/a6b3938afc0204093b5356ade2be30b461a698c5/django/test/testcases.py#L146 — but thanks for the change!
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.
Yes that's what I meant :) django.TestCase
inherits from django.SimpleTestCase
which inherits from unittest.TestCase
😄 Anyhoo, no worries, thanks for the feedback :)
@kerrermanisNL I've removed the set of reviewers you requested review from as that group are the org owners. They don't have context on this |
Add an OIDCClient for django tests. This can be useful when you need to use the
oidc_id_token
in your views. The default Django test client does not provide this. By making use of this new client your requests will contain aoidc_id_token
key with a random value (which you can specify). Because there is no easy way to do this yourself for the Django test client (you can either fix it by writing a custom client or by using aRequestFactory
, both are not very ideal) I thought it would be useful to provide a default client that comes with the mozilla library.