ForcePilot/backend/test/integration/api/test_auth_router.py
Wenjie Zhang 4f6353d555 feat(test): 重组测试目录结构为 integration/unit/e2e 三层架构
- 将 api 测试重组为 integration 测试
- 新增 unit 和 e2e 测试目录分类
- 新增 testing-guidelines.md 测试指南文档
- 更新 pyproject.toml 和 run_tests.sh 以适配新结构

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-30 15:24:47 +08:00

54 lines
1.9 KiB
Python

"""
Integration tests for authentication-related API routes.
"""
from __future__ import annotations
import uuid
import pytest
pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
async def test_login_with_invalid_credentials(test_client):
response = await test_client.post("/api/auth/token", data={"username": "invalid", "password": "invalid"})
assert response.status_code == 401
assert "detail" in response.json()
async def test_admin_can_login_and_fetch_profile(test_client, admin_headers):
profile_response = await test_client.get("/api/auth/me", headers=admin_headers)
assert profile_response.status_code == 200
data = profile_response.json()
assert data["role"] in {"admin", "superadmin"}
assert data["username"]
assert data["user_id"]
async def test_admin_can_create_and_delete_user(test_client, admin_headers):
suffix = uuid.uuid4().hex[:8]
payload = {
"username": f"rtu_{suffix}",
"password": "routerTest123!",
"role": "user",
}
create_response = await test_client.post("/api/auth/users", json=payload, headers=admin_headers)
assert create_response.status_code == 200, create_response.text
created_user = create_response.json()
assert created_user["username"] == payload["username"]
assert created_user["role"] == payload["role"]
delete_response = await test_client.delete(f"/api/auth/users/{created_user['id']}", headers=admin_headers)
assert delete_response.status_code == 200, delete_response.text
delete_payload = delete_response.json()
assert delete_payload["success"] is True
assert delete_payload["message"] == "用户已删除"
async def test_invalid_token_is_rejected(test_client):
headers = {"Authorization": "Bearer not-a-real-token"}
response = await test_client.get("/api/auth/me", headers=headers)
assert response.status_code == 401