diff options
Diffstat (limited to 'tests/tasks')
| -rw-r--r-- | tests/tasks/test_json_serializer.py | 27 | ||||
| -rw-r--r-- | tests/tasks/test_sender.py | 12 | ||||
| -rw-r--r-- | tests/tasks/test_source_parser.py | 41 | ||||
| -rw-r--r-- | tests/tasks/test_verification.py | 40 |
4 files changed, 120 insertions, 0 deletions
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( + '<div><a href="https://bad.example/post">bad</a>' + '<a href="https://good.example/post">good</a></div>' + ) + 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( + "<div>Hello<script>secret-script</script>" + "<style>secret-style</style><template>secret-template</template>world</div>" + ) + content = document.find("div") + assert content is not None + normalized = source_parser_module.stable_content_text(content) + assert "Hello" in normalized and "world" in normalized + assert "secret-" not in normalized diff --git a/tests/tasks/test_verification.py b/tests/tasks/test_verification.py index f36da44..d9c2f77 100644 --- a/tests/tasks/test_verification.py +++ b/tests/tasks/test_verification.py @@ -834,3 +834,43 @@ def test_stale_recovery_records_history_and_attempt_count( assert events[0].status == "received" assert events[0].recovery_attempts == 2 enqueue.assert_called_once_with(identifier) + + +def test_verification_claim_race_rolls_back_without_status_event( + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + from webmentions_ssg.models.received import ReceivedWebmentionStatusEvent + + identifier = create_webmention(app) + monkeypatch.setattr(verification, "rows_affected", lambda changed: 0) + + with app.app_context(): + assert verification.claim_for_verification(identifier) is None + row = db.session.get(ReceivedWebmention, identifier) + assert row is not None + assert row.status == "received" + assert db.session.scalar( + sa.select(sa.func.count(ReceivedWebmentionStatusEvent.id)) + ) == 0 + + +def test_stale_verification_claim_race_does_not_enqueue( + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + from webmentions_ssg.models.received import ReceivedWebmentionStatusEvent + + identifier = create_webmention(app, status="verifying") + set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) + enqueue = Mock() + monkeypatch.setattr(verification, "verify_webmention", enqueue) + monkeypatch.setattr(verification, "rows_affected", lambda changed: 0) + + with app.app_context(): + assert verification.recover_stale_verifications() == 0 + row = db.session.get(ReceivedWebmention, identifier) + assert row is not None + assert row.status == "verifying" + assert db.session.scalar( + sa.select(sa.func.count(ReceivedWebmentionStatusEvent.id)) + ) == 0 + enqueue.assert_not_called() |
