feat(shipping-notice): 落地 XFA 统一视觉拆分确认门禁与处理投影收敛
闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。 同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d2b3d74def
commit
84a881b554
195 files changed
+20269
-3463
No files matched your search
@@ -0,0 +1,131 @@
|
||||
"""
|
||||
tests/test_shipping_xfa_page_preview.py
|
||||
|
||||
用途:验收阶段 2 XFA PDF 页面预览不再被禁令阻断,且令牌绑定 actor/profile/页。
|
||||
依赖:preview_service、pytest app fixture、XFA phase2 helpers。
|
||||
逻辑:mock PDFium 渲染;断言 authorize 不抛 XFA 禁令;令牌跨 actor/样本隔离。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from app.services.shipping_notice import order_document_profile_preview_service as preview_service
|
||||
from tests.test_order_document_profile_preview_service import _PreviewStore
|
||||
|
||||
|
||||
def test_xfa_profile_frozen_preview_no_longer_raises_blank_pdf_ban(app, monkeypatch):
|
||||
"""关键断言:XFA Profile 签发冻结样本预览时不得再抛 blank PDF 禁令。"""
|
||||
profile = SimpleNamespace(id=1, template_mode='xfa_dataset', definition_json={'template_mode': 'xfa_dataset'})
|
||||
sample = SimpleNamespace(id=2, page_count_snapshot=None)
|
||||
document = SimpleNamespace(id=3)
|
||||
asset = SimpleNamespace()
|
||||
layout = SimpleNamespace()
|
||||
|
||||
monkeypatch.setattr(
|
||||
preview_service,
|
||||
'_frozen_sample_context',
|
||||
lambda **_kwargs: (profile, sample, document, layout, asset),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
preview_service,
|
||||
'_render_pdf_pages',
|
||||
lambda _asset: [
|
||||
SimpleNamespace(page_no=1, image=np.zeros((8, 8, 3), dtype=np.uint8)),
|
||||
SimpleNamespace(page_no=2, image=np.zeros((8, 8, 3), dtype=np.uint8)),
|
||||
SimpleNamespace(page_no=3, image=np.zeros((8, 8, 3), dtype=np.uint8)),
|
||||
],
|
||||
)
|
||||
store = _PreviewStore()
|
||||
monkeypatch.setattr(preview_service, '_preview_grant_store', lambda: store)
|
||||
monkeypatch.setattr(preview_service, '_audit', lambda **_kwargs: None)
|
||||
monkeypatch.setattr(preview_service.db.session, 'commit', lambda: None)
|
||||
monkeypatch.setattr(
|
||||
preview_service.db.session,
|
||||
'get',
|
||||
lambda model, pk: profile if pk == 1 else None,
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
payload = preview_service.authorize_frozen_sample_preview_pages(
|
||||
1, sample_binding_id=2, actor_id=99,
|
||||
)
|
||||
|
||||
assert payload['page_count'] == 3
|
||||
assert len(payload['pages']) == 3
|
||||
assert all(item.get('preview_token') for item in payload['pages'])
|
||||
|
||||
|
||||
def test_xfa_readonly_mapping_evidence_allows_pdf_preview():
|
||||
"""只读映射 DTO 应声明 PDF 预览可用(阶段 2)。"""
|
||||
from app.models import ShippingOrderDocumentProfile
|
||||
from app.services.shipping_notice.order_document_profile_preview_service import (
|
||||
build_xfa_readonly_mapping_evidence,
|
||||
)
|
||||
|
||||
profile = ShippingOrderDocumentProfile(
|
||||
id=1,
|
||||
profile_code='XFA',
|
||||
display_name='XFA',
|
||||
version=1,
|
||||
status='published',
|
||||
schema_version_id=1,
|
||||
structure_fingerprint='c' * 64,
|
||||
template_mode='xfa_dataset',
|
||||
definition_json={'template_mode': 'xfa_dataset', 'source_contract': {}, 'header_mappings': [], 'line_mapping': {'fields': []}},
|
||||
)
|
||||
evidence = build_xfa_readonly_mapping_evidence(profile)
|
||||
assert evidence['pdf_preview_available'] is True
|
||||
|
||||
|
||||
def test_preview_token_binds_actor_profile_page(app, monkeypatch):
|
||||
"""render 消费 token 时必须匹配 actor_id、profile_id、sample_binding_id、page_no。"""
|
||||
store = _PreviewStore()
|
||||
monkeypatch.setattr(preview_service, '_preview_grant_store', lambda: store)
|
||||
store.setex(
|
||||
'shipping:order-profile-frozen-preview:tok-1',
|
||||
120,
|
||||
'{"profile_id":1,"sample_binding_id":2,"actor_id":5,"page_no":1}',
|
||||
)
|
||||
profile = SimpleNamespace(id=1)
|
||||
sample = SimpleNamespace(id=2)
|
||||
document = SimpleNamespace(id=3)
|
||||
layout = SimpleNamespace()
|
||||
asset = SimpleNamespace()
|
||||
monkeypatch.setattr(
|
||||
preview_service,
|
||||
'_frozen_sample_context',
|
||||
lambda **_kwargs: (profile, sample, document, layout, asset),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
preview_service,
|
||||
'_render_pdf_pages',
|
||||
lambda _asset: [SimpleNamespace(page_no=1, image=np.zeros((4, 4, 3), dtype=np.uint8))],
|
||||
)
|
||||
monkeypatch.setattr(preview_service, '_audit', lambda **_kwargs: None)
|
||||
monkeypatch.setattr(preview_service.db.session, 'commit', lambda: None)
|
||||
|
||||
with app.app_context():
|
||||
png = preview_service.render_frozen_sample_preview_page(
|
||||
profile_id=1,
|
||||
sample_binding_id=2,
|
||||
token='tok-1',
|
||||
page_no=1,
|
||||
actor_id=5,
|
||||
)
|
||||
assert isinstance(png, bytes) and len(png) > 0
|
||||
|
||||
from app.core.errors import NotFoundError
|
||||
|
||||
with app.app_context():
|
||||
with pytest.raises(NotFoundError):
|
||||
preview_service.render_frozen_sample_preview_page(
|
||||
profile_id=1,
|
||||
sample_binding_id=2,
|
||||
token='tok-1',
|
||||
page_no=1,
|
||||
actor_id=6,
|
||||
)
|
||||
Reference in new issue
Block a user