- 在环境配置中新增进度细化、订单提取取消与审计导出相关的开关 - 实现订单抽取运行的取消功能,允许管理员在运行中进行操作 - 引入审计事件的保留策略,支持三年保留期的自动清理 - 更新相关模型与服务,确保新功能的完整性与可追溯性 - 增强文档与代码注释,反映新的业务逻辑与审计要求
81 lines
3.0 KiB
Python
81 lines
3.0 KiB
Python
"""
|
|
backend/tests/test_shipping_access_audit_strict.py
|
|
|
|
用途:验证敏感访问审计必须逐次留存,失败时阻断调用。
|
|
依赖:fulfillment_audit.record_access_audit、ShippingAuditEvent、pytest app fixture。
|
|
逻辑:成功写入可查询;同资源连续两次访问落两条;模拟写入失败时 strict 抛错。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from app.core.errors import ServiceUnavailableError
|
|
from app.models import ShippingAuditEvent
|
|
from app.services.shipping_notice import fulfillment_audit
|
|
|
|
|
|
def test_access_audit_persists_and_is_queryable(app):
|
|
"""敏感访问审计写入后可按 action/aggregate 检索。"""
|
|
with app.app_context():
|
|
wrote = fulfillment_audit.record_access_audit(
|
|
action='view_order_extraction_run',
|
|
aggregate_type='order_extraction_run',
|
|
aggregate_id='SNER-ACCESS-1',
|
|
actor_id=42,
|
|
run_no='SNER-ACCESS-1',
|
|
metadata={'status': 'processing'},
|
|
)
|
|
assert wrote is True
|
|
row = ShippingAuditEvent.query.filter_by(
|
|
event_type='shipping_resource_accessed',
|
|
action='view_order_extraction_run',
|
|
aggregate_id='SNER-ACCESS-1',
|
|
).one()
|
|
assert row.actor_id == '42'
|
|
assert row.run_no == 'SNER-ACCESS-1'
|
|
|
|
|
|
def test_access_audit_records_every_call(app):
|
|
"""每次访问操作都必须独立留痕,禁止按窗口合并。"""
|
|
with app.app_context():
|
|
first = fulfillment_audit.record_access_audit(
|
|
action='view_order_extraction_documents',
|
|
aggregate_type='order_extraction_run',
|
|
aggregate_id='SNER-ACCESS-2',
|
|
actor_id=7,
|
|
run_no='SNER-ACCESS-2',
|
|
)
|
|
second = fulfillment_audit.record_access_audit(
|
|
action='view_order_extraction_documents',
|
|
aggregate_type='order_extraction_run',
|
|
aggregate_id='SNER-ACCESS-2',
|
|
actor_id=7,
|
|
run_no='SNER-ACCESS-2',
|
|
)
|
|
assert first is True
|
|
assert second is True
|
|
assert ShippingAuditEvent.query.filter_by(
|
|
action='view_order_extraction_documents',
|
|
aggregate_id='SNER-ACCESS-2',
|
|
).count() == 2
|
|
|
|
|
|
def test_access_audit_strict_raises_when_append_fails(app, monkeypatch):
|
|
"""审计写入失败时必须抛错,禁止业务侧静默成功。"""
|
|
with app.app_context():
|
|
def _boom(**_kwargs):
|
|
raise RuntimeError('audit table down')
|
|
|
|
monkeypatch.setattr(fulfillment_audit, 'append_shipping_audit_event', _boom)
|
|
with pytest.raises(ServiceUnavailableError) as exc:
|
|
fulfillment_audit.record_access_audit(
|
|
action='preview_order_review_document',
|
|
aggregate_type='order_review_case',
|
|
aggregate_id='CASE-1',
|
|
actor_id=1,
|
|
review_case_no='CASE-1',
|
|
strict=True,
|
|
)
|
|
assert exc.value.code == 'SHIPPING_NOTICE.AUDIT_PERSIST_FAILED'
|