You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.3 KiB

  1. from unittest.mock import patch
  2. from django.contrib.auth.models import User
  3. from django.test.utils import override_settings
  4. from hc.test import BaseTestCase
  5. @override_settings(
  6. REMOTE_USER_HEADER="AUTH_USER",
  7. AUTHENTICATION_BACKENDS=("hc.accounts.backends.CustomHeaderBackend",),
  8. )
  9. class RemoteUserHeaderTestCase(BaseTestCase):
  10. @override_settings(REMOTE_USER_HEADER=None)
  11. def test_it_does_nothing_when_not_configured(self):
  12. r = self.client.get("/accounts/profile/", AUTH_USER="[email protected]")
  13. self.assertRedirects(r, "/accounts/login/?next=/accounts/profile/")
  14. def test_it_logs_user_in(self):
  15. r = self.client.get("/accounts/profile/", AUTH_USER="[email protected]")
  16. self.assertContains(r, "[email protected]")
  17. def test_it_does_nothing_when_header_not_set(self):
  18. r = self.client.get("/accounts/profile/")
  19. self.assertRedirects(r, "/accounts/login/?next=/accounts/profile/")
  20. def test_it_does_nothing_when_header_is_empty_string(self):
  21. r = self.client.get("/accounts/profile/", AUTH_USER="")
  22. self.assertRedirects(r, "/accounts/login/?next=/accounts/profile/")
  23. def test_it_creates_user(self):
  24. r = self.client.get("/accounts/profile/", AUTH_USER="[email protected]")
  25. self.assertContains(r, "[email protected]")
  26. q = User.objects.filter(email="[email protected]")
  27. self.assertTrue(q.exists())
  28. def test_it_logs_out_another_user_when_header_is_empty_string(self):
  29. self.client.login(remote_user_email="[email protected]")
  30. r = self.client.get("/accounts/profile/", AUTH_USER="")
  31. self.assertRedirects(r, "/accounts/login/?next=/accounts/profile/")
  32. def test_it_logs_out_another_user(self):
  33. self.client.login(remote_user_email="[email protected]")
  34. r = self.client.get("/accounts/profile/", AUTH_USER="[email protected]")
  35. self.assertContains(r, "[email protected]")
  36. def test_it_handles_already_logged_in_user(self):
  37. self.client.login(remote_user_email="[email protected]")
  38. with patch("hc.accounts.middleware.auth") as mock_auth:
  39. r = self.client.get("/accounts/profile/", AUTH_USER="[email protected]")
  40. self.assertFalse(mock_auth.authenticate.called)
  41. self.assertContains(r, "[email protected]")