Files
张翔 1e3dc11d59 refactor(test): 重构测试套件结构并优化测试配置
feat(test-suite): 新增测试套件模块,包含API测试客户端和测试配置
fix(api): 修复数据库实体和仓库的删除操作返回值
style(api): 统一数据库表名和字段命名
perf(api): 添加缓存注解提升配置查询性能
test(api): 添加H2测试数据库配置支持
chore: 清理旧的测试文件和脚本
2026-04-01 20:57:24 +08:00

81 lines
2.4 KiB
Python

"""
认证测试用例
"""
import pytest
from config.settings import settings
@pytest.mark.auth
@pytest.mark.smoke
class TestAuth:
"""认证测试类"""
@pytest.mark.asyncio
async def test_login_success(self, http_client):
"""测试成功登录"""
response = await http_client.post("/api/auth/login", json={
"username": settings.TEST_USERNAME,
"password": settings.TEST_PASSWORD
})
assert response.status_code == 200
data = response.json()
assert "token" in data
assert isinstance(data["token"], str)
assert "userId" in data
assert "username" in data
@pytest.mark.asyncio
async def test_login_invalid_credentials(self, http_client):
"""测试无效凭证登录"""
response = await http_client.post("/api/auth/login", json={
"username": "invalid_user",
"password": "invalid_password"
})
assert response.status_code == 401
@pytest.mark.asyncio
async def test_login_missing_fields(self, http_client):
"""测试缺少必填字段"""
response = await http_client.post("/api/auth/login", json={
"username": "test"
})
assert response.status_code == 400
@pytest.mark.asyncio
async def test_register_success(self, http_client):
"""测试注册成功"""
import time
timestamp = int(time.time() * 1000)
response = await http_client.post("/api/auth/register", json={
"username": f"testuser_{timestamp}",
"password": "password123",
"email": f"test_{timestamp}@example.com"
})
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["username"] == f"testuser_{timestamp}"
@pytest.mark.asyncio
async def test_register_duplicate_username(self, http_client):
"""测试注册重复用户名"""
response = await http_client.post("/api/auth/register", json={
"username": "admin",
"password": "password123",
"email": "admin@example.com"
})
assert response.status_code == 400
@pytest.mark.asyncio
async def test_logout_success(self, http_client):
"""测试登出成功"""
response = await http_client.post("/api/auth/logout")
assert response.status_code == 200