1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# 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")
|