From 6144e3c18d310b58cf107a9c451384ddad793a31 Mon Sep 17 00:00:00 2001 From: Colin Maudry Date: Wed, 24 Jun 2026 18:49:52 +0200 Subject: [PATCH] feat(auth): route delete-account avec confirmation par mot de passe (#73) --- src/auth/routes.py | 16 ++++++++++++++++ tests/auth/test_account.py | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/auth/routes.py b/src/auth/routes.py index c50e7cf..d85d3bc 100644 --- a/src/auth/routes.py +++ b/src/auth/routes.py @@ -199,3 +199,19 @@ def confirm_email_change(): return redirect("/compte/admin?error=invalid_token") db.promote_pending_email(user_id) return redirect("/compte/admin?email_changed=1") + + +@auth_bp.route("/delete-account", methods=["POST"]) +@login_required +def delete_account(): + current_pw = request.form.get("current_password") or "" + row = db.get_user_by_id(current_user.id) + if not check_password_hash(row["password_hash"], current_pw): + return _redirect_with_error("/compte/admin", "invalid_current_password") + + user_id = current_user.id + db.delete_email_verification_tokens_for_user(user_id) + db.delete_password_reset_tokens_for_user(user_id) + db.delete_user(user_id) + logout_user() + return redirect("/?account_deleted=1") diff --git a/tests/auth/test_account.py b/tests/auth/test_account.py index d1ec760..8487a61 100644 --- a/tests/auth/test_account.py +++ b/tests/auth/test_account.py @@ -78,3 +78,25 @@ def test_change_password_mismatch(client, users_db_path): }, ) assert "error=password_mismatch" in resp.headers["Location"] + + +def test_delete_account_requires_login(client, users_db_path): + resp = client.post("/auth/delete-account", data={"current_password": "x"}) + assert resp.status_code in (302, 401) + + +def test_delete_account_wrong_password(client, users_db_path): + uid = _login(client) + resp = client.post("/auth/delete-account", data={"current_password": "wrong"}) + assert "error=invalid_current_password" in resp.headers["Location"] + assert db.get_user_by_id(uid) is not None + + +def test_delete_account_success(client, users_db_path): + uid = _login(client) + resp = client.post( + "/auth/delete-account", data={"current_password": "old-password12"} + ) + assert resp.status_code == 302 + assert "account_deleted=1" in resp.headers["Location"] + assert db.get_user_by_id(uid) is None