Files
ZY-Agent/backend/tests/test_threads_post_workspace.py
T
1445043649 2461a923be feat(gateway): dual-mount legacy routers on /api and /api/v1 (Stage 1 PR5)
Strip /api prefix from 13 legacy router APIRouter() declarations and dual-mount
each on prefix="/api" (backward compat) and prefix="/api/v1" (versioned surface)
in app.py. Auth, service-accounts, api-keys, assistants-compat remain single-mount.
Update 10 test files to pass prefix="/api" when directly including stripped routers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-28 19:49:05 +08:00

60 lines
2.2 KiB
Python

"""PR6 T6.7 — POST /api/threads writes workspace_id from contextvar.
The `routers/threads.py:create_thread` path delegates to
``ThreadMetaStore.create`` *without* an explicit ``workspace_id`` — it
relies on the AUTO sentinel pulling the value from the active workspace
contextvar that AuthMiddleware (or the test stub) sets. This test
covers the integration through the FastAPI TestClient stack: post a
thread under workspace A, then verify the persisted record carries
``workspace_id="ws-alpha"``.
"""
from __future__ import annotations
from collections.abc import Callable
from _router_auth_helpers import make_authed_test_app
from fastapi.testclient import TestClient
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.store.memory import InMemoryStore
from app.gateway.auth.models import ActiveWorkspace
from app.gateway.routers import threads
from deerflow.persistence.thread_meta.memory import MemoryThreadMetaStore
def _workspace_factory(wid: str) -> Callable[[], ActiveWorkspace]:
def _factory() -> ActiveWorkspace:
return ActiveWorkspace(id=wid, role="owner")
return _factory
def _build_app(workspace_id: str):
app = make_authed_test_app(workspace_factory=_workspace_factory(workspace_id))
store = InMemoryStore()
checkpointer = InMemorySaver()
app.state.store = store
app.state.checkpointer = checkpointer
app.state.thread_store = MemoryThreadMetaStore(store)
app.include_router(threads.router, prefix="/api")
return app, store
def test_post_thread_stamps_workspace_id_from_contextvar():
app, store = _build_app("ws-alpha")
with TestClient(app) as client:
response = client.post("/api/threads", json={"thread_id": "t1", "metadata": {}})
assert response.status_code == 200, response.text
item = store.get(("threads",), "t1")
assert item is not None
assert item.value["workspace_id"] == "ws-alpha"
def test_post_thread_under_different_workspace():
app, store = _build_app("ws-beta")
with TestClient(app) as client:
client.post("/api/threads", json={"thread_id": "t2", "metadata": {}})
assert store.get(("threads",), "t2").value["workspace_id"] == "ws-beta"