test_casbackend.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from unittest import TestCase
  2. from unittest.mock import MagicMock, patch
  3. from django_sso.backends import CASBackend
  4. from django_sso.backends.cas_backend import get_cas_client
  5. class CASBackendSettingsTests(TestCase):
  6. def setUp(self):
  7. from django.conf import settings
  8. if not settings.configured:
  9. settings.configure(DEBUG=True) # pragma: no cover
  10. settings.CAS_VERSION = "foo"
  11. settings.CAS_SERVER_URL = "foo.bar"
  12. def test_authenticate_no_attributes(self):
  13. request = MagicMock()
  14. ticket = "mock_ticket"
  15. service = "foo.bar"
  16. mock_cas_client = MagicMock()
  17. mock_cas_client.verify_ticket.return_value = ("", {}, "")
  18. cas_backend = CASBackend()
  19. with (
  20. patch("django_sso.backends.cas_backend.get_user_model"),
  21. patch(
  22. "django_sso.backends.cas_backend.get_cas_client",
  23. return_value=mock_cas_client,
  24. ),
  25. ):
  26. _ = cas_backend.authenticate(request, ticket, service)
  27. def test_authenticate_user_dne(self):
  28. request = MagicMock()
  29. ticket = "mock_ticket"
  30. service = "foo.bar"
  31. mock_cas_client = MagicMock()
  32. mock_cas_client.verify_ticket.return_value = (
  33. "fake_username",
  34. {"foo": "bar"},
  35. "",
  36. )
  37. mock_user_model = MagicMock()
  38. mock_user_model.DoesNotExist = Exception
  39. mock_user_model.objects.get.side_effect = Exception
  40. cas_backend = CASBackend()
  41. with (
  42. patch(
  43. "django_sso.backends.cas_backend.get_user_model",
  44. return_value=mock_user_model,
  45. ),
  46. patch(
  47. "django_sso.backends.cas_backend.get_cas_client",
  48. return_value=mock_cas_client,
  49. ),
  50. ):
  51. _ = cas_backend.authenticate(request, ticket, service)
  52. def test_authenticate(self):
  53. request = MagicMock()
  54. ticket = "mock_ticket"
  55. service = "foo.bar"
  56. mock_cas_client = MagicMock()
  57. mock_cas_client.verify_ticket.return_value = (
  58. "fake_username",
  59. {"foo": "bar"},
  60. "",
  61. )
  62. cas_backend = CASBackend()
  63. with (
  64. patch("django_sso.backends.cas_backend.get_user_model"),
  65. patch(
  66. "django_sso.backends.cas_backend.get_cas_client",
  67. return_value=mock_cas_client,
  68. ),
  69. ):
  70. _ = cas_backend.authenticate(request, ticket, service)
  71. class CASBackendTests(TestCase):
  72. def test_init(self):
  73. _ = CASBackend()
  74. def test_get_user(self):
  75. user_id = "foo"
  76. cas_backend = CASBackend()
  77. with patch("django_sso.backends.cas_backend.get_user_model"):
  78. _ = cas_backend.get_user(user_id)
  79. def test_get_user_dne(self):
  80. user_id = "foo"
  81. mock_user_model = MagicMock()
  82. mock_user_model.DoesNotExist = Exception
  83. mock_user_model.objects.get.side_effect = Exception
  84. cas_backend = CASBackend()
  85. with patch(
  86. "django_sso.backends.cas_backend.get_user_model",
  87. return_value=mock_user_model,
  88. ):
  89. _ = cas_backend.get_user(user_id)
  90. def test_get_cas_client(self):
  91. server_url = ""
  92. service_url = ""
  93. version = ""
  94. with patch("django_sso.backends.cas_backend.CASClient"):
  95. _ = get_cas_client(server_url, service_url, version)