统一货代发货通知的业务阶段为四个标准阶段:接收解析、订单复核、发货准备、上传完成 设置 ui_state 为前端唯一状态数据源,不再依赖 process_status 驱动业务流程 删除 shipping_notices 表的废弃 process_status 列及对应复合索引,新增数据库迁移脚本 替换全栈代码中所有对 process_status 的引用为生命周期、上传状态等领域字段 更新相关文档、测试用例与前端展示逻辑适配新的状态管理规则
556 lines
25 KiB
Python
556 lines
25 KiB
Python
"""
|
|
backend/tests/test_admin_shipping_customers.py
|
|
|
|
用途:验证货代客户管理 API 的权限、成功响应与错误 envelope。
|
|
依赖:Flask 测试客户端、User、客户管理路由。
|
|
逻辑:创建真实 admin/user 账户,通过 legacy header 访问管理路由并断言 RBAC 与响应契约。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app import db
|
|
from app.models import (
|
|
FileAsset,
|
|
ShippingCase,
|
|
ShippingCustomer,
|
|
ShippingCustomerIdentifier,
|
|
ShippingIdentityDefinition,
|
|
ShippingIdentityEvidence,
|
|
ShippingCustomerShadowResolution,
|
|
ShippingOrderCandidate,
|
|
ShippingOrderDocument,
|
|
ShippingOrderIntake,
|
|
ShippingOrderField,
|
|
ShippingOrderSchema,
|
|
ShippingOrderSchemaVersion,
|
|
ShippingNotice,
|
|
User,
|
|
)
|
|
from tests.conftest import parse_api_data, parse_api_error
|
|
|
|
|
|
def _create_users() -> None:
|
|
"""创建管理 API 权限测试所需的 admin 与普通用户。"""
|
|
db.session.add_all([
|
|
User(username='shipping-admin', email='shipping-admin@example.com', role='admin', status='active'),
|
|
User(username='shipping-user', email='shipping-user@example.com', role='user', status='active'),
|
|
])
|
|
db.session.commit()
|
|
|
|
|
|
def _identity_field() -> ShippingOrderField:
|
|
"""发布 API 测试使用的客户识别 Header 字段(标准 bill_to_customer)。"""
|
|
from app.services.shipping_notice import field_asset_service
|
|
|
|
field_asset_service.ensure_standard_assets_seeded()
|
|
field = ShippingOrderField.query.filter_by(field_code='bill_to_customer').one()
|
|
schema = ShippingOrderSchema.query.filter_by(schema_code='SHIPPING_ORDER').one_or_none()
|
|
if schema is None:
|
|
schema = ShippingOrderSchema(schema_code='SHIPPING_ORDER', display_name='Order')
|
|
db.session.add(schema)
|
|
db.session.flush()
|
|
if ShippingOrderSchemaVersion.query.filter_by(status='published').one_or_none() is None:
|
|
db.session.add(ShippingOrderSchemaVersion(
|
|
schema_id=schema.id, version=1, status='published', definition_json={'fields': [{
|
|
'field_id': field.id, 'field_code': field.field_code, 'display_name': 'Bill To Customer',
|
|
'scope': 'header', 'value_type': 'string', 'enabled': True,
|
|
'available_for_customer_identity': True, 'normalizer_key': 'lowercase_trim',
|
|
}]}, checksum='1' * 64,
|
|
))
|
|
db.session.commit()
|
|
return field
|
|
|
|
|
|
def _headers(username: str, role: str) -> dict[str, str]:
|
|
"""构造 legacy header 认证信息。"""
|
|
return {'X-Username': username, 'X-Role': role, 'Content-Type': 'application/json'}
|
|
|
|
|
|
def test_shipping_customer_admin_crud_uses_standard_contract(app, client):
|
|
"""管理员可创建和查询客户,校验失败返回标准错误 envelope。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
|
|
headers = _headers('shipping-admin', 'admin')
|
|
created_response = client.post(
|
|
'/api/admin/shipping-customers',
|
|
json={'standard_name': '无锡宇辰有限公司', 'short_code': 'wyc'},
|
|
headers=headers,
|
|
)
|
|
assert created_response.status_code == 201
|
|
created = parse_api_data(created_response.get_json())
|
|
assert created['short_code'] == 'WYC'
|
|
|
|
list_response = client.get('/api/admin/shipping-customers', headers=headers)
|
|
assert list_response.status_code == 200
|
|
listed = parse_api_data(list_response.get_json())
|
|
assert listed['total'] == 1
|
|
|
|
invalid_response = client.post(
|
|
'/api/admin/shipping-customers',
|
|
json={'standard_name': '错误客户', 'short_code': 'A1'},
|
|
headers=headers,
|
|
)
|
|
assert invalid_response.status_code == 400
|
|
assert parse_api_error(invalid_response.get_json())['code'] == 'SHIPPING.CUSTOMER_SHORT_CODE_INVALID'
|
|
|
|
|
|
def test_shipping_customer_admin_routes_reject_non_admin(app, client):
|
|
"""普通用户访问客户主数据和导入模板均返回 403。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
|
|
headers = _headers('shipping-user', 'user')
|
|
for path in ('/api/admin/shipping-customers', '/api/admin/shipping-customers/import-template'):
|
|
response = client.get(path, headers=headers)
|
|
assert response.status_code == 403
|
|
assert parse_api_error(response.get_json())['code'] == 'AUTH.FORBIDDEN'
|
|
|
|
|
|
def test_configured_identity_evidence_api_requires_published_definition_and_disables_legacy_writes(app, client):
|
|
"""后台只能维护已发布定义的证据;旧固定类型写入口必须明确拒绝,避免写入运行期不消费的数据。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
customer = ShippingCustomer(standard_name='配置客户', short_code='CFG', status='active')
|
|
db.session.add(customer)
|
|
db.session.commit()
|
|
customer_id = customer.id
|
|
|
|
headers = _headers('shipping-admin', 'admin')
|
|
catalog = client.get('/api/admin/shipping-identity-rules/published-evidence-definitions?target_type=customer', headers=headers)
|
|
assert catalog.status_code == 200
|
|
assert parse_api_data(catalog.get_json())['disabled_reason'] == 'published_rule_set_required'
|
|
|
|
blocked = client.post(
|
|
f'/api/admin/shipping-customers/{customer_id}/identifiers',
|
|
json={'identifier_type': 'email', 'identifier_value': 'legacy@example.com'}, headers=headers,
|
|
)
|
|
assert blocked.status_code == 400
|
|
assert parse_api_error(blocked.get_json())['code'] == 'SHIPPING.CUSTOMER_LEGACY_IDENTIFIER_WRITE_DISABLED'
|
|
|
|
with app.app_context():
|
|
from app.services.shipping_notice.identity_rules import service as identity_rules
|
|
field = _identity_field()
|
|
definition = identity_rules.save_definition({
|
|
'definition_code': 'api_sender_email', 'display_name': 'Sender email',
|
|
'evidence_label': 'Sender email', 'target_type': 'customer',
|
|
'context_field_id': field.id, 'max_automation': 'conditional',
|
|
}, actor_id=1)
|
|
draft = identity_rules.create_rule_set({}, actor_id=1)
|
|
saved = identity_rules.save_rule_set(draft['id'], {
|
|
'expected_lock_version': draft['lock_version'], 'definition_ids': [definition['id']],
|
|
}, actor_id=1)
|
|
identity_rules.publish_rule_set(draft['id'], expected_lock_version=saved['lock_version'], actor_id=1)
|
|
|
|
enabled = client.get('/api/admin/shipping-identity-rules/published-evidence-definitions?target_type=customer', headers=headers)
|
|
definition_payload = parse_api_data(enabled.get_json())['definitions'][0]
|
|
assert definition_payload['definition_code'] == 'api_sender_email'
|
|
|
|
created = client.post(
|
|
f'/api/admin/shipping-customers/{customer_id}/identity-evidences',
|
|
json={'definition_id': definition_payload['id'], 'raw_value': 'buyer@cfg.example.com', 'status': 'pending'},
|
|
headers=headers,
|
|
)
|
|
assert created.status_code == 201
|
|
evidence = parse_api_data(created.get_json())
|
|
assert evidence['definition']['definition_code'] == 'api_sender_email'
|
|
|
|
missing_reason = client.put(
|
|
f'/api/admin/shipping-identity-evidences/{evidence["id"]}', json={'status': 'approved'}, headers=headers,
|
|
)
|
|
assert missing_reason.status_code == 400
|
|
assert parse_api_error(missing_reason.get_json())['code'] == 'SHIPPING.CUSTOMER_CHANGE_REASON_REQUIRED'
|
|
|
|
approved = client.put(
|
|
f'/api/admin/shipping-identity-evidences/{evidence["id"]}',
|
|
json={'status': 'approved', 'reason': '已核验邮箱归属'}, headers=headers,
|
|
)
|
|
assert approved.status_code == 200
|
|
assert parse_api_data(approved.get_json())['status'] == 'approved'
|
|
with app.app_context():
|
|
assert ShippingIdentityEvidence.query.count() == 1
|
|
|
|
|
|
def test_shipping_customer_detail_serializes_identity_evidence_with_multiple_published_schema_families(app, client):
|
|
"""客户详情在多个 Schema 族各自发布时仍读取标准族证据配置并返回成功响应。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
from app.services.shipping_notice.identity_rules import service as identity_rules
|
|
|
|
_create_users()
|
|
customer = ShippingCustomer(standard_name='多族客户', short_code='MFS', status='active')
|
|
db.session.add(customer)
|
|
field = _identity_field()
|
|
schema = ShippingOrderSchema(
|
|
schema_code='STANDARD_SHIPPING_NOTICE', display_name='标准发货通知',
|
|
)
|
|
db.session.add(schema)
|
|
db.session.flush()
|
|
db.session.add(ShippingOrderSchemaVersion(
|
|
schema_id=schema.id, version=1, status='published', checksum='2' * 64,
|
|
definition_json={'fields': [{
|
|
'field_id': field.id, 'field_code': field.field_code, 'display_name': '标准收货客户',
|
|
'scope': 'header', 'value_type': 'string', 'enabled': True,
|
|
'available_for_customer_identity': True, 'normalizer_key': 'lowercase_trim',
|
|
}]},
|
|
))
|
|
db.session.commit()
|
|
definition = identity_rules.save_definition({
|
|
'definition_code': 'multi_family_customer', 'display_name': '多族客户',
|
|
'evidence_label': '客户名称', 'target_type': 'customer',
|
|
'context_field_id': field.id, 'max_automation': 'none',
|
|
}, actor_id=1)
|
|
db.session.add(ShippingIdentityEvidence(
|
|
customer_id=customer.id, definition_id=definition['id'], raw_value='多族客户',
|
|
normalized_value='多族客户', status='approved',
|
|
))
|
|
db.session.commit()
|
|
customer_id = customer.id
|
|
|
|
response = client.get(
|
|
f'/api/admin/shipping-customers/{customer_id}', headers=_headers('shipping-admin', 'admin'),
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
evidence = parse_api_data(response.get_json())['identity_evidences'][0]
|
|
assert evidence['definition']['context_field']['display_name'] == '标准收货客户'
|
|
|
|
|
|
def test_shipping_customer_lifecycle_impact_api_is_admin_only(app, client):
|
|
"""生命周期影响预览返回统一结构,普通用户不能读取删除资格。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
customer = ShippingCustomer(standard_name='生命周期客户', short_code='LIF', status='active')
|
|
db.session.add(customer)
|
|
db.session.commit()
|
|
customer_id = customer.id
|
|
|
|
forbidden = client.get(
|
|
f'/api/admin/shipping-customers/{customer_id}/lifecycle-impact',
|
|
headers=_headers('shipping-user', 'user'),
|
|
)
|
|
assert forbidden.status_code == 403
|
|
|
|
response = client.get(
|
|
f'/api/admin/shipping-customers/{customer_id}/lifecycle-impact',
|
|
headers=_headers('shipping-admin', 'admin'),
|
|
)
|
|
assert response.status_code == 200
|
|
impact = parse_api_data(response.get_json())
|
|
assert impact['can_delete'] is True
|
|
assert impact['cascade']['identifiers'] == 0
|
|
|
|
|
|
def test_shipping_customer_timeline_and_governance_quality_apis_are_admin_only(app, client):
|
|
"""治理时间线和质量汇总必须复用管理员权限,并返回自动确认门禁状态。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
customer = ShippingCustomer(standard_name='时间线客户', short_code='TIM', status='active')
|
|
db.session.add(customer)
|
|
db.session.flush()
|
|
from app.models import ShippingCustomerLifecycleEvent
|
|
|
|
db.session.add(ShippingCustomerLifecycleEvent(
|
|
event_type='created', customer_id_snapshot=customer.id,
|
|
customer_name_snapshot=customer.standard_name, customer_short_code_snapshot=customer.short_code,
|
|
detail_json={},
|
|
))
|
|
db.session.commit()
|
|
customer_id = customer.id
|
|
|
|
admin_headers = _headers('shipping-admin', 'admin')
|
|
for path in (
|
|
f'/api/admin/shipping-customers/{customer_id}/lifecycle-timeline',
|
|
'/api/admin/shipping-customers/governance-quality',
|
|
):
|
|
forbidden = client.get(path, headers=_headers('shipping-user', 'user'))
|
|
assert forbidden.status_code == 403
|
|
|
|
timeline = client.get(
|
|
f'/api/admin/shipping-customers/{customer_id}/lifecycle-timeline', headers=admin_headers,
|
|
)
|
|
assert timeline.status_code == 200
|
|
assert parse_api_data(timeline.get_json())['items'][0]['event_type'] == 'created'
|
|
|
|
quality = client.get('/api/admin/shipping-customers/governance-quality', headers=admin_headers)
|
|
assert quality.status_code == 200
|
|
payload = parse_api_data(quality.get_json())
|
|
assert payload['shadow_quality']['total'] == 0
|
|
assert payload['shadow_quality']['auto_resolution']['quality_gate']['status'] == 'insufficient_labeled_samples'
|
|
assert payload['lifecycle_event_counts']['created'] == 1
|
|
|
|
|
|
def test_shipping_customer_delete_api_is_admin_only_and_audited(app, client):
|
|
"""删除 API 仅管理员可用,并在成功后保留客户快照审计。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
customer = ShippingCustomer(standard_name='可删除客户', short_code='DEL', status='active')
|
|
db.session.add(customer)
|
|
db.session.flush()
|
|
db.session.add(ShippingCustomerIdentifier(
|
|
customer_id=customer.id, identifier_type='name_alias', raw_value='Delete Customer',
|
|
normalized_value='DELETECUSTOMER', status='approved', source='manual',
|
|
))
|
|
db.session.commit()
|
|
customer_id = customer.id
|
|
|
|
payload = {'confirmation_short_code': 'DEL', 'reason': '误建'}
|
|
forbidden = client.delete(
|
|
f'/api/admin/shipping-customers/{customer_id}', json=payload,
|
|
headers=_headers('shipping-user', 'user'),
|
|
)
|
|
assert forbidden.status_code == 403
|
|
|
|
response = client.delete(
|
|
f'/api/admin/shipping-customers/{customer_id}', json=payload,
|
|
headers=_headers('shipping-admin', 'admin'),
|
|
)
|
|
assert response.status_code == 200
|
|
deleted = parse_api_data(response.get_json())
|
|
assert deleted['deleted_customer']['id'] == customer_id
|
|
assert deleted['cascade']['identifiers'] == 1
|
|
|
|
missing = client.get(f'/api/admin/shipping-customers/{customer_id}', headers=_headers('shipping-admin', 'admin'))
|
|
assert missing.status_code == 404
|
|
|
|
|
|
def test_shipping_customer_delete_api_rejects_invalid_confirmation_and_blockers(app, client):
|
|
"""API 必须校验二次确认,并在删除提交时返回完整阻断明细。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
customer = ShippingCustomer(standard_name='有业务客户', short_code='BLK', status='active')
|
|
db.session.add(customer)
|
|
db.session.flush()
|
|
db.session.add(ShippingCase(case_no='CASE-DELETE-API', customer_id=customer.id))
|
|
db.session.commit()
|
|
customer_id = customer.id
|
|
|
|
headers = _headers('shipping-admin', 'admin')
|
|
invalid = client.delete(
|
|
f'/api/admin/shipping-customers/{customer_id}',
|
|
json={'confirmation_short_code': 'BAD', 'reason': '误建'}, headers=headers,
|
|
)
|
|
assert invalid.status_code == 400
|
|
assert parse_api_error(invalid.get_json())['code'] == 'SHIPPING.CUSTOMER_DELETE_CONFIRMATION_INVALID'
|
|
|
|
blocked = client.delete(
|
|
f'/api/admin/shipping-customers/{customer_id}',
|
|
json={'confirmation_short_code': 'BLK', 'reason': '误建'}, headers=headers,
|
|
)
|
|
assert blocked.status_code == 409
|
|
error = parse_api_error(blocked.get_json())
|
|
assert error['code'] == 'SHIPPING.CUSTOMER_DELETE_BLOCKED'
|
|
assert error['details']['blockers']['business_cases'] == 1
|
|
|
|
|
|
def test_shipping_customer_merge_api_is_admin_only_and_preserves_source_history(app, client):
|
|
"""管理员归并客户时仅迁移未来证据,源客户历史业务仍保持原关联。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
source = ShippingCustomer(standard_name='重复客户', short_code='SRC', status='active')
|
|
target = ShippingCustomer(standard_name='标准客户', short_code='TGT', status='active')
|
|
db.session.add_all([source, target])
|
|
db.session.flush()
|
|
db.session.add_all([
|
|
ShippingCustomerIdentifier(
|
|
customer_id=source.id, identifier_type='name_alias', raw_value='Source Alias',
|
|
normalized_value='SOURCEALIAS', status='approved', source='manual',
|
|
),
|
|
ShippingCase(case_no='CASE-MERGE-API', customer_id=source.id, business_no='SRC26071601'),
|
|
])
|
|
db.session.commit()
|
|
source_id = source.id
|
|
target_id = target.id
|
|
|
|
admin_headers = _headers('shipping-admin', 'admin')
|
|
forbidden = client.post(
|
|
f'/api/admin/shipping-customers/{source_id}/merge',
|
|
json={'target_customer_id': target_id, 'confirmation_short_code': 'SRC', 'reason': '重复建档'},
|
|
headers=_headers('shipping-user', 'user'),
|
|
)
|
|
assert forbidden.status_code == 403
|
|
|
|
preview = client.get(
|
|
f'/api/admin/shipping-customers/{source_id}/merge-impact?target_customer_id={target_id}',
|
|
headers=admin_headers,
|
|
)
|
|
assert preview.status_code == 200
|
|
assert parse_api_data(preview.get_json())['historical_references']['business_cases'] == 1
|
|
|
|
response = client.post(
|
|
f'/api/admin/shipping-customers/{source_id}/merge',
|
|
json={'target_customer_id': target_id, 'confirmation_short_code': 'SRC', 'reason': '重复建档'},
|
|
headers=admin_headers,
|
|
)
|
|
assert response.status_code == 200
|
|
result = parse_api_data(response.get_json())
|
|
assert result['source']['id'] == source_id
|
|
assert result['target']['id'] == target_id
|
|
with app.app_context():
|
|
assert db.session.get(ShippingCustomer, source_id).status == 'merged'
|
|
assert db.session.query(ShippingCustomerIdentifier).filter_by(customer_id=target_id).count() == 1
|
|
assert db.session.query(ShippingCase).filter_by(case_no='CASE-MERGE-API').one().customer_id == source_id
|
|
|
|
|
|
def test_shipping_customer_merge_api_validates_target_confirmation_and_reason(app, client):
|
|
"""合并 API 由服务端校验目标状态、源简码与原因,不能依赖前端限制。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
source = ShippingCustomer(standard_name='源客户', short_code='SRC', status='active')
|
|
target = ShippingCustomer(standard_name='停用目标', short_code='TGT', status='inactive')
|
|
db.session.add_all([source, target])
|
|
db.session.commit()
|
|
source_id = source.id
|
|
target_id = target.id
|
|
|
|
headers = _headers('shipping-admin', 'admin')
|
|
invalid_target = client.post(
|
|
f'/api/admin/shipping-customers/{source_id}/merge',
|
|
json={'target_customer_id': target_id, 'confirmation_short_code': 'SRC', 'reason': '重复'}, headers=headers,
|
|
)
|
|
assert invalid_target.status_code == 400
|
|
assert parse_api_error(invalid_target.get_json())['code'] == 'SHIPPING.CUSTOMER_MERGE_TARGET_INVALID'
|
|
|
|
empty_reason = client.post(
|
|
f'/api/admin/shipping-customers/{source_id}/merge',
|
|
json={'target_customer_id': source_id, 'confirmation_short_code': 'SRC', 'reason': ''}, headers=headers,
|
|
)
|
|
assert empty_reason.status_code == 400
|
|
assert parse_api_error(empty_reason.get_json())['code'] == 'SHIPPING.CUSTOMER_MERGE_REASON_REQUIRED'
|
|
|
|
|
|
def test_shipping_customer_historical_backfill_api_is_not_registered(app, client):
|
|
"""新契约不保留历史客户回填入口,管理员也不能调用。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
|
|
response = client.post(
|
|
'/api/admin/shipping-customers/historical-task-backfill',
|
|
json={'task_no': 'SN-20200101-HISTAPI', 'customer_id': 1},
|
|
headers=_headers('shipping-admin', 'admin'),
|
|
)
|
|
assert response.status_code == 404
|
|
|
|
|
|
def test_shipping_customer_shadow_report_and_label_api(app, client):
|
|
"""管理员可读取影子报告并写真值标签,普通用户不可访问。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
admin = db.session.query(User).filter_by(username='shipping-admin').one()
|
|
customer = ShippingCustomer(standard_name='ACME', short_code='ACM', status='active')
|
|
db.session.add(customer)
|
|
db.session.flush()
|
|
asset = FileAsset(
|
|
asset_no='FA-SHADOW-API', owner_user_id=admin.id, original_filename='notice.txt',
|
|
storage_provider='minio', storage_bucket='test', storage_key='shadow-api',
|
|
content_hash='b' * 64, file_size=1, upload_status='uploaded', lifecycle_status='temporary',
|
|
)
|
|
db.session.add(asset)
|
|
db.session.flush()
|
|
intake = ShippingOrderIntake(
|
|
intake_no='SNI-SHADOW-API', asset_id=asset.id, user_id=admin.id, parser_version='v6', parse_status='parsed',
|
|
)
|
|
db.session.add(intake)
|
|
db.session.flush()
|
|
document = ShippingOrderDocument(
|
|
intake_id=intake.id, asset_id=asset.id, document_type='attachment', attachment_key='file',
|
|
classification='order_notice', parse_status='parsed',
|
|
)
|
|
db.session.add(document)
|
|
db.session.flush()
|
|
candidate = ShippingOrderCandidate(
|
|
candidate_no='SNC-SHADOW-API', intake_id=intake.id, primary_document_id=document.id,
|
|
candidate_key='document:1', validation_status='ready',
|
|
)
|
|
db.session.add(candidate)
|
|
db.session.flush()
|
|
shadow = ShippingCustomerShadowResolution(
|
|
candidate_id=candidate.id, intake_id=intake.id, resolution_version='shadow-v1',
|
|
status='unique_candidate', evidence_json=[], candidate_matches_json=[],
|
|
top_customer_id=customer.id, top_score=60,
|
|
)
|
|
db.session.add(shadow)
|
|
db.session.commit()
|
|
shadow_id = shadow.id
|
|
customer_id = customer.id
|
|
|
|
admin_headers = _headers('shipping-admin', 'admin')
|
|
report_response = client.get('/api/admin/shipping-customer-shadow-report', headers=admin_headers)
|
|
assert report_response.status_code == 200
|
|
report = parse_api_data(report_response.get_json())
|
|
assert report['total'] == 1
|
|
assert report['items'][0]['candidate_no'] == 'SNC-SHADOW-API'
|
|
# 当前安全默认是开启受控自动客户确认;质量门禁仍决定是否允许实际自动确认。
|
|
assert report['auto_resolution']['enabled'] is True
|
|
assert report['auto_resolution']['recent_events'] == []
|
|
|
|
label_response = client.put(
|
|
f'/api/admin/shipping-customer-shadow-resolutions/{shadow_id}/label',
|
|
json={'customer_id': customer_id},
|
|
headers=admin_headers,
|
|
)
|
|
assert label_response.status_code == 200
|
|
assert parse_api_data(label_response.get_json())['is_correct'] is True
|
|
|
|
forbidden = client.get(
|
|
'/api/admin/shipping-customer-shadow-report',
|
|
headers=_headers('shipping-user', 'user'),
|
|
)
|
|
assert forbidden.status_code == 403
|
|
|
|
|
|
def test_shipping_customer_numbered_correction_api_preserves_old_number(app, client):
|
|
"""管理员纠错 API 作废原号并返回替代新号,普通用户不可调用。"""
|
|
app.config['AUTH_JWT_REQUIRED'] = False
|
|
with app.app_context():
|
|
_create_users()
|
|
wrong = ShippingCustomer(standard_name='错误客户', short_code='OLD', status='active')
|
|
correct = ShippingCustomer(standard_name='正确客户', short_code='NEW', status='active')
|
|
case = ShippingCase(case_no='CASE-CORRECTION-API', numbering_status='pending')
|
|
db.session.add_all([wrong, correct, case])
|
|
db.session.flush()
|
|
task = ShippingNotice(
|
|
task_no='SN-CORRECTION-API', shipping_case_id=case.id,
|
|
source_type='manual', customer_resolution_status='pending',
|
|
)
|
|
db.session.add(task)
|
|
db.session.commit()
|
|
from app.services.shipping_notice.customer_identity_gate import confirm_task_customer
|
|
|
|
confirm_task_customer(task, customer_id=wrong.id, actor_id=None)
|
|
old_business_no = task.shipping_case.business_no
|
|
correct_id = correct.id
|
|
|
|
forbidden = client.post(
|
|
'/api/admin/shipping-customers/numbered-task-correction',
|
|
json={'task_ref': old_business_no, 'customer_id': correct_id, 'reason': '客户错误'},
|
|
headers=_headers('shipping-user', 'user'),
|
|
)
|
|
assert forbidden.status_code == 403
|
|
|
|
response = client.post(
|
|
'/api/admin/shipping-customers/numbered-task-correction',
|
|
json={'task_ref': old_business_no, 'customer_id': correct_id, 'reason': '客户错误'},
|
|
headers=_headers('shipping-admin', 'admin'),
|
|
)
|
|
assert response.status_code == 200
|
|
payload = parse_api_data(response.get_json())
|
|
assert payload['old_business_no'] == old_business_no
|
|
assert payload['new_business_no'].startswith('NEW')
|
|
with app.app_context():
|
|
old_case = ShippingCase.query.filter_by(business_no=old_business_no).one()
|
|
replacement = ShippingCase.query.filter_by(replaces_case_id=old_case.id).one()
|
|
assert old_case.status == 'voided'
|
|
assert replacement.business_no == payload['new_business_no']
|