test_views.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from unittest import TestCase
  2. from unittest.mock import MagicMock, patch
  3. from django_sso.views import login, logout
  4. class ViewsLoginTests(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. def test_login_is_authenticated(self):
  10. mock_request = MagicMock()
  11. mock_request.GET = {}
  12. mock_request.is_secure.return_value = True
  13. mock_request.get_host.return_value = "foo.bar"
  14. mock_settings = MagicMock()
  15. mock_settings.CAS_REDIRECT_URL = "/"
  16. _ = login(request=mock_request, settings=mock_settings)
  17. def test_login_no_ticket(self):
  18. mock_request = MagicMock()
  19. mock_request.GET = {}
  20. mock_request.is_secure.return_value = True
  21. mock_request.get_host.return_value = "foo.bar"
  22. mock_request.user.is_authenticated = False
  23. mock_settings = MagicMock()
  24. mock_settings.CAS_REDIRECT_URL = "/"
  25. mock_settings.CAS_SERVER_URL = "sso.example.com"
  26. _ = login(request=mock_request, settings=mock_settings)
  27. def test_login_has_ticket(self):
  28. mock_request = MagicMock()
  29. mock_request.GET = {"ticket": "foo"}
  30. mock_request.is_secure.return_value = True
  31. mock_request.get_host.return_value = "foo.bar"
  32. mock_request.user.is_authenticated = False
  33. mock_settings = MagicMock()
  34. mock_settings.CAS_REDIRECT_URL = "/"
  35. mock_settings.CAS_SERVER_URL = "sso.example.com"
  36. mock_settings.CAS_RETRY_LOGIN = True
  37. mock_user = None
  38. with patch("django_sso.business._get_user", return_value=mock_user):
  39. _ = login(request=mock_request, settings=mock_settings)
  40. def test_login_user_not_none(self):
  41. mock_request = MagicMock()
  42. mock_request.GET = {"ticket": "foo"}
  43. mock_request.is_secure.return_value = True
  44. mock_request.get_host.return_value = "foo.bar"
  45. mock_request.user.is_authenticated = False
  46. mock_settings = MagicMock()
  47. mock_settings.CAS_REDIRECT_URL = "/"
  48. mock_settings.CAS_SERVER_URL = "sso.example.com"
  49. mock_settings.CAS_RETRY_LOGIN = False
  50. mock_user = MagicMock()
  51. with patch("django_sso.business._get_user", return_value=mock_user):
  52. with patch("django_sso.business._login_user"):
  53. _ = login(request=mock_request, settings=mock_settings)
  54. def test_login_user_no_retry(self):
  55. mock_request = MagicMock()
  56. mock_request.GET = {"ticket": "foo"}
  57. mock_request.is_secure.return_value = True
  58. mock_request.get_host.return_value = "foo.bar"
  59. mock_request.user.is_authenticated = False
  60. mock_settings = MagicMock()
  61. mock_settings.CAS_REDIRECT_URL = "/"
  62. mock_settings.CAS_SERVER_URL = "sso.example.com"
  63. mock_settings.CAS_RETRY_LOGIN = False
  64. mock_user = None
  65. with patch("django_sso.business._get_user", return_value=mock_user):
  66. _ = login(request=mock_request, settings=mock_settings)
  67. class ViewsLogoutTests(TestCase):
  68. def setUp(self):
  69. from django.conf import settings
  70. if not settings.configured:
  71. settings.configure(DEBUG=True) # pragma: no cover
  72. def test_logout_logout_completely(self):
  73. mock_request = MagicMock()
  74. mock_settings = MagicMock()
  75. mock_settings.CAS_LOGOUT_COMPLETELY = True
  76. mock_settings.CAS_SERVER_URL = "sso.example.com"
  77. with patch("django_sso.business._logout_user"):
  78. _ = logout(request=mock_request, settings=mock_settings)
  79. def test_logout(self):
  80. mock_request = MagicMock()
  81. mock_request.GET = {}
  82. mock_request.is_secure.return_value = True
  83. mock_request.get_host.return_value = "foo.bar"
  84. mock_settings = MagicMock()
  85. mock_settings.CAS_LOGOUT_COMPLETELY = False
  86. mock_settings.CAS_SERVER_URL = "sso.example.com"
  87. mock_settings.CAS_REDIRECT_URL = "/"
  88. mock_settings.CAS_IGNORE_REFERER = True
  89. with patch("django_sso.business._logout_user"):
  90. _ = logout(request=mock_request, settings=mock_settings)