aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/test_receiver.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/tasks/test_receiver.py113
1 files changed, 58 insertions, 55 deletions
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py
index feb7d7d..7241247 100644
--- a/tests/tasks/test_receiver.py
+++ b/tests/tasks/test_receiver.py
@@ -70,7 +70,7 @@ def set_stream_response(httpx_client: Mock, response: httpx.Response) -> Mock:
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_returns_response_and_body(
+def test_fetch_source_document_returns_response_and_body(
httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
response = httpx.Response(
@@ -82,7 +82,7 @@ def test_fetch_source_returns_response_and_body(
client = set_stream_response(httpx_client, response)
with app.app_context():
- fetched_response, body = receiver.fetch_source(SOURCE_URL)
+ fetched_response, body = receiver.fetch_source_document(SOURCE_URL)
assert fetched_response is response
assert body == b"<p>Document</p>"
@@ -107,14 +107,14 @@ def test_fetch_source_returns_response_and_body(
(HTTPStatus.BAD_REQUEST, "VerificationError"),
(HTTPStatus.NOT_FOUND, "VerificationError"),
(HTTPStatus.GONE, "SourceGoneError"),
- (HTTPStatus.REQUEST_TIMEOUT, "TemporaryFetchError"),
- (HTTPStatus.TOO_EARLY, "TemporaryFetchError"),
- (HTTPStatus.TOO_MANY_REQUESTS, "TemporaryFetchError"),
- (HTTPStatus.INTERNAL_SERVER_ERROR, "TemporaryFetchError"),
- (HTTPStatus.SERVICE_UNAVAILABLE, "TemporaryFetchError"),
+ (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_maps_http_status_to_exception(
+def test_fetch_source_document_maps_http_status_to_exception(
httpx_client: Mock,
app: Flask,
receiver: ModuleType,
@@ -129,11 +129,11 @@ def test_fetch_source_maps_http_status_to_exception(
exception_type = getattr(receiver, exception_name)
with app.app_context(), pytest.raises(exception_type, match=f"HTTP {status_code}"):
- receiver.fetch_source(SOURCE_URL)
+ receiver.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_propagates_network_error(
+def test_fetch_source_document_propagates_network_error(
httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
client = httpx_client.return_value.__enter__.return_value
@@ -145,14 +145,14 @@ def test_fetch_source_propagates_network_error(
app.app_context(),
pytest.raises(httpx.ConnectError, match="Connection refused"),
):
- receiver.fetch_source(SOURCE_URL)
+ receiver.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_rejects_declared_oversized_body(
+def test_fetch_source_document_rejects_declared_oversized_body(
httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
- app.config["MAX_SOURCE_BYTES"] = 10
+ app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10
set_stream_response(
httpx_client,
@@ -168,14 +168,14 @@ def test_fetch_source_rejects_declared_oversized_body(
app.app_context(),
pytest.raises(receiver.VerificationError, match="Source document is too large"),
):
- receiver.fetch_source(SOURCE_URL)
+ receiver.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_rejects_streamed_oversized_body(
+def test_fetch_source_document_rejects_streamed_oversized_body(
httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
- app.config["MAX_SOURCE_BYTES"] = 10
+ app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10
class BodyStream(httpx.SyncByteStream):
def __iter__(self):
@@ -196,11 +196,11 @@ def test_fetch_source_rejects_streamed_oversized_body(
app.app_context(),
pytest.raises(receiver.VerificationError, match="Source document is too large"),
):
- receiver.fetch_source(SOURCE_URL)
+ receiver.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_does_not_mask_unexpected_value_error(
+def test_fetch_source_document_does_not_mask_unexpected_value_error(
httpx_client: Mock,
app: Flask,
receiver: ModuleType,
@@ -215,10 +215,10 @@ def test_fetch_source_does_not_mask_unexpected_value_error(
)
with app.app_context(), pytest.raises(ValueError, match="unexpected bug"):
- receiver.fetch_source(SOURCE_URL)
+ receiver.fetch_source_document(SOURCE_URL)
-@patch("webmentions_ssg.tasks.receiver.fetch_source")
+@patch("webmentions_ssg.tasks.receiver.fetch_source_document")
@pytest.mark.parametrize(
("content_type", "body", "expected"),
[
@@ -270,7 +270,7 @@ def test_fetch_source_does_not_mask_unexpected_value_error(
],
)
def test_source_mentions_target_by_media_type(
- fetch_source: Mock,
+ fetch_source_document: Mock,
receiver: ModuleType,
content_type: str,
body: bytes,
@@ -282,13 +282,13 @@ def test_source_mentions_target_by_media_type(
content=body,
request=httpx.Request("GET", SOURCE_URL),
)
- fetch_source.return_value = response, body
+ fetch_source_document.return_value = response, body
assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL) is expected
- fetch_source.assert_called_once_with(SOURCE_URL)
+ fetch_source_document.assert_called_once_with(SOURCE_URL)
-@patch("webmentions_ssg.tasks.receiver.fetch_source")
+@patch("webmentions_ssg.tasks.receiver.fetch_source_document")
@pytest.mark.parametrize(
("content_type", "expected_media_type"),
[
@@ -298,7 +298,7 @@ def test_source_mentions_target_by_media_type(
],
)
def test_source_mentions_target_rejects_unsupported_media_type(
- fetch_source: Mock,
+ fetch_source_document: Mock,
receiver: ModuleType,
content_type: str,
expected_media_type: str,
@@ -314,7 +314,7 @@ def test_source_mentions_target_rejects_unsupported_media_type(
content=b"Document",
request=httpx.Request("GET", SOURCE_URL),
)
- fetch_source.return_value = response, b"Document"
+ fetch_source_document.return_value = response, b"Document"
with pytest.raises(
receiver.VerificationError,
@@ -327,7 +327,7 @@ def test_source_mentions_target_rejects_unsupported_media_type(
("mentions", "expected_status", "expected_reason"),
[(True, "verified", None), (False, "deleted", "Source does not mention target")],
)
-def test_check_source_returns_outcome(
+def test_determine_verification_outcome_returns_outcome(
receiver: ModuleType,
monkeypatch: pytest.MonkeyPatch,
mentions: bool,
@@ -339,7 +339,10 @@ def test_check_source_returns_outcome(
uuid.uuid7(), SOURCE_URL, TARGET_URL, datetime.now(UTC)
)
- assert receiver.check_source(claim) == (expected_status, expected_reason)
+ assert receiver.determine_verification_outcome(claim) == (
+ expected_status,
+ expected_reason,
+ )
@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
@@ -415,7 +418,7 @@ def test_verify_webmention_persists_retryable_failure_and_reraises(
match failure:
case "temporary-http":
- exception = receiver.TemporaryFetchError("Source returned HTTP 503")
+ exception = receiver.RetryableFetchError("Source returned HTTP 503")
case "network":
exception = httpx.ConnectError(
"Connection refused", request=httpx.Request("GET", SOURCE_URL)
@@ -460,15 +463,15 @@ def set_webmention_updated_at(
def test_reaper_requeues_stale_verifications(
app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch, status: str
) -> None:
- app.config["VERIFICATION_TIMEOUT_SECONDS"] = 900
+ 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_webmentions() == 1
- assert receiver.recover_stale_webmentions() == 0
+ 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(
@@ -494,7 +497,7 @@ def test_reaper_does_not_touch_recent_or_terminal_webmentions(
monkeypatch.setattr(receiver, "verify_webmention", enqueue)
with app.app_context():
- assert receiver.recover_stale_webmentions() == 0
+ assert receiver.recover_stale_verifications() == 0
assert get_webmention_state(app, recent_verifying) == ("verifying", None)
assert get_webmention_state(app, recent_received) == ("received", None)
@@ -510,17 +513,17 @@ def test_reaper_recovers_after_enqueue_failure(
monkeypatch.setattr(receiver, "verify_webmention", Mock(side_effect=RuntimeError))
with app.app_context(), pytest.raises(RuntimeError):
- receiver.recover_stale_webmentions()
+ 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_TIMEOUT_SECONDS"] = 0
+ app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0
enqueue = Mock()
monkeypatch.setattr(receiver, "verify_webmention", enqueue)
with app.app_context():
- assert receiver.recover_stale_webmentions() == 1
+ assert receiver.recover_stale_verifications() == 1
enqueue.assert_called_once_with(identifier)
@@ -534,10 +537,10 @@ def test_late_worker_cannot_overwrite_recovered_verification(
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_TIMEOUT_SECONDS"] = 0
+ app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0
with app.app_context():
with patch.object(receiver, "verify_webmention", Mock()) as enqueue:
- assert receiver.recover_stale_webmentions() == 1
+ assert receiver.recover_stale_verifications() == 1
enqueue.assert_called_once_with(identifier)
db.session.execute(
sa.update(ReceivedWebmention)
@@ -562,7 +565,7 @@ def test_reaper_requeues_and_verifies_integration(
monkeypatch.setattr(receiver, "source_mentions_target", check)
with app.app_context():
- assert receiver.recover_stale_webmentions() == 1
+ assert receiver.recover_stale_verifications() == 1
assert get_webmention_state(app, identifier) == ("verified", None)
check.assert_called_once_with(SOURCE_URL, TARGET_URL)
@@ -594,7 +597,7 @@ def test_verify_webmention_records_unexpected_error_as_terminal_failure(
# 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_webmentions() == 0
+ assert receiver.recover_stale_verifications() == 0
def test_reaper_caps_stale_verification_recoveries(
@@ -611,7 +614,7 @@ def test_reaper_caps_stale_verification_recoveries(
app, identifier, datetime.now(UTC) - timedelta(hours=1)
)
with app.app_context():
- assert receiver.recover_stale_webmentions() == 1
+ assert receiver.recover_stale_verifications() == 1
row = db.session.get(ReceivedWebmention, identifier)
assert row is not None
assert row.status == "received"
@@ -619,13 +622,13 @@ def test_reaper_caps_stale_verification_recoveries(
set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
with app.app_context():
- assert receiver.recover_stale_webmentions() == 0
+ 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_webmentions() == 0
+ assert receiver.recover_stale_verifications() == 0
assert enqueue.call_count == 3
@@ -639,7 +642,7 @@ def test_reaper_caps_received_rows_lost_before_first_task(
monkeypatch.setattr(receiver, "verify_webmention", enqueue)
with app.app_context():
- assert receiver.recover_stale_webmentions() == 0
+ assert receiver.recover_stale_verifications() == 0
assert get_webmention_state(app, identifier) == (
"failed",
receiver.RECOVERY_EXHAUSTED_REASON,
@@ -680,7 +683,7 @@ def test_verify_webmention_ignores_unknown_identifier(
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_rejects_unsafe_destination(
+def test_fetch_source_document_rejects_unsafe_destination(
httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
from webmentions_ssg.net import UnsafeAddressError
@@ -691,24 +694,24 @@ def test_fetch_source_rejects_unsafe_destination(
app.app_context(),
pytest.raises(receiver.VerificationError, match="non-public address"),
):
- receiver.fetch_source(SOURCE_URL)
+ receiver.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_treats_dns_failure_as_temporary(
+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.TemporaryFetchError, match="could not be resolved"),
+ pytest.raises(receiver.RetryableFetchError, match="could not be resolved"),
):
- receiver.fetch_source(SOURCE_URL)
+ receiver.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
-def test_fetch_source_ignores_invalid_content_length(
+def test_fetch_source_document_ignores_invalid_content_length(
httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
response = httpx.Response(
@@ -720,15 +723,15 @@ def test_fetch_source_ignores_invalid_content_length(
set_stream_response(httpx_client, response)
with app.app_context():
- fetched_response, body = receiver.fetch_source(SOURCE_URL)
+ 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")
+@patch("webmentions_ssg.tasks.receiver.fetch_source_document")
def test_source_mentions_target_falls_back_for_unknown_encoding(
- fetch_source: Mock, receiver: ModuleType
+ fetch_source_document: Mock, receiver: ModuleType
) -> None:
body = f"Reply to {TARGET_URL}".encode()
@@ -739,7 +742,7 @@ def test_source_mentions_target_falls_back_for_unknown_encoding(
request=httpx.Request("GET", SOURCE_URL),
)
response.encoding = "unknown-encoding"
- fetch_source.return_value = response, body
+ fetch_source_document.return_value = response, body
assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL)
- fetch_source.assert_called_once_with(SOURCE_URL)
+ fetch_source_document.assert_called_once_with(SOURCE_URL)