From c74feda222745ab7cca8993df83d9ea5f9d07e5b Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 14 Nov 2026 19:43:28 +0100 Subject: 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. --- tests/views/test_endpoint.py | 47 ++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 19 deletions(-) (limited to 'tests/views/test_endpoint.py') diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py index 4b81d70..52ef77d 100644 --- a/tests/views/test_endpoint.py +++ b/tests/views/test_endpoint.py @@ -4,6 +4,7 @@ import uuid from datetime import UTC, datetime, timedelta +from http import HTTPStatus from types import ModuleType from unittest.mock import ANY, Mock @@ -34,7 +35,7 @@ def views_module(app: Flask) -> ModuleType: def test_endpoint_only_accepts_post(client: FlaskClient) -> None: response = client.get("/endpoint") - assert response.status_code == 405 + assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED def test_endpoint_returns_form_errors( @@ -45,7 +46,7 @@ def test_endpoint_returns_form_errors( response = client.post("/endpoint") - assert response.status_code == 400 + assert response.status_code == HTTPStatus.BAD_REQUEST assert response.get_json() == errors @@ -61,7 +62,7 @@ def test_endpoint_creates_webmention( response = client.post("/endpoint") - assert response.status_code == 201 + assert response.status_code == HTTPStatus.CREATED form.validate_on_submit.assert_called_once_with() with app.app_context(): @@ -92,8 +93,8 @@ def test_endpoint_is_idempotent( first_response = client.post("/endpoint") second_response = client.post("/endpoint") - assert first_response.status_code == 201 - assert second_response.status_code == 201 + assert first_response.status_code == HTTPStatus.CREATED + assert second_response.status_code == HTTPStatus.CREATED assert first_response.headers["Location"] == second_response.headers["Location"] with app.app_context(): @@ -140,7 +141,7 @@ def test_resending_resets_failure_state( response = client.post("/endpoint") - assert response.status_code == 201 + assert response.status_code == HTTPStatus.CREATED with app.app_context(): webmention = db.session.get(ReceivedWebmention, identifier) @@ -155,7 +156,7 @@ def test_resending_resets_failure_state( def test_status_returns_404_for_unknown_webmention(client: FlaskClient) -> None: response = client.get(f"/status/{uuid.uuid7()}") - assert response.status_code == 404 + assert response.status_code == HTTPStatus.NOT_FOUND def test_status_displays_webmention(app: Flask, client: FlaskClient) -> None: @@ -165,7 +166,7 @@ def test_status_displays_webmention(app: Flask, client: FlaskClient) -> None: response = client.get(f"/status/{identifier}") - assert response.status_code == 200 + assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert SOURCE_URL in html assert TARGET_URL in html @@ -222,7 +223,7 @@ def test_endpoint_recovers_from_concurrent_insert( response = client.post("/endpoint") - assert response.status_code == 201 + assert response.status_code == HTTPStatus.CREATED assert response.headers["Location"].endswith(f"/status/{competing_identifier}") assert execute.call_count == 3 @@ -261,7 +262,7 @@ def test_endpoint_rejects_digest_collision( response = client.post("/endpoint") - assert response.status_code == 409 + assert response.status_code == HTTPStatus.CONFLICT assert response.get_json() == {"error": "Webmention URL fingerprint conflict"} verify.assert_not_called() with app.app_context(): @@ -288,7 +289,11 @@ def test_endpoint_rate_limits_requests_per_ip( client.post("/endpoint", environ_overrides={"REMOTE_ADDR": "192.0.2.10"}) ) - assert [r.status_code for r in responses] == [201, 201, 429] + assert [r.status_code for r in responses] == [ + HTTPStatus.CREATED, + HTTPStatus.CREATED, + HTTPStatus.TOO_MANY_REQUESTS, + ] assert verify.call_count == 2 @@ -320,9 +325,9 @@ def test_endpoint_rate_limit_distinguishes_clients_behind_trusted_proxy( headers={"X-Forwarded-For": client_ip}, ).status_code - assert attempt("198.51.100.11", 0) == 201 - assert attempt("198.51.100.11", 1) == 429 - assert attempt("198.51.100.12", 2) == 201 + assert attempt("198.51.100.11", 0) == HTTPStatus.CREATED + assert attempt("198.51.100.11", 1) == HTTPStatus.TOO_MANY_REQUESTS + assert attempt("198.51.100.12", 2) == HTTPStatus.CREATED def test_endpoint_global_rate_limit( @@ -348,7 +353,11 @@ def test_endpoint_global_rate_limit( ) ) - assert [r.status_code for r in responses] == [201, 201, 429] + assert [r.status_code for r in responses] == [ + HTTPStatus.CREATED, + HTTPStatus.CREATED, + HTTPStatus.TOO_MANY_REQUESTS, + ] assert verify.call_count == 2 @@ -377,7 +386,7 @@ def test_endpoint_recent_duplicate_does_not_reverify( response = client.post("/endpoint") - assert response.status_code == 201 + assert response.status_code == HTTPStatus.CREATED assert response.headers["Location"].endswith(str(identifier)) with app.app_context(): @@ -416,7 +425,7 @@ def test_endpoint_reverifies_after_cooldown( response = client.post("/endpoint") - assert response.status_code == 201 + assert response.status_code == HTTPStatus.CREATED with app.app_context(): stored = db.session.get(ReceivedWebmention, identifier) @@ -435,7 +444,7 @@ def test_endpoint_rejects_request_bodies_over_configured_maximum( response = client.post( "/endpoint", data={"source": SOURCE_URL + "x" * 150, "target": TARGET_URL} ) - assert response.status_code == 413 + assert response.status_code == HTTPStatus.CONTENT_TOO_LARGE def test_endpoint_rejects_bad_url_before_inserting_row( @@ -450,7 +459,7 @@ def test_endpoint_rejects_bad_url_before_inserting_row( "/endpoint", data={"source": "http://ex☃mple.test/", "target": TARGET_URL} ) - assert response.status_code == 400 + assert response.status_code == HTTPStatus.BAD_REQUEST assert "source" in response.get_json() verify_webmention.assert_not_called() with app.app_context(): -- cgit v1.3.1