"""Auth API tests""" import pytest from httpx import AsyncClient def auth_header(token: str) -> dict: return {"Authorization": f"Bearer {token}"} class TestLogin: async def test_admin_login(self, client: AsyncClient): resp = await client.post("/api/v1/auth/login", json={ "username": "admin", "password": "admin123", }) assert resp.status_code == 200 assert "access_token" in resp.json() async def test_all_seed_users_login(self, client: AsyncClient): """验证所有种子用户都能登录""" users = { "admin": "admin123", "test123": "test123", "test234": "test234", "testtest": "testtest", "logan": "zxc987", } for username, password in users.items(): resp = await client.post("/api/v1/auth/login", json={ "username": username, "password": password, }) assert resp.status_code == 200, f"{username} login failed" assert "access_token" in resp.json() async def test_wrong_password(self, client: AsyncClient): resp = await client.post("/api/v1/auth/login", json={ "username": "admin", "password": "wrong", }) assert resp.status_code == 401 async def test_nonexistent_user(self, client: AsyncClient): resp = await client.post("/api/v1/auth/login", json={ "username": "nobody", "password": "xxx", }) assert resp.status_code == 401 class TestGetMe: async def test_get_me(self, client: AsyncClient, admin_token): resp = await client.get("/api/v1/auth/me", headers=auth_header(admin_token)) assert resp.status_code == 200 data = resp.json() assert data["username"] == "admin" assert data["role"] == "admin" async def test_no_token(self, client: AsyncClient): resp = await client.get("/api/v1/auth/me") assert resp.status_code == 401