aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/test_receiver.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tasks/test_receiver.py')
-rw-r--r--tests/tasks/test_receiver.py748
1 files changed, 0 insertions, 748 deletions
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py
deleted file mode 100644
index 7241247..0000000
--- a/tests/tasks/test_receiver.py
+++ /dev/null
@@ -1,748 +0,0 @@
-# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
-#
-# SPDX-License-Identifier: BSD-3-Clause
-
-import logging
-import uuid
-from datetime import UTC, datetime, timedelta
-from http import HTTPStatus
-from types import ModuleType
-from unittest.mock import Mock, patch
-
-import httpx
-import pytest
-import sqlalchemy as sa
-from flask import Flask
-
-from webmentions_ssg import db
-from webmentions_ssg.models import ReceivedWebmention
-from webmentions_ssg.net import AddressResolutionError
-
-SOURCE_URL = "https://source.example/article"
-TARGET_URL = "https://dennisfink.me/blog/example/"
-
-
-@pytest.fixture
-def receiver(app: Flask) -> ModuleType:
- from webmentions_ssg.tasks import receiver
-
- return receiver
-
-
-def create_webmention(
- app: Flask,
- *,
- source: str = SOURCE_URL,
- target: str = TARGET_URL,
- status: str = "received",
- failure_reason: str | None = None,
- recovery_attempts: int = 0,
-) -> uuid.UUID:
- identifier = uuid.uuid7()
-
- with app.app_context():
- webmention = ReceivedWebmention(
- uuid=identifier,
- source=source,
- target=target,
- status=status,
- failure_reason=failure_reason,
- recovery_attempts=recovery_attempts,
- )
-
- db.session.add(webmention)
- db.session.commit()
-
- return identifier
-
-
-def get_webmention_state(app: Flask, identifier: uuid.UUID) -> tuple[str, str | None]:
- with app.app_context():
- webmention = db.session.get(ReceivedWebmention, identifier)
- assert webmention is not None
- return (webmention.status, webmention.failure_reason)
-
-
-def set_stream_response(httpx_client: Mock, response: httpx.Response) -> Mock:
- client = httpx_client.return_value.__enter__.return_value
- client.stream.return_value.__enter__.return_value = response
- return client
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_document_returns_response_and_body(
- httpx_client: Mock, app: Flask, receiver: ModuleType
-) -> None:
- response = httpx.Response(
- HTTPStatus.OK,
- headers={"Content-Type": "text/html"},
- content=b"<p>Document</p>",
- request=httpx.Request("GET", SOURCE_URL),
- )
- client = set_stream_response(httpx_client, response)
-
- with app.app_context():
- fetched_response, body = receiver.fetch_source_document(SOURCE_URL)
-
- assert fetched_response is response
- assert body == b"<p>Document</p>"
- client.stream.assert_called_once_with("GET", SOURCE_URL)
-
- options = httpx_client.call_args.kwargs
-
- assert options["headers"] == {
- "Accept": "text/html, application/xhtml+xml;q=0.9, text/plain;q=0.8",
- "User-Agent": f"{receiver.APP_NAME}/{receiver.VERSION} ReceivedWebmention",
- }
- assert options["follow_redirects"] is True
- assert options["max_redirects"] == app.config.get("MAX_REDIRECTS", 20)
- assert options["trust_env"] is False
- assert options["transport"].__class__.__name__ == "PublicHTTPTransport"
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-@pytest.mark.parametrize(
- ("status_code", "exception_name"),
- [
- (HTTPStatus.BAD_REQUEST, "VerificationError"),
- (HTTPStatus.NOT_FOUND, "VerificationError"),
- (HTTPStatus.GONE, "SourceGoneError"),
- (HTTPStatus.REQUEST_TIMEOUT, "RetryableFetchError"),
- (HTTPStatus.TOO_EARLY, "RetryableFetchError"),
- (HTTPStatus.TOO_MANY_REQUESTS, "RetryableFetchError"),
- (HTTPStatus.INTERNAL_SERVER_ERROR, "RetryableFetchError"),
- (HTTPStatus.SERVICE_UNAVAILABLE, "RetryableFetchError"),
- ],
-)
-def test_fetch_source_document_maps_http_status_to_exception(
- httpx_client: Mock,
- app: Flask,
- receiver: ModuleType,
- status_code: int,
- exception_name: str,
-) -> None:
- set_stream_response(
- httpx_client,
- httpx.Response(status_code, request=httpx.Request("GET", SOURCE_URL)),
- )
-
- exception_type = getattr(receiver, exception_name)
-
- with app.app_context(), pytest.raises(exception_type, match=f"HTTP {status_code}"):
- receiver.fetch_source_document(SOURCE_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_document_propagates_network_error(
- httpx_client: Mock, app: Flask, receiver: ModuleType
-) -> None:
- client = httpx_client.return_value.__enter__.return_value
- client.stream.side_effect = httpx.ConnectError(
- "Connection refused", request=httpx.Request("GET", SOURCE_URL)
- )
-
- with (
- app.app_context(),
- pytest.raises(httpx.ConnectError, match="Connection refused"),
- ):
- receiver.fetch_source_document(SOURCE_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_document_rejects_declared_oversized_body(
- httpx_client: Mock, app: Flask, receiver: ModuleType
-) -> None:
- app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10
-
- set_stream_response(
- httpx_client,
- httpx.Response(
- HTTPStatus.OK,
- headers={"Content-Type": "text/html", "Content-Length": "11"},
- content=b"x" * 11,
- request=httpx.Request("GET", SOURCE_URL),
- ),
- )
-
- with (
- app.app_context(),
- pytest.raises(receiver.VerificationError, match="Source document is too large"),
- ):
- receiver.fetch_source_document(SOURCE_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_document_rejects_streamed_oversized_body(
- httpx_client: Mock, app: Flask, receiver: ModuleType
-) -> None:
- app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10
-
- class BodyStream(httpx.SyncByteStream):
- def __iter__(self):
- yield b"x" * 6
- yield b"x" * 6
-
- set_stream_response(
- httpx_client,
- httpx.Response(
- HTTPStatus.OK,
- headers={"Content-Type": "text/html"},
- stream=BodyStream(),
- request=httpx.Request("GET", SOURCE_URL),
- ),
- )
-
- with (
- app.app_context(),
- pytest.raises(receiver.VerificationError, match="Source document is too large"),
- ):
- receiver.fetch_source_document(SOURCE_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_document_does_not_mask_unexpected_value_error(
- httpx_client: Mock,
- app: Flask,
- receiver: ModuleType,
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- set_stream_response(
- httpx_client,
- httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", SOURCE_URL)),
- )
- monkeypatch.setattr(
- receiver, "read_limited_body", Mock(side_effect=ValueError("unexpected bug"))
- )
-
- with app.app_context(), pytest.raises(ValueError, match="unexpected bug"):
- receiver.fetch_source_document(SOURCE_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.fetch_source_document")
-@pytest.mark.parametrize(
- ("content_type", "body", "expected"),
- [
- pytest.param(
- "text/html; charset=utf-8",
- f'<a href="{TARGET_URL}">Reply</a>'.encode(),
- True,
- id="html",
- ),
- pytest.param(
- "TEXT/HTML; CHARSET=UTF-8",
- f'<a href="{TARGET_URL}">Reply</a>'.encode(),
- True,
- id="case-insensitive-html",
- ),
- pytest.param(
- "application/xhtml+xml",
- b'<a href="https://example.com/">Other</a>',
- False,
- id="xhtml-without-target",
- ),
- pytest.param(
- "text/plain; charset=utf-8",
- f"Reply to {TARGET_URL}".encode(),
- True,
- id="plain-text-utf-8",
- ),
- pytest.param(
- "text/plain; charset=utf-8",
- f"See ({TARGET_URL}), thanks".encode(),
- True,
- id="plain-text-with-surrounding-punctuation",
- ),
- pytest.param(
- "text/plain; charset=iso-8859-1",
- (f"Grüße. Reply to {TARGET_URL}").encode("iso-8859-1"),
- True,
- id="plain-text-declared-encoding",
- ),
- pytest.param(
- "text/plain; charset=utf-8",
- b"\xff Reply to " + TARGET_URL.encode(),
- True,
- id="plain-text-invalid-byte",
- ),
- pytest.param(
- "text/plain", b"No target here.", False, id="plain-text-without-target"
- ),
- ],
-)
-def test_source_mentions_target_by_media_type(
- fetch_source_document: Mock,
- receiver: ModuleType,
- content_type: str,
- body: bytes,
- expected: bool,
-) -> None:
- response = httpx.Response(
- HTTPStatus.OK,
- headers={"Content-Type": content_type},
- content=body,
- request=httpx.Request("GET", SOURCE_URL),
- )
- fetch_source_document.return_value = response, body
-
- assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL) is expected
- fetch_source_document.assert_called_once_with(SOURCE_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.fetch_source_document")
-@pytest.mark.parametrize(
- ("content_type", "expected_media_type"),
- [
- ("application/json", "application/json"),
- ("application/pdf", "application/pdf"),
- ("", "missing"),
- ],
-)
-def test_source_mentions_target_rejects_unsupported_media_type(
- fetch_source_document: Mock,
- receiver: ModuleType,
- content_type: str,
- expected_media_type: str,
-) -> None:
- headers = {}
-
- if content_type:
- headers["Content-Type"] = content_type
-
- response = httpx.Response(
- HTTPStatus.OK,
- headers=headers,
- content=b"Document",
- request=httpx.Request("GET", SOURCE_URL),
- )
- fetch_source_document.return_value = response, b"Document"
-
- with pytest.raises(
- receiver.VerificationError,
- match=f"Unsupported source content type: {expected_media_type}",
- ):
- receiver.source_mentions_target(SOURCE_URL, TARGET_URL)
-
-
-@pytest.mark.parametrize(
- ("mentions", "expected_status", "expected_reason"),
- [(True, "verified", None), (False, "deleted", "Source does not mention target")],
-)
-def test_determine_verification_outcome_returns_outcome(
- receiver: ModuleType,
- monkeypatch: pytest.MonkeyPatch,
- mentions: bool,
- expected_status: str,
- expected_reason: str | None,
-) -> None:
- monkeypatch.setattr(receiver, "source_mentions_target", Mock(return_value=mentions))
- claim = receiver.VerificationClaim(
- uuid.uuid7(), SOURCE_URL, TARGET_URL, datetime.now(UTC)
- )
-
- assert receiver.determine_verification_outcome(claim) == (
- expected_status,
- expected_reason,
- )
-
-
-@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
-def test_verify_webmention_marks_row_verifying_before_check(
- source_mentions_target: Mock, app: Flask, receiver: ModuleType
-) -> None:
- identifier = create_webmention(
- app, status="failed", failure_reason="Earlier failure"
- )
-
- def verify_source(source_url: str, target_url: str) -> bool:
- webmention = db.session.get(ReceivedWebmention, identifier)
-
- assert webmention is not None
- assert webmention.status == "verifying"
- assert webmention.failure_reason is None
- assert source_url == webmention.source
- assert target_url == webmention.target
-
- return True
-
- source_mentions_target.side_effect = verify_source
- receiver.verify_webmention.call_local(identifier)
- assert get_webmention_state(app, identifier) == ("verified", None)
-
-
-@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
-@pytest.mark.parametrize(
- ("outcome", "expected_status", "expected_reason"),
- [
- ("verified", "verified", None),
- ("missing", "deleted", "Source does not mention target"),
- ("gone", "deleted", "Source returned HTTP 410"),
- ("permanent-failure", "failed", "Source returned HTTP 404"),
- ],
-)
-def test_verify_webmention_persists_final_state(
- source_mentions_target: Mock,
- app: Flask,
- receiver: ModuleType,
- outcome: str,
- expected_status: str,
- expected_reason: str | None,
-) -> None:
- identifier = create_webmention(app)
-
- match outcome:
- case "verified":
- source_mentions_target.return_value = True
- case "missing":
- source_mentions_target.return_value = False
- case "gone":
- source_mentions_target.side_effect = receiver.SourceGoneError(
- "Source returned HTTP 410"
- )
- case "permanent-failure":
- source_mentions_target.side_effect = receiver.VerificationError(
- "Source returned HTTP 404"
- )
- case _:
- raise AssertionError(f"Unexpected outcome: {outcome}")
- receiver.verify_webmention.call_local(identifier)
-
- assert get_webmention_state(app, identifier) == (expected_status, expected_reason)
-
-
-@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
-@pytest.mark.parametrize("failure", ["temporary-http", "network"])
-def test_verify_webmention_persists_retryable_failure_and_reraises(
- source_mentions_target: Mock, app: Flask, receiver: ModuleType, failure: str
-) -> None:
- identifier = create_webmention(app)
-
- match failure:
- case "temporary-http":
- exception = receiver.RetryableFetchError("Source returned HTTP 503")
- case "network":
- exception = httpx.ConnectError(
- "Connection refused", request=httpx.Request("GET", SOURCE_URL)
- )
- case _:
- raise AssertionError(f"Unexpected failure: {failure}")
-
- source_mentions_target.side_effect = exception
-
- with pytest.raises(type(exception), match=str(exception)):
- receiver.verify_webmention.call_local(identifier)
-
- assert get_webmention_state(app, identifier) == ("failed", str(exception))
-
-
-@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
-@pytest.mark.parametrize("status", ["verifying", "verified", "deleted"])
-def test_verify_webmention_skips_active_and_terminal_states(
- source_mentions_target: Mock, app: Flask, receiver: ModuleType, status: str
-) -> None:
- identifier = create_webmention(app, status=status)
-
- receiver.verify_webmention.call_local(identifier)
-
- assert get_webmention_state(app, identifier) == (status, None)
- source_mentions_target.assert_not_called()
-
-
-def set_webmention_updated_at(
- app: Flask, identifier: uuid.UUID, when: datetime
-) -> None:
- with app.app_context():
- db.session.execute(
- sa.update(ReceivedWebmention)
- .where(ReceivedWebmention.uuid == identifier)
- .values(updated_at=when)
- )
- db.session.commit()
-
-
-@pytest.mark.parametrize("status", ["verifying", "received"])
-def test_reaper_requeues_stale_verifications(
- app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch, status: str
-) -> None:
- app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 900
- identifier = create_webmention(app, status=status, failure_reason="Interrupted")
- set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
- enqueue = Mock()
- monkeypatch.setattr(receiver, "verify_webmention", enqueue)
-
- with app.app_context():
- assert receiver.recover_stale_verifications() == 1
- assert receiver.recover_stale_verifications() == 0
- row = db.session.get(ReceivedWebmention, identifier)
- assert row is not None
- assert row.updated_at.replace(tzinfo=UTC) > datetime.now(UTC) - timedelta(
- seconds=10
- )
-
- assert get_webmention_state(app, identifier) == ("received", None)
- enqueue.assert_called_once_with(identifier)
-
-
-def test_reaper_does_not_touch_recent_or_terminal_webmentions(
- app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- recent_verifying = create_webmention(app, status="verifying")
- recent_received = create_webmention(
- app, source="https://source.example/received", status="received"
- )
- old_verified = create_webmention(
- app, source="https://source.example/verified", status="verified"
- )
- set_webmention_updated_at(app, old_verified, datetime.now(UTC) - timedelta(hours=1))
- enqueue = Mock()
- monkeypatch.setattr(receiver, "verify_webmention", enqueue)
-
- with app.app_context():
- assert receiver.recover_stale_verifications() == 0
-
- assert get_webmention_state(app, recent_verifying) == ("verifying", None)
- assert get_webmention_state(app, recent_received) == ("received", None)
- assert get_webmention_state(app, old_verified) == ("verified", None)
- enqueue.assert_not_called()
-
-
-def test_reaper_recovers_after_enqueue_failure(
- app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- identifier = create_webmention(app, status="verifying")
- set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
- monkeypatch.setattr(receiver, "verify_webmention", Mock(side_effect=RuntimeError))
-
- with app.app_context(), pytest.raises(RuntimeError):
- receiver.recover_stale_verifications()
-
- assert get_webmention_state(app, identifier) == ("received", None)
-
- # The reaper can recover the row again if the process died after the commit
- # but before Huey persisted the task.
- app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0
- enqueue = Mock()
- monkeypatch.setattr(receiver, "verify_webmention", enqueue)
- with app.app_context():
- assert receiver.recover_stale_verifications() == 1
-
- enqueue.assert_called_once_with(identifier)
-
-
-def test_late_worker_cannot_overwrite_recovered_verification(
- app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- identifier = create_webmention(app)
- original_verify = receiver.verify_webmention
-
- def concurrent_recovery(_source: str, _target: str) -> bool:
- # The reaper supersedes this still-running attempt; a newer attempt
- # subsequently completes before the original fetch returns.
- app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0
- with app.app_context():
- with patch.object(receiver, "verify_webmention", Mock()) as enqueue:
- assert receiver.recover_stale_verifications() == 1
- enqueue.assert_called_once_with(identifier)
- db.session.execute(
- sa.update(ReceivedWebmention)
- .where(ReceivedWebmention.uuid == identifier)
- .values(status="verified", updated_at=datetime.now(UTC))
- )
- db.session.commit()
- return False
-
- monkeypatch.setattr(receiver, "source_mentions_target", concurrent_recovery)
- original_verify.call_local(identifier)
-
- assert get_webmention_state(app, identifier) == ("verified", None)
-
-
-def test_reaper_requeues_and_verifies_integration(
- app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- identifier = create_webmention(app, status="verifying")
- set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
- check = Mock(return_value=True)
- monkeypatch.setattr(receiver, "source_mentions_target", check)
-
- with app.app_context():
- assert receiver.recover_stale_verifications() == 1
-
- assert get_webmention_state(app, identifier) == ("verified", None)
- check.assert_called_once_with(SOURCE_URL, TARGET_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
-@pytest.mark.parametrize(
- "unexpected_error",
- [
- pytest.param(httpx.InvalidURL("Invalid IDNA hostname"), id="invalid-url"),
- pytest.param(
- RuntimeError("Unexpected HTML parser error"), id="unexpected-error"
- ),
- ],
-)
-def test_verify_webmention_records_unexpected_error_as_terminal_failure(
- source_mentions_target: Mock,
- app: Flask,
- receiver: ModuleType,
- unexpected_error: Exception,
-) -> None:
- identifier = create_webmention(app, source="http://ex☃mple.test/")
- source_mentions_target.side_effect = unexpected_error
-
- receiver.verify_webmention.call_local(identifier)
-
- assert get_webmention_state(app, identifier) == ("failed", str(unexpected_error))
- source_mentions_target.assert_called_once()
- # Failed rows are not eligible for automatic reaper recovery.
- set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
- with app.app_context():
- assert receiver.recover_stale_verifications() == 0
-
-
-def test_reaper_caps_stale_verification_recoveries(
- app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- app.config["VERIFICATION_MAX_RECOVERIES"] = 3
- identifier = create_webmention(app, status="verifying")
- enqueue = Mock()
- monkeypatch.setattr(receiver, "verify_webmention", enqueue)
-
- # Simulate workers that repeatedly crash before completing the task.
- for attempt in range(3):
- set_webmention_updated_at(
- app, identifier, datetime.now(UTC) - timedelta(hours=1)
- )
- with app.app_context():
- assert receiver.recover_stale_verifications() == 1
- row = db.session.get(ReceivedWebmention, identifier)
- assert row is not None
- assert row.status == "received"
- assert row.recovery_attempts == attempt + 1
-
- set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
- with app.app_context():
- assert receiver.recover_stale_verifications() == 0
- row = db.session.get(ReceivedWebmention, identifier)
- assert row is not None
- assert row.status == "failed"
- assert row.failure_reason == receiver.RECOVERY_EXHAUSTED_REASON
- assert row.recovery_attempts == 3
- assert receiver.recover_stale_verifications() == 0
- assert enqueue.call_count == 3
-
-
-def test_reaper_caps_received_rows_lost_before_first_task(
- app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- app.config["VERIFICATION_MAX_RECOVERIES"] = 1
- identifier = create_webmention(app, status="received", recovery_attempts=1)
- set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
- enqueue = Mock()
- monkeypatch.setattr(receiver, "verify_webmention", enqueue)
-
- with app.app_context():
- assert receiver.recover_stale_verifications() == 0
- assert get_webmention_state(app, identifier) == (
- "failed",
- receiver.RECOVERY_EXHAUSTED_REASON,
- )
- enqueue.assert_not_called()
-
-
-def test_exhausted_row_cannot_be_revived_by_late_huey_retry(
- app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- identifier = create_webmention(
- app,
- status="failed",
- failure_reason=receiver.RECOVERY_EXHAUSTED_REASON,
- recovery_attempts=3,
- )
- source_mentions_target = Mock(return_value=True)
- monkeypatch.setattr(receiver, "source_mentions_target", source_mentions_target)
-
- receiver.verify_webmention.call_local(identifier)
-
- assert get_webmention_state(app, identifier) == (
- "failed",
- receiver.RECOVERY_EXHAUSTED_REASON,
- )
- source_mentions_target.assert_not_called()
-
-
-def test_verify_webmention_ignores_unknown_identifier(
- receiver: ModuleType, caplog: pytest.LogCaptureFixture
-) -> None:
- identifier = uuid.uuid7()
-
- with caplog.at_level(logging.WARNING):
- receiver.verify_webmention.call_local(identifier)
-
- assert f"Cannot verify unknown ReceivedWebmention {identifier}" in caplog.text
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_document_rejects_unsafe_destination(
- httpx_client: Mock, app: Flask, receiver: ModuleType
-) -> None:
- from webmentions_ssg.net import UnsafeAddressError
-
- 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"),
- ):
- receiver.fetch_source_document(SOURCE_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_document_treats_dns_failure_as_temporary(
- httpx_client: Mock, app: Flask, receiver: ModuleType
-) -> None:
- client = httpx_client.return_value.__enter__.return_value
- client.stream.side_effect = AddressResolutionError("DNS timeout")
- with (
- app.app_context(),
- pytest.raises(receiver.RetryableFetchError, match="could not be resolved"),
- ):
- receiver.fetch_source_document(SOURCE_URL)
-
-
-@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_document_ignores_invalid_content_length(
- httpx_client: Mock, app: Flask, receiver: ModuleType
-) -> None:
- response = httpx.Response(
- HTTPStatus.OK,
- headers={"Content-Type": "text/html", "Content-Length": "invalid"},
- content=b"<p>Document</p>",
- request=httpx.Request("GET", SOURCE_URL),
- )
- set_stream_response(httpx_client, response)
-
- with app.app_context():
- fetched_response, body = receiver.fetch_source_document(SOURCE_URL)
-
- assert fetched_response is response
- assert body == b"<p>Document</p>"
-
-
-@patch("webmentions_ssg.tasks.receiver.fetch_source_document")
-def test_source_mentions_target_falls_back_for_unknown_encoding(
- fetch_source_document: Mock, receiver: ModuleType
-) -> None:
- body = f"Reply to {TARGET_URL}".encode()
-
- response = httpx.Response(
- HTTPStatus.OK,
- headers={"Content-Type": "text/plain"},
- content=body,
- request=httpx.Request("GET", SOURCE_URL),
- )
- response.encoding = "unknown-encoding"
- fetch_source_document.return_value = response, body
-
- assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL)
- fetch_source_document.assert_called_once_with(SOURCE_URL)