~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
# allow: no-sut-import — black-box HTTP test; drives the real routers through
# the ASGI test client with real users and real per-user encrypted databases.
"""Cross-user IDOR regression suite for the Notes, Chat, and Follow-up-Research
HTTP APIs.
Same bug class as the fixed cross-user issues (cached-connection auth bypass
#5596, benchmark active_runs cross-user leak, research cancel_research missing
ownership check): an authenticated user must never be able to read, list,
create-on, or mutate ANOTHER user's objects by supplying that object's id.
Architecture recap (see notes/chat/followup route + service source):
* Every user has their OWN encrypted SQLCipher database, opened via
``get_user_db_session(username)`` where ``username`` always comes from
the authenticated session (never from client-supplied request data) in
every route touched below.
* NoteService(username), ChatService(username), and
FollowUpResearchService(username=username) all resolve *every* query
through that per-user database, so an id belonging to another user's
database simply does not exist in the caller's database.
* The invariant under test: supplying another user's object id (note_id,
chat session_id, research_id) must 404 / be excluded from listings /
fail to attach, never return or mutate that user's data.
Because the databases are physically separate, a foreign id is ABSENT rather
than refused — 404 is the expected shape. A bare 404 assertion would be
vacuous (an unrouted path 404s too), so every negative below additionally
pins the *reason*: the JSON body must be the router's own
``{"success": false, "error": "... not found"}`` shape, which FastAPI's
"no such route" 404 (``{"detail": "Not Found"}``) never produces. And every
negative has an OWNER-side control through the identical call, proving the
harness can observe the opposite outcome.
PORT NOTE (Flask -> FastAPI)
---------------------------
Upstream (fa466ad13, #5600) built its app with ``web.app_factory.create_app``
and Flask test clients, with WTF-CSRF switched off in app config. That
factory no longer exists on this branch. The fixtures below were rewritten to
drive the real FastAPI app (``web.fastapi_app.app``) through Starlette's
``TestClient`` (via the repo's ``_make_flask_compat_client`` shim, so the
response accessors used in the assertions keep working verbatim). Two
substantive harness differences:
* CSRF cannot be turned off — ``CSRFMiddleware`` is added unconditionally
in ``fastapi_app.py`` and runs BEFORE auth, so every state-changing
request must carry an ``X-CSRFToken`` header or it 403s (CSRF) instead
of reaching the route. Each user's client therefore pins its token as a
default header after login.
* ``/auth/register`` is rate-limited per client key, so each user gets a
unique ``X-Forwarded-For`` bucket (same idiom as
``tests/chat/test_chat_followup_contracts.py::_new_user``).
Every test's URL, payload and assertion semantics are unchanged from
upstream; the only assertion edits are the added owner-side controls and
404-reason checks called out above (and the ``/api/followup/start`` case
documented on that test).
"""
import os
# Rate limiting is read once at import time in
# ``web/dependencies/rate_limit.py``; disable it before the app (and that
# module) is imported. The chat routes are capped at 10-20/minute per user,
# which the seeding in ``two_users`` would otherwise brush against.
os.environ.setdefault("LDR_DISABLE_RATE_LIMITING", "true")
import uuid # noqa: E402
from contextlib import contextmanager # noqa: E402
from datetime import datetime, timezone # noqa: E402
from unittest.mock import patch # noqa: E402
import pytest # noqa: E402
def _unique_forwarded_ip() -> str:
"""A distinct client key per user so the per-IP /auth/register cap
doesn't reject the second registration."""
n = uuid.uuid4().int
return f"10.{n % 250 + 1}.{(n >> 8) % 250 + 1}.{(n >> 16) % 250 + 1}"
# ---------------------------------------------------------------------------
# App fixture (real FastAPI app + real singleton db_manager, isolated data dir)
# ---------------------------------------------------------------------------
@pytest.fixture
def app_client(tmp_path, monkeypatch):
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "true")
monkeypatch.setenv("LDR_DISABLE_RATE_LIMITING", "true")
monkeypatch.setenv("LDR_RATE_LIMITING_ENABLED", "false")
monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
from local_deep_research.database.auth_db import init_auth_database
from local_deep_research.database.encrypted_db import db_manager
from local_deep_research.web.fastapi_app import app
# PORT: the Flask auth blueprint moved to web/routers/auth.py and
# _perform_post_login_tasks grew a session_id parameter.
import local_deep_research.web.routers.auth as auth_routes
if not db_manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
original_data_dir = db_manager.data_dir
try:
db_manager.data_dir = tmp_path / "encrypted_databases"
init_auth_database()
# Keep the synchronous test off the real post-login worker threads.
monkeypatch.setattr(
auth_routes,
"_perform_post_login_tasks",
lambda _u, _p, _session_id=None: None,
)
yield app, db_manager
finally:
try:
from local_deep_research.security.account_lockout import (
get_account_lockout_manager,
)
get_account_lockout_manager()._state.clear()
except Exception:
pass
db_manager.close_all_databases()
db_manager.data_dir = original_data_dir
# ---------------------------------------------------------------------------
# Registration / login / seeding helpers
# ---------------------------------------------------------------------------
def _csrf(client):
"""Mint (or fetch) this client's session CSRF token.
PORT: FastAPI's CSRFMiddleware is always on and runs before auth, so
every mutating request in this file needs a token.
"""
client.get("/auth/login")
resp = client.get("/auth/csrf-token")
assert resp.status_code == 200, resp.get_data(as_text=True)
return resp.get_json()["csrf_token"]
def _register(client, username, password):
return client.post(
"/auth/register",
data={
"username": username,
"password": password,
"confirm_password": password,
"acknowledge": "true",
"csrf_token": _csrf(client),
},
follow_redirects=False,
)
def _login(client, username, password):
return client.post(
"/auth/login",
data={
"username": username,
"password": password,
"csrf_token": _csrf(client),
},
follow_redirects=False,
)
def _assert_ownership_404(resp, why, expected_error_fragment="not found"):
"""A 404 that is about OWNERSHIP, not about a missing route.
``why`` is the upstream invariant message, reported verbatim on failure.
An unmatched FastAPI path also returns 404 (``{"detail": "Not Found"}``),
which would make every negative in this file pass vacuously if the route
were ever renamed away. Pin the router's own error body instead.
"""
assert resp.status_code == 404, (
f"{why} (got {resp.status_code}: {resp.get_data(as_text=True)})"
)
body = resp.get_json()
assert isinstance(body, dict) and body.get("success") is False, (
f"{why} -- but this 404 did not come from the route handler "
f"(no {{success: false}} body), so it proves nothing: "
f"{resp.get_data(as_text=True)}"
)
assert (
expected_error_fragment.lower() in str(body.get("error", "")).lower()
), (
f"{why} -- the 404 reason is not about the missing/foreign "
f"object: {body!r}"
)
class _User:
"""A registered + logged-in user with a live authenticated test client."""
def __init__(self, app, tag):
from tests.conftest import _make_flask_compat_client
self.username = f"{tag}_{uuid.uuid4().hex[:10]}"
# Guaranteed upper/lower/digit/symbol so the password policy can't
# reject a hex suffix that happens to contain no digits.
self.password = f"P@ss1-{tag}-{uuid.uuid4().hex[:8]}"
self.client = _make_flask_compat_client(app)
self.client.headers.update({"X-Forwarded-For": _unique_forwarded_ip()})
reg = _register(self.client, self.username, self.password)
assert reg.status_code in (200, 302), (
f"registration failed for {self.username}: {reg.status_code} "
f"{reg.get_data(as_text=True)[:300]}"
)
login = _login(self.client, self.username, self.password)
assert login.status_code in (200, 302), (
f"login failed for {self.username}: {login.status_code} "
f"{login.get_data(as_text=True)[:300]}"
)
# Pin the post-login CSRF token for every later mutating request.
self.client.headers.update({"X-CSRFToken": _csrf(self.client)})
def seed_llm_model_setting(self, model_name="test-model"):
"""Give this user a non-empty llm.model setting so
/api/followup/start's pre-flight model check doesn't 400 before
reaching the code path under test."""
from local_deep_research.database.session_context import (
get_user_db_session,
)
from local_deep_research.database.models.settings import (
Setting,
SettingType,
)
with get_user_db_session(self.username, self.password) as s:
existing = (
s.query(Setting).filter(Setting.key == "llm.model").first()
)
if existing:
existing.value = model_name
else:
s.add(
Setting(
key="llm.model",
value=model_name,
type=SettingType.LLM,
name="llm.model",
ui_element="text",
category="llm",
visible=True,
editable=True,
)
)
s.commit()
def seed_research(
self, research_id, query, title, report="body", chat_session_id=None
):
"""Directly write a completed ResearchHistory row into THIS user's
own encrypted DB (bypasses the real research pipeline; used as the
"parent"/"linked" research object for notes/followup tests)."""
from local_deep_research.database.session_context import (
get_user_db_session,
)
from local_deep_research.database.models import ResearchHistory
now = datetime.now(timezone.utc).isoformat()
with get_user_db_session(self.username, self.password) as s:
s.add(
ResearchHistory(
id=research_id,
query=query,
mode="quick_summary",
status="completed",
created_at=now,
completed_at=now,
duration_seconds=1,
progress=100,
title=title,
report_content=report,
chat_session_id=chat_session_id,
)
)
s.commit()
# --- Notes, via the real API ---
def create_note(self, title, content):
resp = self.client.post(
"/notes/api/notes",
json={"title": title, "content": content},
)
assert resp.status_code == 201, resp.get_data(as_text=True)
return resp.get_json()["id"]
# --- Chat, via the real API ---
def create_chat_session(self, title):
resp = self.client.post(
"/api/chat/sessions",
json={"title": title},
)
assert resp.status_code == 200, resp.get_data(as_text=True)
return resp.get_json()["session_id"]
def send_chat_message(self, session_id, content):
"""Send a message WITHOUT triggering research (keeps the test
hermetic — no search/LLM stack needed)."""
resp = self.client.post(
f"/api/chat/sessions/{session_id}/messages",
json={"content": content, "trigger_research": False},
)
assert resp.status_code == 200, resp.get_data(as_text=True)
return resp.get_json()["message_id"]
@pytest.fixture
def two_users(app_client):
"""Two fully-provisioned users A and B, each with distinguishable
notes, chat sessions/messages, and a seeded "completed" research run."""
app, db_manager = app_client
a = _User(app, "alice")
b = _User(app, "bob")
a.marker = f"ALICE_MARKER_{uuid.uuid4().hex[:8]}"
b.marker = f"BOB_MARKER_{uuid.uuid4().hex[:8]}"
# Research (used both standalone and as the notes "linked research" /
# followup "parent research" target).
a.research_id = str(uuid.uuid4())
b.research_id = str(uuid.uuid4())
a.seed_research(a.research_id, f"query {a.marker}", f"title {a.marker}")
b.seed_research(b.research_id, f"query {b.marker}", f"title {b.marker}")
# Needed so /api/followup/start's pre-flight "llm.model configured"
# check doesn't 400 before reaching the ownership-scoping code path.
a.seed_llm_model_setting()
b.seed_llm_model_setting()
# Notes.
a.note_id = a.create_note(f"Note {a.marker}", f"content {a.marker}")
b.note_id = b.create_note(f"Note {b.marker}", f"content {b.marker}")
# Chat sessions + one message each.
a.session_id = a.create_chat_session(f"Chat {a.marker}")
b.session_id = b.create_chat_session(f"Chat {b.marker}")
a.message_id = a.send_chat_message(a.session_id, f"hello {a.marker}")
b.message_id = b.send_chat_message(b.session_id, f"hello {b.marker}")
return app, a, b
@contextmanager
def _no_spawn():
"""Patch out the actual research-thread spawn so /api/followup/start can
be exercised without a real search/LLM stack. We only care whether the
route/service layer rejects (or accepts) a cross-user parent_research_id
BEFORE any thread would be spawned.
``_start_followup_sync`` imports ``start_research_process`` INSIDE the
function body (``from ..services.research_service import
start_research_process, ...``), so the name to patch is the real
definition site, not an attribute of the router module.
"""
with patch(
"local_deep_research.web.services.research_service.start_research_process"
) as mock_start:
mock_start.return_value = None
yield mock_start
# ===========================================================================
# Notes router (/notes/api/...)
# ===========================================================================
def test_notes_list_scoped_to_session_user(two_users):
app, a, b = two_users
resp = a.client.get("/notes/api/notes")
assert resp.status_code == 200
body = resp.get_data(as_text=True)
assert a.marker in body, "A must see its own notes"
assert b.marker not in body, "A must NOT see B's notes in the list"
def test_notes_get_idor_blocked(two_users):
app, a, b = two_users
ok = a.client.get(f"/notes/api/notes/{a.note_id}")
assert ok.status_code == 200
leaked = a.client.get(f"/notes/api/notes/{b.note_id}")
# PORT: added the reason check — a bare 404 would also be produced by an
# unrouted path, making the assertion vacuous.
_assert_ownership_404(
leaked, "A must not read B's note via /notes/api/notes/<id> IDOR"
)
assert b.marker not in leaked.get_data(as_text=True)
def test_notes_update_idor_blocked(two_users):
app, a, b = two_users
resp = a.client.put(
f"/notes/api/notes/{b.note_id}",
json={"title": "PWNED", "content": "PWNED"},
)
_assert_ownership_404(resp, "A must not be able to update B's note by id")
# B's note must be unchanged.
still_b = b.client.get(f"/notes/api/notes/{b.note_id}")
assert still_b.status_code == 200
assert "PWNED" not in still_b.get_data(as_text=True)
assert b.marker in still_b.get_data(as_text=True)
# OWNER CONTROL: the identical call on A's OWN note succeeds, so the 404
# above is about ownership and not about PUT being broken for everyone.
own = a.client.put(
f"/notes/api/notes/{a.note_id}",
json={"title": "A renamed", "content": f"content {a.marker} v2"},
)
assert own.status_code == 200, own.get_data(as_text=True)
def test_notes_delete_idor_blocked(two_users):
app, a, b = two_users
resp = a.client.delete(f"/notes/api/notes/{b.note_id}")
_assert_ownership_404(resp, "A must not be able to delete B's note by id")
still_b = b.client.get(f"/notes/api/notes/{b.note_id}")
assert still_b.status_code == 200, (
"B's note must survive A's delete attempt"
)
# OWNER CONTROL: A really can delete its own note through this route.
own = a.client.delete(f"/notes/api/notes/{a.note_id}")
assert own.status_code == 200, own.get_data(as_text=True)
assert a.client.get(f"/notes/api/notes/{a.note_id}").status_code == 404
def test_notes_collections_idor_blocked(two_users):
app, a, b = two_users
resp = a.client.get(f"/notes/api/notes/{b.note_id}/collections")
_assert_ownership_404(
resp, "A must not list the collections of B's note by id"
)
# OWNER CONTROL: same route, A's own note id -> 200.
own = a.client.get(f"/notes/api/notes/{a.note_id}/collections")
assert own.status_code == 200, own.get_data(as_text=True)
def test_notes_link_research_no_cross_user_row_persisted(two_users):
"""A cannot end up with a persisted NoteResearch link from A's note to
B's research_id.
POST /notes/api/notes/<note_id>/research with a foreign research_id
answers 404 ("Research run not found"): B's research run does not exist
in A's own database, so the insert violates a foreign key. The engine
loads the SQLCipher DBAPI, so SQLAlchemy wraps the driver's error as
``sqlalchemy.exc.IntegrityError`` and the route's IntegrityError handler
maps it to 404 (#5721). Before that the driver's raw error skipped the
handler and the route answered 500; see
tests/chat/test_chat_followup_contracts.py::TestUserDatabaseErrorWrapping.
No B data is returned and no link is created.
"""
app, a, b = two_users
resp = a.client.post(
f"/notes/api/notes/{a.note_id}/research",
json={"research_id": b.research_id},
)
_assert_ownership_404(
resp,
"A must not be able to link B's research run to A's note",
expected_error_fragment="research run not found",
)
assert b.marker not in resp.get_data(as_text=True)
# No NoteResearch row was persisted linking A's note to B's research,
# from either A's or B's point of view.
linked = a.client.get(f"/notes/api/notes/{a.note_id}/research")
assert linked.status_code == 200
assert b.research_id not in linked.get_data(as_text=True)
# A's own research_id links fine (positive control) -- the endpoint
# works correctly for a same-user, valid research_id.
ok = a.client.post(
f"/notes/api/notes/{a.note_id}/research",
json={"research_id": a.research_id},
)
assert ok.status_code == 201, ok.get_data(as_text=True)
def test_research_notes_panel_idor_blocked(two_users):
"""GET/POST /notes/api/research/<research_id>/notes scoped to caller's
own research."""
app, a, b = two_users
leaked = a.client.get(f"/notes/api/research/{b.research_id}/notes")
_assert_ownership_404(
leaked, "A must not read the notes panel of B's research"
)
created = a.client.post(
f"/notes/api/research/{b.research_id}/notes",
json={"title": "x", "content": "y"},
)
_assert_ownership_404(
created,
"A must not be able to create a note pre-linked to B's research",
)
# OWNER CONTROL: identical GET + POST against A's own research succeed.
own_get = a.client.get(f"/notes/api/research/{a.research_id}/notes")
assert own_get.status_code == 200, own_get.get_data(as_text=True)
own_post = a.client.post(
f"/notes/api/research/{a.research_id}/notes",
json={"title": "x", "content": "y"},
)
assert own_post.status_code == 201, own_post.get_data(as_text=True)
def test_research_save_as_note_idor_blocked(two_users):
app, a, b = two_users
resp = a.client.post(f"/notes/api/research/{b.research_id}/save-as-note")
_assert_ownership_404(
resp, "A must not be able to save B's research report as a note"
)
# OWNER CONTROL: A can save its OWN research report as a note.
own = a.client.post(f"/notes/api/research/{a.research_id}/save-as-note")
assert own.status_code in (200, 201), own.get_data(as_text=True)
def test_research_annotations_idor_blocked(two_users):
app, a, b = two_users
leaked = a.client.get(f"/notes/api/research/{b.research_id}/annotations")
_assert_ownership_404(
leaked, "A must not read the annotations on B's research"
)
created = a.client.post(
f"/notes/api/research/{b.research_id}/annotations",
json={"quote": "q", "comment": "c"},
)
_assert_ownership_404(
created, "A must not be able to annotate B's research report"
)
# OWNER CONTROL: identical GET + POST against A's own research succeed.
own_get = a.client.get(f"/notes/api/research/{a.research_id}/annotations")
assert own_get.status_code == 200, own_get.get_data(as_text=True)
own_post = a.client.post(
f"/notes/api/research/{a.research_id}/annotations",
json={"quote": "q", "comment": "c"},
)
assert own_post.status_code == 201, own_post.get_data(as_text=True)
# ===========================================================================
# Chat router (/api/chat/...)
# ===========================================================================
def test_chat_sessions_list_scoped_to_session_user(two_users):
app, a, b = two_users
resp = a.client.get("/api/chat/sessions")
assert resp.status_code == 200
body = resp.get_data(as_text=True)
assert a.marker in body, "A must see its own chat sessions"
assert b.marker not in body, "A must NOT see B's chat sessions in the list"
def test_chat_get_session_idor_blocked(two_users):
app, a, b = two_users
ok = a.client.get(f"/api/chat/sessions/{a.session_id}")
assert ok.status_code == 200
leaked = a.client.get(f"/api/chat/sessions/{b.session_id}")
_assert_ownership_404(leaked, "A must not read B's chat session via IDOR")
assert b.marker not in leaked.get_data(as_text=True)
def test_chat_get_messages_idor_blocked(two_users):
app, a, b = two_users
ok = a.client.get(f"/api/chat/sessions/{a.session_id}/messages")
assert ok.status_code == 200
leaked = a.client.get(f"/api/chat/sessions/{b.session_id}/messages")
_assert_ownership_404(leaked, "A must not read B's chat messages via IDOR")
assert b.marker not in leaked.get_data(as_text=True)
def test_chat_post_message_idor_blocked(two_users):
"""A cannot post a message ONTO B's chat session by id."""
app, a, b = two_users
resp = a.client.post(
f"/api/chat/sessions/{b.session_id}/messages",
json={"content": "injected", "trigger_research": False},
)
_assert_ownership_404(
resp, "A must not be able to send a message into B's chat session"
)
# B's session must show no trace of A's message.
still_b = b.client.get(f"/api/chat/sessions/{b.session_id}/messages")
assert still_b.status_code == 200
assert "injected" not in still_b.get_data(as_text=True)
# OWNER CONTROL: the identical POST into A's OWN session is accepted and
# persisted, so the 404 above is about ownership.
own = a.client.post(
f"/api/chat/sessions/{a.session_id}/messages",
json={"content": "injected", "trigger_research": False},
)
assert own.status_code == 200, own.get_data(as_text=True)
own_msgs = a.client.get(f"/api/chat/sessions/{a.session_id}/messages")
assert "injected" in own_msgs.get_data(as_text=True)
def test_chat_update_session_idor_blocked(two_users):
app, a, b = two_users
resp = a.client.patch(
f"/api/chat/sessions/{b.session_id}",
json={"title": "PWNED"},
)
_assert_ownership_404(
resp, "A must not be able to rename/archive B's chat session"
)
still_b = b.client.get(f"/api/chat/sessions/{b.session_id}")
assert still_b.status_code == 200
assert "PWNED" not in still_b.get_data(as_text=True)
# OWNER CONTROL: A really can rename its own session with this call.
own = a.client.patch(
f"/api/chat/sessions/{a.session_id}",
json={"title": "A renamed"},
)
assert own.status_code == 200, own.get_data(as_text=True)
assert "A renamed" in a.client.get(
f"/api/chat/sessions/{a.session_id}"
).get_data(as_text=True)
def test_chat_delete_session_idor_blocked(two_users):
app, a, b = two_users
resp = a.client.delete(f"/api/chat/sessions/{b.session_id}")
_assert_ownership_404(resp, "A must not be able to delete B's chat session")
still_b = b.client.get(f"/api/chat/sessions/{b.session_id}")
assert still_b.status_code == 200, (
"B's session must survive A's delete attempt"
)
# OWNER CONTROL: A really can delete its own session with this call.
own = a.client.delete(f"/api/chat/sessions/{a.session_id}")
assert own.status_code == 200, own.get_data(as_text=True)
assert a.client.get(f"/api/chat/sessions/{a.session_id}").status_code == 404
def test_chat_generate_title_idor_blocked(two_users):
app, a, b = two_users
resp = a.client.post(
f"/api/chat/sessions/{b.session_id}/generate-title",
json={"query": "what is this about"},
)
_assert_ownership_404(
resp, "A must not be able to regenerate the title of B's chat session"
)
# OWNER CONTROL: the same call on A's OWN session passes the ownership
# gate and reaches the LLM step. With no model backend configured in the
# test environment ``regenerate_title_with_llm`` returns falsy, which the
# route reports as HTTP 200 ``{"success": false, "title": null}`` -- the
# point is that it is NOT the 404 above, i.e. the gate is ownership.
own = a.client.post(
f"/api/chat/sessions/{a.session_id}/generate-title",
json={"query": "what is this about"},
)
assert own.status_code == 200, own.get_data(as_text=True)
assert "not found" not in own.get_data(as_text=True).lower()
def test_chat_delete_attempt_idor_blocked(two_users):
"""A cannot delete a chat "attempt" (message+research turn) scoped to
B's session, even when supplying A's OWN research_id as the attempt id
(the session_id ownership check must fire first)."""
app, a, b = two_users
resp = a.client.delete(
f"/api/chat/sessions/{b.session_id}/attempts/{a.research_id}"
)
_assert_ownership_404(
resp,
"A must not be able to delete an attempt scoped to B's chat session",
)
# OWNER CONTROL: seed a real completed attempt on A's OWN session and
# delete it through the identical route. Proves the 404 above is the
# ownership/scoping gate and not "this route 404s for everybody".
own_attempt_id = str(uuid.uuid4())
a.seed_research(
own_attempt_id,
f"attempt {a.marker}",
f"attempt title {a.marker}",
chat_session_id=a.session_id,
)
own = a.client.delete(
f"/api/chat/sessions/{a.session_id}/attempts/{own_attempt_id}"
)
assert own.status_code == 200, own.get_data(as_text=True)
# ===========================================================================
# Follow-up research router (/api/followup/...)
# ===========================================================================
def test_followup_prepare_idor_blocked(two_users):
app, a, b = two_users
resp = a.client.post(
"/api/followup/prepare",
json={
"parent_research_id": b.research_id,
"question": "tell me more",
},
)
_assert_ownership_404(
resp,
"A must not be able to prepare a follow-up against B's research",
"parent research not found",
)
assert b.marker not in resp.get_data(as_text=True)
def test_followup_prepare_own_research_succeeds(two_users):
"""Positive control: A can prepare a follow-up against A's own research."""
app, a, b = two_users
resp = a.client.post(
"/api/followup/prepare",
json={
"parent_research_id": a.research_id,
"question": "tell me more",
},
)
assert resp.status_code == 200, resp.get_data(as_text=True)
body = resp.get_json()
assert body["success"] is True
assert body["parent_research"]["id"] == a.research_id
def test_followup_start_idor_blocked(two_users):
"""A must not be able to start a follow-up research "on" B's
research_id. Mirrors /api/followup/prepare's 404 contract for the same
cross-user id.
This is the behaviour change shipped in the same upstream commit as this
file: /api/followup/start used to start a run with an empty parent
context for a foreign parent id. It now refuses with 404 BEFORE any
ResearchHistory row is written or any thread is spawned, which is what
the ``mock_start.called is False`` assertion pins.
"""
app, a, b = two_users
with _no_spawn() as mock_start:
resp = a.client.post(
"/api/followup/start",
json={
"parent_research_id": b.research_id,
"question": "tell me more",
},
)
_assert_ownership_404(
resp,
"A must not be able to start a follow-up referencing B's "
"research_id",
"parent research not found",
)
assert mock_start.called is False, (
"no research thread should ever be spawned for a rejected "
"cross-user follow-up"
)
def test_followup_start_own_research_succeeds(two_users):
app, a, b = two_users
with _no_spawn() as mock_start:
resp = a.client.post(
"/api/followup/start",
json={
"parent_research_id": a.research_id,
"question": "tell me more",
},
)
assert resp.status_code == 200, resp.get_data(as_text=True)
assert resp.get_json()["success"] is True
mock_start.assert_called_once()