2017-04-18 09:25:16 +00:00
|
|
|
from django.conf.urls import url
|
|
|
|
from django.test import TestCase, override_settings
|
2017-04-18 12:24:21 +00:00
|
|
|
from django.views.generic import View
|
2017-04-18 09:25:16 +00:00
|
|
|
from mock import mock
|
|
|
|
|
|
|
|
|
|
|
|
class StubbedViews:
|
|
|
|
class SampleView(View):
|
|
|
|
pass
|
|
|
|
|
|
|
|
urlpatterns = [url('^test/', SampleView.as_view())]
|
|
|
|
|
2017-04-18 12:24:21 +00:00
|
|
|
MW_CLASSES = ('django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'oidc_provider.middleware.SessionManagementMiddleware')
|
|
|
|
|
2017-04-18 09:25:16 +00:00
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF=StubbedViews,
|
2017-04-18 12:24:21 +00:00
|
|
|
MIDDLEWARE=MW_CLASSES,
|
|
|
|
MIDDLEWARE_CLASSES=MW_CLASSES,
|
2017-04-18 09:25:16 +00:00
|
|
|
OIDC_SESSION_MANAGEMENT_ENABLE=True)
|
|
|
|
class MiddlewareTestCase(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
patcher = mock.patch('oidc_provider.middleware.get_browser_state_or_default')
|
|
|
|
self.mock_get_state = patcher.start()
|
|
|
|
|
|
|
|
def test_session_management_middleware_sets_cookie_on_response(self):
|
|
|
|
response = self.client.get('/test/')
|
|
|
|
|
|
|
|
self.assertIn('op_browser_state', response.cookies)
|
|
|
|
self.assertEqual(response.cookies['op_browser_state'].value,
|
|
|
|
str(self.mock_get_state.return_value))
|
|
|
|
self.mock_get_state.assert_called_once_with(response.wsgi_request)
|
|
|
|
|
|
|
|
@override_settings(OIDC_SESSION_MANAGEMENT_ENABLE=False)
|
|
|
|
def test_session_management_middleware_does_not_set_cookie_if_session_management_disabled(self):
|
|
|
|
response = self.client.get('/test/')
|
|
|
|
|
|
|
|
self.assertNotIn('op_browser_state', response.cookies)
|