初版:小白超人(从线上实例导出)

This commit is contained in:
deploy
2026-10-29 15:20:39 +08:00
commit b77a309873
100 changed files with 32168 additions and 0 deletions
+103
View File
@@ -0,0 +1,103 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""验证码邮件的 HTML 版本回归测试。
背景:注册/重置验证码邮件此前调用 send_email(..., html_body=None),只发纯文本。
现在改为走统一 HTML 模板(email_utils.render_email_html)。本用例固化:
1) 必须携带 HTML 版本(html_body 非空且含 <!doctype html>)
2) HTML 里能看到验证码本身与有效期(用户能直接读出)
3) 仍然保留纯文本版本(无 HTML 能力的客户端可用)
4) 注册 / 重置两种用途的主题与视觉色不同
5) 渲染失败必须安全退回纯文本,不能因样式问题导致验证码发不出去
"""
import re
import unittest
from unittest.mock import patch
import email_verify
from email_utils import render_email_html, render_email_safe
class _CodeMailCapture(unittest.TestCase):
def setUp(self):
email_verify._codes.clear()
self.sent = []
def tearDown(self):
email_verify._codes.clear()
def _send(self, purpose):
def capture(to_email, subject, plain, html_body):
self.sent.append({"to": to_email, "subject": subject,
"plain": plain, "html": html_body})
return True
with patch.object(email_verify, "send_email", side_effect=capture):
ok, message = email_verify.send_code("rider@example.test", purpose=purpose)
self.assertTrue(ok, message)
return self.sent[-1]
def _code_in(self, payload, text):
"""从正文里取出 6 位验证码。"""
m = re.search(r"\b(\d{6})\b", text)
self.assertIsNotNone(m, f"未在正文中找到验证码: {text[:120]}")
return m.group(1)
class CodeMailTests(_CodeMailCapture):
def test_register_code_mail_includes_html_version(self):
mail = self._send("register")
# 核心回归:以前这里是 None
self.assertIsNotNone(mail["html"])
self.assertTrue(mail["html"].strip().startswith("<!doctype html>"))
self.assertIn("style=", mail["html"])
def test_html_contains_the_actual_code_and_ttl(self):
mail = self._send("register")
code = self._code_in(mail, mail["plain"])
# HTML 里必须能读到验证码本身(用户是照 HTML 抄的)
self.assertIn(code, mail["html"])
self.assertIn(str(email_verify.CODE_TTL_SECONDS // 60), mail["html"])
def test_plain_text_version_is_still_present(self):
mail = self._send("register")
self.assertTrue(mail["plain"].strip())
self.assertIn("验证码", mail["plain"])
def test_reset_uses_distinct_subject_and_accent(self):
reg = self._send("register")
email_verify._codes.clear() # 绕开 60 秒冷却
rst = self._send("reset")
self.assertNotEqual(reg["subject"], rst["subject"])
self.assertIn("注册", reg["subject"])
self.assertIn("重置", rst["subject"])
# 两种用途用不同主色(注册蓝 / 重置红),便于一眼区分
self.assertIn("#2563eb", reg["html"])
self.assertIn("#dc2626", rst["html"])
# 重置邮件的 HTML 里不应残留注册用途的文案
self.assertNotIn("账号注册", rst["html"])
self.assertIn("重置登录密码", rst["html"])
def test_html_escapes_user_supplied_values(self):
"""注入式邮箱不应把 HTML 带进邮件正文。"""
html = render_email_html(
kicker="<script>alert(1)</script>", title="t", lead="l",
details=[("<b>x</b>", "<img src=x onerror=alert(1)>")])
self.assertNotIn("<script>", html)
self.assertNotIn("<img", html)
self.assertIn("&lt;script&gt;", html)
class RenderFallbackTests(unittest.TestCase):
def test_render_failure_returns_none_instead_of_raising(self):
class _Boom:
def __str__(self):
raise RuntimeError("渲染期异常")
# 关键:绝不能把异常抛给发信链路(否则验证码直接发不出去)
self.assertIsNone(render_email_safe(
kicker="k", title="t", lead="l", details=[("a", _Boom())]))
if __name__ == "__main__":
unittest.main()