257 lines
11 KiB
Python
257 lines
11 KiB
Python
"""
|
||
tests/test_shipping_xfa_unified_audit.py
|
||
|
||
用途:验收统一视觉/拆分审计事件已注册,并在确认视觉绑定与保存草稿路径被触发;
|
||
confirm 路径双写平台审计含加密快照引用。
|
||
依赖:xfa_contract、progress_audit_contract、visual_binding_service、order_document_profile_service、
|
||
audit_service、ShippingAuditEvent、ShippingXfaAuditSnapshot。
|
||
逻辑:契约白名单断言 + mock 审计写入 + confirm 路径 snapshot refs 集成断言。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
from app import db
|
||
from app.models import ShippingAuditEvent, ShippingXfaAuditSnapshot
|
||
from app.services.shipping_notice.document_layout.xfa_contract import XFA_AUDIT_EVENT_TYPES
|
||
from app.services.shipping_notice.progress_audit_contract import XFA_PROGRESS_EVENT_CODES
|
||
from app.services.shipping_notice import order_document_profile_service as profiles
|
||
from app.services.shipping_notice import order_document_profile_visual_binding_service as visual_svc
|
||
from app.services.shipping_notice.order_document_profile_audit_service import (
|
||
XFA_VISUAL_BINDING_AUDIT_EVENTS,
|
||
build_profile_audit_context,
|
||
record_visual_binding_audit,
|
||
)
|
||
|
||
|
||
REQUIRED_UNIFIED_AUDIT_EVENTS = (
|
||
'xfa_visual_binding_suggested',
|
||
'xfa_visual_binding_confirmed',
|
||
'xfa_visual_binding_rejected',
|
||
'xfa_visual_binding_updated',
|
||
'xfa_split_rule_suggested',
|
||
'xfa_split_rule_previewed',
|
||
'xfa_profile_draft_saved',
|
||
'order_profile_unified_migration_dry_run',
|
||
'order_profile_unified_migration_executed',
|
||
)
|
||
|
||
|
||
def _ensure_secret_key(app) -> None:
|
||
"""确保 SECRET_KEY 可用于派生 XFA 审计快照 Fernet。"""
|
||
if not str(app.config.get('SECRET_KEY') or '').strip():
|
||
app.config['SECRET_KEY'] = 'test-shipping-xfa-visual-audit-secret'
|
||
|
||
|
||
def test_unified_visual_and_split_audit_events_registered():
|
||
"""关键审计事件必须同时出现在 XFA 契约与进度事件白名单。"""
|
||
for event in REQUIRED_UNIFIED_AUDIT_EVENTS:
|
||
assert event in XFA_AUDIT_EVENT_TYPES, event
|
||
assert event in XFA_PROGRESS_EVENT_CODES, event
|
||
for event in XFA_VISUAL_BINDING_AUDIT_EVENTS:
|
||
assert event in XFA_AUDIT_EVENT_TYPES
|
||
|
||
|
||
def test_confirm_visual_binding_fires_confirmed_audit():
|
||
"""确认视觉绑定路径必须写入 xfa_visual_binding_confirmed(Profile _audit)。"""
|
||
with patch.object(profiles, '_audit') as audit_mock:
|
||
with patch(
|
||
'app.services.shipping_notice.audit_service.append_shipping_audit_event',
|
||
) as platform_mock:
|
||
record_visual_binding_audit(
|
||
profile_id=11,
|
||
document_id=None,
|
||
event_type='xfa_visual_binding_confirmed',
|
||
actor_id=3,
|
||
before={'status': 'suggested', 'field_code': 'part_number'},
|
||
after={'status': 'confirmed', 'field_code': 'part_number'},
|
||
detail={'binding_id': 'bind-1', 'action': 'confirm'},
|
||
operation_id='op-confirm-1',
|
||
profile_context={
|
||
'profile_code': 'PO-XFA',
|
||
'profile_version': 1,
|
||
'definition_checksum': 'sha256:abc',
|
||
'revision_no': 2,
|
||
'template_mode': 'xfa_dataset',
|
||
},
|
||
)
|
||
assert audit_mock.call_count == 1
|
||
kwargs = audit_mock.call_args.kwargs
|
||
assert kwargs['event_type'] == 'xfa_visual_binding_confirmed'
|
||
assert kwargs['detail']['before_visual_binding']['status'] == 'suggested'
|
||
assert kwargs['detail']['after_visual_binding']['status'] == 'confirmed'
|
||
assert platform_mock.call_count == 1
|
||
platform_kwargs = platform_mock.call_args.kwargs
|
||
assert platform_kwargs['event_type'] == 'xfa_visual_binding_confirmed'
|
||
assert platform_kwargs['aggregate_type'] == 'shipping_order_document_profile'
|
||
assert platform_kwargs['aggregate_id'] == '11'
|
||
assert platform_kwargs['sensitivity_level'] == 'sensitive'
|
||
assert platform_kwargs['context_refs']['binding_id'] == 'bind-1'
|
||
assert platform_kwargs['context_refs']['action'] == 'confirm'
|
||
|
||
|
||
def test_confirm_visual_binding_platform_audit_has_snapshot_refs(app):
|
||
"""confirm 路径双写平台事件含加密 full 快照引用。"""
|
||
with app.app_context():
|
||
_ensure_secret_key(app)
|
||
record_visual_binding_audit(
|
||
profile_id=77,
|
||
document_id=None,
|
||
event_type='xfa_visual_binding_confirmed',
|
||
actor_id=3,
|
||
before={'status': 'suggested', 'field_code': 'part_number', 'binding_id': 'bind-77'},
|
||
after={'status': 'confirmed', 'field_code': 'part_number', 'binding_id': 'bind-77'},
|
||
detail={'binding_id': 'bind-77', 'action': 'confirm'},
|
||
operation_id='op-confirm-snap',
|
||
request_id='req-confirm-snap',
|
||
raw_binding_before={'binding_id': 'bind-77', 'field_code': 'part_number', 'visual_binding': {'status': 'suggested'}},
|
||
raw_binding_after={'binding_id': 'bind-77', 'field_code': 'part_number', 'visual_binding': {'status': 'confirmed'}},
|
||
profile_context={
|
||
'profile_code': 'PO-XFA-77',
|
||
'profile_version': 3,
|
||
'definition_checksum': 'sha256:before-cs',
|
||
'revision_no': 4,
|
||
'template_mode': 'xfa_dataset',
|
||
},
|
||
)
|
||
db.session.commit()
|
||
|
||
event = (
|
||
ShippingAuditEvent.query
|
||
.filter_by(
|
||
aggregate_type='shipping_order_document_profile',
|
||
aggregate_id='77',
|
||
event_type='xfa_visual_binding_confirmed',
|
||
)
|
||
.order_by(ShippingAuditEvent.id.desc())
|
||
.first()
|
||
)
|
||
assert event is not None
|
||
assert event.operation_id == 'op-confirm-snap'
|
||
assert event.request_id == 'req-confirm-snap'
|
||
ctx = event.context_refs_json or {}
|
||
assert ctx.get('snapshot_before_ref', '').startswith('xfa-snap-')
|
||
assert ctx.get('snapshot_after_ref', '').startswith('xfa-snap-')
|
||
assert ctx.get('binding_id') == 'bind-77'
|
||
assert ctx.get('action') == 'confirm'
|
||
assert event.before_hash
|
||
assert event.after_hash
|
||
assert ShippingXfaAuditSnapshot.query.filter_by(object_id='77').count() >= 2
|
||
assert event.metadata_json.get('binding_id') == 'bind-77'
|
||
|
||
|
||
def test_save_draft_path_emits_draft_saved_audit_hook():
|
||
"""保存草稿路径上的 binding/split diff 审计钩子可被调用(mock)。"""
|
||
before = {'bindings': [], 'split_rules': []}
|
||
after = {
|
||
'bindings': [{
|
||
'binding_id': 'bind-0001-aaaa',
|
||
'field_id': 'fld-1',
|
||
'field_code': 'part_number',
|
||
'scope': 'line',
|
||
'value_binding': {
|
||
'type': 'xfa_dataset',
|
||
'path_id': 'path-1',
|
||
'data_path': '/data/ITEMS/DATA/MATNR',
|
||
},
|
||
'visual_binding': {
|
||
'page_no': 1,
|
||
'normalized_bbox': {'x0': 0.1, 'y0': 0.2, 'x1': 0.3, 'y1': 0.25},
|
||
'source': 'manual_draw',
|
||
'status': 'confirmed',
|
||
'confidence': 1.0,
|
||
},
|
||
}],
|
||
'split_rules': [],
|
||
}
|
||
with patch.object(profiles, '_audit') as audit_mock:
|
||
profiles._audit_xfa_binding_changes(
|
||
profile_id=21,
|
||
actor_id=5,
|
||
operation_id='op-save-1',
|
||
browser_session_id='tab-1',
|
||
previous_definition=before,
|
||
next_definition=after,
|
||
)
|
||
profiles._audit(
|
||
profile_id=21,
|
||
event_type='xfa_profile_draft_saved',
|
||
actor_id=5,
|
||
operation_id='op-save-1',
|
||
detail={'definition_checksum': 'sha256:test'},
|
||
)
|
||
events = [call.kwargs['event_type'] for call in audit_mock.call_args_list]
|
||
assert 'xfa_profile_draft_saved' in events
|
||
# binding 变更至少产出一条审计(created/updated 族)
|
||
assert any(
|
||
event.startswith('xfa_visual_binding_') or event == 'xfa_mapping_saved' or 'binding' in event
|
||
for event in events
|
||
) or audit_mock.call_count >= 2
|
||
|
||
|
||
def test_confirm_service_calls_record_audit_when_definition_ready():
|
||
"""confirm_xfa_visual_binding 在成功路径委托 record_visual_binding_audit(mock 编排)。"""
|
||
profile = MagicMock()
|
||
profile.id = 9
|
||
profile.status = 'draft'
|
||
profile.template_mode = 'xfa_dataset'
|
||
profile.definition_json = {
|
||
'template_mode': 'xfa_dataset',
|
||
'source_contract': {
|
||
'template_packet_checksum': 'sha256:' + ('a' * 64),
|
||
'dataset_structure_fingerprint': 'sha256:' + ('b' * 64),
|
||
'dataset_root_path': '/data',
|
||
'authoritative_line_collection_path': '/data/ITEMS/DATA',
|
||
'mirror_collection_paths': [],
|
||
},
|
||
'header_mappings': [],
|
||
'line_mapping': {'row_path': '/data/ITEMS/DATA', 'fields': []},
|
||
'bindings': [{
|
||
'binding_id': 'bind-0001-aaaa',
|
||
'field_id': 'fld-1',
|
||
'field_code': 'part_number',
|
||
'scope': 'line',
|
||
'value_binding': {
|
||
'type': 'xfa_dataset',
|
||
'path_id': 'path-1',
|
||
'data_path': '/data/ITEMS/DATA/MATNR',
|
||
},
|
||
'visual_binding': {
|
||
'page_no': 1,
|
||
'normalized_bbox': {'x0': 0.1, 'y0': 0.2, 'x1': 0.3, 'y1': 0.25},
|
||
'source': 'suggested',
|
||
'status': 'suggested',
|
||
'confidence': 0.5,
|
||
},
|
||
}],
|
||
'split_rules': [],
|
||
}
|
||
with (
|
||
patch.object(visual_svc, '_profile', return_value=profile),
|
||
patch.object(visual_svc.lease_service, 'require_active_lease'),
|
||
patch.object(visual_svc, 'record_visual_binding_audit') as audit_mock,
|
||
patch.object(visual_svc.db.session, 'flush'),
|
||
patch.object(visual_svc.db.session, 'commit'),
|
||
):
|
||
result = visual_svc.confirm_xfa_visual_binding(
|
||
9,
|
||
'bind-0001-aaaa',
|
||
visual_binding={
|
||
'page_no': 1,
|
||
'normalized_bbox': {'x0': 0.1, 'y0': 0.2, 'x1': 0.3, 'y1': 0.25},
|
||
'source': 'manual_draw',
|
||
'confidence': 1.0,
|
||
},
|
||
path_id=None,
|
||
actor_id=7,
|
||
browser_session_id='sess',
|
||
lease_token='lease',
|
||
operation_id='op-1',
|
||
preview_render_checksum='sha256:' + ('d' * 64),
|
||
render_mode='xfa_dataset_preview',
|
||
)
|
||
assert result['status'] == 'confirmed'
|
||
assert audit_mock.call_count == 1
|
||
assert audit_mock.call_args.kwargs['event_type'] == 'xfa_visual_binding_confirmed'
|