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.

5.0 KB
Raw Blame
"""``POST /auth/validate-password`` response body.

Ported from ``tests/web/auth/test_validate_password_endpoint.py`` on main,
which registered the Flask ``auth_bp`` blueprint on a bare app. The
successor route is ``web/routers/auth.py::validate_password``; its body
contract (``{"valid": bool, "errors": [str, ...]}``) is unchanged.

What was already covered on the branch, and is therefore not repeated here:

* the happy path — ``tests/web/routers/test_endpoint_coverage.py::
  test_validate_password`` asserts ``valid is True`` for a strong password;
* the route's rate-limit bucket — ``tests/web/routers/
  test_auth_rate_limits.py::TestValidatePasswordBucket``;
* its CSRF status — ``tests/web/test_csrf_middleware_edges.py``.

What had NO successor is the rejection half: nothing on the branch asserted
that a weak password produces ``valid: False`` with a populated ``errors``
list, nor that the endpoint reports one error per unmet requirement. Those
strings are what the register and change-password forms render inline, so a
regression that returned ``{"valid": false, "errors": []}`` (or a 4xx) would
leave the user staring at a rejected form with no reason given, and every
existing test would stay green.

This endpoint takes no session, so it is exercised with an anonymous
client rather than ``authenticated_client``. The client is built locally
rather than reusing the shared ``client`` fixture because the route is
CSRF-protected on this branch (deliberately — see the note at
``web/dependencies/csrf.py``), and ``CSRFMiddleware`` runs before routing,
so an unarmed POST returns 403 without the handler ever executing.
"""

import pytest
from fastapi.testclient import TestClient


@pytest.fixture
def anon_client(app):
    """Anonymous TestClient with a CSRF token armed as a default header."""
    client = TestClient(app, raise_server_exceptions=False)
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})
    return client


class TestValidatePasswordEndpoint:
    def test_strong_password_returns_valid_with_no_errors(self, anon_client):
        """The positive control. ``test_endpoint_coverage`` pins ``valid``
        but not the empty ``errors`` list, and a route that returned every
        requirement as an "error" alongside ``valid: True`` would satisfy
        it."""
        response = anon_client.post(
            "/auth/validate-password",
            data={"password": "strongp4ss"},
        )

        assert response.status_code == 200
        data = response.json()
        assert data["valid"] is True
        assert data["errors"] == []

    def test_weak_password_returns_errors(self, anon_client):
        response = anon_client.post(
            "/auth/validate-password",
            data={"password": "abc"},
        )

        assert response.status_code == 200
        data = response.json()
        assert data["valid"] is False
        assert len(data["errors"]) > 0

    def test_empty_password_returns_all_errors(self, anon_client):
        """An empty password fails every requirement — length, lowercase
        and digit — and each is reported separately so the form can show
        them all at once."""
        response = anon_client.post(
            "/auth/validate-password",
            data={"password": ""},
        )

        data = response.json()
        assert data["valid"] is False
        assert len(data["errors"]) == 3

    def test_missing_password_field(self, anon_client):
        """A POST with no ``password`` field is treated as the empty string,
        not as a 4xx.

        Kept as a distinct case from ``test_empty_password_returns_all_
        errors`` because the mechanism differs: the Flask route reached it
        through ``request.form.get("password", "")`` and the FastAPI one
        through ``password: str = Form("")``. A missing default on the
        ``Form`` would turn this into a 422 that no other test would see.
        """
        response = anon_client.post("/auth/validate-password", data={})

        assert response.status_code == 200
        data = response.json()
        assert data["valid"] is False
        assert len(data["errors"]) == 3


def test_the_route_is_served_by_the_handler_this_file_targets(app):
    """Pin the wiring, not just the response shape.

    Every assertion above goes through HTTP, so they would all still pass if
    ``/auth/validate-password`` were re-pointed at some other handler that
    happened to return the same envelope. This audit found several guards that
    survived the port but stopped being *reached* (see #5959), so the wiring is
    worth asserting separately from the behaviour.
    """
    from local_deep_research.web.routers.auth import validate_password

    matches = [
        r
        for r in app.routes
        if getattr(r, "path", None) == "/auth/validate-password"
    ]
    assert matches, "route /auth/validate-password is not mounted"
    assert [r.endpoint for r in matches] == [validate_password]