Files
crm_project/server/tests/api/test_contacts.py
T
hankin 815cbf9d8c v0.2.0: CRM/ERP 系统升级 - 清理 .gitignore 并移除误提交的 venv/env/db 文件
- 更新 .gitignore:全面覆盖环境变量、数据库、日志、缓存、上传文件
- 移除误跟踪的 server/venv/、crm_data.db、.env 文件
- 新增 server/.env.example 模板
- 新增合同管理、利润核算、AI教练等功能模块
- 新增 Playwright e2e 测试套件
- 前后端多项功能升级和 bug 修复
2026-05-11 07:24:19 +00:00

77 lines
2.6 KiB
Python

"""
联系人模块测试 —— /api/customers/{cid}/contacts & /api/contacts/{id}
覆盖: CRUD 完整链路
"""
import uuid
import pytest
from httpx import AsyncClient
from tests.conftest import CUSTOMER_ID
class TestContactsCRUD:
"""联系人 CRUD 全链路"""
async def test_list_contacts_empty(self, client: AsyncClient, admin_headers):
"""初始无联系人 → 200 + 空数组"""
resp = await client.get(
f"/api/customers/{CUSTOMER_ID}/contacts",
headers=admin_headers
)
assert resp.status_code == 200
assert isinstance(resp.json()["data"], list)
async def test_create_contact(self, client: AsyncClient, admin_headers):
"""新增联系人 → 200"""
resp = await client.post(
f"/api/customers/{CUSTOMER_ID}/contacts",
headers=admin_headers,
json={"name": "王采购", "phone": "13700137000", "title": "采购总监"}
)
assert resp.status_code == 200
data = resp.json()["data"]
assert data["name"] == "王采购"
return data.get("id")
async def test_create_and_update_contact(self, client: AsyncClient, admin_headers):
"""新增后编辑联系人 → 200"""
# 新增
create_resp = await client.post(
f"/api/customers/{CUSTOMER_ID}/contacts",
headers=admin_headers,
json={"name": "临时联系人", "phone": "10000"}
)
assert create_resp.status_code == 200
contact_id = create_resp.json()["data"]["id"]
# 编辑
update_resp = await client.put(
f"/api/contacts/{contact_id}",
headers=admin_headers,
json={"name": "正式联系人", "title": "技术经理"}
)
assert update_resp.status_code == 200
async def test_create_and_delete_contact(self, client: AsyncClient, admin_headers):
"""新增后删除联系人 → 200"""
create_resp = await client.post(
f"/api/customers/{CUSTOMER_ID}/contacts",
headers=admin_headers,
json={"name": "待删联系人"}
)
contact_id = create_resp.json()["data"]["id"]
del_resp = await client.delete(
f"/api/contacts/{contact_id}",
headers=admin_headers
)
assert del_resp.status_code == 200
async def test_create_contact_no_name(self, client: AsyncClient, admin_headers):
"""缺少 name → 422"""
resp = await client.post(
f"/api/customers/{CUSTOMER_ID}/contacts",
headers=admin_headers,
json={"phone": "123"}
)
assert resp.status_code == 422