refactor(auth): replace user variable with auth context in integration and unit tests

This commit is contained in:
Anish Sarkar 2026-06-20 03:11:00 +05:30
parent 14cb0a22e9
commit af5a112212
9 changed files with 86 additions and 56 deletions

View file

@ -1,9 +1,9 @@
"""Notifications integration fixtures.
The app's DB session and current-user dependencies are overridden to ride the
The app's DB session and auth-context dependencies are overridden to ride the
test's transactional `db_session`, so API calls and seeded rows share one
transaction that rolls back per test. Overriding `current_active_user` also
bypasses real JWT auth, so these tests don't depend on AUTH_TYPE.
transaction that rolls back per test. Overriding `get_auth_context` also bypasses
real JWT auth, so these tests don't depend on AUTH_TYPE.
"""
from __future__ import annotations
@ -17,8 +17,9 @@ from httpx import ASGITransport
from sqlalchemy.ext.asyncio import AsyncSession
from app.app import app, limiter
from app.auth.context import AuthContext
from app.db import User, get_async_session
from app.users import current_active_user
from app.users import get_auth_context
pytestmark = pytest.mark.integration
@ -33,12 +34,12 @@ async def client(
async def override_session() -> AsyncGenerator[AsyncSession, None]:
yield db_session
async def override_user() -> User:
return db_user
async def override_auth() -> AuthContext:
return AuthContext.session(db_user)
previous_overrides = app.dependency_overrides.copy()
app.dependency_overrides[get_async_session] = override_session
app.dependency_overrides[current_active_user] = override_user
app.dependency_overrides[get_auth_context] = override_auth
try:
async with httpx.AsyncClient(