Files
xiaoxia-saas/tests/integration/test_api.py
T
灵应 8935196fcd
Deploy / Staging E2E Tests (push) Has been skipped
Deploy / Build Production Runtime Images (push) Has been skipped
Deploy / Deploy Production (push) Has been skipped
Deploy / Production Browser E2E (push) Has been skipped
Deploy / Deploy Staging (push) Failing after 138h4m33s
CI/CD Pipeline / Frontend Lint (push) Failing after 138h4m39s
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 138h4m39s
style: 后端代码black格式化
2026-07-03 18:49:54 +08:00

135 lines
3.6 KiB
Python
Executable File

"""
API 集成测试
测试认证 API 的集成流程。
需要 PostgreSQL 数据库才能运行。在没有数据库的环境中会被跳过。
"""
import os
import uuid
import pytest
# 检测是否有可用的 PostgreSQL 数据库
_HAS_PG = False
try:
if os.environ.get("USE_IN_MEMORY_DB", "").lower() != "true":
import psycopg
conn = psycopg.connect(
os.environ.get(
"DATABASE_URL",
"postgresql+psycopg://postgres:postgres@localhost:5432/xiaoxia_saas",
).replace("postgresql+psycopg://", "postgresql://"),
connect_timeout=3,
)
conn.close()
_HAS_PG = True
except Exception:
pass
needs_pg = pytest.mark.skipif(not _HAS_PG, reason="Requires PostgreSQL database")
from fastapi.testclient import TestClient
from apps.api.main import app
client = TestClient(app)
@needs_pg
class TestAuthAPI:
"""认证 API 集成测试"""
def test_register_success(self):
"""测试注册成功"""
unique = uuid.uuid4().hex[:8]
response = client.post(
"/api/v1/auth/register",
json={
"email": f"test-{unique}@example.com",
"password": "SecurePass123",
"username": f"testuser-{unique}",
"display_name": "Test User",
},
)
assert response.status_code == 200
data = response.json()
assert data["username"] == f"testuser-{unique}"
assert "user_id" in data
def test_register_duplicate_email(self):
"""测试重复邮箱注册"""
unique = uuid.uuid4().hex[:8]
email = f"dup-{unique}@example.com"
client.post(
"/api/v1/auth/register",
json={
"email": email,
"password": "SecurePass123",
"username": f"user1-{unique}",
"display_name": "User 1",
},
)
response = client.post(
"/api/v1/auth/register",
json={
"email": email,
"password": "SecurePass123",
"username": f"user2-{unique}",
"display_name": "User 2",
},
)
assert response.status_code == 400
detail = response.json().get("detail", "")
assert "邮箱" in detail or "already" in detail.lower() or "注册" in detail
def test_login_success(self):
"""测试登录成功"""
unique = uuid.uuid4().hex[:8]
email = f"login-{unique}@example.com"
reg = client.post(
"/api/v1/auth/register",
json={
"email": email,
"password": "SecurePass123",
"username": f"loginuser-{unique}",
"display_name": "Login User",
},
)
assert reg.status_code == 200, f"Register failed: {reg.json()}"
response = client.post(
"/api/v1/auth/login",
json={
"email": email,
"password": "SecurePass123",
},
)
assert response.status_code == 200
data = response.json()
assert "access_token" in data
assert "refresh_token" in data
assert data["token_type"] == "bearer"
def test_login_wrong_password(self):
"""测试密码错误"""
response = client.post(
"/api/v1/auth/login",
json={
"email": "nobody@example.com",
"password": "WrongPassword123",
},
)
assert response.status_code == 401
if __name__ == "__main__":
pytest.main([__file__, "-v"])