Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2064: Check catcha url exists in signup form #2470

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions adhocracy-plus/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,7 @@
SITE_ID = 1 # overwrite this in local.py if needed

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

# Add a Captcheck captcha URL in the production server's local.py to use it
# Captcha software we use: https://source.netsyms.com/Netsyms/Captcheck
CAPTCHA_URL = ""
4 changes: 3 additions & 1 deletion adhocracy-plus/templates/account/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ <h1>{% translate "Register" %}</h1>
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}"/>
{% endif %}

{% include 'a4_candy_contrib/includes/form_field.html' with field=form.captcha tabindex=0 %}
{% if form.captcha %}
{% include 'a4_candy_contrib/includes/form_field.html' with field=form.captcha tabindex=0 %}
{% endif %}

<p>
{% blocktranslate with data_protection_url=settings.a4_candy_cms_settings.ImportantPages.data_protection_policy.url platformname=settings.a4_candy_cms_settings.OrganisationSettings.platform_name %}
Expand Down
16 changes: 9 additions & 7 deletions apps/cms/contacts/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib import messages
from django.db import models
from django.shortcuts import redirect
Expand Down Expand Up @@ -26,14 +27,15 @@ class FormField(AbstractFormField):
class WagtailCaptchaFormBuilder(FormBuilder):
@property
def formfields(self):
# Add captcha to formfields property
fields = super().formfields
fields["captcha"] = CaptcheckCaptchaField(
label=_("I am not a robot"),
help_text=_(
"If you are having difficulty please contact" "us, details adjacent"
),
)
# Add captcha to formfields property if the URL exists in settings
if hasattr(settings, "CAPTCHA_URL") and settings.CAPTCHA_URL:
fields["captcha"] = CaptcheckCaptchaField(
label=_("I am not a robot"),
help_text=_(
"If you are having difficulty please contact" "us, details adjacent"
),
)

return fields

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div class="widget widget--{{ field|widget_type }}">
{% with describedby="hint_"|add:field.id_for_label %}
{% if field.errors %}
{% render_field field aria-invalid="true" aria-describedby=describedby %}
{% render_field field aria-invalid="true" aria-describedby=describedby %}
{% else %}
{% render_field field aria-describedby=describedby %}
{% endif %}
Expand Down
9 changes: 6 additions & 3 deletions apps/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ def __init__(self, *args, **kwargs):
self.fields["email"].widget.attrs["autocomplete"] = "username"
self.fields["password1"].widget.attrs["autocomplete"] = "new-password"
self.fields["password2"].widget.attrs["autocomplete"] = "new-password"
self.fields["captcha"].help_text = helpers.add_email_link_to_helptext(
self.fields["captcha"].help_text, CAPTCHA_HELP
)
if not (hasattr(settings, "CAPTCHA_URL") and settings.CAPTCHA_URL):
del self.fields["captcha"]
else:
self.fields["captcha"].help_text = helpers.add_email_link_to_helptext(
self.fields["captcha"].help_text, CAPTCHA_HELP
)

def save(self, request):
user = super().save(request)
Expand Down
3 changes: 3 additions & 0 deletions changelog/2064.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- captcha becomes optional depending on project settings (#2449)
42 changes: 42 additions & 0 deletions tests/users/test_signup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
from django.conf import settings
from django.test import override_settings
from django.urls import reverse

from apps.users.models import User
Expand Down Expand Up @@ -56,3 +58,43 @@ def test_signup_user_unchecked_terms_of_use(client):
assert User.objects.count() == 0
assert not resp.context["form"].is_valid()
assert list(resp.context["form"].errors.keys()) == ["terms_of_use"]


@override_settings()
@pytest.mark.django_db
def test_signup_user_without_captcha(client):
del settings.CAPTCHA_URL
resp = client.post(
reverse("account_signup"),
{
"username": "dauser",
"email": "mail@example.com",
"get_newsletters": "on",
"password1": "password",
"password2": "password",
"terms_of_use": "on",
},
)
assert resp.status_code == 302
user = User.objects.get()
assert user.get_newsletters


@override_settings()
@pytest.mark.django_db
def test_signup_user_when_not_captcha(client):
settings.CAPTCHA_URL = ""
resp = client.post(
reverse("account_signup"),
{
"username": "dauser",
"email": "mail@example.com",
"get_newsletters": "on",
"password1": "password",
"password2": "password",
"terms_of_use": "on",
},
)
assert resp.status_code == 302
user = User.objects.get()
assert user.get_newsletters