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 all commits
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()
23 changes: 23 additions & 0 deletions tests/test_oidc_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from unittest import TestCase

from django.test.client import Client

from mozilla_django_oidc.test import OIDCClient


class TestOIDCClient(TestCase):
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')