aboutsummaryrefslogtreecommitdiff
path: root/tests/test_forms.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 19:57:08 +0100
committerDennis Fink2026-11-12 19:57:08 +0100
commit4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch)
treef7ed41a374a8a4136792039a262dc62aa8cf8c59 /tests/test_forms.py
parent7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff)
downloadwebmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.tar.gz
webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.zip
refactor(core): split modules by responsibility
Break up monolithic views, task helpers, networking, CLI, and application extension setup so each module owns a focused concern. Reorganize the test suite around the new boundaries while preserving existing Webmention behavior and coverage.
Diffstat (limited to '')
-rw-r--r--tests/test_forms.py (renamed from tests/forms/test_init.py)115
1 files changed, 113 insertions, 2 deletions
diff --git a/tests/forms/test_init.py b/tests/test_forms.py
index 42e6a2c..03789da 100644
--- a/tests/forms/test_init.py
+++ b/tests/test_forms.py
@@ -5,13 +5,31 @@
import pytest
from flask import Flask
from werkzeug.datastructures import MultiDict
+from wtforms import Form, StringField, ValidationError
-from webmentions_ssg.forms import AdminActionForm, EndpointForm, LoginForm
+from webmentions_ssg.forms import (
+ AdminActionForm,
+ AllowedHostname,
+ EndpointForm,
+ LoginForm,
+ NotEqualTo,
+ PublicIPLiteral,
+ URLMaxBytes,
+)
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")
+
+
@pytest.mark.parametrize(
("form_data", "invalid_field", "expected_error"),
[
@@ -113,7 +131,7 @@ def test_endpoint_does_not_do_dns_in_web_request(
raise AssertionError("No DNS lookup belongs in form validation")
monkeypatch.setattr(
- "webmentions_ssg.url_security.dns.resolver.resolve_name", unexpected_dns_lookup
+ "webmentions_ssg.net.dns.resolver.resolve_name", unexpected_dns_lookup
)
with app.test_request_context("/endpoint", method="POST"):
for source in (VALID_SOURCE, "ftp://source.example/post", "not a URL"):
@@ -203,3 +221,96 @@ def test_endpoint_accepts_encoded_path_space(app: Flask) -> None:
meta={"csrf": False},
)
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_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)
+
+
+def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None:
+ app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"]
+ form = URLForm(MultiDict({"url": "https://dennisfink.me/blog/example/"}))
+ with app.app_context():
+ AllowedHostname()(form, form.url)
+
+
+def test_allowed_hostname_rejects_unconfigured_hostname(app: Flask) -> None:
+ app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"]
+ form = URLForm(MultiDict({"url": "https://example.com/post"}))
+ with app.app_context(), pytest.raises(ValidationError, match="Invalid input"):
+ AllowedHostname()(form, form.url)
+
+
+def test_allowed_hostname_uses_custom_message(app: Flask) -> None:
+ app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"]
+ form = URLForm(MultiDict({"url": "https://example.com/post"}))
+ with (
+ app.app_context(),
+ pytest.raises(ValidationError, match="Hostname is not allowed"),
+ ):
+ AllowedHostname("Hostname is not allowed")(form, form.url)
+
+
+@pytest.mark.parametrize(
+ "address",
+ ["64:ff9b::a9fe:a9fe", "::10.0.0.1", "2002:a9fe:a9fe::1", "::ffff:169.254.169.254"],
+)
+def test_public_ip_literal_rejects_ipv6_transition_addresses(address: str) -> None:
+ form = URLForm(MultiDict({"url": f"https://[{address}]/"}))
+ with pytest.raises(ValidationError, match="URL must use a public address"):
+ PublicIPLiteral()(form, form.url)
+
+
+def test_public_ip_literal_accepts_public_ipv6_address() -> None:
+ form = URLForm(MultiDict({"url": "https://[2606:2800:220:1:248:1893:25c8:1946]/"}))
+ PublicIPLiteral()(form, form.url)
+
+
+def test_allowed_hostname_handles_invalid_ipv6(app: Flask) -> None:
+ form = URLForm(MultiDict({"url": "http://[::1"}))
+ with app.app_context(), pytest.raises(ValidationError, match="Invalid input"):
+ AllowedHostname()(form, form.url)
+
+
+def test_url_max_bytes_respects_utf8_byte_length_and_current_config(app: Flask) -> None:
+ validator = URLMaxBytes()
+ form = URLForm(MultiDict({"url": "é" * 3}))
+ with app.app_context():
+ app.config["MAX_WEBMENTION_URL_BYTES"] = 6
+ validator(form, form.url)
+ app.config["MAX_WEBMENTION_URL_BYTES"] = 5
+ with pytest.raises(ValidationError, match="5 bytes"):
+ validator(form, form.url)
+
+
+def test_url_max_bytes_custom_message(app: Flask) -> None:
+ form = URLForm(MultiDict({"url": "too-long"}))
+ with app.app_context():
+ app.config["MAX_WEBMENTION_URL_BYTES"] = 2
+ with pytest.raises(ValidationError, match="URL too long"):
+ URLMaxBytes(message="URL too long")(form, form.url)
+
+
+def test_url_max_bytes_rejects_malformed_unicode(app: Flask) -> None:
+ form = URLForm(MultiDict({"url": "\ud800"}))
+ with app.app_context(), pytest.raises(ValidationError, match="valid UTF-8"):
+ URLMaxBytes()(form, form.url)