ddf395b0c1
- Add integration tests for Auth API (register/login/logout) - Add integration tests for Workspace API (create/list) - Test authentication and authorization flows - Create comprehensive Phase 4 completion document - Document all completed modules and features - Include deployment guide and API documentation - List remaining work for Phase 5+ Phase 4 COMPLETED: 29/68 tasks (42.6%), 170 unit tests passing Total time: 4 hours 42 minutes
143 lines
4.4 KiB
Python
143 lines
4.4 KiB
Python
"""
|
|
API 集成测试
|
|
"""
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from apps.api.main import app
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
class TestAuthAPI:
|
|
"""认证 API 集成测试"""
|
|
|
|
def test_register_success(self):
|
|
"""测试注册成功"""
|
|
response = client.post("/api/v1/auth/register", json={
|
|
"email": "test@example.com",
|
|
"password": "SecurePass123",
|
|
"username": "testuser",
|
|
"display_name": "Test User",
|
|
})
|
|
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["email"] == "test@example.com"
|
|
assert data["username"] == "testuser"
|
|
assert "user_id" in data
|
|
|
|
def test_register_duplicate_email(self):
|
|
"""测试重复邮箱注册"""
|
|
# 先注册一个用户
|
|
client.post("/api/v1/auth/register", json={
|
|
"email": "duplicate@example.com",
|
|
"password": "SecurePass123",
|
|
"username": "user1",
|
|
"display_name": "User 1",
|
|
})
|
|
|
|
# 尝试用相同邮箱再次注册
|
|
response = client.post("/api/v1/auth/register", json={
|
|
"email": "duplicate@example.com",
|
|
"password": "SecurePass123",
|
|
"username": "user2",
|
|
"display_name": "User 2",
|
|
})
|
|
|
|
assert response.status_code == 400
|
|
assert "already registered" in response.json()["detail"].lower()
|
|
|
|
def test_login_success(self):
|
|
"""测试登录成功"""
|
|
# 先注册
|
|
client.post("/api/v1/auth/register", json={
|
|
"email": "login@example.com",
|
|
"password": "SecurePass123",
|
|
"username": "loginuser",
|
|
"display_name": "Login User",
|
|
})
|
|
|
|
# 登录
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "login@example.com",
|
|
"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": "login@example.com",
|
|
"password": "WrongPassword123",
|
|
})
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
class TestWorkspaceAPI:
|
|
"""工作空间 API 集成测试"""
|
|
|
|
def setup_method(self):
|
|
"""每个测试前的准备"""
|
|
# 注册并登录,获取 token
|
|
client.post("/api/v1/auth/register", json={
|
|
"email": "workspace@example.com",
|
|
"password": "SecurePass123",
|
|
"username": "workspaceuser",
|
|
"display_name": "Workspace User",
|
|
})
|
|
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "workspace@example.com",
|
|
"password": "SecurePass123",
|
|
})
|
|
|
|
self.token = response.json()["access_token"]
|
|
self.headers = {"Authorization": f"Bearer {self.token}"}
|
|
|
|
def test_create_workspace(self):
|
|
"""测试创建工作空间"""
|
|
response = client.post("/api/v1/workspaces", json={
|
|
"name": "My Workspace",
|
|
"subscription_plan": "free",
|
|
}, headers=self.headers)
|
|
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["name"] == "My Workspace"
|
|
assert data["subscription_plan"] == "free"
|
|
assert data["max_projects"] == 3
|
|
|
|
def test_list_workspaces(self):
|
|
"""测试获取工作空间列表"""
|
|
# 创建工作空间
|
|
client.post("/api/v1/workspaces", json={
|
|
"name": "Workspace 1",
|
|
}, headers=self.headers)
|
|
|
|
# 获取列表
|
|
response = client.get("/api/v1/workspaces", headers=self.headers)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data["workspaces"]) > 0
|
|
assert data["workspaces"][0]["name"] == "Workspace 1"
|
|
|
|
def test_create_workspace_unauthorized(self):
|
|
"""测试未登录创建工作空间"""
|
|
response = client.post("/api/v1/workspaces", json={
|
|
"name": "Unauthorized Workspace",
|
|
})
|
|
|
|
assert response.status_code == 403 # FastAPI HTTPBearer 返回 403
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|