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
"""Recovery contract for an authenticated root-page settings failure."""

from unittest.mock import patch

from local_deep_research.web import fastapi_app


def test_root_settings_failure_logs_out_and_clears_the_signed_session(
    authenticated_client,
):
    """The broad root-route guard must not create a stale-cookie login loop.

    The recovery branch catches database acquisition, settings-manager, and
    policy-display failures.  Whatever raised, it promises one observable
    diagnostic and a real logout before redirecting the browser.
    """
    before = authenticated_client.get("/auth/check")
    assert before.status_code == 200
    username = before.json()["username"]
    assert authenticated_client.cookies.get("session")

    with (
        patch(
            "local_deep_research.settings.manager.SettingsManager.get_setting",
            side_effect=RuntimeError("simulated settings read failure"),
        ),
        patch.object(fastapi_app.logger, "exception") as logged,
    ):
        response = authenticated_client.get("/", follow_redirects=False)

    assert response.status_code == 302
    assert response.headers["location"] == "/auth/login"
    logged.assert_called_once_with(
        "Root route failed to load settings for user {} — clearing session "
        "and redirecting to login",
        username,
    )

    clearing_headers = [
        value.decode()
        for key, value in response.headers.raw
        if key.lower() == b"set-cookie"
        and value.decode().startswith("session=")
    ]
    assert len(clearing_headers) == 1, response.headers.raw
    assert "expires=Thu, 01 Jan 1970" in clearing_headers[0]
    assert authenticated_client.cookies.get("session") is None

    check = authenticated_client.get("/auth/check", follow_redirects=False)
    assert check.status_code == 401
    assert check.json()["authenticated"] is False


def test_root_invalid_setting_shows_the_diagnostic_and_keeps_the_session(
    authenticated_client,
):
    """An invalid configured setting must not log the user out.

    Clearing the session here is a login loop: login redirects back to the
    root page, whose settings read fails again. The page shows the
    value-free diagnostic instead, and the user stays signed in.
    """
    from local_deep_research.settings.exceptions import SettingsValueError

    before = authenticated_client.get("/auth/check")
    assert before.status_code == 200
    error = SettingsValueError(
        "search.iterations",
        source="environment",
        reason="expected a valid number value",
    )

    with patch(
        "local_deep_research.settings.manager.SettingsManager.get_setting",
        side_effect=error,
    ):
        response = authenticated_client.get("/", follow_redirects=False)

    assert response.status_code == 500
    assert "location" not in response.headers
    assert "search.iterations" in response.text
    assert "expected a valid number value" in response.text
    assert not [
        value
        for key, value in response.headers.raw
        if key.lower() == b"set-cookie"
        and value.decode().startswith("session=")
        and "expires=Thu, 01 Jan 1970" in value.decode()
    ]

    check = authenticated_client.get("/auth/check", follow_redirects=False)
    assert check.status_code == 200
    assert check.json()["authenticated"] is True