123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- from unittest import TestCase
- from unittest.mock import MagicMock, patch
- from django_sso.middleware import CASMiddleware
- class MiddlewareTests(TestCase):
- def setUp(self):
- from django.conf import settings
- if not settings.configured:
- settings.configure(DEBUG=True) # pragma: no cover
- settings.CAS_ADMIN_PREFIX = None
- def test_init(self):
- mock_middleware_mixin = MagicMock()
- _ = CASMiddleware(mock_middleware_mixin)
- def test_process_request(self):
- mock_middleware_mixin = MagicMock()
- mock_request = MagicMock()
- cas_middleware = CASMiddleware(mock_middleware_mixin)
- cas_middleware.process_request(request=mock_request)
- def test_process_view_login(self):
- mock_middleware_mixin = MagicMock()
- mock_request = MagicMock()
- view_func = MagicMock()
- view_args = []
- view_kwargs = {}
- cas_middleware = CASMiddleware(mock_middleware_mixin)
- mock_auth_views = (view_func, MagicMock())
- with patch(
- "django_sso.middleware.cas_middleware.get_auth_views",
- return_value=mock_auth_views,
- ):
- with patch("django_sso.middleware.cas_middleware.sso_login"):
- cas_middleware.process_view(
- mock_request,
- view_func,
- view_args,
- view_kwargs,
- )
- def test_process_view_logout(self):
- mock_middleware_mixin = MagicMock()
- mock_request = MagicMock()
- view_func = MagicMock()
- view_args = []
- view_kwargs = {}
- cas_middleware = CASMiddleware(mock_middleware_mixin)
- mock_auth_views = (MagicMock(), view_func)
- with patch(
- "django_sso.middleware.cas_middleware.get_auth_views",
- return_value=mock_auth_views,
- ):
- with patch("django_sso.middleware.cas_middleware.sso_logout"):
- cas_middleware.process_view(
- mock_request,
- view_func,
- view_args,
- view_kwargs,
- )
- def test_process_view_general(self):
- mock_middleware_mixin = MagicMock()
- mock_request = MagicMock()
- view_func = MagicMock()
- view_args = []
- view_kwargs = {}
- cas_middleware = CASMiddleware(mock_middleware_mixin)
- mock_auth_views = (MagicMock(), MagicMock())
- with patch(
- "django_sso.middleware.cas_middleware.get_auth_views",
- return_value=mock_auth_views,
- ):
- cas_middleware.process_view(
- mock_request,
- view_func,
- view_args,
- view_kwargs,
- )
- class MiddlewareTestsAdminPrefix(TestCase):
- def setUp(self):
- from django.conf import settings
- if not settings.configured:
- settings.configure(DEBUG=True) # pragma: no cover
- settings.CAS_ADMIN_PREFIX = "potato"
- def test_process_view_general(self):
- mock_middleware_mixin = MagicMock()
- mock_request = MagicMock()
- mock_request.path.startswith.return_value = False
- view_func = MagicMock()
- view_args = []
- view_kwargs = {}
- cas_middleware = CASMiddleware(mock_middleware_mixin)
- mock_auth_views = (MagicMock(), MagicMock())
- with patch(
- "django_sso.middleware.cas_middleware.get_auth_views",
- return_value=mock_auth_views,
- ):
- cas_middleware.process_view(
- mock_request,
- view_func,
- view_args,
- view_kwargs,
- )
- def test_process_view_general_is_staff(self):
- mock_middleware_mixin = MagicMock()
- mock_request = MagicMock()
- mock_request.path.startswith.return_value = True
- view_func = MagicMock()
- view_args = []
- view_kwargs = {}
- cas_middleware = CASMiddleware(mock_middleware_mixin)
- mock_auth_views = (MagicMock(), MagicMock())
- with patch(
- "django_sso.middleware.cas_middleware.get_auth_views",
- return_value=mock_auth_views,
- ):
- cas_middleware.process_view(
- mock_request,
- view_func,
- view_args,
- view_kwargs,
- )
- def test_process_view_general_not_staff(self):
- mock_middleware_mixin = MagicMock()
- mock_request = MagicMock()
- mock_request.path.startswith.return_value = True
- mock_request.user.is_staff = False
- view_func = MagicMock()
- view_args = []
- view_kwargs = {}
- cas_middleware = CASMiddleware(mock_middleware_mixin)
- mock_auth_views = (MagicMock(), MagicMock())
- with patch(
- "django_sso.middleware.cas_middleware.get_auth_views",
- return_value=mock_auth_views,
- ):
- cas_middleware.process_view(
- mock_request,
- view_func,
- view_args,
- view_kwargs,
- )
- def test_process_view_general_not_authed(self):
- mock_middleware_mixin = MagicMock()
- mock_request = MagicMock()
- mock_request.path.startswith.return_value = True
- mock_request.user.is_authenticated = False
- view_func = MagicMock()
- view_args = []
- view_kwargs = {}
- cas_middleware = CASMiddleware(mock_middleware_mixin)
- mock_auth_views = (MagicMock(), MagicMock())
- with patch(
- "django_sso.middleware.cas_middleware.get_auth_views",
- return_value=mock_auth_views,
- ):
- with patch(
- "django_sso.middleware.cas_middleware.reverse",
- return_value="/",
- ):
- cas_middleware.process_view(
- mock_request,
- view_func,
- view_args,
- view_kwargs,
- )
|