""" backend/tests/test_freight_receipt_rpa_access.py 用途:入站 RPA 管理员补偿 API 契约(失败单元列表/重试);工作台收料文件与 resend 已拆除。 依赖:Flask test client、freight_rpa 入站模型与 schedule runs 路由。 逻辑:仅覆盖 /api/admin/inbound-rpa-units 与 retry;审计 surface 为 admin_inbound_rpa_units。 """ from __future__ import annotations def _headers(username: str, role: str) -> dict[str, str]: return {'X-Username': username, 'X-Role': role, 'Content-Type': 'application/json'} def test_admin_retry_creates_a_new_receipt_unit_linked_to_failed_unit(app, client, monkeypatch): """失败单元重试保留旧失败事实,创建新的 callback 单元和独立 trigger。""" from app import db from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun, User app.config['AUTH_JWT_REQUIRED'] = False with app.app_context(): admin = User(username='receipt-retry-admin', email='receipt-retry-admin@example.com', role='admin', status='active') db.session.add(admin); db.session.flush() source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one() task = ScheduleTask(schedule_no='SCH-RECEIPT-RETRY', name='Receipt retry', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=admin.id, cron_expr='0 8 * * *') db.session.add(task); db.session.flush() run = ScheduleRun(run_no='RUN-RECEIPT-RETRY', schedule_task_id=task.id, status='failed') db.session.add(run); db.session.flush() failed = FreightRpaInboundUnitRun(schedule_run_id=run.id, source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-failed', status='failed', config_snapshot_json={'report_path_json': {}, 'query_parameters_json': []}) db.session.add(failed); db.session.commit() failed_id = failed.id captured = [] monkeypatch.setattr('app.api.schedule.runs.write_inbound_export_batch_trigger', lambda run_no, units, payload: captured.append((run_no, units, payload)) or 'retry-trigger.json') response = client.post(f'/api/admin/inbound-rpa-units/{failed_id}/retry', json={'reason': 'portal recovered'}, headers=_headers('receipt-retry-admin', 'admin')) assert response.status_code == 200 with app.app_context(): retry = FreightRpaInboundUnitRun.query.filter_by(retry_of_id=failed_id).one() assert retry.status == 'awaiting_rpa' assert retry.callback_id != 'receipt-failed' assert retry.callback_id.startswith('inbound:') assert captured and captured[0][1][0]['callback_id'] == retry.callback_id def test_admin_lists_failed_receipt_units_for_manual_compensation(app, client): """管理员可见失败单元用于人工补偿;该接口会记录管理员查看审计。""" from app import db from app.models import ScheduleRun, ScheduleTask, ShippingAuditEvent, FreightRpaSource, FreightRpaInboundUnitRun, User app.config['AUTH_JWT_REQUIRED'] = False with app.app_context(): admin = User(username='receipt-units-admin', email='receipt-units-admin@example.com', role='admin', status='active') db.session.add(admin); db.session.flush() source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one() task = ScheduleTask(schedule_no='SCH-RECEIPT-UNITS', name='Receipt units', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=admin.id, cron_expr='0 8 * * *') db.session.add(task); db.session.flush() run = ScheduleRun(run_no='RUN-RECEIPT-UNITS', schedule_task_id=task.id, status='failed') db.session.add(run); db.session.flush() unit = FreightRpaInboundUnitRun(schedule_run_id=run.id, source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-admin-list', status='failed', error_code='RPA.INBOUND_EXPORT_FAILED', config_snapshot_json={}) db.session.add(unit); db.session.commit() unit_id = unit.id response = client.get('/api/admin/inbound-rpa-units?status=failed', headers=_headers('receipt-units-admin', 'admin')) assert response.status_code == 200 assert [item['id'] for item in response.get_json()['data']['items']] == [unit_id] with app.app_context(): listed = ShippingAuditEvent.query.filter_by(event_type='shipping_inbound_rpa_units_listed').one() assert listed.metadata_json.get('surface') == 'admin_inbound_rpa_units' def test_admin_retry_inbound_unit_messages_use_inbound_not_receipt(app, client): """入站 retry 面向用户的英文错误须用 Inbound,不得再写 Receipt。""" from app import db from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun, User from tests.conftest import parse_api_error app.config['AUTH_JWT_REQUIRED'] = False with app.app_context(): admin = User( username='inbound-retry-msg-admin', email='inbound-retry-msg-admin@example.com', role='admin', status='active', ) db.session.add(admin) db.session.flush() source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one() task = ScheduleTask( schedule_no='SCH-INBOUND-RETRY-MSG', name='Inbound retry messages', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=admin.id, cron_expr='0 8 * * *', ) db.session.add(task) db.session.flush() run = ScheduleRun(run_no='RUN-INBOUND-RETRY-MSG', schedule_task_id=task.id, status='failed') db.session.add(run) db.session.flush() success = FreightRpaInboundUnitRun( schedule_run_id=run.id, source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='inbound-success-no-retry', status='success', config_snapshot_json={}, ) db.session.add(success) db.session.commit() success_id = success.id missing = client.post( '/api/admin/inbound-rpa-units/999999/retry', json={'reason': 'should 404'}, headers=_headers('inbound-retry-msg-admin', 'admin'), ) assert missing.status_code == 404 assert parse_api_error(missing.get_json())['message'] == 'Inbound RPA unit not found' blocked = client.post( f'/api/admin/inbound-rpa-units/{success_id}/retry', json={'reason': 'should conflict'}, headers=_headers('inbound-retry-msg-admin', 'admin'), ) assert blocked.status_code == 409 assert parse_api_error(blocked.get_json())['message'] == 'Only failed inbound RPA units can be retried'