Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test(auth): pin the change-password rekey backstop (#6504)

Lukas088465ee2ece
diff --git a/tests/web/routers/test_auth_flow_gaps.py b/tests/web/routers/test_auth_flow_gaps.py
index fa42db4db8..c1e23fcbf6 100644
--- a/tests/web/routers/test_auth_flow_gaps.py
+++ b/tests/web/routers/test_auth_flow_gaps.py
@@ -28,8 +28,10 @@ observe the real server-issued session_id without any mocking.
"""
import base64
+import contextlib
import json
import uuid
+from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
@@ -473,3 +475,136 @@ class TestChangePasswordWhitespaceCurrent:
# Nothing was rekeyed: the real password still opens the database.
assert _login(_new_client(app), user, pw).status_code == 302
assert _login(_new_client(app), user, new_password).status_code == 401
+
+
+# ----------------------------------------------------------------------------
+# Change-password rekey backstop
+# ----------------------------------------------------------------------------
+
+_AUTH_MOD = "local_deep_research.web.routers.auth"
+_NEW_PASSWORD = "ReplacementPass123"
+_REJECTED = "Password change rejected"
+_REFUSED_KEY_DETAIL = "file is not a database"
+_REFUSED_KEY = f"Invalid encryption key: {_REFUSED_KEY_DETAIL}"
+
+
+class TestChangePasswordBackstop:
+ """A manager `ValueError` must render 400, not 500 (#6497).
+
+ `change_password` validates both keys before it rekeys and raises on an
+ invalid one. The form checks above the `try` catch anything a browser can
+ send, which is what `TestChangePasswordWhitespaceCurrent` pins, and that is
+ also why nothing had ever driven the `except ValueError` beneath them: the
+ whitespace request is intercepted at the form check and never reaches the
+ `try`, and the db-layer tests call the manager directly. So the remedy
+ #6375 added for the 500 was the half of it CI could not see, and removing
+ it, narrowing it to another exception type, or breaking its 400 render
+ would all have stayed green.
+ """
+
+ @contextlib.contextmanager
+ def _manager_raising(self, error: Exception):
+ """Patch the route's `db_manager` down to just `change_password`.
+
+ Same stubbing shape `_rekey_harness` uses in
+ `test_auth_route_port_gaps.py`, kept local because this case needs
+ neither the cleanup patches nor the render stub: the point is the real
+ template rendering at the real status code.
+ """
+ with patch(f"{_AUTH_MOD}.db_manager") as mock_db:
+ mock_db.change_password.side_effect = error
+ yield mock_db
+
+ def _submit(self, client, password: str, error: Exception):
+ """Post a valid change-password form while the manager raises `error`.
+
+ The CSRF token is fetched before `db_manager` is patched, so the page
+ that carries it is rendered by the real manager. Every cell then checks
+ that the request reached the rekey; a form check that turned it away
+ first would make the assertions after it pass for the wrong reason.
+ """
+ token = _csrf(client)
+ with self._manager_raising(error) as mock_db:
+ resp = client.post(
+ "/auth/change-password",
+ data={
+ "current_password": password,
+ "new_password": _NEW_PASSWORD,
+ "confirm_password": _NEW_PASSWORD,
+ "csrf_token": token,
+ },
+ follow_redirects=False,
+ )
+ assert mock_db.change_password.called, (
+ "the request must reach the rekey path; if a form check turned it "
+ "away first this cell is measuring the wrong branch"
+ )
+ return resp
+
+ def _logged_in_client(self, app, user: str, password: str):
+ client = _new_client(app)
+ assert _login(client, user, password).status_code == 302
+ return client
+
+ def test_manager_value_error_renders_400_not_500(
+ self, app, registered_user
+ ):
+ user, pw = registered_user
+ client = self._logged_in_client(app, user, pw)
+
+ resp = self._submit(client, pw, ValueError(_REFUSED_KEY))
+
+ assert resp.status_code == 400, (
+ f"a manager ValueError must come back as a form error, got "
+ f"{resp.status_code}: {resp.text[:300]}"
+ )
+ assert _REJECTED in resp.text
+
+ def test_the_rejection_does_not_echo_the_manager_message(
+ self, app, registered_user
+ ):
+ """The message is built from the password fields this handler holds in
+ plaintext, which is why the route logs the user and not the exception.
+ Rendering it would put that back on the page."""
+ user, pw = registered_user
+ client = self._logged_in_client(app, user, pw)
+
+ resp = self._submit(client, pw, ValueError(_REFUSED_KEY))
+
+ assert resp.status_code == 400
+ assert _REFUSED_KEY_DETAIL not in resp.text
+
+ def test_the_old_password_still_logs_in_after_a_refused_rekey(
+ self, app, registered_user
+ ):
+ """Nothing was rekeyed on this path, and the 400 has to mean that. A
+ backstop that rendered the error after a partial rekey would pass every
+ assertion above and lock the user out of their own database."""
+ user, pw = registered_user
+ client = self._logged_in_client(app, user, pw)
+
+ assert (
+ self._submit(client, pw, ValueError(_REFUSED_KEY)).status_code
+ == 400
+ )
+
+ fresh = _new_client(app)
+ assert _login(fresh, user, pw).status_code == 302
+
+ def test_a_non_value_error_is_not_swallowed_into_a_400(
+ self, app, registered_user
+ ):
+ """The scope control. The backstop exists for the one exception type
+ `change_password` raises for a refused key; widening it to `Exception`
+ would turn a genuine fault into a form error the user retries forever.
+ """
+ user, pw = registered_user
+ client = self._logged_in_client(app, user, pw)
+
+ resp = self._submit(client, pw, RuntimeError("disk gone"))
+
+ assert resp.status_code == 500, (
+ "a RuntimeError is not a refused password field and must surface "
+ f"as a server error, got {resp.status_code}"
+ )
+ assert _REJECTED not in resp.text