Files
summercms/modules/cabana/mail_lifecycle_postgres_test.go
Jakub Zych 67f7164302 feat(cabana): mail templates list grouped by template with per-locale status
Gap G-35-1 (supersedes D-05; D-06 reset and D-07 drift per locale):
- the templates list keeps one row per template (the base row, or the
  lowest variant when only variants ship) with a Customized locales
  column (Default, then locale codes, at most three then +N);
- row states and the status filter aggregate over the template's
  locale versions; the locale filter is removed;
- a not-customized locale without a file shows the base row's current
  subject, body and layout; its info partial says so, its file version
  compares with the base file, and Reset returns it to the base file
  (postcard.ResetOverride accepts a fallback-locale code);
- copy: mail.template.customized_locales, mail.info_locale_default,
  mail.info_locale_fallback (en/pl);
- tests: TestMailScreensSmoke/locales_list, templates and drift updated,
  TestMailDrift on the grouped row, TestMailTemplateFilterScopes.
2026-11-13 20:39:45 +01:00

153 lines
6.5 KiB
Go

package cabana_test
import (
"fmt"
"net/http"
"strings"
"testing"
"testing/fstest"
"git.golem15.com/golem15/summercms/modules/postcard"
)
func TestMailSave(t *testing.T) {
env := newMailScreensEnv(t, nil)
_, tok := env.admin(t, "mail-save", true, "")
tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome)
layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout)
path := fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID)
before := env.expect(t, http.StatusOK, http.MethodGet, path, tok, nil)
beforeSub := fmt.Sprint(before["data"].(map[string]any)["subject"])
cases := []struct {
name string
body map[string]any
want string
}{
{"parse", map[string]any{"subject": "Hi", "body": "Hello {{ .name", "layout": layoutID}, "body"},
{"render", map[string]any{"subject": "Hi", "body": "{{ len 5 }}", "layout": layoutID}, "body"},
{"unsafe", map[string]any{"subject": "Hi", "body": "Hi\n\n<script>x</script>", "layout": layoutID}, "body"},
{"subject_line", map[string]any{"subject": "Hi\nthere", "body": "Fine.", "layout": layoutID}, "subject"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
rec := env.call(t, http.MethodPut, path, tok, tc.body)
if rec.Code != http.StatusUnprocessableEntity {
t.Fatalf("status=%d want 422 body=%s", rec.Code, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), tc.want) {
t.Fatalf("422 body missing field %q: %s", tc.want, rec.Body.String())
}
after := env.expect(t, http.StatusOK, http.MethodGet, path, tok, nil)
if fmt.Sprint(after["data"].(map[string]any)["subject"]) != beforeSub {
t.Fatal("row changed after refused save")
}
})
}
t.Run("brand_colour", func(t *testing.T) {
rec := env.call(t, http.MethodPut, "/settings/mail_brand", tok, map[string]any{"button_primary_bg": "red"})
if rec.Code != http.StatusUnprocessableEntity {
t.Fatalf("brand 422 = %d %s", rec.Code, rec.Body.String())
}
})
t.Run("logo_missing", func(t *testing.T) {
rec := env.call(t, http.MethodPut, "/settings/mail_brand", tok, map[string]any{"logo_file_id": 999999})
if rec.Code != http.StatusUnprocessableEntity {
t.Fatalf("status=%d want 422", rec.Code)
}
})
}
func TestMailDrift(t *testing.T) {
env := newMailScreensEnv(t, nil)
_, tok := env.admin(t, "mail-drift", true, "")
tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcomeEn)
layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout)
path := fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID)
env.expect(t, http.StatusOK, http.MethodPut, path, tok, map[string]any{
"subject": "Custom {{ .name }}", "body": "Custom body for {{ .name }}", "layout": layoutID,
})
// A second process boots with a changed file and syncs the index.
files := mailBlogFiles()
files["views/mail/welcome-en.htm"] = &fstest.MapFile{Data: []byte(`subject = "Welcome, {{ .name }}"
description = "Sent after registration (English)"
layout = "blog"
sample.name = "Ada"
==
Hi **{{ .name }}**, thanks for joining the Acme blog.
`)}
next := newMailScreensEnvWith(t, mailEnvOptions{files: files, keep: true})
// One list row per template: the drifted en version marks welcome's row.
baseID := next.mailRowID(t, postcard.TemplatesTable, blogWelcome)
list := next.expect(t, http.StatusOK, http.MethodGet, mailTemplatesPath+"?filter[status]=outdated", tok, nil)
rows := listRows(t, list)
if rows[blogWelcome] == nil || rows[blogWelcome]["customized_locales"] != "en" {
t.Fatalf("outdated filter missing row: %#v", list["data"])
}
if got := fmt.Sprint(rowStates(next.expect(t, http.StatusOK, http.MethodGet, mailTemplatesPath, tok, nil), baseID)); got != "[customized outdated]" {
t.Fatalf("drifted row states = %s", got)
}
next.expect(t, http.StatusOK, http.MethodPut, path, tok, map[string]any{
"subject": "Custom again {{ .name }}", "body": "Custom body for {{ .name }}", "layout": layoutID,
})
if got := fmt.Sprint(rowStates(next.expect(t, http.StatusOK, http.MethodGet, mailTemplatesPath, tok, nil), baseID)); got != "[customized]" {
t.Fatalf("reviewed row states = %s", got)
}
}
func TestMailReset(t *testing.T) {
env := newMailScreensEnv(t, nil)
_, tok := env.admin(t, "mail-reset", true, "")
tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome)
layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout)
path := fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID)
env.expect(t, http.StatusOK, http.MethodPut, path, tok, map[string]any{
"subject": "Custom", "body": "Body", "layout": layoutID,
})
env.expect(t, http.StatusOK, http.MethodPost, path+"/actions/reset", tok, map[string]any{})
got := env.expect(t, http.StatusOK, http.MethodGet, path, tok, nil)
if fmt.Sprint(got["data"].(map[string]any)["subject"]) == "Custom" {
t.Fatal("subject still custom after reset")
}
}
func TestMailLayoutDelete(t *testing.T) {
env := newMailScreensEnv(t, nil)
_, tok := env.admin(t, "mail-laydel", true, "")
if err := env.gdb.Exec(`INSERT INTO ` + postcard.LayoutsTable + ` (code, source, name, content_html, content_text, is_custom)
VALUES ('promo-del', NULL, 'Promo Del', '<div>{{ .Content }}</div>', '{{ .Content }}', TRUE)`).Error; err != nil {
t.Fatal(err)
}
id := env.mailRowID(t, postcard.LayoutsTable, "promo-del")
tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome)
if err := env.gdb.Exec(`UPDATE `+postcard.TemplatesTable+` SET layout_id = ?, is_custom = TRUE WHERE id = ?`, id, tmplID).Error; err != nil {
t.Fatal(err)
}
rec := env.call(t, http.MethodDelete, fmt.Sprintf("%s/%d", mailLayoutsPath, id), tok, nil)
if rec.Code != http.StatusForbidden {
t.Fatalf("in-use delete status=%d want 403 body=%s", rec.Code, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), "Promo Del") && !strings.Contains(rec.Body.String(), "1") {
t.Fatalf("403 missing name/count: %s", rec.Body.String())
}
fileID := env.mailRowID(t, postcard.LayoutsTable, blogLayout)
form := env.expect(t, http.StatusOK, http.MethodGet, fmt.Sprintf("%s/%d", mailLayoutsPath, fileID), tok, nil)
meta, _ := form["meta"].(map[string]any)
if deletable, ok := meta["deletable"].(bool); ok && deletable {
t.Fatal("file layout marked deletable")
}
}
func TestMailBrand(t *testing.T) {
env := newMailScreensEnv(t, nil)
_, tok := env.admin(t, "mail-brand-unit", true, "")
env.expect(t, http.StatusOK, http.MethodGet, "/settings/mail_brand", tok, nil)
env.expect(t, http.StatusOK, http.MethodPut, "/settings/mail_brand", tok, map[string]any{"button_primary_bg": "#112233"})
env.expect(t, http.StatusUnprocessableEntity, http.MethodPut, "/settings/mail_brand", tok, map[string]any{"button_primary_bg": "nope"})
}