"""集成冒烟测试:登录链路 + 各模块只读接口(需 MySQL/Redis 运行,默认跳过)。""" import pytest pytestmark = pytest.mark.integration @pytest.mark.asyncio async def test_login_flow(client): """完整登录链路:/captcha → 解析验证码 → /login 获取 token。""" import redis as redis_lib from config import REDIS_INDEX r = await client.get("/captcha") body = r.json() assert body["code"] == 0 idkey = body.get("idkey") assert idkey # 从 Redis 读取验证码明文 redis_client = redis_lib.Redis(db=REDIS_INDEX) code = redis_client.get(idkey).decode() resp = await client.post("/login", json={ "username": "admin", "password": "123456", "captcha": code, "idKey": idkey, }) login = resp.json() assert login["code"] == 0 assert login["data"]["access_token"] @pytest.mark.asyncio async def test_admin_read_endpoints(client, admin_headers): """管理员 token 可访问代表性只读接口。""" for path in ("/user/page", "/dept/list", "/menu/list", "/index/menu", "/job/page"): resp = await client.get(path, headers=admin_headers) assert resp.json()["code"] == 0, f"{path} failed" @pytest.mark.asyncio async def test_unauthorized_blocked(client): """无 token 访问受保护接口应返回 401。""" resp = await client.get("/user/page") assert resp.json()["code"] == 401