Files
syops/backend/tests/test_admin_shipping_receipt_config.py
hugo 2199370065 feat(rpa): 收料配置与库存同构并统一站点启停
- 删除收料配置 enabled 与 warehouse_ids_json 列(迁移 rc1a2b3c4d5e、rc2b3c4d5e6f,含存量清单转 Warehouse 条件的数据保全)
- Warehouse 改为查询条件(多值即逐仓拆分维度),排程/按需触发运行时派生仓库清单并在单元快照剥离该条件
- 站点开关即总开关:四处收料门禁只看站点 enabled;校验要求 DBS 恰一条非空 Warehouse 条件,日期禁填不变
- 站点抽屉改纵向折叠卡片与统一「保存更改」,就绪卡压缩为单行状态条并下移
- technical.md 同步设计边界与接口契约
2026-09-18 15:04:33 +08:00

209 lines
10 KiB
Python

"""
收料报表配置管理接口测试。
用途:验证站点内独立收料配置的权限、DBS/Crane 规则及不可影响库存配置的边界。
依赖:Flask 管理 API、库存站点测试种子、货代统一审计事件。
逻辑:配置保存只更新 shipping_receipt_report_configs,并留存前后快照审计。
"""
from __future__ import annotations
from app.models import ShippingAuditEvent, ShippingInventorySource, User
from tests.conftest import parse_api_data, parse_api_error
def _headers(username: str, role: str) -> dict[str, str]:
"""构造当前管理 API 使用的 Header 身份上下文。"""
return {'X-Username': username, 'X-Role': role, 'Content-Type': 'application/json'}
def _create_users() -> None:
"""创建管理员和普通接收人,验证收料配置不向业务用户开放。"""
from app import db
db.session.add_all([
User(username='receipt-admin', email='receipt-admin@example.com', role='admin', status='active'),
User(username='receipt-user', email='receipt-user@example.com', role='user', status='active'),
])
db.session.commit()
def _dbs_payload() -> dict:
"""构造 DBS 收料配置;Warehouse 条件即逐仓拆分维度,日期未出现表示保持系统页面默认 NULL。"""
return {
'report_path_json': {
'program_group': 'Bison', 'program_label': 'AWSBI',
'report_group': 'Inbound', 'report_name': 'Receipt Detail',
},
'query_parameters_json': [
{'name': 'Warehouse', 'mode': 'multi_text', 'values': ['AWSDU', 'AWSDU-02'], 'required': False, 'select_all': False},
{'name': 'Owner', 'mode': 'single', 'values': ['AWS'], 'required': False, 'select_all': False},
],
}
def test_admin_saves_independent_dbs_receipt_config_and_audit(app, client):
"""DBS 收料配置按站点保存,不能变更既有库存路径、条件或版本。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
source_id = source.id
inventory_before = {
'path': dict(source.report_path_json), 'parameters': list(source.query_parameters_json),
'config_version': source.config_version,
}
forbidden = client.put(
f'/api/admin/shipping-notice/sources/{source_id}/receipt-report-config',
json=_dbs_payload(), headers=_headers('receipt-user', 'user'),
)
assert forbidden.status_code == 403
response = client.put(
f'/api/admin/shipping-notice/sources/{source_id}/receipt-report-config',
json=_dbs_payload(), headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 200
config = parse_api_data(response.get_json())
# 关键断言:Warehouse 条件原样保存,作为逐仓拆分维度的唯一持久化位置。
warehouse_condition = next(item for item in config['query_parameters_json'] if item['name'] == 'Warehouse')
assert warehouse_condition['values'] == ['AWSDU', 'AWSDU-02']
assert config['report_path_json']['report_group'] == 'Inbound'
with app.app_context():
source = ShippingInventorySource.query.get(source_id)
# 关键断言:收料配置不会借由站点抽屉保存改写库存 RPA 定义。
assert source.report_path_json == inventory_before['path']
assert source.query_parameters_json == inventory_before['parameters']
assert source.config_version == inventory_before['config_version']
audit = ShippingAuditEvent.query.filter_by(
event_type='shipping_receipt_report_config_saved', aggregate_id=str(source_id),
).one()
assert audit.before_snapshot_json is None
assert audit.after_snapshot_json['query_parameters_json'][0]['values'] == ['AWSDU', 'AWSDU-02']
def test_receipt_config_rejects_date_selector_and_invalid_connector_rules(app, client):
"""拒绝日期/selector 等 Driver 细节;DBS 与 Crane 的 Warehouse 规则必须分开。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
dbs = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
dbs_id = dbs.id
from app import db
crane_profile = dbs.rpa_profile.__class__(
profile_code='CRANE_RECEIPT_CFG', display_name='Crane Receipt', connector_type='crane',
login_url='https://crane.example.invalid', login_config_json={'auth_mode': 'password'},
)
crane_source = ShippingInventorySource(
source_code='CRANE_RECEIPT_CFG', display_name='Crane Receipt', connector_type='crane',
rpa_profile=crane_profile, warehouse_resolution_mode='fixed', fixed_warehouse_id='CRANE',
report_path_json={}, query_parameters_json=[], enabled=False,
)
db.session.add(crane_source)
db.session.commit()
crane_id = crane_source.id
invalid_date = _dbs_payload()
invalid_date['query_parameters_json'] = [{'name': 'Report Date', 'mode': 'single', 'values': ['2026-08-19'], 'required': False, 'select_all': False}]
response = client.put(
f'/api/admin/shipping-notice/sources/{dbs_id}/receipt-report-config',
json=invalid_date, headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 400
assert parse_api_error(response.get_json())['code'] == 'SHIPPING.RECEIPT_REPORT_CONFIG_INVALID'
invalid_selector = _dbs_payload()
invalid_selector['query_parameters_json'] = [{'name': 'Owner', 'mode': 'single', 'values': [], 'selector': '#owner'}]
response = client.put(
f'/api/admin/shipping-notice/sources/{dbs_id}/receipt-report-config',
json=invalid_selector, headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 400
assert parse_api_error(response.get_json())['code'] == 'SHIPPING.RECEIPT_REPORT_CONFIG_INVALID'
# 关键断言:Warehouse 条件即逐仓拆分维度——缺失、重复、空值都必须拒绝。
missing_warehouse = _dbs_payload()
missing_warehouse['query_parameters_json'] = [{'name': 'Owner', 'mode': 'single', 'values': ['AWS'], 'required': False, 'select_all': False}]
response = client.put(
f'/api/admin/shipping-notice/sources/{dbs_id}/receipt-report-config',
json=missing_warehouse, headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 400
assert parse_api_error(response.get_json())['code'] == 'SHIPPING.RECEIPT_REPORT_CONFIG_INVALID'
duplicate_warehouse = _dbs_payload()
duplicate_warehouse['query_parameters_json'] = [
{'name': 'Warehouse', 'mode': 'single', 'values': ['AWSDU'], 'required': False, 'select_all': False},
{'name': 'Warehouse', 'mode': 'single', 'values': ['AWSDU-02'], 'required': False, 'select_all': False},
]
response = client.put(
f'/api/admin/shipping-notice/sources/{dbs_id}/receipt-report-config',
json=duplicate_warehouse, headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 400
assert parse_api_error(response.get_json())['code'] == 'SHIPPING.RECEIPT_REPORT_CONFIG_INVALID'
empty_warehouse = _dbs_payload()
empty_warehouse['query_parameters_json'] = [{'name': 'Warehouse', 'mode': 'multi_text', 'values': [], 'required': False, 'select_all': False}]
response = client.put(
f'/api/admin/shipping-notice/sources/{dbs_id}/receipt-report-config',
json=empty_warehouse, headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 400
assert parse_api_error(response.get_json())['code'] == 'SHIPPING.RECEIPT_REPORT_CONFIG_INVALID'
invalid_crane = {
'report_path_json': {'report_name': 'Other Report'},
'query_parameters_json': [],
}
response = client.put(
f'/api/admin/shipping-notice/sources/{crane_id}/receipt-report-config',
json=invalid_crane, headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 400
assert parse_api_error(response.get_json())['code'] == 'SHIPPING.RECEIPT_REPORT_CONFIG_INVALID'
def test_crane_receipt_config_rejects_query_parameters(app, client):
"""Crane 固定使用页面默认值直接 Run Report,管理员不能维护查询条件。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
from app import db
dbs = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
crane_profile = dbs.rpa_profile.__class__(
profile_code='CRANE_RECEIPT_DEFAULT_CFG', display_name='Crane Receipt Defaults', connector_type='crane',
login_url='https://crane.example.invalid', login_config_json={'auth_mode': 'password'},
)
crane_source = ShippingInventorySource(
source_code='CRANE_RECEIPT_DEFAULT_CFG', display_name='Crane Receipt Defaults', connector_type='crane',
rpa_profile=crane_profile, warehouse_resolution_mode='fixed', fixed_warehouse_id='CRANE',
report_path_json={}, query_parameters_json=[], enabled=False,
)
db.session.add(crane_source)
db.session.commit()
crane_id = crane_source.id
default_query = {
'report_path_json': {'report_name': 'Inbound Details'},
'query_parameters_json': [],
}
response = client.put(
f'/api/admin/shipping-notice/sources/{crane_id}/receipt-report-config',
json=default_query, headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 200
assert parse_api_data(response.get_json())['query_parameters_json'] == []
configured = {**default_query, 'query_parameters_json': [
{'name': 'material', 'mode': 'single', 'values': ['PN-01'], 'required': False, 'select_all': False},
]}
rejected = client.put(
f'/api/admin/shipping-notice/sources/{crane_id}/receipt-report-config',
json=configured, headers=_headers('receipt-admin', 'admin'),
)
assert rejected.status_code == 400
assert parse_api_error(rejected.get_json())['code'] == 'SHIPPING.RECEIPT_REPORT_CONFIG_INVALID'