aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tasks')
-rw-r--r--tests/tasks/test_json_serializer.py99
-rw-r--r--tests/tasks/test_receiver.py10
2 files changed, 105 insertions, 4 deletions
diff --git a/tests/tasks/test_json_serializer.py b/tests/tasks/test_json_serializer.py
new file mode 100644
index 0000000..108e8a6
--- /dev/null
+++ b/tests/tasks/test_json_serializer.py
@@ -0,0 +1,99 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+"""Ensure broker-controlled data is never deserialized using pickle."""
+
+import pickle
+import uuid
+from datetime import UTC, datetime, timedelta
+
+import pytest
+from flask import Flask
+from huey.utils import Error, SKIPPED
+
+from webmentions_ssg.tasks.extension import Huey
+from webmentions_ssg.tasks.json_serializer import JSONSerializer
+
+
+def test_json_is_default_even_for_production_style_broker(app: Flask) -> None:
+ app.config["HUEY"] = {"immediate": False}
+ huey = Huey(app)
+
+ assert isinstance(huey.huey.serializer, JSONSerializer)
+ assert huey.huey.serializer.serialize("hello") == b'"hello"'
+
+
+def test_uuid_and_retry_message_survive_enqueue_dequeue(app: Flask) -> None:
+ app.config["HUEY"] = {"immediate": False}
+ huey = Huey(app)
+ identifier = uuid.uuid4()
+
+ @huey.task(retries=2, retry_delay=50)
+ def check(webmention_uuid: uuid.UUID) -> str:
+ return str(webmention_uuid)
+
+ task = check.s(identifier)
+ task.eta = datetime.now(UTC) + timedelta(minutes=5)
+ raw = huey.huey.serialize_task(task)
+ assert raw.startswith(b'{"type":"message"')
+ assert b"pickle" not in raw
+
+ restored = huey.huey.deserialize_task(raw)
+ assert restored.args == (identifier,)
+ assert restored.eta == task.eta
+ assert restored.retries == 2
+ assert restored.retry_delay == 50
+
+ huey.huey.enqueue(task)
+ from_queue = huey.huey.dequeue()
+ assert from_queue.args == (identifier,)
+
+
+def test_result_and_error_data_roundtrip() -> None:
+ serializer = JSONSerializer()
+ identifier = uuid.uuid4()
+ payload = {
+ "ids": [identifier],
+ "nested": ("str", None, 3),
+ "error": Error({"error": "failed", "retries": 0}),
+ "skipped": SKIPPED,
+ "tag_collision": {"type": "uuid", "value": "not a UUID"},
+ }
+ assert serializer.deserialize(serializer.serialize(payload)) == payload
+
+
+def test_rejects_pickle_payload() -> None:
+ serializer = JSONSerializer()
+ with pytest.raises((ValueError, UnicodeDecodeError)):
+ serializer.deserialize(pickle.dumps({"untrusted": True}))
+
+
+def test_rejects_unknown_and_malformed_types() -> None:
+ serializer = JSONSerializer()
+ with pytest.raises(ValueError, match="Unsupported Huey JSON type"):
+ serializer.deserialize(b'{"type":"arbitrary.class","value":{}}')
+ with pytest.raises(ValueError, match="Invalid Huey JSON data"):
+ serializer.deserialize(b'{"type":"uuid","value":"x","extra":1}')
+ with pytest.raises(ValueError, match="Invalid Huey JSON message field count"):
+ serializer.deserialize(b'{"type":"message","value":[]}')
+ with pytest.raises(ValueError, match="Invalid non-finite JSON value"):
+ serializer.deserialize(b"NaN")
+ with pytest.raises(TypeError, match="Unsupported Huey JSON value"):
+ serializer.serialize(object())
+
+
+def test_refuses_unsafe_serializer_configuration(app: Flask) -> None:
+ app.config["HUEY"] = {"serializer": None}
+ with pytest.raises(RuntimeError, match="JSON is required"):
+ Huey(app)
+
+
+def test_compression_with_json(app: Flask) -> None:
+ app.config["HUEY"] = {"compression": True, "immediate": False}
+ huey = Huey(app)
+ payload = {"id": uuid.uuid4()}
+
+ raw = huey.huey.serializer.serialize(payload)
+ assert raw.startswith(b"\x1f\x8b")
+ assert huey.huey.serializer.deserialize(raw) == payload
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py
index 102410d..c795115 100644
--- a/tests/tasks/test_receiver.py
+++ b/tests/tasks/test_receiver.py
@@ -688,8 +688,9 @@ def test_fetch_source_rejects_unsafe_destination(
client = httpx_client.return_value.__enter__.return_value
client.stream.side_effect = UnsafeAddressError("Local address")
- with app.app_context(), pytest.raises(
- receiver.VerificationError, match="non-public address"
+ with (
+ app.app_context(),
+ pytest.raises(receiver.VerificationError, match="non-public address"),
):
receiver.fetch_source(SOURCE_URL)
@@ -700,8 +701,9 @@ def test_fetch_source_treats_dns_failure_as_temporary(
) -> None:
client = httpx_client.return_value.__enter__.return_value
client.stream.side_effect = AddressResolutionError("DNS timeout")
- with app.app_context(), pytest.raises(
- receiver.TemporaryFetchError, match="could not be resolved"
+ with (
+ app.app_context(),
+ pytest.raises(receiver.TemporaryFetchError, match="could not be resolved"),
):
receiver.fetch_source(SOURCE_URL)