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.8 KB
Raw Blame
"""
Tests for ``web.fastapi_app._load_secret_key``.

The loader must fail loud on any reachable misconfiguration. Falling
back to an ephemeral in-memory key would silently invalidate every
existing session on restart, with no operator visibility — far worse
than a hard error at startup.
"""

from __future__ import annotations

import pathlib
from unittest.mock import patch

import pytest


def test_returns_existing_key(tmp_path, monkeypatch):
    """When the .secret_key file exists with a real key, return it."""
    monkeypatch.setattr(
        "local_deep_research.config.paths.get_data_directory",
        lambda: tmp_path,
    )
    secret_path = tmp_path / ".secret_key"
    secret_path.write_text("a" * 64)

    from local_deep_research.web.fastapi_app import _load_secret_key

    assert _load_secret_key() == "a" * 64


def test_generates_fresh_key_when_missing(tmp_path, monkeypatch):
    """When the file doesn't exist yet, generate a 64-char hex key."""
    monkeypatch.setattr(
        "local_deep_research.config.paths.get_data_directory",
        lambda: tmp_path,
    )

    from local_deep_research.web.fastapi_app import _load_secret_key

    key = _load_secret_key()
    assert len(key) == 64
    # Hex chars only
    int(key, 16)
    # Persisted to file
    persisted = (tmp_path / ".secret_key").read_text().strip()
    assert persisted == key


def test_empty_key_file_raises(tmp_path, monkeypatch):
    """An empty / whitespace-only SECRET_KEY file must raise — itsdangerous
    would accept it and silently produce a deterministic empty-secret signing
    key, breaking session integrity without operator visibility."""
    monkeypatch.setattr(
        "local_deep_research.config.paths.get_data_directory",
        lambda: tmp_path,
    )
    secret_path = tmp_path / ".secret_key"
    secret_path.write_text("   \n")  # whitespace only

    from local_deep_research.web.fastapi_app import _load_secret_key

    with pytest.raises(RuntimeError, match="empty"):
        _load_secret_key()


def test_unreadable_key_file_raises(tmp_path, monkeypatch):
    """If the file exists but read fails, raise — never fall back to an
    in-memory key."""
    monkeypatch.setattr(
        "local_deep_research.config.paths.get_data_directory",
        lambda: tmp_path,
    )
    secret_path = tmp_path / ".secret_key"
    secret_path.write_text("ignored")

    real_open = open

    def boom(path, *args, **kwargs):
        if str(path) == str(secret_path):
            raise PermissionError("denied")
        return real_open(path, *args, **kwargs)

    with patch("builtins.open", boom):
        from local_deep_research.web.fastapi_app import _load_secret_key

        with pytest.raises(RuntimeError, match="Cannot read SECRET_KEY"):
            _load_secret_key()


def test_unwritable_data_dir_raises(tmp_path, monkeypatch):
    """If we can't even create the .secret_key file (e.g. read-only data
    dir), raise — don't silently use an in-memory key."""
    # Point at a path inside tmp_path that we'll make read-only at the
    # parent. Use mkdir(0o400) on the parent — open with O_CREAT will
    # fail with PermissionError (OSError subclass).
    sub = tmp_path / "ro_data"
    sub.mkdir()
    monkeypatch.setattr(
        "local_deep_research.config.paths.get_data_directory",
        lambda: sub,
    )

    real_os_open = pathlib.os.open

    def boom_open(path, flags, mode=0o777, **kwargs):
        # Fail only the .secret_key write; let mkdir / other os.open
        # calls go through.
        if str(path).endswith(".secret_key"):
            raise OSError("read-only filesystem")
        return real_os_open(path, flags, mode, **kwargs)

    with patch("os.open", boom_open):
        from local_deep_research.web.fastapi_app import _load_secret_key

        with pytest.raises(RuntimeError, match="Cannot write SECRET_KEY"):
            _load_secret_key()