1
0
forked from erp-dev/erp

feat: agent api

This commit is contained in:
2026-04-14 18:05:56 +08:00
parent 0167478a25
commit 714d6287fe
6 changed files with 374 additions and 0 deletions

View File

@@ -10,9 +10,11 @@ from rest_framework.test import APIClient, APIRequestFactory
from basic_info import models as basic_models
from printing import models as printing_models
from business import models as business_models
from shipment import models as shipment_models
from api_v2.views.printing import PrintingJobByCustomerView
from django.contrib.contenttypes.models import ContentType
from stateflow import models as stateflow_models
from django.test.utils import override_settings
class QuickCreateEmployeeUserAPITest(TestCase):
@@ -277,6 +279,144 @@ class PrintingJobByCustomerAPITest(TestCase):
self.assertEqual(data['billed_quantity'], '20.00')
@override_settings(AGENT_ACCESS_KEY='agent-test-key')
class AgentUnshippedShipmentListAPITest(TestCase):
def setUp(self):
self.client = APIClient()
self.url = '/api/v2/ai/shipments/unshipped/'
self.merchant = basic_models.Merchant.objects.create(
name='Agent商户',
type=basic_models.MerchantTypeEnum.FACTORY,
)
self.other_merchant = basic_models.Merchant.objects.create(
name='其他商户',
type=basic_models.MerchantTypeEnum.FACTORY,
)
self.customer = basic_models.Customer.objects.create(
merchant=self.merchant,
name='客户A',
area='华东',
created_by=None,
)
self.other_customer = basic_models.Customer.objects.create(
merchant=self.other_merchant,
name='客户B',
area='华东',
created_by=None,
)
self.unshipped_target = shipment_models.Shipment.objects.create(
merchant=self.merchant,
customer=self.customer,
shipment_date=datetime.date(2026, 4, 14),
area='华东',
address='',
contact_name='张三',
contact_phone='13800000000',
remark='目标记录',
)
self.unshipped_other_area = shipment_models.Shipment.objects.create(
merchant=self.merchant,
customer=self.customer,
shipment_date=datetime.date(2026, 4, 14),
area='华南',
)
delivery = shipment_models.ShipmentDelivery.objects.create(
merchant=self.merchant,
driver_name='李司机',
vehicle_trip='TRIP-001',
)
self.shipped_shipment = shipment_models.Shipment.objects.create(
merchant=self.merchant,
customer=self.customer,
shipment_date=datetime.date(2026, 4, 14),
area='华东',
delivery=delivery,
)
self.other_merchant_shipment = shipment_models.Shipment.objects.create(
merchant=self.other_merchant,
customer=self.other_customer,
shipment_date=datetime.date(2026, 4, 14),
area='华东',
)
def _headers(self, key='agent-test-key'):
return {'HTTP_AUTHORIZATION': key}
def test_list_unshipped_shipments_success(self):
response = self.client.get(
self.url,
{'merchant_id': self.merchant.id, 'area': '华东'},
**self._headers(),
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['count'], 1)
self.assertEqual(response.data['results'][0]['id'], self.unshipped_target.id)
self.assertEqual(response.data['results'][0]['customer_name'], '客户A')
self.assertIsNone(response.data['results'][0]['delivery'])
def test_list_unshipped_shipments_requires_access_key(self):
response = self.client.get(
self.url,
{'merchant_id': self.merchant.id, 'area': '华东'},
)
self.assertEqual(response.status_code, 401)
def test_list_unshipped_shipments_rejects_invalid_access_key(self):
response = self.client.get(
self.url,
{'merchant_id': self.merchant.id, 'area': '华东'},
**self._headers(key='wrong-key'),
)
self.assertEqual(response.status_code, 401)
def test_list_unshipped_shipments_requires_merchant_and_area(self):
response = self.client.get(
self.url,
{'area': '华东'},
**self._headers(),
)
self.assertEqual(response.status_code, 400)
self.assertIn('merchant_id', response.data)
response = self.client.get(
self.url,
{'merchant_id': self.merchant.id},
**self._headers(),
)
self.assertEqual(response.status_code, 400)
self.assertIn('area', response.data)
def test_list_unshipped_shipments_filters_by_merchant(self):
response = self.client.get(
self.url,
{'merchant_id': self.other_merchant.id, 'area': '华东'},
**self._headers(),
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['count'], 1)
self.assertEqual(response.data['results'][0]['id'], self.other_merchant_shipment.id)
def test_list_unshipped_shipments_supports_limit_offset(self):
shipment_models.Shipment.objects.create(
merchant=self.merchant,
customer=self.customer,
shipment_date=datetime.date(2026, 4, 15),
area='华东',
remark='第二条',
)
response = self.client.get(
self.url,
{'merchant_id': self.merchant.id, 'area': '华东', 'limit': 1, 'offset': 0},
**self._headers(),
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['count'], 2)
self.assertEqual(len(response.data['results']), 1)
class PrintingJobBatchAdvanceV2APITest(TestCase):
def setUp(self):
self.client = APIClient()