feat(qa): 周报邮件新增测试/审核邮箱
build / build (push) Successful in 1m13s

测试发送只投递到该地址,不打扰主送与抄送名单;未填时退回主送首位。
This commit is contained in:
hermes
2026-10-26 16:06:17 +08:00
parent 1a9d1fff83
commit 23b19350cc
9 changed files with 49 additions and 10 deletions
@@ -0,0 +1 @@
alter table qa_weekly_email_settings drop column if exists review_address;
@@ -0,0 +1,2 @@
-- 审核副本:测试发送只投递到这个地址,不打扰主送与抄送名单。
alter table qa_weekly_email_settings add column if not exists review_address text not null default '';
@@ -11,8 +11,8 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) {
if err != nil {
t.Fatalf("loadMigrations() error = %v", err)
}
if len(migrations) != 62 {
t.Fatalf("migration count = %d, want 62", len(migrations))
if len(migrations) != 63 {
t.Fatalf("migration count = %d, want 63", len(migrations))
}
if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") {
t.Fatalf("migration 1 = %#v", migrations[0])
+19 -7
View File
@@ -39,6 +39,7 @@ type Settings struct {
From string `json:"from"`
Recipients []string `json:"recipients"`
Cc []string `json:"cc_recipients"`
Review string `json:"review_address"`
Weekday int `json:"weekday"`
Hour int `json:"hour"`
Enabled bool `json:"enabled"`
@@ -58,6 +59,7 @@ type SettingsInput struct {
From string `json:"from"`
Recipients []string `json:"recipients"`
Cc []string `json:"cc_recipients"`
Review string `json:"review_address"`
Weekday int `json:"weekday"`
Hour int `json:"hour"`
Enabled bool `json:"enabled"`
@@ -196,6 +198,7 @@ func normalize(input SettingsInput) (SettingsInput, error) {
input.Host = strings.TrimSpace(input.Host)
input.Username = strings.TrimSpace(input.Username)
input.From = strings.TrimSpace(input.From)
input.Review = strings.TrimSpace(input.Review)
seen := map[string]bool{}
recipients := make([]string, 0, len(input.Recipients))
for _, raw := range input.Recipients {
@@ -236,6 +239,9 @@ func normalize(input SettingsInput) (SettingsInput, error) {
if input.Port < 1 || input.Port > 65535 || input.Weekday < 1 || input.Weekday > 7 || input.Hour < 0 || input.Hour > 23 {
return input, ErrInvalid
}
if input.Review != "" && !validAddress(input.Review) {
return input, ErrInvalid
}
if input.Enabled {
if input.Host == "" || input.From == "" || !validAddress(input.From) || len(input.Recipients) == 0 {
return input, ErrInvalid
@@ -250,8 +256,8 @@ func (s *Store) ReadSettings(ctx context.Context) (Settings, error) {
return out, ErrUnavailable
}
var sealed []byte
err := s.pool.QueryRow(ctx, `SELECT smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,send_weekday,send_hour,enabled,revision,updated_by,updated_at FROM qa_weekly_email_settings WHERE tenant_key=$1`, s.tenant).
Scan(&out.Host, &out.Port, &out.Username, &sealed, &out.From, &out.Recipients, &out.Cc, &out.Weekday, &out.Hour, &out.Enabled, &out.Revision, &out.UpdatedBy, &out.UpdatedAt)
err := s.pool.QueryRow(ctx, `SELECT smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,review_address,send_weekday,send_hour,enabled,revision,updated_by,updated_at FROM qa_weekly_email_settings WHERE tenant_key=$1`, s.tenant).
Scan(&out.Host, &out.Port, &out.Username, &sealed, &out.From, &out.Recipients, &out.Cc, &out.Review, &out.Weekday, &out.Hour, &out.Enabled, &out.Revision, &out.UpdatedBy, &out.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return out, nil
}
@@ -304,10 +310,10 @@ func (s *Store) SaveSettings(ctx context.Context, actor string, input SettingsIn
return Settings{}, err
}
}
if _, err = tx.Exec(ctx, `INSERT INTO qa_weekly_email_settings(tenant_key,smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,send_weekday,send_hour,enabled,revision,updated_by,updated_at)
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,now())
ON CONFLICT (tenant_key) DO UPDATE SET smtp_host=excluded.smtp_host,smtp_port=excluded.smtp_port,smtp_username=excluded.smtp_username,smtp_password_sealed=excluded.smtp_password_sealed,from_address=excluded.from_address,recipients=excluded.recipients,cc_recipients=excluded.cc_recipients,send_weekday=excluded.send_weekday,send_hour=excluded.send_hour,enabled=excluded.enabled,revision=excluded.revision,updated_by=excluded.updated_by,updated_at=now()`,
s.tenant, normalized.Host, normalized.Port, normalized.Username, sealed, normalized.From, normalized.Recipients, normalized.Cc, normalized.Weekday, normalized.Hour, normalized.Enabled, next, actor); err != nil {
if _, err = tx.Exec(ctx, `INSERT INTO qa_weekly_email_settings(tenant_key,smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,review_address,send_weekday,send_hour,enabled,revision,updated_by,updated_at)
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,now())
ON CONFLICT (tenant_key) DO UPDATE SET smtp_host=excluded.smtp_host,smtp_port=excluded.smtp_port,smtp_username=excluded.smtp_username,smtp_password_sealed=excluded.smtp_password_sealed,from_address=excluded.from_address,recipients=excluded.recipients,cc_recipients=excluded.cc_recipients,review_address=excluded.review_address,send_weekday=excluded.send_weekday,send_hour=excluded.send_hour,enabled=excluded.enabled,revision=excluded.revision,updated_by=excluded.updated_by,updated_at=now()`,
s.tenant, normalized.Host, normalized.Port, normalized.Username, sealed, normalized.From, normalized.Recipients, normalized.Cc, normalized.Review, normalized.Weekday, normalized.Hour, normalized.Enabled, next, actor); err != nil {
return Settings{}, ErrUnavailable
}
if err = tx.Commit(ctx); err != nil {
@@ -569,8 +575,14 @@ func (s *Store) TestSend(ctx context.Context, week string) (string, error) {
if err != nil {
return "", err
}
// A review copy goes only to the operator's own review address so testing never
// reaches colleagues who are on the real distribution list.
target := settings.Recipients
if settings.Review != "" {
target = []string{settings.Review}
}
return s.mailer.Send(ctx, SMTPConfig{Host: settings.Host, Port: settings.Port, Username: settings.Username, Password: password},
Message{To: settings.Recipients, Cc: settings.Cc, From: settings.From, Subject: fmt.Sprintf("知识库周报 %s(测试发送)", week), HTML: html, Text: text, Images: images})
Message{To: target, Cc: nil, From: settings.From, Subject: fmt.Sprintf("知识库周报 %s(测试发送)", week), HTML: html, Text: text, Images: images})
}
func (s *Store) send(ctx context.Context, settings Settings, week string, revision int64, markdown string, now time.Time) (DueResult, error) {
+10
View File
@@ -119,3 +119,13 @@ func TestBuildMIMEPlacesTheChartAfterTheBodyAsAnInlineImage(t *testing.T) {
t.Fatal("html body must reference the inline cid")
}
}
func TestNormalizeAcceptsAnOptionalReviewAddressAndRejectsMalformedOnes(t *testing.T) {
good, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com"}, Review: "owner@example.com", Enabled: true})
if err != nil || good.Review != "owner@example.com" {
t.Fatalf("review address rejected: %q %v", good.Review, err)
}
if _, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com"}, Review: "not-an-address"}); err != ErrInvalid {
t.Fatal("a malformed review address must be rejected")
}
}