只读查询不再要求同源操作意图,写操作仍需;前端改为直接消费已解包的 data。 调度集成测试按租户清理并要求一次性测试库,避免误清正式数据。
This commit is contained in:
@@ -77,7 +77,10 @@ func qaWeeklyMailSettings(w http.ResponseWriter, r *http.Request, c Config) {
|
||||
}
|
||||
|
||||
func qaWeeklyMailConfirm(w http.ResponseWriter, r *http.Request, c Config) {
|
||||
if !authorizeQAOps(w, r) || !authorizeQAIntent(w, r, "qa-weekly-mail") {
|
||||
if !authorizeQAOps(w, r) {
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet && !authorizeQAIntent(w, r, "qa-weekly-mail") {
|
||||
return
|
||||
}
|
||||
store, ok := c.QAWeeklyMail.(weeklyMailStore)
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package qawemail
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
// TestScheduledSendRequiresConfirmationAndSendsExactlyOnce pins the whole gate against a real
|
||||
// database: only an enabled slot, a closed week and an unchanged confirmation produce one message.
|
||||
func TestScheduledSendRequiresConfirmationAndSendsExactlyOnce(t *testing.T) {
|
||||
dsn := os.Getenv("KG_WEEKLY_MAIL_TEST_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("explicit disposable weekly-mail database required")
|
||||
}
|
||||
if !strings.Contains(dsn, "/kg_weekly_mail_test_") {
|
||||
t.Fatal("refusing a non-disposable database")
|
||||
}
|
||||
if strings.Contains(dsn, "@postgres:") || strings.Contains(dsn, "/governance?") {
|
||||
t.Fatal("refusing the shared governance database")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := postgresstore.ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mailer := &fakeMailer{}
|
||||
store := NewStore(pool, "fixture-weekly-mail", os.Getenv("KG_WEEKLY_MAIL_TEST_KEY"), mailer)
|
||||
if _, err := pool.Exec(ctx, `DELETE FROM qa_weekly_email_sends WHERE tenant_key=$1`, store.tenant); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `DELETE FROM qa_weekly_confirmations WHERE tenant_key=$1`, store.tenant); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `DELETE FROM qa_weekly_email_settings WHERE tenant_key=$1`, store.tenant); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `DELETE FROM qa_ops_weekly_drafts WHERE tenant_key=$1`, store.tenant); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
markdown := "# 知识库周报 2026 WK43\n\n## 二、核心指标\n\n正确率:90.66%\n"
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO qa_ops_weekly_drafts(tenant_key,week,revision,markdown,seed,updated_by,updated_at) VALUES($1,'2026-W43',2,$2,'{}'::jsonb,'fixture-admin',now())`, store.tenant, markdown); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Slot: Monday 09:00 Beijing of the week after 2026-W43 closes.
|
||||
slot := time.Date(2026, 10, 26, 1, 5, 0, 0, time.UTC)
|
||||
if _, err := store.SaveSettings(ctx, "fixture-admin", SettingsInput{
|
||||
BaseRevision: 0, Host: "localhost", Port: 465, From: "kb@example.com",
|
||||
Recipients: []string{"lead@example.com"}, Weekday: 1, Hour: 9, Enabled: true,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := store.Confirm(ctx, "2026-W43", "fixture-admin"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
unconfirmed := []string{"2026-W44"}
|
||||
_ = unconfirmed
|
||||
|
||||
if result, err := store.SendDue(ctx, slot); err != nil || result.State != "blocked" {
|
||||
// No sealed SMTP password was configured, so the send must be blocked rather than faked.
|
||||
if err != nil || result.State != "blocked" {
|
||||
t.Fatalf("missing password must block sending, got %+v %v", result, err)
|
||||
}
|
||||
}
|
||||
if len(mailer.sent) != 0 {
|
||||
t.Fatal("a blocked send must not reach the mailer")
|
||||
}
|
||||
|
||||
// Seed a password through the real encryption path, then send inside the slot.
|
||||
if _, err := store.SaveSettings(ctx, "fixture-admin", SettingsInput{
|
||||
BaseRevision: 1, Host: "localhost", Port: 465, From: "kb@example.com", Password: "app-password",
|
||||
Recipients: []string{"lead@example.com"}, Weekday: 1, Hour: 9, Enabled: true,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
offSlot := slot.Add(2 * time.Hour)
|
||||
if result, err := store.SendDue(ctx, offSlot); err != nil || result.State != "not_due" {
|
||||
t.Fatalf("outside the configured hour nothing may be sent, got %+v %v", result, err)
|
||||
}
|
||||
sent, err := store.SendDue(ctx, slot)
|
||||
if err != nil || sent.State != "sent" || sent.Week != "2026-W43" {
|
||||
t.Fatalf("expected one sent report, got %+v %v", sent, err)
|
||||
}
|
||||
if len(mailer.sent) != 1 {
|
||||
t.Fatalf("mailer calls = %d, want 1", len(mailer.sent))
|
||||
}
|
||||
message := mailer.sent[0]
|
||||
if len(message.To) != 1 || message.To[0] != "lead@example.com" || !strings.Contains(message.Subject, "2026-W43") {
|
||||
t.Fatalf("message envelope wrong: %+v", message)
|
||||
}
|
||||
if !strings.Contains(message.HTML, "90.66%") || !strings.Contains(message.Text, "90.66%") {
|
||||
t.Fatal("both HTML and text must carry the confirmed report body")
|
||||
}
|
||||
if mailer.configs[0].Password != "app-password" {
|
||||
t.Fatal("the sealed password must be unsealed for the SMTP call")
|
||||
}
|
||||
again, err := store.SendDue(ctx, slot)
|
||||
if err != nil || again.State != "nothing_confirmed" {
|
||||
t.Fatalf("a second attempt in the same slot must not resend, got %+v %v", again, err)
|
||||
}
|
||||
if len(mailer.sent) != 1 {
|
||||
t.Fatal("duplicate send detected")
|
||||
}
|
||||
|
||||
// Editing the draft invalidates the confirmation: the changed body must never be mailed.
|
||||
if _, err := pool.Exec(ctx, `UPDATE qa_ops_weekly_drafts SET revision=3,markdown=$2 WHERE tenant_key=$1 AND week='2026-W43'`, store.tenant, markdown+"\n改过的正文\n"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stale, err := store.SendDue(ctx, time.Date(2026, 11, 2, 1, 5, 0, 0, time.UTC))
|
||||
if err != nil || stale.State != "nothing_confirmed" {
|
||||
t.Fatalf("a changed body must invalidate the confirmation, got %+v %v", stale, err)
|
||||
}
|
||||
if len(mailer.sent) != 1 {
|
||||
t.Fatal("changed body was mailed despite confirmation invalidation")
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ const endpoint = (path: string, week?: string) =>
|
||||
const headers = { 'Content-Type': 'application/json', 'X-Governance-Action': 'qa-weekly-mail' }
|
||||
|
||||
export async function fetchWeeklyMailSettings(): Promise<WeeklyMailSettings> {
|
||||
return parseWeeklyMailSettings(((await getPayload(endpoint('settings'))) as { data: unknown }).data)
|
||||
return parseWeeklyMailSettings(await getPayload(endpoint('settings')))
|
||||
}
|
||||
|
||||
export async function saveWeeklyMailSettings(
|
||||
@@ -127,12 +127,12 @@ export async function saveWeeklyMailSettings(
|
||||
enabled: form.enabled,
|
||||
}),
|
||||
})
|
||||
return parseWeeklyMailSettings((payload as { data: unknown }).data)
|
||||
return parseWeeklyMailSettings(payload)
|
||||
}
|
||||
|
||||
export async function fetchWeeklyMailConfirmation(week: string): Promise<WeeklyMailConfirmation | null> {
|
||||
const payload = await getPayload(endpoint('confirmations', week))
|
||||
return parseWeeklyMailConfirmation((payload as { data: { confirmation: unknown } }).data.confirmation, week)
|
||||
const payload = (await getPayload(endpoint('confirmations', week))) as { confirmation: unknown }
|
||||
return parseWeeklyMailConfirmation(payload.confirmation, week)
|
||||
}
|
||||
|
||||
export async function confirmWeeklyMail(week: string, confirm: boolean): Promise<WeeklyMailConfirmation | null> {
|
||||
@@ -140,12 +140,14 @@ export async function confirmWeeklyMail(week: string, confirm: boolean): Promise
|
||||
method: confirm ? 'POST' : 'DELETE',
|
||||
headers,
|
||||
})
|
||||
return parseWeeklyMailConfirmation((payload as { data: { confirmation: unknown } }).data.confirmation, week)
|
||||
return parseWeeklyMailConfirmation((payload as { confirmation: unknown }).confirmation, week)
|
||||
}
|
||||
|
||||
export async function fetchWeeklyMailSends(week: string): Promise<WeeklyMailSend[]> {
|
||||
const payload = await getPayload(endpoint('sends') + '?' + new URLSearchParams({ week }).toString())
|
||||
return parseWeeklyMailSends((payload as { data: { sends: unknown } }).data.sends)
|
||||
const payload = (await getPayload(endpoint('sends') + '?' + new URLSearchParams({ week }).toString())) as {
|
||||
sends: unknown
|
||||
}
|
||||
return parseWeeklyMailSends(payload.sends)
|
||||
}
|
||||
|
||||
export const weeklyMailWeekdayLabels = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
|
||||
|
||||
Reference in New Issue
Block a user