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.

3.3 KB
Raw Blame
"""A post-creation session failure must not leave the user half-logged-in.

FastAPI port of the Flask ``test_auth_routes`` regression from #5006 (F2
follow-up to #4934). ``create_user_database()`` succeeds (the account is
legitimately created), but a later step in the inline session setup — storing
the session password — fails. Registration returns 500, and NO auth material
may survive: the partially-set cookie would otherwise log the user in on their
next request despite the reported failure, and the server-side session +
password entry would leak.

The FastAPI branch creates sessions inline (not via Flask's shared
``_create_user_session``); the rollback lives in ``_rollback_partial_session``.
Rather than decode Starlette's signed session cookie, this asserts the
end-to-end effect (a protected route redirects to login) plus the server-side
teardown directly.
"""

import uuid

import pytest
from fastapi.testclient import TestClient


@pytest.fixture(scope="module")
def client():
    from local_deep_research.web.fastapi_app import app

    return TestClient(app, raise_server_exceptions=False)


def _csrf(client):
    client.get("/auth/login")
    r = client.get("/auth/csrf-token")
    return r.json().get("csrf_token", "") if r.status_code == 200 else ""


def test_post_creation_session_failure_does_not_leave_user_logged_in(
    client, monkeypatch
):
    from local_deep_research.database.session_passwords import (
        session_password_store,
    )
    from local_deep_research.web.auth.session_manager import session_manager

    username = f"sessfail_{uuid.uuid4().hex[:8]}"
    captured = {}

    def _boom(*args, **kwargs):
        # store_session_password(username, session_id, password) — capture the
        # server-side session_id so we can assert it was torn down.
        captured["session_id"] = (
            args[1] if len(args) > 1 else kwargs.get("session_id")
        )
        raise RuntimeError("simulated post-creation session failure")

    # Fails AFTER the inline session setup has already set session_id/username/
    # temp_auth_token in the cookie AND created the server-side session —
    # exactly the half-logged-in window.
    monkeypatch.setattr(session_password_store, "store_session_password", _boom)

    response = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": "TestPass123!",
            "confirm_password": "TestPass123!",
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert response.status_code == 500
    assert captured.get("session_id")  # the failing step actually ran

    # Server-side state torn down (not just the cookie): the session and its
    # password-store entry must be gone, or they leak.
    assert session_manager.validate_session(captured["session_id"]) is None
    assert (
        session_password_store.get_session_password(
            username, captured["session_id"]
        )
        is None
    )

    # End-to-end: the client carries whatever cookie the 500 set; the user must
    # be genuinely unauthenticated — a protected route redirects to login.
    protected = client.get("/", follow_redirects=False)
    assert protected.status_code == 302
    assert "/auth/login" in protected.headers.get("location", "")