Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mozilla_django_oidc/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.test.client import Client


class OIDCClient(Client):
oidc_id_token = 'some_oidc_token'

def __init__(self, enforce_csrf_checks=False, **defaults):
super(OIDCClient, self).__init__(enforce_csrf_checks=enforce_csrf_checks, **defaults)
session = self.session
session['oidc_id_token'] = self.oidc_id_token
session.save()
22 changes: 22 additions & 0 deletions tests/test_oidc_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.test.client import Client
from django.test.testcases import SimpleTestCase

from mozilla_django_oidc.test import OIDCClient


class TestOIDCClient(SimpleTestCase):

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?

Copy link
Contributor Author

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 :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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 :)

def test_inherits_from_django_test_client(self):
self.assertIsInstance(OIDCClient(), Client)

def test_oidc_id_token_is_set_to_some_token_by_default(self):
self.assertEqual(OIDCClient.oidc_id_token, 'some_oidc_token')

def test_sets_oidc_id_token_for_session(self):
self.assertEqual(OIDCClient().session['oidc_id_token'], 'some_oidc_token')

def test_sets_specified_id_token_for_session(self):
class StubOIDCClient(OIDCClient):
oidc_id_token = 'some_other_oidc_token'
Copy link
Contributor Author

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.


client = StubOIDCClient()
self.assertEqual(client.session['oidc_id_token'], 'some_other_oidc_token')