test_middleware.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from unittest import TestCase
  2. from unittest.mock import MagicMock, patch
  3. from django_sso.middleware import CASMiddleware
  4. class MiddlewareTests(TestCase):
  5. def setUp(self):
  6. from django.conf import settings
  7. if not settings.configured:
  8. settings.configure(DEBUG=True) # pragma: no cover
  9. settings.CAS_ADMIN_PREFIX = None
  10. def test_init(self):
  11. mock_middleware_mixin = MagicMock()
  12. _ = CASMiddleware(mock_middleware_mixin)
  13. def test_process_request(self):
  14. mock_middleware_mixin = MagicMock()
  15. mock_request = MagicMock()
  16. cas_middleware = CASMiddleware(mock_middleware_mixin)
  17. cas_middleware.process_request(request=mock_request)
  18. def test_process_view_login(self):
  19. mock_middleware_mixin = MagicMock()
  20. mock_request = MagicMock()
  21. view_func = MagicMock()
  22. view_args = []
  23. view_kwargs = {}
  24. cas_middleware = CASMiddleware(mock_middleware_mixin)
  25. mock_auth_views = (view_func, MagicMock())
  26. with patch(
  27. "django_sso.middleware.cas_middleware.get_auth_views",
  28. return_value=mock_auth_views,
  29. ):
  30. with patch("django_sso.middleware.cas_middleware.sso_login"):
  31. cas_middleware.process_view(
  32. mock_request,
  33. view_func,
  34. view_args,
  35. view_kwargs,
  36. )
  37. def test_process_view_logout(self):
  38. mock_middleware_mixin = MagicMock()
  39. mock_request = MagicMock()
  40. view_func = MagicMock()
  41. view_args = []
  42. view_kwargs = {}
  43. cas_middleware = CASMiddleware(mock_middleware_mixin)
  44. mock_auth_views = (MagicMock(), view_func)
  45. with patch(
  46. "django_sso.middleware.cas_middleware.get_auth_views",
  47. return_value=mock_auth_views,
  48. ):
  49. with patch("django_sso.middleware.cas_middleware.sso_logout"):
  50. cas_middleware.process_view(
  51. mock_request,
  52. view_func,
  53. view_args,
  54. view_kwargs,
  55. )
  56. def test_process_view_general(self):
  57. mock_middleware_mixin = MagicMock()
  58. mock_request = MagicMock()
  59. view_func = MagicMock()
  60. view_args = []
  61. view_kwargs = {}
  62. cas_middleware = CASMiddleware(mock_middleware_mixin)
  63. mock_auth_views = (MagicMock(), MagicMock())
  64. with patch(
  65. "django_sso.middleware.cas_middleware.get_auth_views",
  66. return_value=mock_auth_views,
  67. ):
  68. cas_middleware.process_view(
  69. mock_request,
  70. view_func,
  71. view_args,
  72. view_kwargs,
  73. )
  74. class MiddlewareTestsAdminPrefix(TestCase):
  75. def setUp(self):
  76. from django.conf import settings
  77. if not settings.configured:
  78. settings.configure(DEBUG=True) # pragma: no cover
  79. settings.CAS_ADMIN_PREFIX = "potato"
  80. def test_process_view_general(self):
  81. mock_middleware_mixin = MagicMock()
  82. mock_request = MagicMock()
  83. mock_request.path.startswith.return_value = False
  84. view_func = MagicMock()
  85. view_args = []
  86. view_kwargs = {}
  87. cas_middleware = CASMiddleware(mock_middleware_mixin)
  88. mock_auth_views = (MagicMock(), MagicMock())
  89. with patch(
  90. "django_sso.middleware.cas_middleware.get_auth_views",
  91. return_value=mock_auth_views,
  92. ):
  93. cas_middleware.process_view(
  94. mock_request,
  95. view_func,
  96. view_args,
  97. view_kwargs,
  98. )
  99. def test_process_view_general_is_staff(self):
  100. mock_middleware_mixin = MagicMock()
  101. mock_request = MagicMock()
  102. mock_request.path.startswith.return_value = True
  103. view_func = MagicMock()
  104. view_args = []
  105. view_kwargs = {}
  106. cas_middleware = CASMiddleware(mock_middleware_mixin)
  107. mock_auth_views = (MagicMock(), MagicMock())
  108. with patch(
  109. "django_sso.middleware.cas_middleware.get_auth_views",
  110. return_value=mock_auth_views,
  111. ):
  112. cas_middleware.process_view(
  113. mock_request,
  114. view_func,
  115. view_args,
  116. view_kwargs,
  117. )
  118. def test_process_view_general_not_staff(self):
  119. mock_middleware_mixin = MagicMock()
  120. mock_request = MagicMock()
  121. mock_request.path.startswith.return_value = True
  122. mock_request.user.is_staff = False
  123. view_func = MagicMock()
  124. view_args = []
  125. view_kwargs = {}
  126. cas_middleware = CASMiddleware(mock_middleware_mixin)
  127. mock_auth_views = (MagicMock(), MagicMock())
  128. with patch(
  129. "django_sso.middleware.cas_middleware.get_auth_views",
  130. return_value=mock_auth_views,
  131. ):
  132. cas_middleware.process_view(
  133. mock_request,
  134. view_func,
  135. view_args,
  136. view_kwargs,
  137. )
  138. def test_process_view_general_not_authed(self):
  139. mock_middleware_mixin = MagicMock()
  140. mock_request = MagicMock()
  141. mock_request.path.startswith.return_value = True
  142. mock_request.user.is_authenticated = False
  143. view_func = MagicMock()
  144. view_args = []
  145. view_kwargs = {}
  146. cas_middleware = CASMiddleware(mock_middleware_mixin)
  147. mock_auth_views = (MagicMock(), MagicMock())
  148. with patch(
  149. "django_sso.middleware.cas_middleware.get_auth_views",
  150. return_value=mock_auth_views,
  151. ):
  152. with patch(
  153. "django_sso.middleware.cas_middleware.reverse",
  154. return_value="/",
  155. ):
  156. cas_middleware.process_view(
  157. mock_request,
  158. view_func,
  159. view_args,
  160. view_kwargs,
  161. )