diff options
Diffstat (limited to 'tests/views/test_session.py')
| -rw-r--r-- | tests/views/test_session.py | 63 |
1 files changed, 34 insertions, 29 deletions
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"] |
