From eb11fb64e9cad6fbda2065362189653416d98577 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 19 Nov 2026 21:18:37 +0100 Subject: test: expand failure and migration coverage Add regression coverage for malformed input, lost task claims, moderation races, network failures, serializer validation, and production configuration guards. Exercise migration downgrade, backfill, and autogeneration paths plus additional receiver and sender edge cases. --- tests/tasks/test_json_serializer.py | 27 ++++++++++++++++++++++++ tests/tasks/test_sender.py | 12 +++++++++++ tests/tasks/test_source_parser.py | 41 +++++++++++++++++++++++++++++++++++++ tests/tasks/test_verification.py | 40 ++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) (limited to 'tests/tasks') diff --git a/tests/tasks/test_json_serializer.py b/tests/tasks/test_json_serializer.py index 6077ebd..32a862a 100644 --- a/tests/tasks/test_json_serializer.py +++ b/tests/tasks/test_json_serializer.py @@ -98,3 +98,30 @@ def test_compression_with_json(app: Flask) -> None: raw = huey.huey.serializer.serialize(payload) assert raw.startswith(b"\x1f\x8b") assert huey.huey.serializer.deserialize(raw) == payload + + +@pytest.mark.parametrize( + ("encoded", "expected_error", "message"), + [ + ( + b'{"type":"error","value":"not metadata"}', + TypeError, + "Invalid Huey JSON error metadata", + ), + ( + b'{"type":"dict","value":[["key",1],["key",2]]}', + ValueError, + "Duplicate Huey JSON dictionary key", + ), + ( + b'{"type":"dict","value":[["key",1],[42,2]]}', + ValueError, + "Invalid Huey JSON dictionary entry", + ), + ], +) +def test_rejects_invalid_huey_json_structure( + encoded: bytes, expected_error: type[Exception], message: str +) -> None: + with pytest.raises(expected_error, match=message): + TaggedJSONSerializer().deserialize(encoded) diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 5be2177..4954d04 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -739,3 +739,15 @@ def test_send_does_not_persist_unsafe_status_url( assert mention.status == SentWebmentionStatus.SENT assert mention.endpoint_response_status == HTTPStatus.CREATED assert mention.status_url == expected + + +def test_reserve_send_attempt_rejects_lost_claim( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + identifier = create_sent_webmention(app) + monkeypatch.setattr(sender_module, "rows_affected", lambda result: 0) + + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert sender_module.reserve_send_attempt(webmention, datetime.now(UTC)) is None diff --git a/tests/tasks/test_source_parser.py b/tests/tasks/test_source_parser.py index 38752b0..38a5dc1 100644 --- a/tests/tasks/test_source_parser.py +++ b/tests/tasks/test_source_parser.py @@ -1014,3 +1014,44 @@ def test_normalize_target_url_ignores_url_rejected_by_validator( ) is None ) + + +def test_outgoing_targets_skip_urls_with_unparseable_hostname( + source_parser_module: ModuleType, monkeypatch: MonkeyPatch +) -> None: + document = parse_html( + '
' + ) + content = document.find("div") + assert content is not None + original = source_parser_module.normalize_hostname + + def invalid_hostname(url: str, *, from_url: bool = False) -> str: + if "bad.example" in url: + raise ValueError("Malformed URL hostname") + return original(url, from_url=from_url) + + monkeypatch.setattr(source_parser_module, "normalize_hostname", invalid_hostname) + assert list( + source_parser_module.iter_outgoing_targets( + content, + {}, + base_url=SOURCE_URL, + source_url=SOURCE_URL, + ) + ) == ["https://good.example/post"] + + +def test_stable_content_text_excludes_hidden_markup( + source_parser_module: ModuleType, +) -> None: + document = parse_html( + "