~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Fixtures for chat feature tests."""
import importlib
import pytest
import sys
import uuid
from unittest.mock import MagicMock, patch
from contextlib import contextmanager
from datetime import datetime
# Capture the genuine ChatService class ONCE, at conftest import time --
# before any test body can patch ``chat.service.ChatService``. The heal
# fixture below rebinds *this* captured reference (never a value re-read
# from the service module at heal time, which could itself be a leaked
# mock).
from local_deep_research.chat.service import ChatService as _REAL_CHAT_SERVICE
# Same principle for the settings-read path ``send_message`` walks:
# ``_load_settings`` (chat/routes.py) does
# ``SettingsManager(db_session=db).get_all_settings(bypass_cache=True)``
# with ``SettingsManager`` and ``get_user_db_session`` bound as
# module-level names on ``chat.routes``. If any earlier test in an
# xdist worker leaks a MagicMock onto either name (or onto
# ``_load_settings`` itself) and fails to restore it, a later chat test's
# ``settings_snapshot`` becomes a MagicMock -- and the #5549 write
# ``UserActiveResearch(..., settings_snapshot=<MagicMock>)`` then fails to
# JSON-serialize on commit, surfacing as a spurious HTTP 500 instead of the
# expected status. Capture the GENUINE objects from their source modules
# (never re-read from ``chat.routes`` at heal time, which could itself hold
# a leaked mock) so the heal below can rebind them.
from local_deep_research.settings.manager import (
SettingsManager as _REAL_SETTINGS_MANAGER,
)
from local_deep_research.database.session_context import (
get_user_db_session as _REAL_GET_USER_DB_SESSION,
)
# ``web.routers.chat`` (main's ``chat/routes.py``) binds process-global names
# (``ChatService``, ``SettingsManager``, ``get_user_db_session``,
# ``_load_settings``) that a mock leaked by an earlier test can poison, so the
# heal fixture rebinds them around every chat test.
#
# Exactly ONE name is listed. This used to also carry the
# ``src.local_deep_research`` alias, on the theory that healing both objects
# was the safer choice under the CI import layout. It was the opposite:
# ``_capture_real_load_settings`` calls ``importlib.import_module`` over this
# tuple, so listing the alias did not merely tolerate a second module object,
# it CREATED one. main's .pre-commit-hooks/check-no-src-test-imports.py now
# bans that alias outright -- ``security/dns_pinning`` installs a process-wide
# getaddrinfo shim whose identity check fails closed if two copies exist.
_CHAT_ROUTES_ALIASES = ("local_deep_research.web.routers.chat",)
def _capture_real_load_settings():
"""Grab each alias's genuine ``_load_settings`` function at import time.
``_load_settings`` is defined *inside* ``chat.routes``, so each alias
module object carries its own function object. Capturing them now --
before any test body can patch the name -- lets the heal fixture restore
a leaked ``_load_settings`` mock too, not just its ``SettingsManager`` /
``get_user_db_session`` dependencies.
"""
captured = {}
for modname in _CHAT_ROUTES_ALIASES:
try:
mod = importlib.import_module(modname)
except Exception:
continue
fn = getattr(mod, "_load_settings", None)
if fn is not None:
captured[modname] = fn
return captured
_REAL_LOAD_SETTINGS = _capture_real_load_settings()
@pytest.fixture(autouse=True)
def _stub_request_path_llm():
"""Keep the send-message request path from making real LLM calls.
build_research_context() condenses prior turns into a query-focused
summary via get_llm() before research is dispatched. Most chat tests
don't exercise that summary (they mock the research worker), so stub the
LLM to keep them fast and deterministic regardless of provider
availability. Tests that assert on the summary patch get_llm themselves,
which overrides this stub within their own context.
"""
fake_llm = MagicMock()
fake_llm.invoke.return_value = MagicMock(
content="stub conversation summary"
)
with patch(
"local_deep_research.config.llm_config.get_llm",
return_value=fake_llm,
):
yield
@pytest.fixture(autouse=True)
def _restore_chat_routes_service():
"""Force-rebind real chat.routes globals around every chat test.
Some tests patch process-global ``chat.routes`` names -- ``ChatService``
(or the underlying ``chat.service.ChatService`` the route binds from), and
the settings-read path ``SettingsManager`` / ``get_user_db_session`` /
``_load_settings`` -- with a MagicMock (e.g. to simulate a service failure
or an inflated stored setting). Under pytest-xdist a binding leaked by an
earlier test (in this OR another directory) can bleed into a later chat
test running in the same worker, so the leaked mock's side effects fire on
an unrelated request; the resulting error is swallowed by the route's
exception handler and surfaces as a spurious HTTP 500 instead of the
expected status. For the settings path specifically, a leaked
``settings_snapshot`` MagicMock fails to JSON-serialize when
``send_message`` commits ``UserActiveResearch(..., settings_snapshot=...)``
(#5549), turning an expected 200/429 into a generic 500.
Two subtleties this heals that a single-module rebind missed:
* **Module duplication.** ``chat.routes`` is loaded under two distinct
module objects (see ``_CHAT_ROUTES_ALIASES``), each with its own
attributes. Rebind whichever aliases are loaded.
* **A poisoned source module.** Re-reading ``ChatService`` /
``SettingsManager`` / ``get_user_db_session`` at heal time would re-bind a
*leaked mock* if that module is the one poisoned. Rebind the genuine
objects captured at import time (``_REAL_CHAT_SERVICE`` et al.) instead.
Rebinding before AND after each test heals any leak inflicted by an earlier
test (pre-yield) and prevents this test from leaking to the next
(post-yield). Tests that establish their own ``with patch(...)`` inside the
test body still override these bindings for their own duration (the heal
runs strictly before and after the test body, never during it).
"""
def _heal():
for modname in _CHAT_ROUTES_ALIASES:
mod = sys.modules.get(modname)
if mod is None:
continue
mod.ChatService = _REAL_CHAT_SERVICE
mod.SettingsManager = _REAL_SETTINGS_MANAGER
mod.get_user_db_session = _REAL_GET_USER_DB_SESSION
real_load_settings = _REAL_LOAD_SETTINGS.get(modname)
if real_load_settings is not None:
mod._load_settings = real_load_settings
_heal()
try:
yield
finally:
_heal()
def generate_unique_test_username():
"""Generate a unique test username to avoid conflicts in parallel tests."""
return f"test_user_{uuid.uuid4().hex[:8]}"
def setup_query_mock_with_session(db_session_mock, session_obj):
"""
Set up query mock to return session for the call patterns used by ChatService:
- filter_by().first() — get_session, archive, delete, etc.
- filter_by().with_for_update().first() — update_accumulated_context
- execute(update(...).returning(...)).scalar_one_or_none() — add_message
(atomic increment-and-return; mutates session_obj.message_count to
mirror the SQL UPDATE side effect so callers can assert on it).
Args:
db_session_mock: The mock database session
session_obj: The mock session object to return (or None for not found)
"""
query_mock = MagicMock()
filter_result = MagicMock()
# Set up both patterns to return the same session
filter_result.first.return_value = session_obj
filter_result.with_for_update.return_value.first.return_value = session_obj
query_mock.filter_by.return_value = filter_result
db_session_mock.query.return_value = query_mock
def _execute_side_effect(_stmt, *args, **kwargs):
result = MagicMock()
if session_obj is None:
result.scalar_one_or_none.return_value = None
else:
current = session_obj.message_count or 0
# These mocks model a session without deleted messages. Real
# sequence gaps are exercised by the service contract tests.
query_mock.filter.return_value.scalar.return_value = current
session_obj.message_count = current + 1
result.scalar_one_or_none.return_value = session_obj.message_count
return result
db_session_mock.execute.side_effect = _execute_side_effect
return query_mock
@pytest.fixture
def mock_user_db_session():
"""Mock the database session context manager."""
session_mock = MagicMock()
@contextmanager
def _mock_session(username, password=None):
yield session_mock
with patch(
"local_deep_research.chat.service.get_user_db_session",
side_effect=_mock_session,
):
yield session_mock
@pytest.fixture
def chat_service(setup_database_for_all_tests):
"""Provide ChatService with test database."""
from local_deep_research.chat.service import ChatService
return ChatService(username="test_user")
@pytest.fixture
def csrf_authenticated_client(app_with_csrf, temp_data_dir):
"""Authenticated test client against the CSRF-enforcing app.
Returns ``(client, csrf_token)``. Under FastAPI the ``CSRFMiddleware``
is always active; ``csrf_token`` is the per-session token that mutating
requests must echo back as the ``X-CSRFToken`` header to be accepted.
Unlike the standard ``authenticated_client`` fixture, this client does
NOT auto-attach ``X-CSRFToken`` — the CSRF-enforcement tests control
whether (and which) token is sent, so the "omit the token" cases
actually omit it and exercise the middleware's reject path (403).
"""
import shutil
test_username = generate_unique_test_username()
test_password = "TestPass123"
encrypted_db_dir = temp_data_dir / "encrypted_databases"
if encrypted_db_dir.exists():
try:
shutil.rmtree(encrypted_db_dir)
except Exception:
pass
from tests.conftest import _make_flask_compat_client
client = _make_flask_compat_client(app_with_csrf)
# Unique X-Forwarded-For so this client doesn't share the slowapi
# rate-limit bucket with other clients in the same test.
import uuid as _uuid
client.headers.update(
{
"X-Forwarded-For": (
f"10.{_uuid.uuid4().int % 254 + 1}."
f"{_uuid.uuid4().int % 254 + 1}.1"
)
}
)
def _csrf():
# Stamp the session with a CSRF token, then read it back as a string
# — register/login are not CSRF-exempt, so both forms need a token.
client.get("/auth/login")
return client.get("/auth/csrf-token").json()["csrf_token"]
register_resp = client.post(
"/auth/register",
data={
"username": test_username,
"password": test_password,
"confirm_password": test_password,
"acknowledge": "true",
"csrf_token": _csrf(),
},
follow_redirects=False,
)
if register_resp.status_code not in [200, 302]:
raise RuntimeError(
f"CSRF-fixture registration failed: {register_resp.status_code} "
f"{register_resp.text[:300]}"
)
login_resp = client.post(
"/auth/login",
data={
"username": test_username,
"password": test_password,
"csrf_token": _csrf(),
},
follow_redirects=False,
)
if login_resp.status_code not in [200, 302]:
raise RuntimeError(
f"CSRF-fixture login failed: {login_resp.status_code} "
f"{login_resp.text[:300]}"
)
# Post-login session token that mutating chat-API requests must echo
# back as X-CSRFToken. (Login clears the session, so fetch it AFTER.)
api_token = client.get("/auth/csrf-token").json()["csrf_token"]
if not api_token:
raise RuntimeError("CSRF-fixture: empty post-login csrf token")
return client, api_token
@pytest.fixture
def sample_messages():
"""Sample message list for context manager tests."""
return [
{
"id": "msg-001",
"role": "user",
"content": "What is quantum computing?",
"message_type": "query",
"created_at": datetime(2024, 1, 15, 10, 0, 0),
},
{
"id": "msg-002",
"role": "assistant",
"content": "Quantum computing uses quantum mechanics principles like superposition and entanglement to process information. Unlike classical computers that use bits (0 or 1), quantum computers use qubits that can exist in multiple states simultaneously.",
"message_type": "response",
"research_id": "research-abc123",
"created_at": datetime(2024, 1, 15, 10, 1, 0),
},
{
"id": "msg-003",
"role": "user",
"content": "What are the practical applications?",
"message_type": "query",
"created_at": datetime(2024, 1, 15, 10, 2, 0),
},
{
"id": "msg-004",
"role": "assistant",
"content": "Practical applications include cryptography, drug discovery, financial modeling, and optimization problems.",
"message_type": "response",
"research_id": "research-def456",
"created_at": datetime(2024, 1, 15, 10, 3, 0),
},
]
@pytest.fixture
def sample_accumulated_context():
"""Sample accumulated context."""
return {
"key_entities": [
"quantum computing",
"qubits",
"superposition",
"entanglement",
],
"topics": ["physics", "computing", "cryptography"],
"summary": "Discussion about quantum computing fundamentals and applications.",
}
@pytest.fixture
def mock_chat_session():
"""Create a mock ChatSession object."""
session = MagicMock()
session.id = "session-test-123"
session.title = "Test Chat Session"
session.status = "active"
session.message_count = 0
session.accumulated_context = {}
session.created_at = datetime(2024, 1, 15, 10, 0, 0)
session.updated_at = datetime(2024, 1, 15, 10, 0, 0).isoformat()
return session
@pytest.fixture
def mock_settings_manager():
"""Mock the SettingsManager."""
manager_mock = MagicMock()
manager_mock.get_all_settings.return_value = {
"llm.provider": {"value": "ollama"},
"llm.model": {"value": "gemma:latest"},
"search.tool": {"value": "searxng"},
"search.iterations": {"value": 2},
"search.questions_per_iteration": {"value": 3},
}
return manager_mock
@pytest.fixture
def long_query_text():
"""A query text longer than 100 characters for title truncation tests."""
return (
"Can you explain the fundamental principles of quantum computing and how "
"it differs from classical computing, including practical applications "
"in cryptography, drug discovery, and financial modeling?"
)
@pytest.fixture
def many_messages():
"""Generate more than 10 messages for limit testing."""
messages = []
for i in range(15):
messages.append(
{
"id": f"msg-{i:03d}",
"role": "user" if i % 2 == 0 else "assistant",
"content": f"Message content {i}",
"message_type": "query" if i % 2 == 0 else "response",
"research_id": f"research-{i:03d}" if i % 2 == 1 else None,
"created_at": datetime(2024, 1, 15, 10, i, 0),
}
)
return messages
# =============================================================================
# Security Test Fixtures
# =============================================================================
@pytest.fixture
def second_user_client(app, temp_data_dir):
"""
Create a second authenticated test client for cross-user tests.
This creates a separate user to test user isolation and
cross-user access prevention.
"""
# Create unique test username
test_username = generate_unique_test_username()
test_password = "testpassword456"
# Create a test client (FastAPI shim or legacy Flask). Mirrors the root
# ``authenticated_client`` fixture so the second user registers + logs in
# through the same CSRF + rate-limit-bucket flow.
if hasattr(app, "test_client"):
client = app.test_client()
_flask = True
else:
from tests.conftest import _make_flask_compat_client
client = _make_flask_compat_client(app)
_flask = False
# Unique X-Forwarded-For so this client doesn't share the slowapi
# rate-limit bucket with the first user's client (the per-IP
# /auth/register rate limit could otherwise 429 the second registration).
import uuid as _uuid
_fwd_ip = (
f"10.{_uuid.uuid4().int % 254 + 1}.{_uuid.uuid4().int % 254 + 1}.1"
)
client.headers.update({"X-Forwarded-For": _fwd_ip})
def _csrf():
# register/login are no longer CSRF-exempt — both forms must POST a
# real token (empty string for the Flask client, which bypasses CSRF).
client.get("/auth/login")
return client.get("/auth/csrf-token").json()["csrf_token"]
# Register and login the second user
with client:
register_response = client.post(
"/auth/register",
data={
"username": test_username,
"password": test_password,
"confirm_password": test_password,
"acknowledge": "true",
"csrf_token": _csrf() if not _flask else "",
},
follow_redirects=False,
)
if register_response.status_code not in [200, 302]:
raise Exception(
f"Second user registration failed: {register_response.status_code}"
)
login_response = client.post(
"/auth/login",
data={
"username": test_username,
"password": test_password,
"csrf_token": _csrf() if not _flask else "",
},
follow_redirects=False,
)
if login_response.status_code not in [200, 302]:
raise Exception(
f"Second user login failed: {login_response.status_code}"
)
# Attach the session CSRF token as a default header so subsequent
# state-changing requests pass the fail-closed CSRFMiddleware.
if not _flask:
try:
tok = client.get("/auth/csrf-token").json().get("csrf_token")
if tok:
client.headers.update({"X-CSRFToken": tok})
except Exception:
pass
return client
# =============================================================================
# XSS Test Fixtures
# =============================================================================
@pytest.fixture
def xss_payloads():
"""Common XSS attack vectors for testing."""
return {
"script_tags": [
"<script>alert('XSS')</script>",
"<script>alert(document.cookie)</script>",
"<SCRIPT>alert('XSS')</SCRIPT>",
"<script src='http://evil.com/xss.js'></script>",
],
"event_handlers": [
"<img src=x onerror=alert('XSS')>",
"<svg onload=alert('XSS')>",
"<body onload=alert('XSS')>",
"<div onmouseover=alert('XSS')>hover</div>",
],
"javascript_urls": [
"<a href='javascript:alert(1)'>Click</a>",
"<iframe src='javascript:alert(1)'></iframe>",
],
"nested": [
"<<script>script>alert('XSS')<</script>/script>",
"<scr<script>ipt>alert('XSS')</scr</script>ipt>",
],
"encoded": [
"<script>alert('XSS')</script>",
"%3Cscript%3Ealert('XSS')%3C/script%3E",
],
}
@pytest.fixture
def malicious_session_ids():
"""Session ID values that could be used in attacks."""
return [
# Path traversal
"../../../etc/passwd",
"..%2F..%2Fetc%2Fpasswd",
"session-id/../../admin",
# Null bytes
"valid-id%00.txt",
"test\x00admin",
# SQL injection
"' OR '1'='1",
"'; DROP TABLE chat_sessions; --",
"1 UNION SELECT * FROM users--",
# XSS in IDs
"<script>alert(1)</script>",
"session-<img onerror=alert(1)>",
]
# =============================================================================
# Fuzz Test Fixtures
# =============================================================================
@pytest.fixture
def random_unicode_strings():
"""Collection of Unicode strings for fuzz testing."""
return [
"", # Empty
" ", # Whitespace
"\t\n\r", # Control chars
"Hello, World!", # Normal ASCII
"Привет мир", # Cyrillic
"你好世界", # Chinese
"こんにちは", # Japanese
"مرحبا", # Arabic
"🎉🔥💯", # Emoji
"a" * 10000, # Very long
"\x00\x01\x02", # Null and control
"line1\nline2\rline3", # Newlines
"'\"<>&", # HTML special chars
]
@pytest.fixture
def boundary_values():
"""Boundary values for testing limits."""
return {
"message_lengths": [0, 1, 99, 100, 101, 9999, 10000, 10001],
"title_lengths": [0, 1, 499, 500, 501],
"pagination_limits": [-1, 0, 1, 20, 100, 101, 1000],
"pagination_offsets": [-1, 0, 1, 100, 10000],
}