后端开发完成

This commit is contained in:
Your Name
2026-01-26 18:21:34 +08:00
parent 40d2f3f6ac
commit b3876bba89
64 changed files with 1736 additions and 0 deletions
+186
View File
@@ -0,0 +1,186 @@
def test_create_project_marketing(client, marketing_token):
"""测试市场部创建项目"""
new_project = {
"name": "Test Project",
"description": "Test Project Description",
"department": "Marketing",
"budget": 10000,
"status": "pending"
}
response = client.post(
"/api/projects",
json=new_project,
headers={"Authorization": f"Bearer {marketing_token}"}
)
assert response.status_code == 200
assert response.json()["name"] == "Test Project"
assert response.json()["description"] == "Test Project Description"
assert response.json()["department"] == "Marketing"
def test_create_project_non_marketing(client, other_token):
"""测试非市场部创建项目(权限不足)"""
new_project = {
"name": "Test Project",
"description": "Test Project Description",
"department": "Other",
"budget": 10000,
"status": "pending"
}
response = client.post(
"/api/projects",
json=new_project,
headers={"Authorization": f"Bearer {other_token}"}
)
assert response.status_code == 403
assert "Only marketing department can create projects" in response.json()["detail"]
def test_get_projects(client, admin_token):
"""测试获取项目列表"""
# 先创建一个项目
new_project = {
"name": "Test Project",
"description": "Test Project Description",
"department": "Marketing",
"budget": 10000,
"status": "pending"
}
client.post(
"/api/projects",
json=new_project,
headers={"Authorization": f"Bearer {admin_token}"}
)
# 获取项目列表
response = client.get(
"/api/projects",
headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 200
assert len(response.json()) >= 1
def test_update_project(client, marketing_token, admin_token):
"""测试更新项目"""
# 先创建一个项目
new_project = {
"name": "Test Project",
"description": "Test Project Description",
"department": "Marketing",
"budget": 10000,
"status": "pending"
}
create_response = client.post(
"/api/projects",
json=new_project,
headers={"Authorization": f"Bearer {marketing_token}"}
)
project_id = create_response.json()["id"]
# 更新项目
update_data = {
"name": "Updated Test Project",
"description": "Updated Test Project Description",
"budget": 15000,
"status": "in_progress"
}
response = client.put(
f"/api/projects/{project_id}",
json=update_data,
headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 200
assert response.json()["name"] == "Updated Test Project"
assert response.json()["description"] == "Updated Test Project Description"
assert response.json()["budget"] == 15000
assert response.json()["status"] == "in_progress"
def test_delete_project_admin(client, marketing_token, admin_token):
"""测试管理员删除项目"""
# 先创建一个项目
new_project = {
"name": "Delete Test Project",
"description": "Delete Test Project Description",
"department": "Marketing",
"budget": 10000,
"status": "pending"
}
create_response = client.post(
"/api/projects",
json=new_project,
headers={"Authorization": f"Bearer {marketing_token}"}
)
project_id = create_response.json()["id"]
# 删除项目
response = client.delete(
f"/api/projects/{project_id}",
headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 200
assert "Project deleted successfully" in response.json()["message"]
def test_delete_project_non_admin(client, marketing_token):
"""测试非管理员删除项目(权限不足)"""
# 先创建一个项目
new_project = {
"name": "Delete Test Project",
"description": "Delete Test Project Description",
"department": "Marketing",
"budget": 10000,
"status": "pending"
}
create_response = client.post(
"/api/projects",
json=new_project,
headers={"Authorization": f"Bearer {marketing_token}"}
)
project_id = create_response.json()["id"]
# 尝试删除项目
response = client.delete(
f"/api/projects/{project_id}",
headers={"Authorization": f"Bearer {marketing_token}"}
)
assert response.status_code == 403
assert "Not enough permissions" in response.json()["detail"]
def test_project_history(client, marketing_token, admin_token):
"""测试项目历史记录"""
# 先创建一个项目
new_project = {
"name": "History Test Project",
"description": "History Test Project Description",
"department": "Marketing",
"budget": 10000,
"status": "pending"
}
create_response = client.post(
"/api/projects",
json=new_project,
headers={"Authorization": f"Bearer {marketing_token}"}
)
project_id = create_response.json()["id"]
# 更新项目
update_data = {
"name": "Updated History Test Project",
"budget": 15000
}
client.put(
f"/api/projects/{project_id}",
json=update_data,
headers={"Authorization": f"Bearer {admin_token}"}
)
# 获取项目历史记录
response = client.get(
f"/api/projects/{project_id}/history",
headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 200
assert len(response.json()) >= 2 # 至少有两条历史记录(创建和更新)