feat(shipping-notice): 增强发货通知与审计功能,支持敏感数据控制与哈希链校验

- 更新发货通知工具 API,剥离不可信的操作人身份字段,确保审计安全
- 引入审计事件的哈希链校验机制,支持数据完整性验证
- 增强订单抽取与审计功能,支持敏感快照的访问控制
- 更新相关模型与服务,确保新功能的完整性与可追溯性
- 完善文档与代码注释,反映新的业务逻辑与审计要求
This commit is contained in:
hugo committed 2026-07-31 08:59:44 +08:00
1 parent 95cdfe66b0
commit d0fe084999
225 files changed
+25923 -6653

No files matched your search

@@ -0,0 +1,96 @@
"""
backend/tests/test_shipping_audit_db_immutability.py
用途:验收 shipping_audit_events 不可变约束(ORM 层始终;PostgreSQL 触发器在 CI/生产)。
依赖:ShippingAuditEvent ORM 守卫、audit_service.append_shipping_audit_event、SQLAlchemy dialect。
逻辑:pytest TestingConfig 强制 SQLite create_all,无 DB 触发器 —— 仅断言 ORM append-only;
触发器断言在非 PostgreSQL 时 pytest.skip(迁移 a0e1f2b3c4d5 仅对 PG 生效)。
目标 PostgreSQL 环境请另跑:
``python scripts/verify_shipping_audit_pg_immutability.py``(日志不输出连接凭据)。
"""
from __future__ import annotations
import pytest
from sqlalchemy import text
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 _is_postgresql() -> bool:
"""当前应用绑定是否为 PostgreSQL。"""
bind = db.session.bind
return bind is not None and bind.dialect.name == 'postgresql'
def test_shipping_audit_orm_immutability_still_holds(app):
"""任意方言:已提交审计经 ORM update/delete 必须被拒绝(测试库通常无 DB 触发器)。"""
with app.app_context():
event = append_shipping_audit_event(
event_type='workflow_item_created',
aggregate_type='shipping_notice_workflow_item',
aggregate_id='WFI-DB-IMM-1',
workflow_id='WF-DB-IMM-1',
workflow_item_id='WFI-DB-IMM-1',
)
db.session.commit()
event_id = int(event.id)
event.reason_code = 'tampered'
with pytest.raises(ValidationError, match='append-only'):
db.session.commit()
db.session.rollback()
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_shipping_audit_pg_trigger_blocks_core_update(app):
"""PostgreSQL:Core UPDATE 须被触发器拒绝;SQLite 跳过。"""
with app.app_context():
if not _is_postgresql():
pytest.skip('DB triggers only exist on PostgreSQL after migration a0e1f2b3c4d5')
event = append_shipping_audit_event(
event_type='workflow_created',
aggregate_type='shipping_notice_workflow',
aggregate_id='WF-DB-IMM-TRIG',
workflow_id='WF-DB-IMM-TRIG',
)
db.session.commit()
with pytest.raises(Exception):
db.session.execute(
text('UPDATE shipping_audit_events SET reason_code = :rc WHERE id = :eid'),
{'rc': 'x', 'eid': int(event.id)},
)
db.session.commit()
db.session.rollback()
def test_shipping_audit_pg_trigger_blocks_core_delete_without_purge_guc(app):
"""PostgreSQL:未设置 purge GUC 时 Core DELETE 须被触发器拒绝;SQLite 跳过。"""
with app.app_context():
if not _is_postgresql():
pytest.skip('DB triggers only exist on PostgreSQL after migration a0e1f2b3c4d5')
event = append_shipping_audit_event(
event_type='workflow_created',
aggregate_type='shipping_notice_workflow',
aggregate_id='WF-DB-IMM-DEL',
workflow_id='WF-DB-IMM-DEL',
)
db.session.commit()
with pytest.raises(Exception):
db.session.execute(
text('DELETE FROM shipping_audit_events WHERE id = :eid'),
{'eid': int(event.id)},
)
db.session.commit()
db.session.rollback()
assert db.session.get(ShippingAuditEvent, int(event.id)) is not None