Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

captcha: Avoid duplicate errors when the captcha is missing.

Alex Vandiveref516db81485
diff --git a/zerver/lib/captcha.py b/zerver/lib/captcha.py
index 4af5617bb71..f3c19841e7e 100644
--- a/zerver/lib/captcha.py
+++ b/zerver/lib/captcha.py
@@ -11,6 +11,7 @@ from django.http import HttpRequest
from django.utils.html import format_html
from django.utils.safestring import SafeString
from django.utils.translation import gettext as _
+from django.utils.translation import gettext_lazy
from typing_extensions import override
@@ -46,7 +47,10 @@ class AltchaWidget(forms.TextInput):
class CaptchaFormMixin(forms.Form):
- captcha = forms.CharField(required=True, widget=AltchaWidget)
+ captcha = forms.CharField(
+ widget=AltchaWidget,
+ error_messages={"required": gettext_lazy("Validation failed, please try again.")},
+ )
def __init__(self, *args: Any, request: HttpRequest, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
@@ -54,12 +58,6 @@ class CaptchaFormMixin(forms.Form):
if not settings.USING_CAPTCHA or not settings.ALTCHA_HMAC_KEY:
del self.fields["captcha"]
- @override
- def clean(self) -> None:
- super().clean()
- if "captcha" in self.fields and not self.data.get("captcha"):
- self.add_error("captcha", _("Validation failed, please try again."))
-
def clean_captcha(self) -> str:
payload = self.data.get("captcha", "")
validate_captcha_payload(self.request, payload)
diff --git a/zerver/tests/test_realm_creation.py b/zerver/tests/test_realm_creation.py
index 35682108f7c..bfc490eb652 100644
--- a/zerver/tests/test_realm_creation.py
+++ b/zerver/tests/test_realm_creation.py
@@ -146,9 +146,9 @@ class DemoCreationTest(ZulipTestCase):
result = self.client_get("/new/demo/")
self.assert_not_in_success_response(["Validation failed"], result)
- # Without the CAPTCHA value, we get an error
+ # Without the CAPTCHA value, we get a single error
result = self.submit_demo_creation_form()
- self.assert_in_success_response(["Validation failed, please try again."], result)
+ self.assertEqual(result.content.decode().count("Validation failed, please try again."), 1)
# With an invalid value, we also get an error
with self.assertLogs(level="WARNING") as logs:
@@ -1149,11 +1149,11 @@ class RealmCreationTest(ZulipTestCase):
result = self.client_get("/new/")
self.assert_not_in_success_response(["Validation failed"], result)
- # Without the CAPTCHA value, we get an error
+ # Without the CAPTCHA value, we get a single error
result = self.submit_realm_creation_form(
email, realm_subdomain=string_id, realm_name=realm_name
)
- self.assert_in_success_response(["Validation failed, please try again."], result)
+ self.assertEqual(result.content.decode().count("Validation failed, please try again."), 1)
# With an invalid value, we also get an error
with self.assertLogs(level="WARNING") as logs: