From 761305d04f5910ba14cc04d1255a8f1afdbb87f3 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 9 Feb 2023 16:35:52 +0200 Subject: [PATCH] Show captcha on password reset form if enabled --- tcms/kiwi_auth/forms.py | 8 +++++++ tcms/kiwi_auth/tests/test_views.py | 21 +++++++++++++++++-- .../registration/password_reset_form.html | 4 ++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tcms/kiwi_auth/forms.py b/tcms/kiwi_auth/forms.py index 9483cf814f..c87160d463 100644 --- a/tcms/kiwi_auth/forms.py +++ b/tcms/kiwi_auth/forms.py @@ -94,6 +94,14 @@ class PasswordResetForm( kiwitcms-tenants is installed. """ + captcha = ( + fields.CaptchaField( + widget=CustomCaptchaTextInput(attrs={"class": "form-control"}) + ) + if settings.USE_CAPTCHA + else None + ) + def save( # pylint: disable=too-many-arguments self, domain_override=None, diff --git a/tcms/kiwi_auth/tests/test_views.py b/tcms/kiwi_auth/tests/test_views.py index 183f07ff72..8505ba786e 100644 --- a/tcms/kiwi_auth/tests/test_views.py +++ b/tcms/kiwi_auth/tests/test_views.py @@ -400,8 +400,25 @@ def test_send_mail_for_password_reset(self, mail_sent): user = User.objects.create_user("kiwi-tester", "tester@example.com", "password") user.is_active = True user.save() - data = {"email": "tester@example.com"} - response = self.client.post(self.password_reset_url, data, follow=True) + + try: + # https://github.com/mbi/django-simple-captcha/issues/84 + # pylint: disable=import-outside-toplevel + from captcha.conf import settings as captcha_settings + + captcha_settings.CAPTCHA_TEST_MODE = True + + response = self.client.post( + self.password_reset_url, + { + "email": "tester@example.com", + "captcha_0": "PASSED", + "captcha_1": "PASSED", + }, + follow=True, + ) + finally: + captcha_settings.CAPTCHA_TEST_MODE = False self.assertContains(response, _("Password reset email was sent")) diff --git a/tcms/templates/registration/password_reset_form.html b/tcms/templates/registration/password_reset_form.html index 6a64fc3247..f61073a111 100644 --- a/tcms/templates/registration/password_reset_form.html +++ b/tcms/templates/registration/password_reset_form.html @@ -18,6 +18,10 @@ + {% if 'captcha' in form.fields %} + {{ form.captcha.errors }} + {{ form.captcha }} + {% endif %}