From 3659c81f991c31b2d161e97885d9497974df9307 Mon Sep 17 00:00:00 2001 From: Shea Polansky Date: Fri, 4 Dec 2020 20:00:55 -0800 Subject: [PATCH] Add test cases for remote header login --- .../tests/test_remote_user_header_login.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 hc/accounts/tests/test_remote_user_header_login.py diff --git a/hc/accounts/tests/test_remote_user_header_login.py b/hc/accounts/tests/test_remote_user_header_login.py new file mode 100644 index 00000000..f071ee39 --- /dev/null +++ b/hc/accounts/tests/test_remote_user_header_login.py @@ -0,0 +1,34 @@ +from unittest.mock import patch +from django.test.utils import override_settings +from hc.test import BaseTestCase +from hc.accounts.middleware import CustomHeaderMiddleware +from django.conf import settings + +class RemoteUserHeaderTestCase(BaseTestCase): + @override_settings(REMOTE_USER_HEADER_TYPE="") + def test_it_does_nothing_when_disabled(self): + r = self.client.get("/accounts/profile/", AUTH_USER="alice@example.org") + self.assertRedirects(r, "/accounts/login/?next=/accounts/profile/") + + @override_settings(REMOTE_USER_HEADER_TYPE="EMAIL") + def test_it_logs_users_in_by_email(self): + r = self.client.get("/accounts/profile/", AUTH_USER="alice@example.org") + self.assertContains(r, "alice@example.org") + + @override_settings(REMOTE_USER_HEADER="HTTP_AUTH_TEST", REMOTE_USER_HEADER_TYPE="EMAIL") + def test_it_allows_customizing_the_header(self): + # patch the CustomHeaderMiddleware's header value since it's static and + # won't be updated automatically --- this is OK outside of test, since + # that value shouldn't change after instantiation anyway + _old_header = CustomHeaderMiddleware.header + CustomHeaderMiddleware.header = settings.REMOTE_USER_HEADER + r = self.client.get("/accounts/profile/", HTTP_AUTH_TEST="alice@example.org") + self.assertContains(r, "alice@example.org") + + # un-patch the header + CustomHeaderMiddleware.header = _old_header + + @override_settings(REMOTE_USER_HEADER_TYPE="ID") + def test_it_logs_users_in_by_id(self): + r = self.client.get("/accounts/profile/", AUTH_USER="alice") + self.assertContains(r, "alice@example.org") \ No newline at end of file