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.

4.4 KB
Raw Blame
"""Extended tests for database/session_context.py password-resolution paths.

Pre-FastAPI-migration these reused Flask ``g.db_session``/``g.user_password``
and read the username from ``flask.session`` inside
``app.test_request_context()``. That path is gone — ``get_user_db_session``
now takes an explicit ``username``/``session_id`` and resolves the password
from the session-password store (by session_id, then any session) and the
per-thread search context. The ``g.db_session`` reuse and ``g.user_password``
storage tests were dropped (the behaviour no longer exists); the
password-resolution tests pass the credentials explicitly.
"""

from unittest.mock import Mock, patch

import pytest


class TestGetUserDbSessionPasswordPaths:
    """Tests for the password resolution fallback chain."""

    def test_password_from_session_password_store(self):
        """Password is resolved from session_password_store by session_id."""
        from local_deep_research.database.session_context import (
            get_user_db_session,
        )

        mock_session = Mock()

        with (
            patch(
                "local_deep_research.database.session_context.db_manager"
            ) as mock_db,
            patch(
                "local_deep_research.database.session_context.get_search_context",
                return_value=None,  # no thread context
            ),
            patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_store,
            patch(
                "local_deep_research.database.thread_local_session.get_metrics_session",
                return_value=mock_session,
            ) as mock_get,
        ):
            mock_db.has_encryption = True
            mock_store.get_session_password.return_value = "store-pass"

            with get_user_db_session(
                username="testuser", session_id="sess-abc"
            ) as session:
                assert session is mock_session

            mock_get.assert_called_once_with("testuser", "store-pass")
            mock_store.get_session_password.assert_called_once_with(
                "testuser", "sess-abc"
            )

    def test_password_from_thread_context(self):
        """Password is resolved from the per-thread search context."""
        from local_deep_research.database.session_context import (
            get_user_db_session,
        )

        mock_session = Mock()

        with (
            patch(
                "local_deep_research.database.session_context.db_manager"
            ) as mock_db,
            patch(
                "local_deep_research.database.session_context.get_search_context",
                return_value={"user_password": "thread-pass"},
            ),
            patch(
                "local_deep_research.database.thread_local_session.get_metrics_session",
                return_value=mock_session,
            ) as mock_get,
        ):
            mock_db.has_encryption = True

            with get_user_db_session(username="testuser") as session:
                assert session is mock_session

            mock_get.assert_called_once_with("testuser", "thread-pass")

    def test_get_metrics_session_returns_none_raises(self):
        """When get_metrics_session returns None, DatabaseSessionError is raised."""
        from local_deep_research.database.session_context import (
            get_user_db_session,
            DatabaseSessionError,
        )

        with (
            patch(
                "local_deep_research.database.session_context.db_manager"
            ) as mock_db,
            patch(
                "local_deep_research.database.session_context.get_search_context",
                return_value=None,
            ),
            patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_store,
            patch(
                "local_deep_research.database.thread_local_session.get_metrics_session",
                return_value=None,
            ),
        ):
            mock_db.has_encryption = False  # unencrypted → placeholder pw
            mock_store.get_session_password.return_value = None
            mock_store.get_any_session_password.return_value = None

            with pytest.raises(
                DatabaseSessionError, match="Could not establish session"
            ):
                with get_user_db_session(username="testuser"):
                    pass