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.1 KB
Raw Blame
"""Registration must not leave a bricked account behind (FastAPI port).

If ``create_user_database()`` fails *after* the auth-DB ``User`` row has been
committed, the register handler must delete that orphaned row. Otherwise the
username stays taken (``user_exists()`` blocks re-registration) while login
also fails (no encrypted DB to open) — a permanently unusable account.

Ported from the Flask ``tests/auth_tests/test_auth_routes.py`` orphan-cleanup
suite (PR #4934 / #3142). That suite drove the Flask app via ``test_client``;
this exercises the same route-level cleanup through the FastAPI router, which
splits registration into two ``try`` blocks — only a DB-creation failure
cleans up the auth row; post-creation setup failures do not.

The on-disk (salt/.db) cleanup sites tested by the rest of that suite live in
the shared ``database`` package (``create_user_database`` /
``initialize_database``) and are covered by those modules 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_orphaned_auth_row_cleaned_up_on_db_creation_failure(
    client, monkeypatch
):
    from local_deep_research.database.auth_db import get_auth_db_session
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.database.models.auth import User

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

    def _fail_create(uname, password):
        # The auth row is committed before create_user_database() runs.
        # Record its presence here so the test proves the row was
        # created-then-cleaned, not merely never created.
        check_db = get_auth_db_session()
        row_existed_at_failure["value"] = (
            check_db.query(User).filter_by(username=uname).first() is not None
        )
        check_db.close()
        raise RuntimeError("simulated encrypted DB creation failure")

    monkeypatch.setattr(db_manager, "create_user_database", _fail_create)

    response = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": "TestPass123!",
            "confirm_password": "TestPass123!",
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )

    # Registration reports failure to the user...
    assert response.status_code == 500
    assert "Registration failed" in response.text
    # ...but only after the auth row was actually committed...
    assert row_existed_at_failure.get("value") is True

    # ...and the orphaned row must have been cleaned up, freeing the
    # username so the user can retry registration.
    auth_db = get_auth_db_session()
    orphan = auth_db.query(User).filter_by(username=username).first()
    auth_db.close()
    assert orphan is None