Skip to content

Commit

Permalink
Show captcha on password reset form if enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Feb 9, 2023
1 parent 0ed213f commit 761305d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
8 changes: 8 additions & 0 deletions tcms/kiwi_auth/forms.py
Expand Up @@ -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,
Expand Down
21 changes: 19 additions & 2 deletions tcms/kiwi_auth/tests/test_views.py
Expand Up @@ -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"))

Expand Down
4 changes: 4 additions & 0 deletions tcms/templates/registration/password_reset_form.html
Expand Up @@ -18,6 +18,10 @@
<input type="text" class="form-control" id="inputEmail" name="email" placeholder="" tabindex="1">
</div>
</div>
{% if 'captcha' in form.fields %}
{{ form.captcha.errors }}
{{ form.captcha }}
{% endif %}
<div class="form-group">
<div class="col-xs-12 col-sm-offset-2 col-sm-10 col-md-offset-2 col-md-10 submit">
<button type="submit" class="btn btn-primary btn-lg" tabindex="4">{% trans "Password reset" %}</button>
Expand Down

0 comments on commit 761305d

Please sign in to comment.