refactor(routes): replace user variable with auth context in tests

This commit is contained in:
Anish Sarkar 2026-06-20 03:34:40 +05:30
parent af5a112212
commit 1e8baa10ec
5 changed files with 34 additions and 36 deletions

View file

@ -23,6 +23,7 @@ import pytest
from fastapi import HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from app.auth.context import AuthContext
from app.db import (
SearchSourceConnector,
SearchSourceConnectorType,
@ -109,7 +110,7 @@ class TestConnectorIndexCrossSpaceAuthz:
connector_id=connector_a.id,
search_space_id=space_b.id, # the attacker's own space
session=db_session,
user=attacker,
auth=AuthContext.session(attacker),
)
assert exc_info.value.status_code == 404
@ -140,7 +141,7 @@ class TestConnectorIndexCrossSpaceAuthz:
connector_id=connector.id,
search_space_id=space.id, # the connector's own space
session=db_session,
user=owner,
auth=AuthContext.session(owner),
)
check_permission_mock.assert_awaited_once()

View file

@ -38,7 +38,9 @@ async def cleanup_supervisors():
@pytest.mark.asyncio
async def test_start_byo_long_poll_noops_when_mode_is_webhook(monkeypatch):
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_ENABLED", True)
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_TELEGRAM_INTAKE_MODE", "webhook")
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_WHATSAPP_INTAKE_MODE", "disabled")
await byo_long_poll.start_byo_long_poll_supervisors()
@ -47,9 +49,11 @@ async def test_start_byo_long_poll_noops_when_mode_is_webhook(monkeypatch):
@pytest.mark.asyncio
async def test_start_byo_long_poll_noops_when_no_byo_accounts(mocker, monkeypatch):
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_ENABLED", True)
monkeypatch.setattr(
byo_long_poll.config, "GATEWAY_TELEGRAM_INTAKE_MODE", "longpoll"
)
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_WHATSAPP_INTAKE_MODE", "disabled")
session = mocker.AsyncMock()
session.execute.return_value = ScalarResult([])
monkeypatch.setattr(
@ -67,9 +71,11 @@ async def test_start_byo_long_poll_noops_when_no_byo_accounts(mocker, monkeypatc
async def test_start_byo_long_poll_spawns_one_supervisor_per_account(
mocker, monkeypatch
):
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_ENABLED", True)
monkeypatch.setattr(
byo_long_poll.config, "GATEWAY_TELEGRAM_INTAKE_MODE", "longpoll"
)
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_WHATSAPP_INTAKE_MODE", "disabled")
accounts = [mocker.Mock(id=1), mocker.Mock(id=2)]
session = mocker.AsyncMock()
session.execute.return_value = ScalarResult(accounts)
@ -115,9 +121,11 @@ async def test_supervisor_retries_after_run_returns(mocker, monkeypatch):
@pytest.mark.asyncio
async def test_shutdown_cancels_running_supervisors(mocker, monkeypatch):
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_ENABLED", True)
monkeypatch.setattr(
byo_long_poll.config, "GATEWAY_TELEGRAM_INTAKE_MODE", "longpoll"
)
monkeypatch.setattr(byo_long_poll.config, "GATEWAY_WHATSAPP_INTAKE_MODE", "disabled")
session = mocker.AsyncMock()
session.execute.return_value = ScalarResult([mocker.Mock(id=1)])
monkeypatch.setattr(

View file

@ -27,6 +27,7 @@ async def test_inbox_worker_claims_and_processes_in_fastapi_process(
async def test_start_stop_gateway_inbox_worker(mocker, monkeypatch):
started = asyncio.Event()
stopped = asyncio.Event()
monkeypatch.setattr(inbox_worker.config, "GATEWAY_ENABLED", True)
async def run_forever():
started.set()

View file

@ -19,6 +19,7 @@ from unittest.mock import AsyncMock, patch
import pytest
from app.agents.chat.multi_agent_chat.shared.feature_flags import AgentFeatureFlags
from app.auth.context import AuthContext
from app.routes import agent_revert_route
from app.services.revert_service import RevertOutcome
@ -147,7 +148,7 @@ class TestFlagGuard:
thread_id=1,
chat_turn_id="42:1700000000000",
session=session,
user=_FakeUser(),
auth=AuthContext.session(_FakeUser()),
)
assert getattr(exc.value, "status_code", None) == 503
@ -167,7 +168,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-empty",
session=session,
user=_FakeUser(),
auth=AuthContext.session(_FakeUser()),
)
assert response.status == "ok"
assert response.total == 0
@ -209,7 +210,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-3",
session=session,
user=_FakeUser(),
auth=AuthContext.session(_FakeUser()),
)
assert response.status == "ok"
@ -248,7 +249,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-i",
session=session,
user=_FakeUser(),
auth=AuthContext.session(_FakeUser()),
)
assert response.status == "ok"
assert response.already_reverted == 1
@ -275,7 +276,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-rev",
session=session,
user=_FakeUser(),
auth=AuthContext.session(_FakeUser()),
)
assert response.status == "ok"
assert response.results[0].status == "skipped"
@ -315,7 +316,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-mix",
session=session,
user=_FakeUser(),
auth=AuthContext.session(_FakeUser()),
)
assert response.status == "partial"
assert response.reverted == 1
@ -354,7 +355,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-fail",
session=session,
user=_FakeUser(),
auth=AuthContext.session(_FakeUser()),
)
assert response.status == "partial"
assert response.failed == 1
@ -386,7 +387,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-perm",
session=session,
user=_FakeUser(id="not-owner"),
auth=AuthContext.session(_FakeUser(id="not-owner")),
)
assert response.status == "partial"
assert response.results[0].status == "permission_denied"
@ -449,7 +450,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-mixed-all",
session=session,
user=_FakeUser(), # only id=7 has a different user_id
auth=AuthContext.session(_FakeUser()), # only id=7 has a different user_id
)
assert response.total == len(rows) == 6
@ -518,7 +519,7 @@ class TestRevertTurnDispatch:
thread_id=1,
chat_turn_id="ct-race",
session=session,
user=_FakeUser(),
auth=AuthContext.session(_FakeUser()),
)
assert response.failed == 0, (