aboutsummaryrefslogtreecommitdiff
path: root/tests/views
diff options
context:
space:
mode:
authorDennis Fink2026-11-14 19:43:28 +0100
committerDennis Fink2026-11-14 19:43:28 +0100
commitc74feda222745ab7cca8993df83d9ea5f9d07e5b (patch)
treeacc7bf9bd7223f9d685d6c2954c5c90d7a9e36f0 /tests/views
parente52e554e2d033dd5ef8397617644e90e354f9560 (diff)
downloadwebmentions-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 'tests/views')
-rw-r--r--tests/views/helpers.py9
-rw-r--r--tests/views/test_admin.py41
-rw-r--r--tests/views/test_endpoint.py47
-rw-r--r--tests/views/test_session.py63
4 files changed, 89 insertions, 71 deletions
diff --git a/tests/views/helpers.py b/tests/views/helpers.py
index da98aa6..1fe1ed2 100644
--- a/tests/views/helpers.py
+++ b/tests/views/helpers.py
@@ -4,6 +4,7 @@
import uuid
from datetime import UTC, datetime
+from http import HTTPStatus
from types import ModuleType
from unittest.mock import Mock
@@ -80,7 +81,7 @@ def create_sent_source(app: Flask) -> uuid.UUID:
sent_revision=2,
status=SentWebmentionStatus.SENT,
endpoint="https://example.com/webmention",
- response_status=202,
+ response_status=HTTPStatus.ACCEPTED,
),
SentWebmention(
target="https://example.com/a",
@@ -91,7 +92,7 @@ def create_sent_source(app: Flask) -> uuid.UUID:
status=SentWebmentionStatus.FAILED,
failure_reason="Webmention endpoint returned HTTP 400",
endpoint="https://example.com/webmention",
- response_status=400,
+ response_status=HTTPStatus.BAD_REQUEST,
),
]
)
@@ -173,7 +174,7 @@ def create_resendable_sent_webmention(app: Flask) -> tuple[uuid.UUID, uuid.UUID]
status=SentWebmentionStatus.FAILED,
failure_reason="Previous failure",
endpoint="https://example.com/webmention",
- response_status=500,
+ response_status=HTTPStatus.INTERNAL_SERVER_ERROR,
status_url="https://example.com/webmention/status/123",
)
source.sent_webmentions.append(webmention)
@@ -202,7 +203,7 @@ def create_outgoing(
active=True,
desired_revision=1,
status_url=status_url,
- response_status=201 if status_url is not None else None,
+ response_status=HTTPStatus.CREATED if status_url is not None else None,
)
source.sent_webmentions.append(mention)
db.session.add(source)
diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py
index f166318..a88fbfa 100644
--- a/tests/views/test_admin.py
+++ b/tests/views/test_admin.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 Mock
@@ -38,7 +39,7 @@ def views_module(app: Flask) -> ModuleType:
def test_sent_views_require_login(client: FlaskClient, path: str) -> None:
response = client.get(path)
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert "/login" in response.headers["Location"]
@@ -48,7 +49,7 @@ def test_sent_lists_sources(app: Flask, client: FlaskClient) -> None:
response = client.get("/sent")
- assert response.status_code == 200
+ assert response.status_code == HTTPStatus.OK
html = response.get_data(as_text=True)
@@ -63,7 +64,7 @@ def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None:
response = client.get(f"/sent/{identifier}")
- assert response.status_code == 200
+ assert response.status_code == HTTPStatus.OK
html = response.get_data(as_text=True)
@@ -86,13 +87,13 @@ def test_sent_source_returns_404_for_unknown_source(
) -> None:
log_in(app, client)
response = client.get(f"/sent/{uuid.uuid7()}")
- assert response.status_code == 404
+ assert response.status_code == HTTPStatus.NOT_FOUND
def test_received_requires_login(client: FlaskClient) -> None:
response = client.get("/received")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert "/login" in response.headers["Location"]
@@ -102,7 +103,7 @@ def test_received_lists_webmentions(app: Flask, client: FlaskClient) -> None:
response = client.get("/received")
- 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
@@ -112,7 +113,7 @@ def test_received_lists_webmentions(app: Flask, client: FlaskClient) -> None:
def test_received_admin_actions_require_login(client: FlaskClient, action: str) -> None:
response = client.post(f"/received/{uuid.uuid7()}/{action}")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert "/login" in response.headers["Location"]
@@ -130,7 +131,7 @@ def test_received_admin_actions_reject_invalid_form(
response = client.post(f"/received/{identifier}/{action}")
- assert response.status_code == 400
+ assert response.status_code == HTTPStatus.BAD_REQUEST
form.validate_on_submit.assert_called_once_with()
@@ -147,7 +148,7 @@ def test_received_admin_actions_return_404_for_unknown_webmention(
response = client.post(f"/received/{uuid.uuid7()}/{action}")
- assert response.status_code == 404
+ assert response.status_code == HTTPStatus.NOT_FOUND
def test_delete_received_webmention(
@@ -162,7 +163,7 @@ def test_delete_received_webmention(
response = client.post(f"/received/{identifier}/delete?page=3")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"].endswith("/received?page=3")
with app.app_context():
@@ -190,7 +191,7 @@ def test_reverify_received_webmention(
response = client.post(f"/received/{identifier}/reverify?page=2")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"].endswith("/received?page=2")
with app.app_context():
@@ -206,7 +207,7 @@ def test_reverify_received_webmention(
def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None:
response = client.post("/sent/rescan")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert "/login" in response.headers["Location"]
@@ -225,7 +226,9 @@ def test_rescan_sent_sources(
response = client.post("/sent/rescan")
- assert response.status_code == (302 if valid else 400)
+ assert response.status_code == (
+ HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST
+ )
form.validate_on_submit.assert_called_once_with()
if valid:
@@ -238,7 +241,7 @@ def test_rescan_sent_sources(
def test_resend_sent_webmention_requires_login(client: FlaskClient) -> None:
response = client.post(f"/sent/{uuid.uuid7()}/{uuid.uuid7()}/resend")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert "/login" in response.headers["Location"]
@@ -254,7 +257,7 @@ def test_resend_sent_webmention_rejects_invalid_form(
response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend")
- assert response.status_code == 400
+ assert response.status_code == HTTPStatus.BAD_REQUEST
def test_resend_sent_webmention_returns_404_for_unknown_webmention(
@@ -269,7 +272,7 @@ def test_resend_sent_webmention_returns_404_for_unknown_webmention(
response = client.post(f"/sent/{source_identifier}/{uuid.uuid7()}/resend")
- assert response.status_code == 404
+ assert response.status_code == HTTPStatus.NOT_FOUND
def test_resend_sent_webmention_rejects_wrong_source(
@@ -287,7 +290,7 @@ def test_resend_sent_webmention_rejects_wrong_source(
f"/sent/{wrong_source_identifier}/{webmention_identifier}/resend"
)
- assert response.status_code == 404
+ assert response.status_code == HTTPStatus.NOT_FOUND
def test_resend_sent_webmention_reopens_current_revision(
@@ -311,7 +314,7 @@ def test_resend_sent_webmention_reopens_current_revision(
response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"].endswith(f"/sent/{source_identifier}")
with app.app_context():
@@ -356,7 +359,7 @@ def test_admin_only_links_safe_existing_status_urls(
session["_fresh"] = True
response = client.get(f"/sent/{source_identifier}")
- assert response.status_code == 200
+ assert response.status_code == HTTPStatus.OK
html = response.get_data(as_text=True)
assert (f'href="{status_url}"' in html) is safe
assert status_url in html
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():
diff --git a/tests/views/test_session.py b/tests/views/test_session.py
index 1a58f52..a91a6b2 100644
--- a/tests/views/test_session.py
+++ b/tests/views/test_session.py
@@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: BSD-3-Clause
+from http import HTTPStatus
from types import ModuleType
import pytest
@@ -25,7 +26,7 @@ def views_module(app: Flask) -> ModuleType:
def test_login_renders_form(client: FlaskClient) -> None:
response = client.get("/login")
- assert response.status_code == 200
+ assert response.status_code == HTTPStatus.OK
assert "login" in response.get_data(as_text=True).lower()
@@ -47,7 +48,7 @@ def test_login_rejects_invalid_credentials(
response = client.post("/login")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"].endswith("/login")
with client.session_transaction() as session:
@@ -81,7 +82,7 @@ def test_login_redirects_to_local_next_page(
response = client.post("/login", query_string={"next": next_url})
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"] == next_url
with client.session_transaction() as session:
@@ -123,7 +124,7 @@ def test_login_rejects_unsafe_next_page(
query_string = {} if next_url is None else {"next": next_url}
response = client.post("/login", query_string=query_string)
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"] == "/"
with client.session_transaction() as session:
@@ -135,7 +136,7 @@ def test_login_redirects_authenticated_user(app: Flask, client: FlaskClient) ->
response = client.get("/login")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"].endswith("/")
@@ -170,7 +171,7 @@ def test_login_always_checks_password(
response = client.post("/login")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"].endswith("/login")
expected_hash = (
real_password_hash
@@ -205,7 +206,7 @@ def test_login_rejects_unknown_user_even_if_password_matches(
response = client.post("/login")
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"].endswith("/login")
assert checked_passwords == [(views_module.dummy_password_hash(), "anything")]
@@ -233,12 +234,12 @@ def test_login_rate_limit_is_independent_per_ip(
response = client.post("/login", environ_overrides={"REMOTE_ADDR": ip})
return response.status_code
- assert attempt("192.0.2.10") == 302
- assert attempt("192.0.2.10") == 429
+ assert attempt("192.0.2.10") == HTTPStatus.FOUND
+ assert attempt("192.0.2.10") == HTTPStatus.TOO_MANY_REQUESTS
# Another IP still has its own allowance.
- assert attempt("192.0.2.11") == 302
- assert attempt("192.0.2.11") == 429
+ assert attempt("192.0.2.11") == HTTPStatus.FOUND
+ assert attempt("192.0.2.11") == HTTPStatus.TOO_MANY_REQUESTS
def test_login_rate_limit_uses_forwarded_client_ip_when_proxy_is_trusted(
@@ -267,10 +268,10 @@ def test_login_rate_limit_uses_forwarded_client_ip_when_proxy_is_trusted(
)
return response.status_code
- assert attempt("198.51.100.11") == 302
+ assert attempt("198.51.100.11") == HTTPStatus.FOUND
# A forged earlier X-Forwarded-For entry cannot change the client bucket.
- assert attempt("203.0.113.99, 198.51.100.11") == 429
- assert attempt("198.51.100.12") == 302
+ assert attempt("203.0.113.99, 198.51.100.11") == HTTPStatus.TOO_MANY_REQUESTS
+ assert attempt("198.51.100.12") == HTTPStatus.FOUND
def test_login_username_limit_throttles_across_ips(
@@ -298,16 +299,16 @@ def test_login_username_limit_throttles_across_ips(
environ_overrides={"REMOTE_ADDR": ip},
).status_code
- assert attempt("192.0.2.10", "Admin") == 302
- assert attempt("192.0.2.11", " admin ") == 302
- assert attempt("192.0.2.12", "ADMIN") == 429
+ assert attempt("192.0.2.10", "Admin") == HTTPStatus.FOUND
+ assert attempt("192.0.2.11", " admin ") == HTTPStatus.FOUND
+ assert attempt("192.0.2.12", "ADMIN") == HTTPStatus.TOO_MANY_REQUESTS
# An unrelated username is not locked by attempts against Admin.
- assert attempt("192.0.2.12", "another-user") == 302
+ assert attempt("192.0.2.12", "another-user") == HTTPStatus.FOUND
# Restore the real form: the POST-only limits must not block a GET,
# even from the client IP that submitted the throttled attempt.
response = client.get("/login", environ_overrides={"REMOTE_ADDR": "192.0.2.12"})
- assert response.status_code == 200
+ assert response.status_code == HTTPStatus.OK
def test_login_missing_username_uses_per_ip_bucket(
@@ -329,9 +330,9 @@ def test_login_missing_username_uses_per_ip_bucket(
def attempt(ip: str) -> int:
return client.post("/login", environ_overrides={"REMOTE_ADDR": ip}).status_code
- assert attempt("192.0.2.10") == 302
- assert attempt("192.0.2.10") == 429
- assert attempt("192.0.2.11") == 302
+ assert attempt("192.0.2.10") == HTTPStatus.FOUND
+ assert attempt("192.0.2.10") == HTTPStatus.TOO_MANY_REQUESTS
+ assert attempt("192.0.2.11") == HTTPStatus.FOUND
def test_login_rate_limits_requests_per_ip(
@@ -356,14 +357,18 @@ def test_login_rate_limits_requests_per_ip(
for _ in range(3)
]
- assert [response.status_code for response in responses] == [302, 302, 429]
+ assert [response.status_code for response in responses] == [
+ HTTPStatus.FOUND,
+ HTTPStatus.FOUND,
+ HTTPStatus.TOO_MANY_REQUESTS,
+ ]
assert form.validate_on_submit.call_count == 2
assert "Too many login attempts" in responses[-1].get_data(as_text=True)
# The real LoginForm is restored here.
# GET requests must remain accessible after throttling.
response = client.get("/login", environ_overrides={"REMOTE_ADDR": "192.0.2.10"})
- assert response.status_code == 200
+ assert response.status_code == HTTPStatus.OK
with client.session_transaction() as session:
assert "_user_id" not in session
@@ -374,7 +379,7 @@ def test_logout_rejects_get(app: Flask, client: FlaskClient) -> None:
response = client.get("/logout")
- assert response.status_code == 405
+ assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED
with client.session_transaction() as session:
assert "_user_id" in session
@@ -395,7 +400,7 @@ def test_logout_rejects_invalid_csrf(
response = client.post("/logout", data=data)
- assert response.status_code == 400
+ assert response.status_code == HTTPStatus.BAD_REQUEST
with client.session_transaction() as session:
assert "_user_id" in session
@@ -408,7 +413,7 @@ def test_logout_ends_session(app: Flask, client: FlaskClient) -> None:
log_in(app, client)
page = client.get("/received")
- assert page.status_code == 200
+ assert page.status_code == HTTPStatus.OK
html = page.get_data(as_text=True)
@@ -420,12 +425,12 @@ def test_logout_ends_session(app: Flask, client: FlaskClient) -> None:
response = client.post("/logout", data={"csrf_token": match.group(1)})
- assert response.status_code == 302
+ assert response.status_code == HTTPStatus.FOUND
assert response.headers["Location"].endswith("/")
with client.session_transaction() as session:
assert "_user_id" not in session
protected_response = client.get("/received")
- assert protected_response.status_code == 302
+ assert protected_response.status_code == HTTPStatus.FOUND
assert "/login" in protected_response.headers["Location"]