diff --git a/docs/whats_new.rst b/docs/whats_new.rst index 0eda0317d..41ff291b0 100644 --- a/docs/whats_new.rst +++ b/docs/whats_new.rst @@ -42,6 +42,7 @@ See: :doc:`tethys_sdk/gizmos/time_picker` Bug Fixes --------- +* Captcha field rendering fix for the login and register pages: `PR 1297 `_ * Login and register form fixes: `PR 1293 `_ * Static file discovery fix for ``STATICFILES_USE_NPM``: `PR 1291 `_ * Django 5 app initialization warning fix: `PR 1288 `_ diff --git a/tests/unit_tests/test_tethys_portal/test_views/test_accounts_captcha.py b/tests/unit_tests/test_tethys_portal/test_views/test_accounts_captcha.py new file mode 100644 index 000000000..584e77fee --- /dev/null +++ b/tests/unit_tests/test_tethys_portal/test_views/test_accounts_captcha.py @@ -0,0 +1,77 @@ +from importlib import reload +from unittest import mock + +from django.test import TestCase, override_settings +from django.urls import reverse + +import tethys_portal.forms as tp_forms + + +class TethysPortalAccountsCaptchaRenderingTest(TestCase): + def tearDown(self): + reload(tp_forms) + + def render_login(self): + with mock.patch("tethys_portal.views.accounts.LoginForm", tp_forms.LoginForm): + return self.client.get(reverse("accounts:login")) + + def render_register(self): + with mock.patch( + "tethys_portal.views.accounts.RegisterForm", tp_forms.RegisterForm + ): + return self.client.get(reverse("accounts:register")) + + @override_settings( + ENABLE_CAPTCHA=True, + RECAPTCHA_PRIVATE_KEY="", + RECAPTCHA_PUBLIC_KEY="", + SHOW_PUBLIC_IF_NO_TENANT_FOUND=True, + ) + def test_login_page_renders_captcha_when_enabled(self): + reload(tp_forms) + self.assertIn("captcha", tp_forms.LoginForm().fields) + + response = self.render_login() + + self.assertEqual(200, response.status_code) + self.assertContains(response, 'name="captcha_1"') + + @override_settings(ENABLE_CAPTCHA=False, SHOW_PUBLIC_IF_NO_TENANT_FOUND=True) + def test_login_page_omits_captcha_when_disabled(self): + reload(tp_forms) + self.assertNotIn("captcha", tp_forms.LoginForm().fields) + + response = self.render_login() + + self.assertEqual(200, response.status_code) + self.assertNotContains(response, 'name="captcha_1"') + + @override_settings( + ENABLE_CAPTCHA=True, + ENABLE_OPEN_SIGNUP=True, + RECAPTCHA_PRIVATE_KEY="", + RECAPTCHA_PUBLIC_KEY="", + SHOW_PUBLIC_IF_NO_TENANT_FOUND=True, + ) + def test_register_page_renders_captcha_when_enabled(self): + reload(tp_forms) + self.assertIn("captcha", tp_forms.RegisterForm().fields) + + response = self.render_register() + + self.assertEqual(200, response.status_code) + self.assertContains(response, 'name="captcha_1"') + + @override_settings( + ENABLE_CAPTCHA=False, + ENABLE_OPEN_SIGNUP=True, + SHOW_PUBLIC_IF_NO_TENANT_FOUND=True, + ) + def test_register_page_omits_captcha_when_disabled(self): + reload(tp_forms) + self.assertNotIn("captcha", tp_forms.RegisterForm().fields) + + response = self.render_register() + + self.assertEqual(200, response.status_code) + self.assertNotContains(response, 'name="captcha_1"') diff --git a/tethys_portal/templates/tethys_portal/accounts/login.html b/tethys_portal/templates/tethys_portal/accounts/login.html index aeea6be06..1508f34ec 100644 --- a/tethys_portal/templates/tethys_portal/accounts/login.html +++ b/tethys_portal/templates/tethys_portal/accounts/login.html @@ -24,6 +24,7 @@ {% csrf_token %} {% bootstrap_field form.username show_label=False %} {% bootstrap_field form.password show_label=False %} + {% if form.captcha %}{% bootstrap_field form.captcha show_label=False %}{% endif %} {% if signup_enabled %} Don't have an account? Sign Up diff --git a/tethys_portal/templates/tethys_portal/accounts/register.html b/tethys_portal/templates/tethys_portal/accounts/register.html index 917dd5bd4..167eae6c5 100644 --- a/tethys_portal/templates/tethys_portal/accounts/register.html +++ b/tethys_portal/templates/tethys_portal/accounts/register.html @@ -26,6 +26,7 @@ {% bootstrap_field form.email show_label=False %} {% bootstrap_field form.password1 show_label=False %} {% bootstrap_field form.password2 show_label=False %} + {% if form.captcha %}{% bootstrap_field form.captcha show_label=False %}{% endif %} Already have an account? Login