django-oidc-provider/oidc_provider/tests/test_middleware.py
Wojciech Bartosiak a829726be8 Merge develop to v0.5.x (#179)
* Log create_uri_response exceptions to logger.exception

* Support grant type password - basics

* Add tests for Resource Owner Password Credentials Flow

* Password Grant -Response according to specification

* Better tests for errors, disable grant type password by default

* Add documentation for grant type password

* User authentication failure to return 403

* Add id_token to response

* skipping consent only works for confidential clients

* fix URI fragment

example not working URL `http://localhost:8100/#/auth/callback/`

* OIDC_POST_END_SESSION_HOOK + tests

* Explicit function naming

* Remove print statements

* No need for semicolons, this is Python

* Update CHANGELOG.md

* fixed logger message

* Improved `exp` value calculation

* rename OIDC_POST_END_SESSION_HOOK to OIDC_AFTER_END_SESSION_HOOK

* added docs for OIDC_AFTER_END_SESSION_HOOK

*  Replaces `LOGIN_URL` with `OIDC_LOGIN_URL`
so users can use a different login path for their oidc requests.

* Adds a setting variable for custom template paths

* Updates documentation

* Fixed bad try/except/finally block

* Adds test for OIDC_TEMPLATES settings

* Determine value for op_browser_state from session_key or default

* Do not use cookie for browser_state. It may not yet be there

* Add docs on new setting

OIDC_UNAUTHENTICATED_SESSION_MANAGEMENT_KEY

* Fix compatibility for older versions of Django

* solved merging typo for missing @property
2017-05-05 05:19:57 +02:00

39 lines
1.4 KiB
Python

from django.conf.urls import url
from django.test import TestCase, override_settings
from django.views.generic import View
from mock import mock
class StubbedViews:
class SampleView(View):
pass
urlpatterns = [url('^test/', SampleView.as_view())]
MW_CLASSES = ('django.contrib.sessions.middleware.SessionMiddleware',
'oidc_provider.middleware.SessionManagementMiddleware')
@override_settings(ROOT_URLCONF=StubbedViews,
MIDDLEWARE=MW_CLASSES,
MIDDLEWARE_CLASSES=MW_CLASSES,
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)