From 4bd1d9e348918b2e55db47a1ac3489d035cf55e2 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 12 Nov 2026 19:57:08 +0100 Subject: 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. --- tests/forms/__init__.py | 3 - tests/forms/test_init.py | 205 ----------------------------------------- tests/forms/test_validators.py | 166 --------------------------------- 3 files changed, 374 deletions(-) delete mode 100644 tests/forms/__init__.py delete mode 100644 tests/forms/test_init.py delete mode 100644 tests/forms/test_validators.py (limited to 'tests/forms') diff --git a/tests/forms/__init__.py b/tests/forms/__init__.py deleted file mode 100644 index 25d78d5..0000000 --- a/tests/forms/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause diff --git a/tests/forms/test_init.py b/tests/forms/test_init.py deleted file mode 100644 index 42e6a2c..0000000 --- a/tests/forms/test_init.py +++ /dev/null @@ -1,205 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -import pytest -from flask import Flask -from werkzeug.datastructures import MultiDict - -from webmentions_ssg.forms import AdminActionForm, EndpointForm, LoginForm - -VALID_SOURCE = "https://source.example/post" -VALID_TARGET = "https://dennisfink.me/blog/example/" - - -@pytest.mark.parametrize( - ("form_data", "invalid_field", "expected_error"), - [ - pytest.param( - {"target": VALID_TARGET}, - "source", - "This field is required.", - id="source-required", - ), - pytest.param( - {"source": "not a URL", "target": VALID_TARGET}, - "source", - "Invalid URL.", - id="source-url", - ), - pytest.param( - {"source": "ftp://source.example/post", "target": VALID_TARGET}, - "source", - "source must begin with http or https", - id="source-scheme", - ), - pytest.param( - {"source": VALID_SOURCE}, - "target", - "This field is required.", - id="target-required", - ), - pytest.param( - {"source": VALID_SOURCE, "target": "not a URL"}, - "target", - "Invalid URL.", - id="target-url", - ), - pytest.param( - {"source": VALID_SOURCE, "target": "ftp://dennisfink.me/blog/example/"}, - "target", - "target must begin with http or https", - id="target-scheme", - ), - ], -) -def test_endpoint_form_rejects_invalid_field_syntax( - app: Flask, form_data: dict[str, str], invalid_field: str, expected_error: str -) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm(formdata=MultiDict(form_data), meta={"csrf": False}) - - assert not form.validate() - assert expected_error in form.errors[invalid_field] - - -def test_endpoint_form_accepts_valid_data(app: Flask) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( - formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET}), - meta={"csrf": False}, - ) - - assert form.validate() - assert form.errors == {} - - -@pytest.mark.parametrize( - ("form_data", "invalid_field"), - [ - pytest.param({"password": "secret"}, "username", id="username-required"), - pytest.param({"username": "admin"}, "password", id="password-required"), - ], -) -def test_login_form_requires_credentials( - app: Flask, form_data: dict[str, str], invalid_field: str -) -> None: - with app.test_request_context("/login", method="POST"): - form = LoginForm(formdata=MultiDict(form_data), meta={"csrf": False}) - - assert not form.validate() - assert "This field is required." in form.errors[invalid_field] - - -def test_login_form_accepts_credentials(app: Flask) -> None: - with app.test_request_context("/login", method="POST"): - form = LoginForm( - formdata=MultiDict({"username": "admin", "password": "secret"}), - meta={"csrf": False}, - ) - assert form.validate() - - -def test_admin_action_form_accepts_submission(app: Flask) -> None: - with app.test_request_context(method="POST"): - form = AdminActionForm(meta={"csrf": False}) - assert form.validate() - - -def test_endpoint_does_not_do_dns_in_web_request( - app: Flask, monkeypatch: pytest.MonkeyPatch -) -> None: - def unexpected_dns_lookup(*args: object, **kwargs: object) -> None: - raise AssertionError("No DNS lookup belongs in form validation") - - monkeypatch.setattr( - "webmentions_ssg.url_security.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"): - form = EndpointForm( - formdata=MultiDict({"source": source, "target": VALID_TARGET}), - meta={"csrf": False}, - ) - assert form.validate() is (source == VALID_SOURCE) - - -@pytest.mark.parametrize( - "source", ["http://127.0.0.1/", "http://[::1]/", "http://169.254.169.254/"] -) -def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( - formdata=MultiDict({"source": source, "target": VALID_TARGET}), - meta={"csrf": False}, - ) - assert not form.validate() - assert "URL must use a public address." in form.errors["source"] - - -def test_endpoint_handles_malformed_ipv6_without_crashing(app: Flask) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( - formdata=MultiDict({"source": VALID_SOURCE, "target": "http://[::1"}), - meta={"csrf": False}, - ) - assert not form.validate() - assert "Invalid URL." in form.errors["target"] - - -def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields( - app: Flask, -) -> None: - with app.test_request_context("/endpoint", method="POST"): - app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_SOURCE.encode("utf-8")) - form = EndpointForm( - formdata=MultiDict({"source": VALID_SOURCE + "x", "target": VALID_TARGET}), - meta={"csrf": False}, - ) - assert not form.validate() - assert "source" in form.errors - assert any("bytes" in message for message in form.errors["source"]) - - app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_TARGET.encode("utf-8")) - form = EndpointForm( - formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET + "x"}), - meta={"csrf": False}, - ) - assert not form.validate() - assert "target" in form.errors - assert any("bytes" in message for message in form.errors["target"]) - - -@pytest.mark.parametrize("field_name", ["source", "target"]) -@pytest.mark.parametrize( - "invalid_url", - [ - pytest.param("http://ex☃mple.test/", id="invalid-idn"), - pytest.param("https://example.com/a\nb", id="newline"), - pytest.param("https://example.com/a\rb", id="carriage-return"), - pytest.param("https://example.com/\x00b", id="nul"), - pytest.param("https://example.com/a b", id="ascii-space"), - pytest.param("https://example.com/a\u00a0b", id="unicode-whitespace"), - pytest.param("https://example.com/\x7fb", id="del"), - ], -) -def test_endpoint_rejects_invalid_httpx_urls_and_whitespace( - app: Flask, field_name: str, invalid_url: str -) -> None: - data = {"source": VALID_SOURCE, "target": VALID_TARGET} - data[field_name] = invalid_url - with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm(formdata=MultiDict(data), meta={"csrf": False}) - assert not form.validate() - assert "Invalid URL." in form.errors[field_name] - - -def test_endpoint_accepts_encoded_path_space(app: Flask) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( - formdata=MultiDict( - {"source": "https://source.example/a%20b", "target": VALID_TARGET} - ), - meta={"csrf": False}, - ) - assert form.validate() diff --git a/tests/forms/test_validators.py b/tests/forms/test_validators.py deleted file mode 100644 index ae4c8f2..0000000 --- a/tests/forms/test_validators.py +++ /dev/null @@ -1,166 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -from unittest.mock import Mock - -import pytest -from flask import Flask -from werkzeug.datastructures import MultiDict -from wtforms import Form, StringField, ValidationError - -from webmentions_ssg.forms.validators import ( - AllowedHostname, - NotEqualTo, - PublicIPLiteral, - PublicURL, - URLMaxBytes, -) -from webmentions_ssg.url_security import AddressResolutionError - - -class ComparisonForm(Form): - source = StringField("Source") - target = StringField("Target") - - -class URLForm(Form): - url = StringField("URL") - - -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) - - -def test_public_url_accepts_public_url(monkeypatch: pytest.MonkeyPatch) -> None: - is_public_url = Mock(return_value=True) - monkeypatch.setattr("webmentions_ssg.forms.validators.is_public_url", is_public_url) - form = URLForm(MultiDict({"url": "https://example.com/post"})) - PublicURL()(form, form.url) - is_public_url.assert_called_once_with("https://example.com/post") - - -@pytest.mark.parametrize( - "outcome", - [ - pytest.param(False, id="non-public-address"), - pytest.param( - AddressResolutionError("Could not resolve hostname"), id="resolution-error" - ), - pytest.param(ValueError("No hostname was specified"), id="missing-hostname"), - ], -) -def test_public_url_rejects_invalid_url( - monkeypatch: pytest.MonkeyPatch, outcome: bool | Exception -) -> None: - is_public_url = Mock() - - if isinstance(outcome, Exception): - is_public_url.side_effect = outcome - else: - is_public_url.return_value = outcome - - monkeypatch.setattr("webmentions_ssg.forms.validators.is_public_url", is_public_url) - form = URLForm(MultiDict({"url": "https://example.com/post"})) - - with pytest.raises(ValidationError, match="URL must resolve to a public address"): - PublicURL()(form, form.url) - - -def test_public_url_uses_custom_message(monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setattr( - "webmentions_ssg.forms.validators.is_public_url", Mock(return_value=False) - ) - form = URLForm(MultiDict({"url": "https://example.com/post"})) - - with pytest.raises(ValidationError, match="Public URL required"): - PublicURL("Public URL required")(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) -- cgit v1.3.1