""" 收料报表配置管理接口测试。 用途:验证站点内独立收料配置的权限、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 收料配置;日期未出现表示保持系统页面默认 NULL。""" return { 'enabled': True, 'report_path_json': { 'program_group': 'Bison', 'program_label': 'AWSBI', 'report_group': 'Inbound', 'report_name': 'Receipt Detail', }, 'warehouse_ids_json': ['AWSDU', 'AWSDU-02'], 'query_parameters_json': [{'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()) assert config['warehouse_ids_json'] == ['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['warehouse_ids_json'] == ['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' invalid_crane = { 'enabled': True, 'report_path_json': {'report_name': 'Other Report'}, 'warehouse_ids_json': ['NOT-ALLOWED'], '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 = { 'enabled': True, 'report_path_json': {'report_name': 'Inbound Details'}, 'warehouse_ids_json': [], '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'