Files
syops/backend/tests/test_shipping_eml_preview_service.py
T
hugo 1b6483a4ea feat(shipping-notice,receipt-report): 新增收料报表功能并优化发货通知流程
清理废弃的shipping xfa ocr automark相关代码和job配置
新增收料报表管理的前端页面、路由和侧边栏入口
新增后端收料报表配置接口、数据库模型和RPA脚本
优化发货通知流程的重试策略,将shipping相关job默认不自动重试
移除废弃的库存源确认相关逻辑,更新流程门禁规则
重构订单模板的值适配器模块,新增坐标OCR值适配器
清理前端shipping菜单和action的废弃选项
更新相关文档和测试用例,调整.gitignore忽略规则
2026-08-20 20:23:10 +08:00

109 lines
4.4 KiB
Python

"""
tests/test_shipping_eml_preview_service.py
用途:锁定 EML 安全预览清洗与快照复用契约。
依赖:eml_preview_service。
逻辑:预览仅保留安全正文布局;图片、脚本和外部资源不得进入浏览器。
"""
from app.services.shipping_notice.eml_preview_service import (
EML_PREVIEW_MAX_BYTES,
consume_eml_preview_grant,
issue_eml_preview_grant,
sanitize_eml_preview_html,
highlight_eml_preview_html,
)
from app.services.shipping_notice.eml_parse import extract_eml_preview_html
def test_sanitize_eml_preview_keeps_table_and_safe_inline_style():
"""安全预览保留表格和有限样式,但剥离主动内容及图片。"""
html = '<div style="color:red;position:fixed" onclick="bad()"><table><tr><td>PO</td></tr></table><img src="cid:logo"><a href="https://x">link</a><script>bad()</script></div>'
preview = sanitize_eml_preview_html(html)
# 关键断言:视觉结构可读,执行、追踪和定位样式均不可进入预览。
assert '<table>' in preview
assert 'color: red' in preview
assert 'position' not in preview
assert 'onclick' not in preview
assert '<img' not in preview
assert 'href' not in preview
assert '<script' not in preview
def test_sanitize_eml_preview_keeps_safe_background_shorthand_for_light_text():
"""Outlook 表头常以 background 简写搭配白字;清洗后必须同时保留,避免文字不可见。"""
html = '<table><tr><td style="background:#ED7D31;color:white">PO</td></tr></table>'
preview = sanitize_eml_preview_html(html)
# 关键断言:纯色背景可保留,url 等外部背景资源仍由样式解析器拒绝。
assert 'background: #ED7D31' in preview
assert 'color: white' in preview
def test_eml_preview_html_size_limit_is_finite():
"""安全预览快照必须有固定尺寸上限,超限邮件回退文本而非撑大工作台。"""
assert EML_PREVIEW_MAX_BYTES == 500_000
def test_extract_eml_preview_html_preserves_the_raw_html_table_and_head_style():
"""视觉预览必须读取原始 HTML MIME,而不是复用已扁平化的文本抽取路径。"""
eml = b"""MIME-Version: 1.0\r
Content-Type: multipart/alternative; boundary=preview\r
\r
--preview\r
Content-Type: text/plain; charset=utf-8\r
\r
PO\tQty\r
--preview\r
Content-Type: text/html; charset=utf-8\r
\r
<html><head><style>.order { border-collapse: collapse; } .order td { border: 1px solid #000; width: 120px; }</style></head><body><table class=\"order\"><tr><td>PO</td><td>Qty</td></tr></table><img src=\"cid:logo\"><script>alert(1)</script></body></html>\r
--preview--\r
"""
preview = extract_eml_preview_html(eml)
# 关键断言:保留供浏览器排版的 table/head/style/class,主动内容和 CID 图片必须剥离。
assert '<table class="order">' in preview
assert 'border-collapse' in preview
assert '<head>' in preview
assert '<img' not in preview
assert '<script' not in preview
def test_eml_preview_grant_is_single_use_and_bound_to_the_case(monkeypatch):
"""iframe 无法带 Bearer Header 时,预览 URL 必须使用短时且不可重放的 Case 绑定令牌。"""
class Store:
def __init__(self):
self.items = {}
def setex(self, key, _ttl, value):
self.items[key] = value
def getdel(self, key):
return self.items.pop(key, None)
monkeypatch.setattr('app.services.shipping_notice.eml_preview_service._eml_preview_grant_store', lambda: Store())
# 同一个 Store 必须跨签发/消费调用复用。
store = Store()
monkeypatch.setattr('app.services.shipping_notice.eml_preview_service._eml_preview_grant_store', lambda: store)
token = issue_eml_preview_grant(user_id=7, case_no='CASE-1', document_id=9, html='<table><tr><td>PO</td></tr></table>')
assert consume_eml_preview_grant(token=token, user_id=7, case_no='CASE-1') == '<table><tr><td>PO</td></tr></table>'
assert consume_eml_preview_grant(token=token, user_id=7, case_no='CASE-1') == ''
def test_highlight_eml_preview_html_marks_one_unique_evidence_quote_only():
"""字段证据只在安全 HTML 中唯一命中引文时加标记,避免全文值猜测。"""
html = '<html><body><p>PO: 33894</p><p>Qty: 10</p></body></html>'
highlighted = highlight_eml_preview_html(html, '33894')
assert highlighted.count('<mark') == 1
assert '33894' in highlighted
assert highlight_eml_preview_html(html, 'missing') == html