aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/test_forms.py34
-rw-r--r--webmentions_ssg/forms.py51
2 files changed, 13 insertions, 72 deletions
diff --git a/tests/test_forms.py b/tests/test_forms.py
index 03789da..87b02cd 100644
--- a/tests/test_forms.py
+++ b/tests/test_forms.py
@@ -12,7 +12,6 @@ from webmentions_ssg.forms import (
AllowedHostname,
EndpointForm,
LoginForm,
- NotEqualTo,
PublicIPLiteral,
URLMaxBytes,
)
@@ -21,11 +20,6 @@ VALID_SOURCE = "https://source.example/post"
VALID_TARGET = "https://dennisfink.me/blog/example/"
-class ComparisonForm(Form):
- source = StringField("Source")
- target = StringField("Target")
-
-
class URLForm(Form):
url = StringField("URL")
@@ -223,27 +217,15 @@ def test_endpoint_accepts_encoded_path_space(app: Flask) -> None:
assert form.validate()
-def test_not_equal_to_accepts_different_values() -> None:
- form = ComparisonForm(MultiDict({"source": "source", "target": "target"}))
- NotEqualTo("target")(form, form.source)
-
-
-def test_not_equal_to_rejects_equal_values() -> None:
- form = ComparisonForm(MultiDict({"source": "same", "target": "same"}))
- with pytest.raises(ValidationError, match="Field must not be equal to target"):
- NotEqualTo("target")(form, form.source)
-
-
-def test_not_equal_to_uses_custom_message() -> None:
- form = ComparisonForm(MultiDict({"source": "same", "target": "same"}))
- with pytest.raises(ValidationError, match="Must differ from Target"):
- NotEqualTo("target", "Must differ from %(other_label)s")(form, form.source)
-
+def test_endpoint_form_rejects_matching_source_and_target(app: Flask) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ form = EndpointForm(
+ formdata=MultiDict({"source": VALID_TARGET, "target": VALID_TARGET}),
+ meta={"csrf": False},
+ )
-def test_not_equal_to_rejects_unknown_field() -> None:
- form = ComparisonForm(MultiDict({"source": "source", "target": "target"}))
- with pytest.raises(ValidationError, match="Invalid field name 'missing'"):
- NotEqualTo("missing")(form, form.source)
+ assert not form.validate()
+ assert "Field must not be equal to target." in form.errors["source"]
def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None:
diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py
index d557907..af24423 100644
--- a/webmentions_ssg/forms.py
+++ b/webmentions_ssg/forms.py
@@ -72,51 +72,6 @@ class HTTPXURL:
raise StopValidation(field.gettext("Invalid URL."))
-class NotEqualTo:
- """Validate that a field does not equal another field.
-
- :param fieldname: Name of the field to compare against.
- :param message: Optional validation error message.
- """
-
- def __init__(self, fieldname: str, message: str | None = None) -> None:
- """Initialize the validator.
-
- :param fieldname: Name of the field to compare against.
- :param message: Optional validation error message.
- """
- self.fieldname = fieldname
- self.message = message
-
- def __call__(self, form: BaseForm, field: Field) -> None:
- """Validate that two fields do not contain equal values.
-
- :param form: Form containing the fields to compare.
- :param field: Field being validated.
- :raises ValidationError: If the comparison field does not exist or both
- fields contain equal values.
- """
- try:
- other = form[self.fieldname]
- except KeyError as exc:
- raise ValidationError(
- field.gettext("Invalid field name '%s'.") % self.fieldname
- ) from exc
-
- if field.data != other.data:
- return
-
- d = {
- "other_label": (hasattr(other, "label") and other.label.text)
- or self.fieldname,
- "other_name": self.fieldname,
- }
- message = self.message or field.gettext(
- "Field must not be equal to %(other_name)s."
- )
- raise ValidationError(message % d)
-
-
class AllowedHostname:
"""Validate that a URL uses an allowed hostname.
@@ -194,7 +149,6 @@ class EndpointForm(FlaskForm):
flags=re.IGNORECASE,
message="source must begin with http or https",
),
- NotEqualTo("target"),
PublicIPLiteral(),
],
)
@@ -214,6 +168,11 @@ class EndpointForm(FlaskForm):
],
)
+ def validate_source(self, field: Field) -> None:
+ """Reject a source URL that equals the target URL."""
+ if field.data == self.target.data:
+ raise ValidationError(field.gettext("Field must not be equal to target."))
+
class AdminActionForm(FlaskForm):
"""Form for confirming an administrative action."""