be5d5ede90
refactor: 重构后端查询逻辑和API响应处理 fix: 修复用户角色更新和文件上传问题 test: 添加前端性能测试脚本和E2E测试用例 chore: 更新依赖版本和配置文件 docs: 添加环境检查脚本和测试文档 style: 统一表格标签样式和路由命名 perf: 优化前端页面加载速度和响应时间
79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
"""
|
|
认证测试用例
|
|
"""
|
|
|
|
import pytest
|
|
from api.auth_api import AuthAPI
|
|
from config.settings import settings
|
|
|
|
|
|
@pytest.mark.auth
|
|
@pytest.mark.smoke
|
|
class TestAuth:
|
|
"""认证测试类"""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_success(self, http_client):
|
|
"""测试成功登录"""
|
|
auth_api = AuthAPI(http_client)
|
|
response = await auth_api.login(settings.TEST_USERNAME, 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):
|
|
"""测试无效凭证登录"""
|
|
auth_api = AuthAPI(http_client)
|
|
response = await auth_api.login("invalid_user", "invalid_password")
|
|
|
|
assert response.status_code == 401
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_missing_fields(self, http_client):
|
|
"""测试缺少必填字段"""
|
|
auth_api = AuthAPI(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
|