95 lines
4.2 KiB
Python
95 lines
4.2 KiB
Python
"""Page Mon compte : changement de mot de passe."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import psycopg
|
|
from fastapi.testclient import TestClient
|
|
|
|
from shortstats.web.auth import hash_password
|
|
|
|
from ..outils.site import ORIGIN, PASSWORD, login
|
|
|
|
NEW_PASSWORD = "un-autre-mot-de-passe-long"
|
|
|
|
|
|
def change(client, current, new, confirm=None):
|
|
r = client.post("/account/password", headers={"Origin": ORIGIN}, follow_redirects=False,
|
|
data={"current": current, "new": new, "confirm": new if confirm is None else confirm})
|
|
return r.headers["location"]
|
|
|
|
|
|
def test_account_page_requires_login(app_client):
|
|
assert app_client.get("/account", follow_redirects=False).headers["location"] == "/login"
|
|
r = app_client.post("/account/password", headers={"Origin": ORIGIN},
|
|
data={"current": PASSWORD, "new": NEW_PASSWORD, "confirm": NEW_PASSWORD},
|
|
follow_redirects=False)
|
|
assert r.headers["location"] == "/login"
|
|
|
|
|
|
def test_change_password_validation(app_client):
|
|
login(app_client)
|
|
assert "Changer de mot de passe" in app_client.get("/account").text
|
|
assert change(app_client, "mauvais", NEW_PASSWORD) == "/account?msg=wrong_current"
|
|
assert change(app_client, PASSWORD, "court") == "/account?msg=too_short"
|
|
assert change(app_client, PASSWORD, NEW_PASSWORD, confirm="autre-chose-long") == "/account?msg=mismatch"
|
|
assert change(app_client, PASSWORD, PASSWORD) == "/account?msg=unchanged"
|
|
# Aucune de ces tentatives n'a changé le mot de passe.
|
|
app_client.post("/logout", headers={"Origin": ORIGIN})
|
|
assert login(app_client).headers["location"] == "/"
|
|
|
|
|
|
def test_change_password_flow(app_client, clean_db, users_file):
|
|
other = TestClient(app_client.app, base_url=ORIGIN) # 2e navigateur de la même personne
|
|
login(app_client)
|
|
login(other)
|
|
assert other.get("/api/charts").status_code == 200
|
|
|
|
assert change(app_client, PASSWORD, NEW_PASSWORD) == "/account?msg=changed"
|
|
# La session courante reste ouverte, l'autre est fermée.
|
|
assert app_client.get("/api/charts").status_code == 200
|
|
assert other.get("/api/charts").status_code == 401
|
|
# Ancien mot de passe refusé, nouveau accepté.
|
|
assert "incorrect" in login(other, password=PASSWORD).text
|
|
assert login(other, password=NEW_PASSWORD).headers["location"] == "/"
|
|
# Le haché est en base, users.txt n'a pas été modifié.
|
|
with psycopg.connect(clean_db["owner"]) as conn:
|
|
row = conn.execute("SELECT password_hash FROM shortstats.user_password WHERE username = 'alice'").fetchone()
|
|
assert row and row[0].startswith("$argon2id$")
|
|
assert "alice:" in users_file.read_text() and row[0] not in users_file.read_text()
|
|
|
|
|
|
def test_admin_reset_in_file_overrides_changed_password(app_client, users_file):
|
|
"""Mot de passe oublié : l'administrateur remplace la ligne de users.txt, qui reprend la main."""
|
|
import os
|
|
login(app_client)
|
|
assert change(app_client, PASSWORD, NEW_PASSWORD) == "/account?msg=changed"
|
|
reset = "mot-de-passe-de-secours"
|
|
users_file.write_text(f"alice:{hash_password(reset)}\n")
|
|
os.utime(users_file, (2, 2))
|
|
assert app_client.get("/api/charts").status_code == 401 # sessions fermées
|
|
assert "incorrect" in login(app_client, password=NEW_PASSWORD).text
|
|
assert login(app_client, password=reset).headers["location"] == "/"
|
|
|
|
|
|
def test_removed_user_cannot_use_changed_password(app_client, users_file):
|
|
import os
|
|
login(app_client)
|
|
change(app_client, PASSWORD, NEW_PASSWORD)
|
|
users_file.write_text("# plus personne\n")
|
|
os.utime(users_file, (3, 3))
|
|
assert app_client.get("/api/charts").status_code == 401
|
|
assert "incorrect" in login(app_client, password=NEW_PASSWORD).text
|
|
|
|
|
|
def test_change_password_is_throttled(app_client):
|
|
login(app_client)
|
|
for _ in range(3): # LOGIN_MAX_FAILURES = 3 dans la fixture
|
|
change(app_client, "mauvais", NEW_PASSWORD)
|
|
assert change(app_client, PASSWORD, NEW_PASSWORD) == "/account?msg=throttled"
|
|
|
|
|
|
def test_change_password_rejects_foreign_origin(app_client):
|
|
login(app_client)
|
|
r = app_client.post("/account/password", headers={"Origin": "https://evil.example"},
|
|
data={"current": PASSWORD, "new": NEW_PASSWORD, "confirm": NEW_PASSWORD})
|
|
assert r.status_code == 403
|