Skip to content

Commit 6c37d0c

Browse files
committed
More tests for improved test coverage
1 parent cfa4d05 commit 6c37d0c

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
lines changed

oauth2_provider/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import functools
2-
import random
32
import hashlib
3+
import random
44

55
from django.conf import settings
66
from jwcrypto import jwk
77
from oauthlib.common import Request
88

9-
from .settings import oauth2_settings
10-
119

1210
@functools.lru_cache()
1311
def jwk_from_pem(pem_string):
@@ -109,5 +107,8 @@ def session_management_state_key(request):
109107
"""
110108
Determine value to use as session state.
111109
"""
110+
111+
from oauth2_provider.settings import oauth2_settings
112+
112113
key = request.session.session_key or str(oauth2_settings.OIDC_SESSION_MANAGEMENT_DEFAULT_SESSION_KEY)
113114
return hashlib.sha256(key.encode("utf-8")).hexdigest()

tests/app/idp/idp/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222

2323
urlpatterns = [
24-
path('', TemplateView.as_view(template_name='home/index.html'), name='home'), # Maps the root URL to your home_view
24+
# Maps the root URL to your home_view
25+
path("", TemplateView.as_view(template_name="home/index.html"), name="home"),
2526
path("admin/", admin.site.urls),
2627
path("o/", include("oauth2_provider.urls", namespace="oauth2_provider")),
2728
path("accounts/", include("django.contrib.auth.urls")),

tests/test_django_checks.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
from copy import deepcopy
2+
13
from django.core.management import call_command
24
from django.core.management.base import SystemCheckError
35
from django.test import override_settings
46

57
from .common_testing import OAuth2ProviderTestCase as TestCase
8+
from .presets import OIDC_SETTINGS_SESSION_MANAGEMENT
9+
10+
11+
MISSING_DEFAULT_SESSION_KEY = deepcopy(OIDC_SETTINGS_SESSION_MANAGEMENT)
12+
MISSING_DEFAULT_SESSION_KEY["OIDC_SESSION_MANAGEMENT_DEFAULT_SESSION_KEY"] = None
613

714

815
class DjangoChecksTestCase(TestCase):
@@ -18,3 +25,11 @@ def test_checks_fail_when_router_crosses_databases(self):
1825
message = "The token models are expected to be stored in the same database."
1926
with self.assertRaisesMessage(SystemCheckError, message):
2027
call_command("check")
28+
29+
@override_settings(OAUTH2_PROVIDER=MISSING_DEFAULT_SESSION_KEY)
30+
def test_checks_fail_when_default_session_key_is_missing(self):
31+
message = (
32+
"OIDC Session management is enabled, OIDC_SESSION_MANAGEMENT_DEFAULT_SESSION_KEY is required."
33+
)
34+
with self.assertRaisesMessage(SystemCheckError, message):
35+
call_command("check")

tests/test_oauth2_validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def test_load_application_uses_cached_when_request_has_valid_client_matching_cli
228228
self.assertIs(self.request.client, self.application)
229229

230230
def test_load_application_succeeds_when_request_has_invalid_client_valid_client_id(self):
231-
self.request.client = 'invalid_client'
231+
self.request.client = "invalid_client"
232232
application = self.validator._load_application("client_id", self.request)
233233
self.assertEqual(application, self.application)
234234
self.assertEqual(self.request.client, self.application)

tests/test_oidc_views.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
)
2121
from oauth2_provider.oauth2_validators import OAuth2Validator
2222
from oauth2_provider.settings import oauth2_settings
23-
from oauth2_provider.views.oidc import RPInitiatedLogoutView, _load_id_token, _validate_claims
23+
from oauth2_provider.views.oidc import (
24+
RPInitiatedLogoutView,
25+
SessionIFrameView,
26+
_load_id_token,
27+
_validate_claims,
28+
)
2429

2530
from . import presets
2631
from .common_testing import OAuth2ProviderTestCase as TestCase
@@ -116,6 +121,13 @@ def test_get_connect_discovery_info_with_rp_logout(self):
116121
self.oauth2_settings.OIDC_RP_INITIATED_LOGOUT_ENABLED = True
117122
self.expect_json_response_with_rp_logout(self.oauth2_settings.OIDC_ISS_ENDPOINT)
118123

124+
def test_get_session_manangement_iframe_endpoint(self):
125+
self.oauth2_settings.OIDC_SESSION_MANAGEMENT_ENABLED = True
126+
response = self.client.get(reverse("oauth2_provider:oidc-connect-discovery-info"))
127+
self.assertEqual(response.status_code, 200)
128+
response_data = response.json()
129+
self.assertIn("check_session_iframe", response_data.keys())
130+
119131
def test_get_connect_discovery_info_without_issuer_url(self):
120132
self.oauth2_settings.OIDC_ISS_ENDPOINT = None
121133
self.oauth2_settings.OIDC_USERINFO_ENDPOINT = None
@@ -216,29 +228,31 @@ def test_get_jwks_info_multiple_rsa_keys(self):
216228

217229
@pytest.mark.usefixtures("oauth2_settings")
218230
@pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_SESSION_MANAGEMENT)
219-
class TestAuthorizationView(TestCase):
220-
def test_session_state_is_present_in_url(self):
231+
class TestSessionManagement(TestCase):
232+
def setUp(self):
221233
User = get_user_model()
222234
Application = get_application_model()
223235

224-
User.objects.create_user("test_user", "test@example.com", "123456")
225-
dev_user = User.objects.create_user("dev_user", "dev@example.com", "123456")
236+
self.user = User.objects.create_user("test_user", "test@example.com", "123456")
237+
self.developer = User.objects.create_user("dev_user", "dev@example.com", "123456")
226238

227-
application = Application.objects.create(
239+
self.application = Application.objects.create(
228240
name="Test Application",
229241
redirect_uris=(
230242
"http://localhost http://example.com http://example.org custom-scheme://example.com"
231243
),
232-
user=dev_user,
244+
user=self.developer,
233245
client_type=Application.CLIENT_CONFIDENTIAL,
234246
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
235247
client_secret="1234567890qwertyuiop",
236248
)
249+
250+
def test_session_state_is_present_in_authorization(self):
237251
self.client.login(username="test_user", password="123456")
238252
response = self.client.post(
239253
reverse("oauth2_provider:authorize"),
240254
{
241-
"client_id": application.client_id,
255+
"client_id": self.application.client_id,
242256
"response_type": "code",
243257
"state": "random_state_string",
244258
"scope": "read write",
@@ -247,7 +261,16 @@ def test_session_state_is_present_in_url(self):
247261
},
248262
)
249263
self.assertEqual(response.status_code, 302)
250-
self.assertTrue("session_state" in response["Location"])
264+
self.assertIn("session_state", response["Location"])
265+
266+
def test_cookie_name_is_included_in_iframe_endpoint(self):
267+
request = RequestFactory().get(reverse("oauth2_provider:session-iframe"))
268+
request.user = self.user
269+
view = SessionIFrameView()
270+
view.setup(request)
271+
context = view.get_context_data()
272+
self.assertIn("cookie_name", context)
273+
self.assertEqual(context["cookie_name"], "oidc_ua_agent_state")
251274

252275

253276
def mock_request():

tests/test_session_management.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class TestOIDCSessionManagementMiddleware(TestCase):
2222
def setUp(self):
2323
User.objects.create_user("test_user", "test@example.com", "123456")
2424

25+
def test_response_is_intact_if_session_management_is_disabled(self):
26+
self.oauth2_settings.OIDC_SESSION_MANAGEMENT_ENABLED = False
27+
response = self.client.get("/a-resource")
28+
self.assertFalse("oidc-session-test" in response.cookies.keys())
29+
2530
def test_session_cookie_is_set_for_logged_users(self):
2631
self.client.login(username="test_user", password="123456")
2732
response = self.client.get("/a-resource")

0 commit comments

Comments
 (0)