aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks
diff options
context:
space:
mode:
authorDennis Fink2026-10-30 11:51:34 +0100
committerDennis Fink2026-10-30 11:51:34 +0100
commit37bc3853dded34e3d1323874927979ce510e4910 (patch)
tree7d6d2181d9bb05f4d4a8d00152c172cc92e7ab63 /tests/tasks
parent4485aeb573ceb37237ea1fbc80665ed6c4136116 (diff)
downloadwebmentions-ssg-37bc3853dded34e3d1323874927979ce510e4910.tar.gz
webmentions-ssg-37bc3853dded34e3d1323874927979ce510e4910.zip
test: align test suite with package structure
Diffstat (limited to 'tests/tasks')
-rw-r--r--tests/tasks/__init__.py3
-rw-r--r--tests/tasks/test_consumer.py45
-rw-r--r--tests/tasks/test_init.py12
-rw-r--r--tests/tasks/test_sender.py63
4 files changed, 123 insertions, 0 deletions
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 <me+coding@dennisfink.me>
+#
+# 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 <me+coding@dennisfink.me>
+#
+# 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 <me+coding@dennisfink.me>
+#
+# 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