- 删除16个旧Python测试文件(970行→400行) - 重写conftest.py适配真实种子用户(admin/test123/test234/testtest/logan) - 5个精简测试文件覆盖核心API: - test_auth.py: 登录/鉴权/种子用户验证 (6 tests) - test_instances.py: 实例列表/详情/连接测试 (6 tests) - test_tickets.py: 工单CRUD/只读SQL拦截/auditor权限 (7 tests) - test_workflow.py: E2E完整审批流/拒绝/撤销 (3 tests) - test_permissions.py: 角色权限/执行权限/workflow权限 (6 tests) - 新增 cleanup_test_data.py 清理测试脏数据(支持--dry-run) - 全量29测试3.27s通过
53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
"""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
|