style: normalize python formatting gates
This commit is contained in:
+108
-72
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
API 集成测试
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
@@ -11,130 +12,165 @@ 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",
|
||||
})
|
||||
|
||||
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",
|
||||
})
|
||||
|
||||
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",
|
||||
})
|
||||
|
||||
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",
|
||||
})
|
||||
|
||||
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",
|
||||
})
|
||||
|
||||
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",
|
||||
})
|
||||
|
||||
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",
|
||||
})
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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",
|
||||
})
|
||||
|
||||
response = client.post(
|
||||
"/api/v1/workspaces",
|
||||
json={
|
||||
"name": "Unauthorized Workspace",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 403 # FastAPI HTTPBearer 返回 403
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user