"""partner 集成测试。""" import pytest from model_bakery import baker from rest_framework.test import APIClient from rest_framework_simplejwt.tokens import RefreshToken from apps.partner.models import Customer, Supplier @pytest.fixture def auth_client(db, user, tenant): c = APIClient() refresh = RefreshToken.for_user(user) c.credentials( HTTP_AUTHORIZATION=f"Bearer {refresh.access_token}", HTTP_X_TENANT_ID=tenant.code, ) return c @pytest.fixture def anon(db): return APIClient() @pytest.mark.django_db def test_create_customer(auth_client, tenant): resp = auth_client.post( "/api/v1/partner/customers/", {"code": "C001", "name": "客户A", "kind": "company", "phone": "13800138000"}, format="json", ) assert resp.status_code == 201, resp.content assert resp.json()["code"] == "C001" @pytest.mark.django_db def test_list_customers(auth_client, tenant): baker.make(Customer, tenant=tenant, code="C001", name="客户1") baker.make(Customer, tenant=tenant, code="C002", name="客户2") resp = auth_client.get("/api/v1/partner/customers/") assert resp.status_code == 200 assert resp.json()["count"] == 2 @pytest.mark.django_db def test_search_customer_by_name(auth_client, tenant): baker.make(Customer, tenant=tenant, code="C001", name="苹果公司") baker.make(Customer, tenant=tenant, code="C002", name="香蕉公司") resp = auth_client.get("/api/v1/partner/customers/?search=苹果") assert resp.json()["count"] == 1 @pytest.mark.django_db def test_customer_tenant_isolation(auth_client, tenant, other_tenant): baker.make(Customer, tenant=other_tenant, code="OTHER_C1") baker.make(Customer, tenant=tenant, code="MY_C1") resp = auth_client.get("/api/v1/partner/customers/") codes = {c["code"] for c in resp.json()["results"]} assert "MY_C1" in codes assert "OTHER_C1" not in codes @pytest.mark.django_db def test_create_supplier(auth_client, tenant): resp = auth_client.post( "/api/v1/partner/suppliers/", {"code": "S001", "name": "供应商A", "phone": "13900139000"}, format="json", ) assert resp.status_code == 201, resp.content assert resp.json()["code"] == "S001" @pytest.mark.django_db def test_create_price_level(auth_client, tenant): resp = auth_client.post( "/api/v1/partner/price-levels/", {"code": "VIP1", "name": "一级VIP", "discount_rate": "0.9000"}, format="json", ) assert resp.status_code == 201 @pytest.mark.django_db def test_anon_cannot_access(anon, tenant): anon.credentials(HTTP_X_TENANT_ID=tenant.code) resp = anon.get("/api/v1/partner/customers/") assert resp.status_code == 401 @pytest.mark.django_db def test_contact_crud(auth_client, tenant): # 新建(无挂靠) resp = auth_client.post( "/api/v1/partner/contacts/", {"name": "张三", "phone": "13800138000", "title": "采购"}, format="json", ) assert resp.status_code == 201, resp.content cid = resp.json()["id"] # 编辑 resp = auth_client.patch( f"/api/v1/partner/contacts/{cid}/", {"title": "采购经理"}, format="json", ) assert resp.status_code == 200, resp.content assert resp.json()["title"] == "采购经理" # 列表可见 resp = auth_client.get("/api/v1/partner/contacts/") assert resp.status_code == 200 assert resp.json()["count"] == 1 # 删除 resp = auth_client.delete(f"/api/v1/partner/contacts/{cid}/") assert resp.status_code in (200, 202, 204), resp.content