- 更新发货通知工具 API,剥离不可信的操作人身份字段,确保审计安全 - 引入审计事件的哈希链校验机制,支持数据完整性验证 - 增强订单抽取与审计功能,支持敏感快照的访问控制 - 更新相关模型与服务,确保新功能的完整性与可追溯性 - 完善文档与代码注释,反映新的业务逻辑与审计要求
85 lines
3.2 KiB
Python
85 lines
3.2 KiB
Python
"""
|
|
backend/tests/test_shipping_audit_immutability.py
|
|
|
|
用途:验收 ShippingAuditEvent ORM 层 append-only 与哈希链字段落库。
|
|
依赖:ShippingAuditEvent 模型事件守卫、audit_service.append_shipping_audit_event。
|
|
逻辑:已提交事件禁止 ORM update/delete;新写入必须带 event_hash / previous_event_hash 字段语义。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import ValidationError
|
|
from app.models import ShippingAuditEvent
|
|
from app.services.shipping_notice.audit_service import append_shipping_audit_event
|
|
|
|
|
|
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
|