From a285fdf29e59c3b957247ea9241943c7202e9a28 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 13 Nov 2026 21:20:40 +0100 Subject: refactor(core): consolidate repeated helper logic Centralize SQL row counts, hostname parsing, and content-type normalization so tasks, forms, and scanners share consistent implementations. Simplify periodic task registration and admin lookups with table-driven setup and Flask-SQLAlchemy's built-in 404 helper. --- tests/test_db_helpers.py | 28 ++++++++++++++++++++++++++++ tests/test_forms.py | 12 ++++++++++++ tests/test_net.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 tests/test_db_helpers.py (limited to 'tests') diff --git a/tests/test_db_helpers.py b/tests/test_db_helpers.py new file mode 100644 index 0000000..ac97f2b --- /dev/null +++ b/tests/test_db_helpers.py @@ -0,0 +1,28 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Regression tests for shared SQLAlchemy row-count handling.""" + +from sqlalchemy import create_engine, text + +from webmentions_ssg.db_helpers import rows_affected + + +def test_rows_affected_for_update_and_delete() -> None: + engine = create_engine("sqlite://") + try: + with engine.begin() as connection: + connection.execute(text("CREATE TABLE entries (id INTEGER PRIMARY KEY)")) + connection.execute(text("INSERT INTO entries (id) VALUES (1)")) + + result = connection.execute(text("UPDATE entries SET id = 2 WHERE id = 1")) + assert rows_affected(result) == 1 + + result = connection.execute(text("DELETE FROM entries WHERE id = 1")) + assert rows_affected(result) == 0 + + result = connection.execute(text("DELETE FROM entries WHERE id = 2")) + assert rows_affected(result) == 1 + finally: + engine.dispose() diff --git a/tests/test_forms.py b/tests/test_forms.py index a37a6d0..e636eae 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -178,6 +178,18 @@ def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: assert "URL must use a public address." in form.errors["source"] +def test_endpoint_rejects_private_ip_literal_with_trailing_dot(app: Flask) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = EndpointForm( + formdata=MultiDict( + {"source": "http://127.0.0.1./", "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( diff --git a/tests/test_net.py b/tests/test_net.py index 8e0744c..42b9261 100644 --- a/tests/test_net.py +++ b/tests/test_net.py @@ -21,6 +21,8 @@ from webmentions_ssg.net import ( PublicNetworkBackend, ResponseTooLargeError, UnsafeAddressError, + content_type_of, + hostname_of, is_http_url, is_public_address, public_addresses, @@ -333,3 +335,34 @@ def test_temporary_http_status(app: Flask, status: int, expected: bool) -> None: from webmentions_ssg.tasks import discovery assert discovery.is_temporary_status(status) is expected + + +@pytest.mark.parametrize( + ("url", "expected"), + [ + pytest.param("https://EXAMPLE.com./path", "example.com", id="trailing-dot"), + pytest.param("http://[2001:db8::1]/", "2001:db8::1", id="ipv6"), + pytest.param("https:///path", None, id="missing-host"), + pytest.param("http://[::1", None, id="malformed-ipv6"), + ], +) +def test_hostname_of(url: str, expected: str | None) -> None: + assert hostname_of(url) == expected + + +@pytest.mark.parametrize( + ("headers", "expected"), + [ + pytest.param({}, "", id="missing"), + pytest.param( + {"Content-Type": " Text/HTML ; charset=UTF-8"}, "text/html", id="params" + ), + pytest.param( + {"content-type": "APPLICATION/XHTML+XML"}, + "application/xhtml+xml", + id="case", + ), + ], +) +def test_content_type_of(headers: dict[str, str], expected: str) -> None: + assert content_type_of(httpx.Headers(headers)) == expected -- cgit v1.3.1