Files
tamabox/internal/mail/mail_alert_test.go
T
tamakyi e3e59e7c69 chore: baseline snapshot before moderation-strictness feature
Repo .git was damaged (refs/ missing, index truncated, object store near-empty).
Repaired via git init (rebuilt refs), stale index preserved as .git/index.corrupt-20260910.bak,
then full worktree committed as a rollback baseline.
2026-09-10 10:56:27 +08:00

60 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package mail
import (
"bytes"
"html/template"
"testing"
"github.com/tamakyi/TamaBox/templates"
)
// TestSecurityAlertTemplateRenders 保证 security-alert.html 模板可解析,
// 且新增的「网络归属 / 代理网络提示」字段在参数齐全时正确渲染、
// proxy_hint 为空时不渲染提示块。
func TestSecurityAlertTemplateRenders(t *testing.T) {
tmpl, err := template.ParseFS(templates.FS, "mail/security-alert.html")
if err != nil {
t.Fatalf("parse template: %v", err)
}
base := map[string]string{
"ip": "203.0.113.7",
"provider": "Cloudflare(CDN)",
"paths": "/admin、/.env",
"duration": "15",
"time": "2026-09-08 12:00:00",
"year": "2026",
}
// 1. 代理网络命中:应有归属行 + 代理提示
var withHint bytes.Buffer
params := cloneParams(base)
params["proxy_hint"] = "1"
if err := tmpl.Execute(&withHint, params); err != nil {
t.Fatalf("execute with proxy_hint: %v", err)
}
out := withHint.String()
for _, want := range []string{"203.0.113.7", "网络归属", "Cloudflare(CDN)", "代理网络"} {
if !bytes.Contains([]byte(out), []byte(want)) {
t.Errorf("rendered mail missing %q", want)
}
}
// 2. 普通网络:不应出现代理提示块
var noHint bytes.Buffer
if err := tmpl.Execute(&noHint, cloneParams(base)); err != nil {
t.Fatalf("execute without proxy_hint: %v", err)
}
if bytes.Contains(noHint.Bytes(), []byte("代理网络")) {
t.Errorf("proxy hint rendered without proxy_hint param")
}
}
func cloneParams(src map[string]string) map[string]string {
dst := make(map[string]string, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}