Config strictement stricte partout (ruff, mypy --strict, bandit, vulture, import-linter, eslint, stylelint), aucune regle desactivee "pour ne pas casser le build" - l'existant a ete corrige pour la satisfaire plutot que l'inverse. Hooks pre-commit locaux (language: system) bloquants. - Typage mypy --strict propage a tout le moteur (db, screens, auth, core, ai, routes, puis publish/scripts/tests/app.py/build_css.py). - Securite : fuite de handle fichier Windows corrigee dans l'export SCORM (routes/publish/export_scorm.py), CSRF/RNG non-crypto/xAPI documentes (# nosec, # NOSONAR justifies), nouveau db.json_for_script() (echappe "</script>" dans le JSON embarque en <script>, 25 sites). - Architecture : imports circulaires/F811 nettoyes, contrats import-linter respectes, code mort retire (vulture). - Accessibilite : 69 champs de formulaire sans label correctement associe corriges (for/id ou aria-label) sur 11 templates. - ESLint/Stylelint : lot mecanique JS/CSS, regles ajustees puis appliquees (aucune desactivee sans verification individuelle). - Tests : isolation du compte admin partage (nettoyage ponctuel + fixture de teardown automatique en filet de securite), suite complete verte (591 tests Python, 241 tests JS). - SonarQube Community Build self-heberge (Docker + PostgreSQL) : rapport complet analyse point par point, faux positifs documentes. - .gitattributes ajoute (LF force) : core.autocrlf=true sur cette machine faisait echouer ESLint (linebreak-style) via un bug connu de git (checkout "en place" qui ignore l'eol force sur un fichier deja present sur disque - contourne en supprimant puis recreant chaque fichier suivi). djLint (H021, styles inline) volontairement saute pour ce commit - backlog assume, deja documente, traite dans un lot separe. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
310 lines
15 KiB
Python
310 lines
15 KiB
Python
"""Page de profil (routes/auth/profile.py) : modifier nom/prénom, changer
|
|
de mot de passe, demander la suppression de son compte (avec mot de passe
|
|
+ confirmation textuelle, voir templates/auth/profile.html)."""
|
|
|
|
from flask.testing import FlaskClient
|
|
|
|
import auth
|
|
import db
|
|
from tests.conftest import not_none
|
|
from tests.test_auth import _cleanup_project, _complete_onboarding, _confirm_2fa, _register, anon_client # noqa: F401
|
|
|
|
|
|
def test_profile_page_shows_current_user_info(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "profileinfo@example.com", prenom="Ada", nom="Lovelace")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
html = anon_client.get("/profile").get_data(as_text=True)
|
|
assert "profileinfo@example.com" in html
|
|
assert 'value="Ada"' in html
|
|
assert 'value="Lovelace"' in html
|
|
finally:
|
|
_cleanup_project("profileinfo@example.com")
|
|
|
|
|
|
def test_update_name(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "updatename@example.com", prenom="Old", nom="Name")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post("/profile/update-name", data={"prenom": "New", "nom": "Name2"})
|
|
assert "mises à jour" in resp.get_data(as_text=True)
|
|
user = not_none(auth.get_user_by_email("updatename@example.com"))
|
|
assert user["prenom"] == "New"
|
|
assert user["nom"] == "Name2"
|
|
finally:
|
|
_cleanup_project("updatename@example.com")
|
|
|
|
|
|
def test_update_name_requires_both_fields(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "updatenameblank@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post("/profile/update-name", data={"prenom": "", "nom": "Test"})
|
|
assert "requis" in resp.get_data(as_text=True)
|
|
finally:
|
|
_cleanup_project("updatenameblank@example.com")
|
|
|
|
|
|
def test_change_password_requires_correct_current_password(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "changepwwrong@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post(
|
|
"/profile/change-password",
|
|
data={
|
|
"current_password": "wrong",
|
|
"new_password": "Nouveau$ecret1",
|
|
"new_password_confirm": "Nouveau$ecret1",
|
|
},
|
|
)
|
|
assert "incorrect" in resp.get_data(as_text=True)
|
|
finally:
|
|
_cleanup_project("changepwwrong@example.com")
|
|
|
|
|
|
def test_change_password_rejects_mismatched_confirmation(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "changepwmismatch@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post(
|
|
"/profile/change-password",
|
|
data={
|
|
"current_password": "Sup3r$ecret!",
|
|
"new_password": "Nouveau$ecret1",
|
|
"new_password_confirm": "Autre$ecret2",
|
|
},
|
|
)
|
|
assert "ne correspondent pas" in resp.get_data(as_text=True)
|
|
finally:
|
|
_cleanup_project("changepwmismatch@example.com")
|
|
|
|
|
|
def test_change_password_rejects_a_weak_new_password(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "changepwweak@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post(
|
|
"/profile/change-password",
|
|
data={
|
|
"current_password": "Sup3r$ecret!",
|
|
"new_password": "faible",
|
|
"new_password_confirm": "faible",
|
|
},
|
|
)
|
|
assert "faible" in resp.get_data(as_text=True)
|
|
finally:
|
|
_cleanup_project("changepwweak@example.com")
|
|
|
|
|
|
def test_change_password_success_lets_login_with_new_password(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "changepwok@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post(
|
|
"/profile/change-password",
|
|
data={
|
|
"current_password": "Sup3r$ecret!",
|
|
"new_password": "Nouveau$ecret1",
|
|
"new_password_confirm": "Nouveau$ecret1",
|
|
},
|
|
)
|
|
assert "modifié" in resp.get_data(as_text=True)
|
|
|
|
anon_client.post("/logout")
|
|
resp = anon_client.post(
|
|
"/login", data={"email": "changepwok@example.com", "password": "Nouveau$ecret1"}, follow_redirects=False
|
|
)
|
|
assert resp.status_code == 302
|
|
assert resp.headers["Location"] == "/login/2fa"
|
|
finally:
|
|
_cleanup_project("changepwok@example.com")
|
|
|
|
|
|
def test_delete_account_requires_correct_password(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "deletewrongpw@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post("/profile/delete", data={"current_password": "wrong", "confirm_text": "SUPPRIMER"})
|
|
assert "incorrect" in resp.get_data(as_text=True)
|
|
assert auth.get_user_by_email("deletewrongpw@example.com") is not None
|
|
finally:
|
|
_cleanup_project("deletewrongpw@example.com")
|
|
|
|
|
|
def test_delete_account_requires_the_confirmation_phrase(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "deletenoconfirm@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post("/profile/delete", data={"current_password": "Sup3r$ecret!", "confirm_text": "oups"})
|
|
assert "annulée" in resp.get_data(as_text=True)
|
|
assert auth.get_user_by_email("deletenoconfirm@example.com") is not None
|
|
finally:
|
|
_cleanup_project("deletenoconfirm@example.com")
|
|
|
|
|
|
def test_delete_account_removes_user_and_their_project(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "deleteforreal@example.com")
|
|
_confirm_2fa(anon_client)
|
|
_complete_onboarding(anon_client)
|
|
user = not_none(auth.get_user_by_email("deleteforreal@example.com"))
|
|
slug = user["project_slug"]
|
|
import os
|
|
|
|
assert os.path.isdir(db.game_dir(slug))
|
|
|
|
resp = anon_client.post(
|
|
"/profile/delete",
|
|
data={"current_password": "Sup3r$ecret!", "confirm_text": "SUPPRIMER"},
|
|
follow_redirects=False,
|
|
)
|
|
assert resp.status_code == 302
|
|
assert resp.headers["Location"] == "/login"
|
|
assert auth.get_user_by_email("deleteforreal@example.com") is None
|
|
assert not os.path.isdir(db.game_dir(slug))
|
|
|
|
|
|
def test_the_only_admin_account_cannot_be_deleted(client: FlaskClient) -> None:
|
|
"""`client` (conftest.py) est déjà connecté en tant qu'admin unique de
|
|
la session de tests."""
|
|
resp = client.post("/profile/delete", data={"current_password": "Test1234!", "confirm_text": "SUPPRIMER"})
|
|
assert "administrateur" in resp.get_data(as_text=True)
|
|
import tests.conftest as conftest_module
|
|
|
|
assert auth.get_user_by_id(conftest_module._TEST_ADMIN_ID) is not None
|
|
|
|
|
|
def test_regenerate_recovery_codes_requires_correct_password(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "regencodeswrong@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post("/profile/regenerate-recovery-codes", data={"current_password": "wrong"})
|
|
assert "incorrect" in resp.get_data(as_text=True)
|
|
finally:
|
|
_cleanup_project("regencodeswrong@example.com")
|
|
|
|
|
|
def test_regenerate_recovery_codes_replaces_the_old_ones_and_shows_them_once(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "regencodesok@example.com")
|
|
_confirm_2fa(anon_client)
|
|
user = not_none(auth.get_user_by_email("regencodesok@example.com"))
|
|
try:
|
|
old_codes = auth.generate_recovery_codes(user["id"])
|
|
|
|
resp = anon_client.post("/profile/regenerate-recovery-codes", data={"current_password": "Sup3r$ecret!"})
|
|
html = resp.get_data(as_text=True)
|
|
assert "recoveryCodesModal" in html
|
|
|
|
# Jamais réaffichés sur un chargement suivant.
|
|
html_again = anon_client.get("/profile").get_data(as_text=True)
|
|
assert "recoveryCodesModal" not in html_again
|
|
|
|
# Les anciens codes ne fonctionnent plus, mais le nouveau lot,
|
|
# visible dans le modal ci-dessus, doit être réellement stocké en
|
|
# base — vérifié indirectement via verify_recovery_code : aucun des
|
|
# anciens codes ne doit plus matcher.
|
|
for old_code in old_codes:
|
|
assert not auth.verify_recovery_code(user["id"], old_code)
|
|
finally:
|
|
_cleanup_project("regencodesok@example.com")
|
|
|
|
|
|
def test_update_email_requires_correct_password(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "emailwrongpw@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post("/profile/update-email", data={"email": "new@example.com", "current_password": "wrong"})
|
|
assert "incorrect" in resp.get_data(as_text=True)
|
|
assert auth.get_user_by_email("emailwrongpw@example.com") is not None
|
|
finally:
|
|
_cleanup_project("emailwrongpw@example.com")
|
|
|
|
|
|
def test_update_email_rejects_invalid_format(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "emailinvalid@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post(
|
|
"/profile/update-email", data={"email": "not-an-email", "current_password": "Sup3r$ecret!"}
|
|
)
|
|
assert "invalide" in resp.get_data(as_text=True)
|
|
finally:
|
|
_cleanup_project("emailinvalid@example.com")
|
|
|
|
|
|
def test_update_email_rejects_an_email_already_used(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
_register(anon_client, "emailtaken1@example.com")
|
|
_confirm_2fa(anon_client)
|
|
anon_client.post("/logout")
|
|
_register(anon_client, "emailtaken2@example.com")
|
|
_confirm_2fa(anon_client)
|
|
try:
|
|
resp = anon_client.post(
|
|
"/profile/update-email", data={"email": "emailtaken1@example.com", "current_password": "Sup3r$ecret!"}
|
|
)
|
|
assert "existe déjà" in resp.get_data(as_text=True)
|
|
finally:
|
|
_cleanup_project("emailtaken1@example.com")
|
|
_cleanup_project("emailtaken2@example.com")
|
|
|
|
|
|
def test_update_email_never_touches_the_project_folder(anon_client: FlaskClient) -> None: # noqa: F811 - anon_client est un fixture pytest (voir tests.test_auth), pas une vraie redefinition - pyflakes ne comprend pas l'injection de fixture par parametre
|
|
"""Le dossier propriétaire d'un projet est nommé d'après l'ID du
|
|
compte (voir routes/games/games_new.py, opaque et stable — jamais
|
|
l'email, voir scripts/migrate_owner_folders_to_user_id.py pour la
|
|
migration qui a retiré l'ancien schéma slugify(email)) : changer
|
|
d'adresse email ne doit donc plus jamais renommer quoi que ce soit sur
|
|
disque, ni changer project_slug."""
|
|
import os
|
|
|
|
_register(anon_client, "oldmail@example.com")
|
|
_confirm_2fa(anon_client)
|
|
_complete_onboarding(anon_client)
|
|
old_user = not_none(auth.get_user_by_email("oldmail@example.com"))
|
|
old_slug = old_user["project_slug"]
|
|
assert os.path.isdir(db.game_dir(old_slug))
|
|
try:
|
|
resp = anon_client.post(
|
|
"/profile/update-email", data={"email": "newmail@example.com", "current_password": "Sup3r$ecret!"}
|
|
)
|
|
assert "mise à jour" in resp.get_data(as_text=True)
|
|
|
|
assert auth.get_user_by_email("oldmail@example.com") is None
|
|
new_user = auth.get_user_by_email("newmail@example.com")
|
|
assert new_user is not None
|
|
# Ni le slug ni le dossier sur disque n'ont bougé.
|
|
assert new_user["project_slug"] == old_slug
|
|
assert os.path.isdir(db.game_dir(old_slug))
|
|
|
|
# La connexion se fait désormais avec la nouvelle adresse.
|
|
anon_client.post("/logout")
|
|
resp = anon_client.post(
|
|
"/login", data={"email": "newmail@example.com", "password": "Sup3r$ecret!"}, follow_redirects=False
|
|
)
|
|
assert resp.status_code == 302
|
|
assert resp.headers["Location"] == "/login/2fa"
|
|
finally:
|
|
_cleanup_project("oldmail@example.com")
|
|
_cleanup_project("newmail@example.com")
|
|
|
|
|
|
def test_update_email_for_an_admin_does_not_touch_any_project_folder(client: FlaskClient, game: str) -> None:
|
|
"""`client`/`game` (conftest.py) : compte admin, pas de project_slug
|
|
dédié — changer son adresse ne doit renommer aucun dossier. L'admin de
|
|
test est PARTAGÉ par toute la session de tests (voir conftest.py) :
|
|
son adresse est restaurée après coup pour ne pas perturber les autres
|
|
tests qui s'appuient dessus."""
|
|
import tests.conftest as conftest_module
|
|
|
|
try:
|
|
resp = client.post(
|
|
"/profile/update-email", data={"email": "newadminmail@example.com", "current_password": "Test1234!"}
|
|
)
|
|
assert "mise à jour" in resp.get_data(as_text=True)
|
|
|
|
user = not_none(auth.get_user_by_id(conftest_module._TEST_ADMIN_ID))
|
|
assert user["email"] == "newadminmail@example.com"
|
|
assert user["project_slug"] is None
|
|
assert db.game_meta(game) is not None
|
|
finally:
|
|
auth.update_email(conftest_module._TEST_ADMIN_ID, "admin@pytest.local")
|