6816289552
build / build (push) Successful in 1m23s
周报页新增自动发送区:管理员配置 SMTP/收件人/发送星期与小时,授权码用主密钥加密保存。 确认绑定当前草稿版本与正文哈希,正文再改则确认失效;worker 每分钟仅在启用、到点、周已结束且未发送过时发一次。 邮件同时提供渲染后的 HTML 与可读纯文本;SMTP 仅允许 TLS,拒绝明文。 迁移 000060 新增设置、确认与发送记录三张表,唯一键防重复发送。
95 lines
3.9 KiB
Go
95 lines
3.9 KiB
Go
package qawemail
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSlotMatchesUsesBeijingWeekdayAndHour(t *testing.T) {
|
|
settings := Settings{Weekday: 1, Hour: 9}
|
|
// Monday 09:30 Beijing == Monday 01:30 UTC.
|
|
if !slotMatches(settings, time.Date(2026, 10, 26, 1, 30, 0, 0, time.UTC)) {
|
|
t.Fatal("Monday 09:30 Beijing must match a Monday-09 slot")
|
|
}
|
|
if slotMatches(settings, time.Date(2026, 10, 26, 2, 30, 0, 0, time.UTC)) {
|
|
t.Fatal("Monday 10:30 Beijing must not match a Monday-09 slot")
|
|
}
|
|
// Sunday 23:30 UTC is Monday 07:30 Beijing: still Monday, wrong hour.
|
|
if slotMatches(settings, time.Date(2026, 10, 25, 23, 30, 0, 0, time.UTC)) {
|
|
t.Fatal("Sunday 23:30 UTC is not the configured hour")
|
|
}
|
|
}
|
|
|
|
func TestReportClosedOnlyAfterTheISOWeekEnds(t *testing.T) {
|
|
if reportClosed("2026-W43", time.Date(2026, 10, 25, 10, 0, 0, 0, time.UTC)) {
|
|
t.Fatal("a week is not closed before its Monday boundary")
|
|
}
|
|
if !reportClosed("2026-W43", time.Date(2026, 10, 26, 1, 0, 0, 0, time.UTC)) {
|
|
t.Fatal("week 43 must be closed after Monday 00:00 Beijing")
|
|
}
|
|
if reportClosed("2026-W99", time.Now()) || reportClosed("2026-W00", time.Now()) {
|
|
t.Fatal("impossible ISO weeks must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeRefusesEnabledSettingsWithoutTransportAndBadRecipients(t *testing.T) {
|
|
if _, err := normalize(SettingsInput{Host: "", From: "", Recipients: nil, Enabled: true}); err != ErrInvalid {
|
|
t.Fatalf("enabled without host/from/recipients must be invalid, got %v", err)
|
|
}
|
|
if _, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"not-an-address"}, Enabled: true}); err == nil {
|
|
t.Fatal("a malformed recipient must be rejected")
|
|
}
|
|
good, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com", "b@example.com"}, Enabled: true})
|
|
if err != nil || good.Port != 465 || good.Weekday != 1 || len(good.Recipients) != 2 {
|
|
t.Fatalf("defaults not applied: %+v %v", good, err)
|
|
}
|
|
duplicated := SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com", "A@example.com"}}
|
|
if _, err := normalize(duplicated); err != ErrInvalid {
|
|
t.Fatal("case-insensitive duplicate recipients must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestBuildMIMEProvidesReadableTextAndHTMLWithEncodedSubject(t *testing.T) {
|
|
html, text := renderReport("# 知识库周报 2026-W43\n\n## 二、核心指标\n\n正确率:90.66%\n")
|
|
if !strings.Contains(html, "<h1>") || !strings.Contains(html, "90.66%") {
|
|
t.Fatalf("rendered weekly HTML missing structure or metrics: %s", html[:120])
|
|
}
|
|
if !strings.Contains(text, "正确率:90.66%") {
|
|
t.Fatal("plain text body must keep the readable report")
|
|
}
|
|
raw := buildMIME(Message{From: "kb@example.com", To: []string{"lead@example.com"}, Subject: "知识库周报 2026-W43", HTML: html, Text: text}, "<id@example.com>", "BOUND")
|
|
for _, want := range []string{"Content-Type: multipart/alternative", "text/plain", "text/html", "Message-ID: <id@example.com>", "Subject: =?UTF-8?B?"} {
|
|
if !strings.Contains(raw, want) {
|
|
t.Fatalf("MIME payload missing %q", want)
|
|
}
|
|
}
|
|
encoded := base64.StdEncoding.EncodeToString([]byte("知识库周报 2026-W43"))
|
|
if !strings.Contains(raw, encoded) {
|
|
t.Fatal("subject must be RFC2047-encoded so Chinese survives clients")
|
|
}
|
|
}
|
|
|
|
type fakeMailer struct {
|
|
sent []Message
|
|
configs []SMTPConfig
|
|
err error
|
|
}
|
|
|
|
func (f *fakeMailer) Send(_ context.Context, cfg SMTPConfig, msg Message) (string, error) {
|
|
f.configs = append(f.configs, cfg)
|
|
if f.err != nil {
|
|
return "", f.err
|
|
}
|
|
f.sent = append(f.sent, msg)
|
|
return "<fake@example.com>", nil
|
|
}
|
|
|
|
func TestMailerRefusesPlaintextServersWithoutSTARTTLS(t *testing.T) {
|
|
if _, err := (SMTPMailer{}).Send(context.Background(), SMTPConfig{Host: "127.0.0.1", Port: 25}, Message{From: "kb@example.com", To: []string{"a@example.com"}, Subject: "x"}); err == nil {
|
|
t.Fatal("a server that cannot encrypt must be refused rather than sent in clear")
|
|
}
|
|
}
|