33 lines
921 B
Python
33 lines
921 B
Python
"""集成测试 fixture:真实 FastAPI 应用 + ASGI client(需 MySQL/Redis 运行)。"""
|
|
|
|
import httpx
|
|
import pytest
|
|
import pytest_asyncio
|
|
|
|
from core import jwt as core_jwt
|
|
from core.app import create_app
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
return create_app()
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def client(app):
|
|
# 手动运行 Redis lifespan(当前 httpx 版本不支持 ASGITransport lifespan 参数)
|
|
from core.redis_client import redis_lifespan
|
|
|
|
transport = httpx.ASGITransport(app=app)
|
|
async with redis_lifespan(app):
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://test") as c:
|
|
yield c
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_headers():
|
|
"""userId=1 管理员 token(绕过 permission_required 权限校验)。"""
|
|
token = core_jwt.create_token(
|
|
{"userId": 1, "username": "admin", "realname": "admin"})
|
|
return {"Authorization": f"Bearer {token}"}
|