Repository navigation
后端:新增管理员「发送测试邮件」接口 - #132
Merged
todosdev[bot] merged 3 commits intoOct 18, 2026
Merged
Conversation
- add POST /api/admin/email/test that sends a test email via saved provider config - validate recipient (trim, reuse admin email regex, <=254 chars), 400 on invalid - 400 on missing provider config, 502 on upstream failure without leaking details - rate limit 5/10min per admin (KV prefix test_email_rate) with Retry-After - write audit log action test_email with recipient in details - add testEmailHtml helper, worker tests, and README/CLAUDE docs Checkpoint: p_xjF55Qagh7pwUy4hgco
- add EmailConfigError to email.ts and throw it for missing provider config - route matches instanceof instead of substring so an SMTP reply containing "not configured" is still treated as a 502 rather than leaking the response - add regression tests for Resend/SMTP config-missing error classification Checkpoint: qywDDbE00QlBPGya1qJpB
…7-78f9-8a7a-1629e5127ff8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POST /api/admin/email/test,用已保存的邮件服务商配置(Resend / SMTP)发送一封测试邮件,让管理员保存配置后能立即验证是否可用。Changes
apps/worker/src/routes/admin.tsPOST /api/admin/email/test(requireAdmin已覆盖,仅管理员可用)。{ "to": "<email>" }:trim()后沿用创建用户的正则校验,长度上限 254,非法返回 400。sendEmail使用已保存设置发送;subject 为${appName} Test Email,html 写明「这是一封测试邮件,说明邮件配置可用」并附带发送时间。EmailConfigError→ 400(可读原因、不含密钥);其余上游失败 → 502 通用文案,日志只记 level/message/requestId,绝不记录错误消息正文或凭据;成功 → 200{ success: true }。test_email,details 记录收件地址。getAppName读取 app_name(DB/KV 缓存)。apps/worker/src/lib/kv.tstest_email_rate:与checkTestEmailRateLimit(复用固定窗口工具,5 次/10 分钟,返回Retry-After)。apps/worker/src/lib/email.tsEmailConfigError专用错误类型,配置缺失改为抛它,供路由用instanceof区分(避免 SMTP 上游响应的「not configured」文本被误判为配置缺失而回显)。testEmailHtml模板(appName 经 HTML 转义,附带发送时间)。文档
README.md/README.zh-CN.md/CLAUDE.md:邮件设置段落补一句「保存后可用『发送测试邮件』验证配置」。Testing
pnpm check、pnpm typecheck、pnpm build、pnpm test全绿。apps/worker/test/admin.test.ts(6 用例):未认证 401 / 非管理员 403、非法邮箱 400、配置缺失 400 且提示可读、上游失败 502 且不泄露上游响应、成功 200 并写审计日志、第 6 次限流 429 +Retry-After。apps/worker/test/email.test.ts(2 用例):Resend / SMTP 配置缺失均抛EmailConfigError。wrangler.toml,新代码只操作tbl()的SL_前缀表。