Files
ai-kefu-jc/tests/test_email_rendering.py
T
2026-09-29 14:15:12 +08:00

67 lines
2.3 KiB
Python

"""邮件渲染单测: 标签主题/HTML转义/内部key过滤(D2/D12)。不触网。"""
import asyncio
from app.services import email_service
def _capture_send(monkeypatch):
captured = {}
async def fake_send(msg):
captured["msg"] = msg
return True
monkeypatch.setattr(email_service, "_send", fake_send)
return captured
def _html(msg) -> str:
return msg.get_payload()[0].get_payload(decode=True).decode("utf-8")
def test_lead_email_subject_tag_and_escaped_body(monkeypatch):
captured = _capture_send(monkeypatch)
ok = asyncio.run(email_service.send_lead_email(
customer_profile={"目标国家": "肯尼亚", "_internal": "x", "备注": "<b>bold</b>"},
contact_info={"phone": "+254712345678", "wechat": "<i>wx</i>"},
reasoning="<b>强意向</b>",
conversation_messages=[
{"role": "user", "content": "<script>alert(1)</script>"},
{"role": "assistant", "content": "ok"},
],
intent_level="interested",
))
assert ok is True
subject = str(captured["msg"]["Subject"])
assert "[新询盘·意向强]" in subject and "肯尼亚" in subject
body = _html(captured["msg"])
assert "<script>" not in body # D12: 转义生效
assert "&lt;script&gt;" in body
assert "_internal" not in body # 内部 key 不渲染
assert "<b>bold</b>" not in body
assert "&lt;b&gt;bold&lt;/b&gt;" in body
assert "<i>wx</i>" not in body
assert "&lt;i&gt;wx&lt;/i&gt;" in body
assert captured["msg"].as_string().isascii()
def test_lead_email_unevaluated_tag(monkeypatch):
captured = _capture_send(monkeypatch)
asyncio.run(email_service.send_lead_email(
customer_profile={}, contact_info={"email": "a@b.com"},
reasoning="", conversation_messages=[], intent_level="",
))
assert "[新询盘·未评估]" in str(captured["msg"]["Subject"])
def test_returning_and_correction_emails(monkeypatch):
captured = _capture_send(monkeypatch)
assert asyncio.run(
email_service.send_returning_lead_email({"whatsapp": "+254700000000"})
) is True
assert "[线索回访]" in str(captured["msg"]["Subject"])
assert asyncio.run(
email_service.send_correction_email({"email": "a@b.com"})
) is True
assert "[询盘·更正1]" in str(captured["msg"]["Subject"])