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.

6.7 KB
Raw Blame
"""Tests for capture_request_db_password (session_passwords module).

The function is the request-thread hook that snapshots the encrypted-DB
password before work is handed to a background worker:

1. If the request context carries a session id, the exact (username,
   session_id) entry wins.
2. Otherwise (or when that entry is missing/expired) it falls back to
   any live session for the user via get_any_session_password.
3. When nothing is stored it returns None.
4. It must never raise — even if the underlying store blows up.
"""

import time

import pytest

from local_deep_research.database import session_passwords as sp_module
from local_deep_research.database.session_passwords import (
    SessionPasswordStore,
    capture_request_db_password,
)
from local_deep_research.utilities.request_context import (
    request_user,
    reset_request_user,
    set_request_user,
)


@pytest.fixture
def fresh_store(monkeypatch):
    """Swap the module-global store for an empty one for each test.

    capture_request_db_password reads the module-global
    ``session_password_store``, so patching that name is enough — and it
    keeps the real global (shared with concurrently running suites)
    untouched.
    """
    store = SessionPasswordStore(ttl_hours=1)
    monkeypatch.setattr(sp_module, "session_password_store", store)
    return store


class TestExactSessionLookup:
    def test_returns_password_for_request_context_session(self, fresh_store):
        """With a session id in the request context, the exact session's
        password is returned — not another session's."""
        # Stored FIRST, so a fallback via get_any_session_password would
        # return this one (dict insertion order). Getting "pass-b" below
        # therefore proves the exact-session path ran.
        fresh_store.store_session_password("alice", "sess-a", "pass-a")
        fresh_store.store_session_password("alice", "sess-b", "pass-b")

        with request_user("alice", session_id="sess-b"):
            assert capture_request_db_password("alice") == "pass-b"

    def test_session_id_scoped_to_username(self, fresh_store):
        """A session id belonging to a different user must not leak that
        user's password; with no sessions of her own, alice gets None."""
        fresh_store.store_session_password("bob", "sess-bob", "bobs-pass")

        with request_user("alice", session_id="sess-bob"):
            assert capture_request_db_password("alice") is None


class TestNoSessionIdIsFailClosed:
    """A thread with no session id must NOT receive a password.

    Regression fence for the security review: an earlier revision fell
    back to a username-wide scan (get_any_session_password), which handed
    any caller with no request context a live password scavenged from
    whatever session happened to be open — including an entry orphaned by
    a re-login and left behind at logout. main returned None here; so do we.
    """

    def test_no_session_id_returns_none_even_with_live_session(
        self, fresh_store
    ):
        fresh_store.store_session_password("carol", "sess-1", "carols-pass")

        with request_user("carol", session_id=None):
            assert capture_request_db_password("carol") is None

    def test_no_request_context_at_all_returns_none(self, fresh_store):
        """Background thread with no request context: no password, even
        though the user has a live web session."""
        fresh_store.store_session_password("dave", "sess-x", "daves-pass")

        assert capture_request_db_password("dave") is None

    def test_missing_entry_does_not_scavenge_another_session(self, fresh_store):
        """A session id whose entry is gone must return None rather than
        borrowing a different session's password."""
        fresh_store.store_session_password("erin", "sess-live", "erins-pass")

        with request_user("erin", session_id="sess-gone"):
            assert capture_request_db_password("erin") is None

    def test_expired_exact_entry_returns_none(self, fresh_store):
        """An EXPIRED exact-session entry yields None; a second live
        session for the same user must not be substituted."""
        fresh_store.store_session_password("ivy", "sess-old", "stale-pass")
        fresh_store._store[("ivy", "sess-old")]["expires_at"] = time.time() - 1
        fresh_store.store_session_password("ivy", "sess-live", "fresh-pass")

        with request_user("ivy", session_id="sess-old"):
            assert capture_request_db_password("ivy") is None


class TestSetResetRequestUser:
    def test_set_then_reset_request_user_switches_lookup_path(
        self, fresh_store
    ):
        """Driving the context via set_request_user/reset_request_user
        directly: while set, the exact session resolves; after reset there
        is no session id, so the lookup is fail-closed."""
        fresh_store.store_session_password("judy", "sess-1", "judy-1")
        fresh_store.store_session_password("judy", "sess-2", "judy-2")

        handles = set_request_user("judy", session_id="sess-2")
        try:
            assert capture_request_db_password("judy") == "judy-2"
        finally:
            reset_request_user(handles)

        # Context cleared -> no session id -> no password.
        assert capture_request_db_password("judy") is None


class TestNothingStored:
    def test_nothing_stored_returns_none(self, fresh_store):
        with request_user("frank", session_id="sess-f"):
            assert capture_request_db_password("frank") is None

    def test_nothing_stored_no_context_returns_none(self, fresh_store):
        assert capture_request_db_password("frank") is None


class TestNeverRaises:
    def test_store_error_on_exact_lookup_returns_none(self, monkeypatch):
        """If the store itself throws, the function swallows it and
        returns None (callers skip DB work rather than crash)."""

        class ExplodingStore:
            def get_session_password(self, username, session_id):
                raise RuntimeError("store exploded")

            def get_any_session_password(self, username):
                raise RuntimeError("store exploded")

        monkeypatch.setattr(
            sp_module, "session_password_store", ExplodingStore()
        )

        with request_user("gina", session_id="sess-g"):
            assert capture_request_db_password("gina") is None

    def test_context_lookup_error_returns_none(self, monkeypatch):
        """If resolving the request context itself throws, the function
        swallows it and returns None rather than propagating."""

        def boom():
            raise RuntimeError("context exploded")

        monkeypatch.setattr(
            "local_deep_research.utilities.request_context.get_current_session_id",
            boom,
        )

        assert capture_request_db_password("henry") is None