# SPDX-FileCopyrightText: 2026 Dennis Fink # # SPDX-License-Identifier: BSD-3-Clause """Tests for the authenticated user page and password changes.""" import re from http import HTTPStatus import pytest from flask import Flask from flask.testing import FlaskClient from werkzeug.middleware.proxy_fix import ProxyFix from webmentions_ssg import db from webmentions_ssg.models.user import User from .helpers import log_in def password_data( current: str = "test", new: str = "a new longer password", confirm: str | None = None, ) -> dict[str, str]: return { "current_password": current, "new_password": new, "confirm_password": new if confirm is None else confirm, } @pytest.mark.parametrize("method", ["get", "post"]) def test_user_page_requires_login(client: FlaskClient, method: str) -> None: response = getattr(client, method)("/user") assert response.status_code == HTTPStatus.FOUND assert "/login?next=" in response.headers["Location"] def test_user_page_shows_account_and_direct_remote_address( app: Flask, client: FlaskClient ) -> None: log_in(app, client) response = client.get( "/user", environ_overrides={"REMOTE_ADDR": "192.0.2.44"}, headers={"X-Forwarded-For": "198.51.100.8"}, ) assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert "admin" in html assert "192.0.2.44" in html assert "198.51.100.8" not in html assert "PROXY_FIX_TRUSTED_HOPS" in html assert 'name="current_password"' in html assert 'name="new_password"' in html assert 'name="confirm_password"' in html assert 'href="/user"' in html def test_user_page_remote_address_respects_trusted_proxy( app: Flask, client: FlaskClient ) -> None: log_in(app, client) app.config["PROXY_FIX_TRUSTED_HOPS"] = 1 app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1) response = client.get( "/user", environ_overrides={"REMOTE_ADDR": "192.0.2.200"}, headers={"X-Forwarded-For": "203.0.113.9, 198.51.100.27"}, ) assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert "198.51.100.27" in html assert "203.0.113.9" not in html assert "192.0.2.200" not in html @pytest.mark.parametrize( ("data", "message"), [ pytest.param( password_data(current="wrong"), "Current password is incorrect.", id="wrong-current", ), pytest.param( password_data(new="short"), "at least 8 characters", id="weak-new" ), pytest.param( password_data(confirm="different"), "Passwords must match.", id="mismatch" ), pytest.param( password_data(new="password\x7f"), "control characters", id="invalid-new" ), ], ) def test_password_change_rejects_invalid_input( app: Flask, client: FlaskClient, data: dict[str, str], message: str ) -> None: log_in(app, client) with client.session_transaction() as session: old_session_id = session["_user_id"] response = client.post("/user", data=data) assert response.status_code == HTTPStatus.OK assert message in response.get_data(as_text=True) with client.session_transaction() as session: assert session["_user_id"] == old_session_id with app.app_context(): user = db.session.get(User, 1) assert user is not None assert user.check_password("test") assert not user.check_password(data["new_password"]) def test_password_change_refreshes_own_session_and_revokes_other_sessions( app: Flask, client: FlaskClient ) -> None: log_in(app, client) with client.session_transaction() as session: old_session_id = session["_user_id"] other_client = app.test_client() with other_client.session_transaction() as session: session["_user_id"] = old_session_id session["_fresh"] = True assert other_client.get("/user").status_code == HTTPStatus.OK response = client.post("/user", data=password_data()) assert response.status_code == HTTPStatus.FOUND assert response.headers["Location"] == "/user" with client.session_transaction() as session: assert session["_user_id"] != old_session_id new_session_id = session["_user_id"] with app.app_context(): user = db.session.get(User, 1) assert user is not None assert user.check_password("a new longer password") assert not user.check_password("test") assert user.get_id() == new_session_id page = client.get("/user") assert page.status_code == HTTPStatus.OK assert "Password changed successfully." in page.get_data(as_text=True) assert other_client.get("/user").status_code == HTTPStatus.FOUND def test_password_change_requires_csrf(app: Flask, client: FlaskClient) -> None: app.config["WTF_CSRF_ENABLED"] = True log_in(app, client) response = client.post("/user", data=password_data()) assert response.status_code == HTTPStatus.BAD_REQUEST page = client.get("/user") assert page.status_code == HTTPStatus.OK match = re.search( r'name="csrf_token"\s+value="([^"]+)"', page.get_data(as_text=True) ) assert match is not None response = client.post( "/user", data={**password_data(), "csrf_token": match.group(1)} ) assert response.status_code == HTTPStatus.FOUND with app.app_context(): user = db.session.get(User, 1) assert user is not None assert user.check_password("a new longer password")