diff options
| author | Dennis Fink | 2026-11-14 19:43:28 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-14 19:43:28 +0100 |
| commit | c74feda222745ab7cca8993df83d9ea5f9d07e5b (patch) | |
| tree | acc7bf9bd7223f9d685d6c2954c5c90d7a9e36f0 /tests/tasks/test_sender.py | |
| parent | e52e554e2d033dd5ef8397617644e90e354f9560 (diff) | |
| download | webmentions-ssg-c74feda222745ab7cca8993df83d9ea5f9d07e5b.tar.gz webmentions-ssg-c74feda222745ab7cca8993df83d9ea5f9d07e5b.zip | |
refactor(core): use HTTPStatus consistently
Replace numeric HTTP response and assertion codes in views and tests
with `HTTPStatus` members so status handling is more descriptive and
consistent.
Diffstat (limited to '')
| -rw-r--r-- | tests/tasks/test_sender.py | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 7f453c1..3a22769 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -5,6 +5,7 @@ import logging import uuid from datetime import UTC, datetime, timedelta +from http import HTTPStatus from types import ModuleType from unittest.mock import MagicMock, Mock @@ -76,7 +77,7 @@ def mock_sender_requests( monkeypatch: pytest.MonkeyPatch, *, endpoint: str | None = ENDPOINT_URL, - result: tuple[int, str | None] = (202, None), + result: tuple[int, str | None] = (HTTPStatus.ACCEPTED, None), ) -> tuple[Mock, Mock, Mock, Mock]: httpx_client = MagicMock() client = httpx_client.return_value.__enter__.return_value @@ -130,7 +131,9 @@ def test_attempt_is_current( def test_deliver_webmention_returns_retryable_outcome( app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: - mock_sender_requests(sender_module, monkeypatch, result=(503, None)) + mock_sender_requests( + sender_module, monkeypatch, result=(HTTPStatus.SERVICE_UNAVAILABLE, None) + ) with app.app_context(): result = sender_module.deliver_webmention( @@ -141,7 +144,7 @@ def test_deliver_webmention_returns_retryable_outcome( status=SentWebmentionStatus.FAILED, reason="Webmention endpoint returned HTTP 503", endpoint=ENDPOINT_URL, - response_status=503, + response_status=HTTPStatus.SERVICE_UNAVAILABLE, terminal=False, temporary_failure=True, ) @@ -153,7 +156,7 @@ def test_send_webmention_persists_success( identifier = create_sent_webmention(app) _, client, discover, post = mock_sender_requests( - sender_module, monkeypatch, result=(201, STATUS_URL) + sender_module, monkeypatch, result=(HTTPStatus.CREATED, STATUS_URL) ) sender_module.send_webmention.call_local(identifier) @@ -172,7 +175,7 @@ def test_send_webmention_persists_success( assert webmention.status == SentWebmentionStatus.SENT assert webmention.failure_reason is None assert webmention.endpoint == ENDPOINT_URL - assert webmention.response_status == 201 + assert webmention.response_status == HTTPStatus.CREATED assert webmention.status_url == STATUS_URL assert webmention.last_attempted_at is not None assert webmention.last_sent_at is not None @@ -190,7 +193,7 @@ def test_send_webmention_marks_unsupported_target_processed( sent_revision=1, status=SentWebmentionStatus.SENT, endpoint="https://old.example/webmention", - response_status=201, + response_status=HTTPStatus.CREATED, status_url="https://old.example/status/123", ) @@ -229,7 +232,9 @@ def test_send_webmention_persists_permanent_failure( status_url="https://old.example/status/123", ) - mock_sender_requests(sender_module, monkeypatch, result=(400, None)) + mock_sender_requests( + sender_module, monkeypatch, result=(HTTPStatus.BAD_REQUEST, None) + ) sender_module.send_webmention.call_local(identifier) @@ -242,7 +247,7 @@ def test_send_webmention_persists_permanent_failure( assert webmention.status == SentWebmentionStatus.FAILED assert webmention.failure_reason == "Webmention endpoint returned HTTP 400" assert webmention.endpoint == ENDPOINT_URL - assert webmention.response_status == 400 + assert webmention.response_status == HTTPStatus.BAD_REQUEST assert webmention.status_url is None @@ -258,7 +263,9 @@ def test_send_webmention_persists_temporary_failure_with_backoff( status_url="https://old.example/status/123", ) - mock_sender_requests(sender_module, monkeypatch, result=(503, None)) + mock_sender_requests( + sender_module, monkeypatch, result=(HTTPStatus.SERVICE_UNAVAILABLE, None) + ) sender_module.send_webmention.call_local(identifier) @@ -271,7 +278,7 @@ def test_send_webmention_persists_temporary_failure_with_backoff( assert webmention.status == SentWebmentionStatus.FAILED assert webmention.failure_reason == "Webmention endpoint returned HTTP 503" assert webmention.endpoint == ENDPOINT_URL - assert webmention.response_status == 503 + assert webmention.response_status == HTTPStatus.SERVICE_UNAVAILABLE assert webmention.status_url is None assert webmention.attempt_count == 1 assert webmention.next_retry_at is not None @@ -304,7 +311,7 @@ def test_temporary_failure_backoff_suppresses_duplicate_tasks_and_caps_attempts( app.config["SEND_MAX_ATTEMPTS"] = 3 identifier = create_sent_webmention(app) _, _, discover, post = mock_sender_requests( - sender_module, monkeypatch, result=(503, None) + sender_module, monkeypatch, result=(HTTPStatus.SERVICE_UNAVAILABLE, None) ) for attempt in range(1, 4): @@ -622,7 +629,7 @@ def test_send_webmention_ignores_superseded_attempt( def post_success(*args, **kwargs) -> tuple[int, None]: supersede_revision() - return (202, None) + return (HTTPStatus.ACCEPTED, None) post.side_effect = post_success @@ -645,7 +652,7 @@ def test_send_webmention_ignores_superseded_attempt( def post_permanent_failure(*args, **kwargs) -> tuple[int, None]: supersede_revision() - return (400, None) + return (HTTPStatus.BAD_REQUEST, None) post.side_effect = post_permanent_failure @@ -696,7 +703,9 @@ def test_send_does_not_persist_unsafe_status_url( sender_module, "discover_webmention_endpoint", Mock(return_value=ENDPOINT_URL) ) monkeypatch.setattr( - sender_module, "post_webmention", Mock(return_value=(201, status_url)) + sender_module, + "post_webmention", + Mock(return_value=(HTTPStatus.CREATED, status_url)), ) sender_module.send_webmention.call_local(identifier) @@ -705,5 +714,5 @@ def test_send_does_not_persist_unsafe_status_url( mention = db.session.get(SentWebmention, identifier) assert mention is not None assert mention.status == SentWebmentionStatus.SENT - assert mention.response_status == 201 + assert mention.response_status == HTTPStatus.CREATED assert mention.status_url == expected |
