119 lines
4.6 KiB
Python
119 lines
4.6 KiB
Python
"""
|
|
backend/tests/test_shipping_audit_immutability.py
|
|
|
|
用途:验收 ShippingAuditEvent / ShippingXfaAuditSnapshot ORM 层 append-only 与哈希链字段落库。
|
|
依赖:模型事件守卫、audit_service、audit_snapshot_service。
|
|
逻辑:已提交事件与 XFA 快照禁止 ORM update/delete;新写入必须带 event_hash 语义。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import ValidationError
|
|
from app.models import ShippingAuditEvent, ShippingXfaAuditSnapshot
|
|
from app.services.shipping_notice.audit_service import append_shipping_audit_event
|
|
from app.services.shipping_notice.audit_snapshot_service import create_xfa_audit_snapshot
|
|
|
|
|
|
def test_shipping_audit_event_orm_update_raises(app):
|
|
"""已提交审计事件经 ORM 更新必须抛 ValidationError。"""
|
|
with app.app_context():
|
|
event = append_shipping_audit_event(
|
|
event_type='workflow_item_created',
|
|
aggregate_type='shipping_notice_workflow_item',
|
|
aggregate_id='WFI-IMM-1',
|
|
workflow_id='WF-IMM-1',
|
|
workflow_item_id='WFI-IMM-1',
|
|
)
|
|
db.session.commit()
|
|
|
|
event.reason_code = 'tampered'
|
|
with pytest.raises(ValidationError, match='append-only'):
|
|
db.session.commit()
|
|
db.session.rollback()
|
|
|
|
|
|
def test_shipping_audit_event_orm_delete_raises(app):
|
|
"""已提交审计事件经 ORM 删除必须抛 ValidationError。"""
|
|
with app.app_context():
|
|
event = append_shipping_audit_event(
|
|
event_type='workflow_item_created',
|
|
aggregate_type='shipping_notice_workflow_item',
|
|
aggregate_id='WFI-IMM-2',
|
|
workflow_id='WF-IMM-2',
|
|
workflow_item_id='WFI-IMM-2',
|
|
)
|
|
db.session.commit()
|
|
event_id = int(event.id)
|
|
|
|
persisted = db.session.get(ShippingAuditEvent, event_id)
|
|
db.session.delete(persisted)
|
|
with pytest.raises(ValidationError, match='append-only'):
|
|
db.session.commit()
|
|
db.session.rollback()
|
|
|
|
# 关键断言:删除失败后记录仍在。
|
|
assert db.session.get(ShippingAuditEvent, event_id) is not None
|
|
|
|
|
|
def test_append_only_persists_hash_chain_fields(app):
|
|
"""append 写入后必须带 event_hash;同聚合第二条 previous 指向首条。"""
|
|
with app.app_context():
|
|
first = append_shipping_audit_event(
|
|
event_type='workflow_created',
|
|
aggregate_type='shipping_notice_workflow',
|
|
aggregate_id='WF-IMM-CHAIN',
|
|
workflow_id='WF-IMM-CHAIN',
|
|
after={'workflow_no': 'WF-IMM-CHAIN', 'lifecycle_status': 'open'},
|
|
)
|
|
second = append_shipping_audit_event(
|
|
event_type='workflow_stage_changed',
|
|
aggregate_type='shipping_notice_workflow',
|
|
aggregate_id='WF-IMM-CHAIN',
|
|
workflow_id='WF-IMM-CHAIN',
|
|
after={'workflow_no': 'WF-IMM-CHAIN', 'lifecycle_status': 'extracting'},
|
|
)
|
|
db.session.commit()
|
|
|
|
# 关键断言:哈希链字段已落库,形成 append-only 可校验事实。
|
|
assert first.event_hash
|
|
assert first.previous_event_hash is None
|
|
assert second.event_hash
|
|
assert second.previous_event_hash == first.event_hash
|
|
assert first.diff_json is not None or first.after_snapshot_json is not None
|
|
|
|
|
|
def test_xfa_audit_snapshot_orm_update_and_delete_blocked(app):
|
|
"""XFA 加密快照更新与删除被服务层阻断。"""
|
|
with app.app_context():
|
|
if not str(app.config.get('SECRET_KEY') or '').strip():
|
|
app.config['SECRET_KEY'] = 'test-shipping-xfa-audit-secret'
|
|
row = create_xfa_audit_snapshot(
|
|
payload={'definition_checksum': 'cs-1', 'definition': {'template_mode': 'xfa_dataset'}},
|
|
object_type='shipping_order_document_profile',
|
|
object_id='7',
|
|
purpose='full',
|
|
operation_id='op-imm-xfa',
|
|
)
|
|
db.session.commit()
|
|
snap_id = int(row.id)
|
|
|
|
persisted = db.session.get(ShippingXfaAuditSnapshot, snap_id)
|
|
persisted.ciphertext = 'tampered'
|
|
with pytest.raises(ValidationError, match='append-only'):
|
|
db.session.commit()
|
|
db.session.rollback()
|
|
|
|
persisted = db.session.get(ShippingXfaAuditSnapshot, snap_id)
|
|
db.session.delete(persisted)
|
|
with pytest.raises(ValidationError, match='append-only'):
|
|
db.session.commit()
|
|
db.session.rollback()
|
|
|
|
# 关键断言:改删失败后快照仍在且密文未变。
|
|
again = db.session.get(ShippingXfaAuditSnapshot, snap_id)
|
|
assert again is not None
|
|
assert again.ciphertext != 'tampered'
|