From 37bc3853dded34e3d1323874927979ce510e4910 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 30 Oct 2026 11:51:34 +0100 Subject: test: align test suite with package structure --- tests/tasks/__init__.py | 3 +++ tests/tasks/test_consumer.py | 45 +++++++++++++++++++++++++++++++ tests/tasks/test_init.py | 12 +++++++++ tests/tasks/test_sender.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 tests/tasks/__init__.py create mode 100644 tests/tasks/test_consumer.py create mode 100644 tests/tasks/test_init.py (limited to 'tests/tasks') diff --git a/tests/tasks/__init__.py b/tests/tasks/__init__.py new file mode 100644 index 0000000..25d78d5 --- /dev/null +++ b/tests/tasks/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause diff --git a/tests/tasks/test_consumer.py b/tests/tasks/test_consumer.py new file mode 100644 index 0000000..6b79e37 --- /dev/null +++ b/tests/tasks/test_consumer.py @@ -0,0 +1,45 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Test scheduled-task registration during consumer creation.""" + +import importlib +from unittest.mock import Mock + +import pytest +from flask import Flask + +from webmentions_ssg import HUEY + + +def test_consumer_registers_scanner_and_digest( + app: Flask, monkeypatch: pytest.MonkeyPatch +) -> None: + import webmentions_ssg + + app.config["SCANNER_SCHEDULE"] = "*/15 * * * *" + app.config["NOTIFICATION_SCHEDULE"] = "0 9 * * *" + + registered_tasks: list[object] = [] + + def register(_schedule: object): + def decorator(task: object) -> object: + registered_tasks.append(task) + return task + + return decorator + + periodic_task = Mock(side_effect=register) + monkeypatch.setattr(webmentions_ssg, "create_app", Mock(return_value=app)) + monkeypatch.setattr(HUEY, "periodic_task", periodic_task) + + consumer = importlib.import_module("webmentions_ssg.tasks.consumer") + from webmentions_ssg.tasks import notifications, scanner + + assert consumer.huey is HUEY.huey + assert periodic_task.call_count == 2 + assert registered_tasks == [ + scanner.scan_sources, + notifications.send_webmention_digest, + ] diff --git a/tests/tasks/test_init.py b/tests/tasks/test_init.py new file mode 100644 index 0000000..1a51fb8 --- /dev/null +++ b/tests/tasks/test_init.py @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Test the public background-task package API.""" + +from webmentions_ssg.tasks import Huey +from webmentions_ssg.tasks.extension import Huey as ExtensionHuey + + +def test_tasks_package_exports_huey_extension() -> None: + assert Huey is ExtensionHuey diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 6a9afc1..ecfc27a 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -1150,3 +1150,66 @@ def test_post_webmention( assert form["source"] == SOURCE_URL assert form["target"] == TARGET_URL + + +@pytest.mark.parametrize( + ("code", "location", "expected"), + [ + (201, "/webmention/status/123", "https://example.com/webmention/status/123"), + (201, "https://status.example.org/123", "https://status.example.org/123"), + (201, "status/123", "https://example.com/status/123"), + (201, "javascript:alert(1)", None), + (201, "data:text/html,evil", None), + (201, "ftp://example.com/status", None), + (201, "http://[invalid", None), + (202, "https://example.com/status", None), + ], +) +def test_post_webmention_validates_location( + sender_module: ModuleType, code: int, location: str, expected: str | None +) -> None: + def handler(_request: httpx.Request) -> httpx.Response: + return httpx.Response(code, headers={"Location": location}) + + with httpx.Client(transport=httpx.MockTransport(handler)) as client: + result = sender_module.post_webmention( + client, endpoint=ENDPOINT_URL, source=SOURCE_URL, target=TARGET_URL + ) + + assert result == (code, expected) + + +@pytest.mark.parametrize( + ("status_url", "expected"), + [ + ("javascript:alert(1)", None), + ("data:text/html,evil", None), + ("ftp://example.com/status", None), + ("https://example.com/status", "https://example.com/status"), + ], +) +def test_send_does_not_persist_unsafe_status_url( + app: Flask, + sender_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + status_url: str, + expected: str | None, +) -> None: + identifier = create_sent_webmention(app) + client_factory = MagicMock() + monkeypatch.setattr(sender_module.httpx, "Client", client_factory) + monkeypatch.setattr( + sender_module, "discover_webmention_endpoint", Mock(return_value=ENDPOINT_URL) + ) + monkeypatch.setattr( + sender_module, "post_webmention", Mock(return_value=(201, status_url)) + ) + + sender_module.send_webmention.call_local(identifier) + + with app.app_context(): + mention = db.session.get(SentWebmention, identifier) + assert mention is not None + assert mention.status == SentWebmentionStatus.SENT + assert mention.response_status == 201 + assert mention.status_url == expected -- cgit v1.3.1