123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- from unittest import TestCase
- from unittest.mock import MagicMock, patch
- from django_sso.backends import CASBackend
- from django_sso.backends.cas_backend import get_cas_client
- class CASBackendSettingsTests(TestCase):
- def setUp(self):
- from django.conf import settings
- if not settings.configured:
- settings.configure(DEBUG=True)
- settings.CAS_VERSION = "foo"
- settings.CAS_SERVER_URL = "foo.bar"
- def test_authenticate_no_attributes(self):
- request = MagicMock()
- ticket = "mock_ticket"
- service = "foo.bar"
- mock_cas_client = MagicMock()
- mock_cas_client.verify_ticket.return_value = ("", {}, "")
- cas_backend = CASBackend()
- with (
- patch("django_sso.backends.cas_backend.get_user_model"),
- patch(
- "django_sso.backends.cas_backend.get_cas_client",
- return_value=mock_cas_client,
- ),
- ):
- _ = cas_backend.authenticate(request, ticket, service)
- def test_authenticate_user_dne(self):
- request = MagicMock()
- ticket = "mock_ticket"
- service = "foo.bar"
- mock_cas_client = MagicMock()
- mock_cas_client.verify_ticket.return_value = (
- "fake_username",
- {"foo": "bar"},
- "",
- )
- mock_user_model = MagicMock()
- mock_user_model.DoesNotExist = Exception
- mock_user_model.objects.get.side_effect = Exception
- cas_backend = CASBackend()
- with (
- patch(
- "django_sso.backends.cas_backend.get_user_model",
- return_value=mock_user_model,
- ),
- patch(
- "django_sso.backends.cas_backend.get_cas_client",
- return_value=mock_cas_client,
- ),
- ):
- _ = cas_backend.authenticate(request, ticket, service)
- def test_authenticate(self):
- request = MagicMock()
- ticket = "mock_ticket"
- service = "foo.bar"
- mock_cas_client = MagicMock()
- mock_cas_client.verify_ticket.return_value = (
- "fake_username",
- {"foo": "bar"},
- "",
- )
- cas_backend = CASBackend()
- with (
- patch("django_sso.backends.cas_backend.get_user_model"),
- patch(
- "django_sso.backends.cas_backend.get_cas_client",
- return_value=mock_cas_client,
- ),
- ):
- _ = cas_backend.authenticate(request, ticket, service)
- class CASBackendTests(TestCase):
- def test_init(self):
- _ = CASBackend()
- def test_get_user(self):
- user_id = "foo"
- cas_backend = CASBackend()
- with patch("django_sso.backends.cas_backend.get_user_model"):
- _ = cas_backend.get_user(user_id)
- def test_get_user_dne(self):
- user_id = "foo"
- mock_user_model = MagicMock()
- mock_user_model.DoesNotExist = Exception
- mock_user_model.objects.get.side_effect = Exception
- cas_backend = CASBackend()
- with patch(
- "django_sso.backends.cas_backend.get_user_model",
- return_value=mock_user_model,
- ):
- _ = cas_backend.get_user(user_id)
- def test_get_cas_client(self):
- server_url = ""
- service_url = ""
- version = ""
- with patch("django_sso.backends.cas_backend.CASClient"):
- _ = get_cas_client(server_url, service_url, version)
|