Files
summercms/modules/postcard/editor_smoke_test.go
Jakub Zych 13ab905f8d fix(postcard): check URL schemes in decoded attribute values, not in text
- validateHTML keeps the raw tag and handler regexes and reads the rest
  through unsafeMarkup: x/net/html tokenizer, decoded attribute values,
  prefix check on URL attributes, whole-value check on CSS, list
  attributes, meta content and comments; any on* attribute is refused
- text is not a URL: "Metadata: Weekly" or "Big Data: Summit" as a site
  name or body text no longer stops a send (CR-01)
- entity-encoded javascript: is refused whether or not the layout
  opts out of inlining (WR-05)
- draft layout CSS is checked as a style element
- fixtures that pinned prose as unsafe now carry markup-level URLs;
  the 35-07 subtest pinning an unsafe stored name is removed
- TestMailSafetySmoke prose_site_name and encoded_scheme
2026-11-14 15:13:35 +01:00

834 lines
38 KiB
Go

package postcard
import (
"context"
"errors"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"testing"
"testing/fstest"
"git.golem15.com/golem15/summercms/modules/backpack"
)
const editorWelcomeHTM = `subject = "Welcome, {{ .Name }}"
description = "Sent after registration"
layout = "blog"
sample.Name = "Ada"
sample.order.code = "ZAM-1"
==
Hi **{{ .Name }}**, order {{ .order.code }} for {{ .company }}.
{{ range .order.items }}- {{ .description }}
{{ end }}
`
func editorFS(welcome string) fstest.MapFS {
return fstest.MapFS{
"views/mail/welcome.htm": {Data: []byte(welcome)},
"views/mail/welcome-en.htm": {Data: []byte("subject = \"Welcome\"\ndescription = \"English welcome\"\n==\nHello.\n")},
"views/mail/layouts/blog.htm": {Data: []byte(`name = "Blog"
==
{{ .Content }}
-- The Acme blog
==
<div class="blog-mail">{{ .Content }}</div>
`)},
"views/mail/partials/note.htm": {Data: []byte(noteHTM)},
}
}
const (
editorWelcome = "acme.blog::mail.welcome"
editorBlogLayout = "acme.blog::mail.layouts.blog"
editorNote = "acme.blog::mail.partials.note"
)
func editorPlugin(welcome string) mailPlugin {
return mailPlugin{
partialsPlugin: partialsPlugin{id: "acme.blog", fsys: editorFS(welcome), partials: []string{editorNote}},
templates: []string{editorWelcome, "acme.blog::mail.welcome-en"},
layouts: map[string]string{"blog": editorBlogLayout},
}
}
func editorCatalog(t *testing.T, welcome string) *Catalog {
t.Helper()
p := editorPlugin(welcome)
cat := NewCatalog()
if err := cat.RegisterPartials(p.id, p.fsys, p.partials); err != nil {
t.Fatal(err)
}
if err := cat.Register(p.id, p.fsys, p.templates, p.layouts); err != nil {
t.Fatal(err)
}
return cat
}
// newEditor boots the fixture the way party does and returns the editor of
// the published mailer. ov, when set, replaces the mailer's override source.
func newEditor(t *testing.T, ov *Overrides) (*Editor, *MemoryDriver) {
t.Helper()
app := backpack.New(nil)
plugin := editorPlugin(editorWelcomeHTM)
if err := Activate(app, []mailPlugin{plugin}); err != nil {
t.Fatal(err)
}
if err := BootPlugin(app, plugin); err != nil {
t.Fatal(err)
}
ed, ok := EditorFrom(app)
if !ok {
t.Fatal("EditorFrom found no mailer")
}
ed.m.opts.From = "blog@example.com"
if ov != nil {
ed.m.source = fakeOverrides{ov: *ov}
}
return ed, ed.m.driver.(*MemoryDriver)
}
var hex64 = regexp.MustCompile(`^[0-9a-f]{64}$`)
// TestMailEditorSmoke covers what the admin layer builds on: sample values
// and placeholders, file hashes, the catalog read API, draft validation,
// preview and test send, and the index rows with reset.
func TestMailEditorSmoke(t *testing.T) {
ctx := context.Background()
t.Run("samples", func(t *testing.T) {
parsed, err := parseTemplateFile(editorWelcome, "acme.blog", editorWelcomeHTM)
if err != nil {
t.Fatal(err)
}
if want := map[string]string{"Name": "Ada", "order.code": "ZAM-1"}; !reflect.DeepEqual(parsed.samples, want) {
t.Errorf("samples = %v, want %v", parsed.samples, want)
}
if parsed.subject != "Welcome, {{ .Name }}" || parsed.description != "Sent after registration" || parsed.layout != "blog" {
t.Errorf("header = %q / %q / %q", parsed.subject, parsed.description, parsed.layout)
}
// A file without sample keys parses exactly as before.
plain, err := parseTemplateFile("acme.blog::mail.plain", "acme.blog", "Subject = \"Hi\"\nLAYOUT = 'blog'\n==\nBody.\n")
if err != nil {
t.Fatal(err)
}
if plain.subject != "Hi" || plain.layout != "blog" || plain.body != "Body." || len(plain.samples) != 0 {
t.Errorf("plain file = %+v", plain)
}
// The prefix is matched in any case; the path keeps its case.
mixed, err := parseTemplateFile("acme.blog::mail.mixed", "acme.blog", "SAMPLE.bankDetailsLines = 'x'\nsample. = \"ignored\"\n==\nBody.\n")
if err != nil {
t.Fatal(err)
}
if want := map[string]string{"bankDetailsLines": "x"}; !reflect.DeepEqual(mixed.samples, want) {
t.Errorf("samples = %v, want %v", mixed.samples, want)
}
file, ok := editorCatalog(t, editorWelcomeHTM).Template(editorWelcome)
if !ok || file.Samples["order.code"] != "ZAM-1" {
t.Errorf("TemplateFile.Samples = %v", file.Samples)
}
})
t.Run("walker", func(t *testing.T) {
render := func(src string, samples map[string]string) string {
t.Helper()
out, err := execHTMLFuncs("t", src, placeholderVars(samples, src), map[string]any{
"partial": func(name string, kv ...any) string { return "" },
})
if err != nil {
t.Fatalf("render %q: %v", src, err)
}
return out
}
samples := map[string]string{"Name": "Ada", "order.code": "ZAM-1"}
src := `{{ .Name }} {{ .order.code }} {{ if .employee.first_name }}x{{ end }} {{ range .order.items }}{{ .description }}{{ end }} {{ range .lines }}{{ . }}{{ end }} {{ with .user }}{{ .email }}{{ end }}`
if got, want := render(src, samples), "Ada ZAM-1 x [order.items.description] [lines] [user.email]"; got != want {
t.Errorf("placeholders = %q, want %q", got, want)
}
cases := map[string]struct {
src, want string
samples map[string]string
}{
"if else": {`{{ if .user }}Hi {{ .user.first_name }},{{ else }}Hi,{{ end }}`, "Hi [user.first_name],", nil},
"range fields": {"{{ range .order.items }}- {{ .description }} x {{ .count }}\n{{ end }}", "- [order.items.description] x [order.items.count]\n", nil},
"range variables": {`{{ range $i, $item := .rows }}{{ $item.name }}{{ end }}`, "[rows.name]", nil},
"range one var": {`{{ range $row := .rows }}{{ $row }}{{ end }}`, "[rows]", nil},
"range else": {`{{ range .rows }}{{ .a }}{{ else }}{{ .none }}{{ end }}`, "[rows.a]", nil},
"nested range": {`{{ range .groups }}{{ .title }}:{{ range .items }}{{ .name }}{{ end }}{{ end }}`, "[groups.title]:[groups.items.name]", nil},
"with else": {`{{ with .a.b }}{{ .c }}{{ else }}no{{ end }}`, "[a.b.c]", nil},
"with dot": {`{{ with .note }}{{ . }}{{ end }}`, "[note]", nil},
"root variable": {`{{ range .rows }}{{ $.shop }}{{ end }}`, "[shop]", nil},
"declared var": {`{{ $u := .user }}{{ $u.email }} {{ $u.name }}`, "[user.email] [user.name]", nil},
"chain": {`{{ (.user).email }}`, "[user.email]", nil},
"pipeline": {`{{ .title | printf "%s!" }}`, "[title]!", nil},
"function args": {`{{ printf "%s/%s" .a .b.c }}`, "[a]/[b.c]", nil},
"partial args": {`{{ partial "button" "url" .Link "body" .label.text }}{{ .Link }} {{ .label.text }}`, "[Link] [label.text]", nil},
"range sample": {`{{ range .lines }}{{ . }}{{ end }}`, "One", map[string]string{"lines": "One"}},
"element sample": {`{{ range .items }}{{ .name }}{{ end }}`, "Pen", map[string]string{"items.name": "Pen"}},
"unused sample": {`{{ .a }}`, "[a]", map[string]string{"b.c": "x"}},
"no variables": {`Plain.`, "Plain.", nil},
}
for name, tc := range cases {
if got := render(tc.src, tc.samples); got != tc.want {
t.Errorf("%s: %q, want %q", name, got, tc.want)
}
}
// A sample is available even when the walked sources do not use it.
vars := placeholderVars(map[string]string{"b.c": "x"}, `{{ .a }}`)
if b, ok := vars["b"].(map[string]any); !ok || b["c"] != "x" {
t.Errorf("an unused sample is missing: %v", vars)
}
// A source that does not parse contributes nothing and is not an error here.
if vars := placeholderVars(nil, `{{ .a`, `{{ .b }}`); vars["b"] != "[b]" {
t.Errorf("vars = %v", vars)
}
})
t.Run("validate", func(t *testing.T) {
ed, _ := newEditor(t, nil)
ok := Draft{Kind: KindTemplate, Name: editorWelcome, Subject: "Hi {{ .Name }}", Body: "Order {{ .order.code }} for {{ .customer.address.city }}.\n\n{{ partial \"button\" \"url\" .link \"body\" \"Go\" }}", Layout: editorBlogLayout}
if err := ed.Validate(ctx, ok); err != nil {
t.Fatalf("a valid template draft was refused: %+v", err)
}
layout := Draft{Kind: KindLayout, Name: "promo", HTML: `<div class="promo">{{ .Content }}</div>`, Text: "{{ .Content }}", CSS: ".promo{margin:0}"}
partial := Draft{Kind: KindPartial, Name: editorNote, HTML: `<b>{{ .text }}</b>`, Text: "{{ .text }}"}
for name, d := range map[string]Draft{"layout": layout, "file layout": with(layout, func(d *Draft) { d.Name = editorBlogLayout }), "partial": partial, "empty template layout": with(ok, func(d *Draft) { d.Layout = "" }),
// Text is not a URL: prose with a scheme-like word is not refused.
"prose body": with(ok, func(d *Draft) { d.Body = "See the metadata: field." })} {
if err := ed.Validate(ctx, d); err != nil {
t.Errorf("a valid %s draft was refused: %+v", name, err)
}
}
cases := []struct {
name string
draft Draft
field, rule string
message string // substring of Message
partial string // Name
}{
{"body parse", with(ok, func(d *Draft) { d.Body = "Hi {{ .Name" }), "body", "parse", "line 1", ""},
{"subject parse", with(ok, func(d *Draft) { d.Subject = "{{ .Name" }), "subject", "parse", "", ""},
{"body render", with(ok, func(d *Draft) { d.Body = "{{ len 5 }}" }), "body", "render", "len of type int", ""},
{"body script", with(ok, func(d *Draft) { d.Body = "Hi\n\n<script>alert(1)</script>" }), "body", "unsafe", "<script>alert(1)", ""},
{"layout scheme", with(layout, func(d *Draft) { d.HTML += `<a href="javascript&#58;alert(1)">x</a>` }), "content_html", "unsafe", "javascript&#58;alert(1)", ""},
{"subject line", with(ok, func(d *Draft) { d.Subject = "Hi\nthere" }), "subject", "subject_line", "", ""},
{"unknown layout", with(ok, func(d *Draft) { d.Layout = "nope" }), "layout", "layout", "", ""},
{"unknown partial", with(ok, func(d *Draft) { d.Body = `{{ partial "nope" "a" "b" }}` }), "body", "partial", "", "postcard::mail.partials.nope"},
{"layout html no content", with(layout, func(d *Draft) { d.HTML = "<div>static</div>" }), "content_html", "no_content", "", ""},
{"layout text no content", with(layout, func(d *Draft) { d.Text = "static" }), "content_text", "no_content", "", ""},
{"layout css", with(layout, func(d *Draft) { d.CSS = "a{}</style><p>x" }), "content_css", "unsafe", "</style>", ""},
{"layout script", with(layout, func(d *Draft) { d.HTML += `<iframe src="https://example.com"></iframe>` }), "content_html", "unsafe", "<iframe", ""},
{"layout html parse", with(layout, func(d *Draft) { d.HTML = "{{ .Content" }), "content_html", "parse", "", ""},
{"layout text parse", with(layout, func(d *Draft) { d.Text = "{{ .Content }}{{ end }}" }), "content_text", "parse", "", ""},
{"layout unknown partial", with(layout, func(d *Draft) { d.HTML += `{{ partial "nope" }}` }), "content_html", "partial", "", "postcard::mail.partials.nope"},
{"partial html parse", with(partial, func(d *Draft) { d.HTML = "{{ .text" }), "content_html", "parse", "", ""},
{"partial text render", with(partial, func(d *Draft) { d.Text = "{{ len 5 }}" }), "content_text", "render", "", ""},
{"partial nested call", with(partial, func(d *Draft) { d.HTML = `{{ partial "button" }}` }), "content_html", "parse", `"partial" not defined`, ""},
{"partial unsafe", with(partial, func(d *Draft) { d.HTML = `<a onclick = "x">{{ .text }}</a>` }), "content_html", "unsafe", "onclick", ""},
{"unknown template", with(ok, func(d *Draft) { d.Name = "acme.blog::mail.gone" }), "body", "render", "acme.blog::mail.gone", ""},
{"unknown partial draft", with(partial, func(d *Draft) { d.Name = "acme.blog::mail.partials.gone" }), "content_html", "partial", "", "acme.blog::mail.partials.gone"},
}
for _, tc := range cases {
got := ed.Validate(ctx, tc.draft)
if got == nil {
t.Errorf("%s: the draft was accepted", tc.name)
continue
}
if got.Field != tc.field || got.Rule != tc.rule || got.Name != tc.partial {
t.Errorf("%s: %+v, want field %q rule %q name %q", tc.name, *got, tc.field, tc.rule, tc.partial)
}
if !strings.Contains(got.Message, tc.message) {
t.Errorf("%s: message %q lacks %q", tc.name, got.Message, tc.message)
}
if strings.Contains(got.Message, "postcard:") || strings.Contains(got.Message, "template: ") {
t.Errorf("%s: message %q carries internal prefixes", tc.name, got.Message)
}
if got.Error() == "" {
t.Errorf("%s: empty Error()", tc.name)
}
}
// A template draft is checked against the layout it selects, with
// the other overrides of the source applied.
ed, _ = newEditor(t, &Overrides{Layouts: map[string]LayoutOverride{"promo": {HTML: `<div class="promo">{{ .Content }}</div>`, Text: "{{ .Content }}"}}})
if err := ed.Validate(ctx, with(ok, func(d *Draft) { d.Layout = "promo" })); err != nil {
t.Errorf("a draft on a database-only layout was refused: %+v", err)
}
})
t.Run("preview", func(t *testing.T) {
ed, drv := newEditor(t, nil)
file, _ := ed.Catalog().Template(editorWelcome)
draft := Draft{Kind: KindTemplate, Name: editorWelcome, Subject: file.Subject, Body: file.Body, Layout: file.Layout}
got, err := ed.Preview(ctx, draft)
if err != nil {
t.Fatalf("Preview: %v", err)
}
if got.Subject != "Welcome, Ada" {
t.Errorf("subject = %q, want the sample value", got.Subject)
}
for _, want := range []string{`<div class="blog-mail"><p>Hi <strong>Ada</strong>, order ZAM-1 for [company].</p>`, "<li>[order.items.description]</li>"} {
if !strings.Contains(got.HTML, want) {
t.Errorf("HTML lacks %q:\n%s", want, got.HTML)
}
}
if !strings.HasPrefix(got.Text, "Hi **Ada**, order ZAM-1 for [company].") || !strings.HasSuffix(got.Text, "-- The Acme blog") {
t.Errorf("text = %q", got.Text)
}
// The preview is what Send produces for the same content and variables.
vars := placeholderVars(file.Samples, file.Subject, file.Body)
if err := ed.m.Send(ctx, Message{Template: editorWelcome, To: []string{"ada@example.com"}, Vars: vars}); err != nil {
t.Fatal(err)
}
sent := lastMessage(t, drv)
if sent.Subject != got.Subject || sent.HTML != got.HTML || sent.Text != got.Text {
t.Errorf("preview differs from the sent mail:\n preview %+v\n sent %+v", got, sent)
}
// A draft that moves the template to another layout.
draft.Layout = systemLayoutName
draft.Subject = "Changed"
got, err = ed.Preview(ctx, draft)
if err != nil {
t.Fatal(err)
}
if got.Subject != "Changed" || !strings.Contains(got.HTML, "layout-system") {
t.Errorf("draft layout not applied: %q / %q", got.Subject, got.HTML)
}
// A layout draft wraps the sample message; this one keeps its style
// block.
got, err = ed.Preview(ctx, Draft{Kind: KindLayout, Name: "promo", HTML: `<div class="promo">{{ .Content }}</div><style>{{ .css }}</style>`, Text: "PROMO\n{{ .Content }}", CSS: ".promo{margin:0}", DisableAutoInlineCSS: true})
if err != nil {
t.Fatalf("layout preview: %v", err)
}
for _, want := range []string{`<div class="promo"><h1>`, `class="button button-primary"`, `<table class="panel`, "<style>.promo{margin:0}</style>"} {
if !strings.Contains(got.HTML, want) {
t.Errorf("layout preview lacks %q:\n%s", want, got.HTML)
}
}
if got.Subject == "" || !strings.HasPrefix(got.Text, "PROMO\n") {
t.Errorf("layout preview: %q / %q", got.Subject, got.Text)
}
// A partial draft is called with placeholder arguments.
got, err = ed.Preview(ctx, Draft{Kind: KindPartial, Name: editorNote, HTML: `<div class="note2">{{ .text }} {{ .author.name }}</div>`, Text: "N: {{ .text }}"})
if err != nil {
t.Fatalf("partial preview: %v", err)
}
if !strings.Contains(got.HTML, `<div class="note2">[text] [author.name]</div>`) || !strings.Contains(got.Text, "N: [text]") {
t.Errorf("partial preview: %q / %q", got.HTML, got.Text)
}
// The other pieces come from the override source: a customized
// partial and a customized default layout show in a template preview.
ed, _ = newEditor(t, &Overrides{
Partials: map[string]PartialOverride{editorNote: {HTML: `<aside>{{ .text }}</aside>`, Text: "* {{ .text }}"}},
Layouts: map[string]LayoutOverride{defaultLayoutName: {HTML: `<main>{{ .Content }}</main>`, Text: "{{ .Content }}"}},
// The stored override of the template itself is not the draft.
Template: &TemplateOverride{Subject: "Stored", Body: "Stored."},
})
got, err = ed.Preview(ctx, Draft{Kind: KindTemplate, Name: editorWelcome, Subject: "Draft", Body: `{{ partial "` + editorNote + `" "text" "hi" }}`, Layout: defaultLayoutName})
if err != nil {
t.Fatal(err)
}
if got.Subject != "Draft" || got.HTML != "<main><aside>hi</aside>\n</main>" || got.Text != "* hi" {
t.Errorf("preview with overrides = %+v", got)
}
// A draft problem is a *DraftError.
_, err = ed.Preview(ctx, Draft{Kind: KindTemplate, Name: editorWelcome, Subject: "x", Body: "{{ .a", Layout: defaultLayoutName})
var de *DraftError
if !errors.As(err, &de) || de.Field != "body" || de.Rule != "parse" {
t.Errorf("Preview of a broken draft = %v", err)
}
})
t.Run("send_draft", func(t *testing.T) {
ed, drv := newEditor(t, nil)
draft := Draft{Kind: KindTemplate, Name: editorWelcome, Subject: "Test {{ .Name }}", Body: "Body for {{ .company }}.", Layout: editorBlogLayout}
if err := ed.SendDraft(ctx, draft, "admin@example.com"); err != nil {
t.Fatalf("SendDraft: %v", err)
}
msgs := drv.Messages()
if len(msgs) != 1 {
t.Fatalf("%d messages sent, want 1", len(msgs))
}
msg := msgs[0]
if len(msg.To) != 1 || msg.To[0] != "admin@example.com" || len(msg.Cc) != 0 || len(msg.Bcc) != 0 {
t.Errorf("recipients = %v / %v / %v, want only the given address", msg.To, msg.Cc, msg.Bcc)
}
if msg.From != "blog@example.com" || msg.Subject != "Test Ada" || !strings.Contains(msg.HTML, "<p>Body for [company].</p>") {
t.Errorf("message = %+v", msg)
}
// Layout and partial drafts are sent as their previews.
if err := ed.SendDraft(ctx, Draft{Kind: KindLayout, Name: "promo", HTML: `<div class="promo">{{ .Content }}</div>`, Text: "{{ .Content }}"}, "admin@example.com"); err != nil {
t.Fatalf("SendDraft layout: %v", err)
}
if msg := lastMessage(t, drv); !strings.Contains(msg.HTML, `<div class="promo">`) {
t.Errorf("layout draft mail = %q", msg.HTML)
}
before := len(drv.Messages())
var de *DraftError
if err := ed.SendDraft(ctx, with(draft, func(d *Draft) { d.Body = "{{ .a" }), "admin@example.com"); !errors.As(err, &de) {
t.Errorf("SendDraft of a broken draft = %v", err)
}
for _, to := range []string{"", "not-an-address", "a@example.com\r\nBcc: b@example.com"} {
if err := ed.SendDraft(ctx, draft, to); err == nil {
t.Errorf("SendDraft to %q succeeded", to)
}
}
if len(drv.Messages()) != before {
t.Error("a refused draft was sent")
}
})
t.Run("hash", func(t *testing.T) {
lf := editorCatalog(t, editorWelcomeHTM)
// The same files with CRLF line endings.
crlfFS := fstest.MapFS{}
for path, file := range editorFS(editorWelcomeHTM) {
crlfFS[path] = &fstest.MapFile{Data: []byte(strings.ReplaceAll(string(file.Data), "\n", "\r\n"))}
}
p := editorPlugin(editorWelcomeHTM)
crlf := NewCatalog()
if err := crlf.RegisterPartials(p.id, crlfFS, p.partials); err != nil {
t.Fatal(err)
}
if err := crlf.Register(p.id, crlfFS, p.templates, p.layouts); err != nil {
t.Fatal(err)
}
templates, layouts, partials := lf.Templates(), lf.Layouts(), lf.Partials()
if len(templates) != 2 || len(layouts) != 4 || len(partials) != 7 {
t.Fatalf("catalog holds %d templates, %d layouts, %d partials", len(templates), len(layouts), len(partials))
}
if templates[0].Name != editorWelcome || templates[1].Name != "acme.blog::mail.welcome-en" || layouts[0].Name != editorBlogLayout || partials[0].Name != editorNote {
t.Errorf("lists are not sorted by name: %q %q %q %q", templates[0].Name, templates[1].Name, layouts[0].Name, partials[0].Name)
}
for _, f := range templates {
other, ok := crlf.Template(f.Name)
if !ok || !hex64.MatchString(f.Hash) || other.Hash != f.Hash || other.Source != f.Source || strings.Contains(other.Source, "\r") {
t.Errorf("template %s: hash %q vs CRLF %q", f.Name, f.Hash, other.Hash)
}
}
for _, f := range layouts {
other, ok := crlf.Layout(f.Name)
if !ok || !hex64.MatchString(f.Hash) || other.Hash != f.Hash || other.Source != f.Source {
t.Errorf("layout %s: hash %q vs CRLF %q", f.Name, f.Hash, other.Hash)
}
}
for _, f := range partials {
other, ok := crlf.Partial(f.Name)
if !ok || !hex64.MatchString(f.Hash) || other.Hash != f.Hash || other.Source != f.Source {
t.Errorf("partial %s: hash %q vs CRLF %q", f.Name, f.Hash, other.Hash)
}
}
// A different file has a different hash.
changed, _ := editorCatalog(t, strings.Replace(editorWelcomeHTM, "Hi **", "Hello **", 1)).Template(editorWelcome)
if changed.Hash == templates[0].Hash {
t.Error("a changed file kept its hash")
}
w := templates[0]
if w.PluginID != "acme.blog" || w.Subject != "Welcome, {{ .Name }}" || w.Description != "Sent after registration" || w.Layout != editorBlogLayout ||
!strings.HasPrefix(w.Body, "Hi **{{ .Name }}**") || w.Source != editorWelcomeHTM {
t.Errorf("TemplateFile = %+v", w)
}
if en := templates[1]; en.Layout != defaultLayoutName {
t.Errorf("a template without a layout resolves to %q, want the default layout's full name", en.Layout)
}
blog, _ := lf.Layout(editorBlogLayout)
if blog.Title != "Blog" || blog.PluginID != "acme.blog" || blog.Text != "{{ .Content }}\n\n-- The Acme blog" || blog.HTML != `<div class="blog-mail">{{ .Content }}</div>` {
t.Errorf("LayoutFile = %+v", blog)
}
note, _ := lf.Partial(editorNote)
if note.Title != "Note" || note.Text != "NOTE: {{ .text }}" || note.HTML != `<div class="note">{{ .text }}</div>` {
t.Errorf("PartialFile = %+v", note)
}
if _, ok := lf.Template("acme.blog::mail.gone"); ok {
t.Error("Template found a name that is not registered")
}
if _, ok := lf.Layout("promo"); ok {
t.Error("Layout found a name that is not registered")
}
if _, ok := lf.Partial("button"); ok {
t.Error("Partial resolves bare names; it takes full names")
}
// The serializers give both sides of a comparison the same shape.
if got, want := FormatTemplate("Hi", editorBlogLayout, "Body."), "subject = \"Hi\"\nlayout = \""+editorBlogLayout+"\"\n==\nBody.\n"; got != want {
t.Errorf("FormatTemplate = %q, want %q", got, want)
}
if got, want := FormatLayout("Blog", "T", "<b>H</b>", ""), "name = \"Blog\"\n==\nT\n==\n<b>H</b>\n"; got != want {
t.Errorf("FormatLayout = %q, want %q", got, want)
}
if got, want := FormatLayout("Blog", "T", "<b>H</b>", "a{}"), "name = \"Blog\"\n==\nT\n==\n<b>H</b>\n==\na{}\n"; got != want {
t.Errorf("FormatLayout with CSS = %q, want %q", got, want)
}
if got, want := FormatPartial("Note", "T", "<b>H</b>"), "name = \"Note\"\n==\nT\n==\n<b>H</b>\n"; got != want {
t.Errorf("FormatPartial = %q, want %q", got, want)
}
if got := FormatLayout(blog.Title, blog.Text, blog.HTML, ""); got != blog.Source {
t.Errorf("FormatLayout of a file's parts = %q, want its source %q", got, blog.Source)
}
})
t.Run("locale", func(t *testing.T) {
locales := []string{"en", "pl", "pt-BR"}
cases := map[string]string{
"acme.blog::mail.welcome-en": "en",
"acme.blog::mail.cancel-notice": "",
"acme.blog::mail.welcome": "",
"acme.blog::mail.two_factor_code-en": "en",
"acme.blog::mail.order.paid-pl": "pl",
"acme.blog::mail.order-en.paid": "",
"acme.blog::mail.welcome-EN": "en",
"acme.blog::mail.welcome-pt-BR": "pt-BR",
"acme.blog::mail.-en": "",
"acme-en": "",
}
for name, want := range cases {
if got := TemplateLocale(name, locales); got != want {
t.Errorf("TemplateLocale(%q) = %q, want %q", name, got, want)
}
}
if got := TemplateLocale("acme.blog::mail.welcome-en", nil); got != "" {
t.Errorf("TemplateLocale without locales = %q", got)
}
})
t.Run("sync", func(t *testing.T) {
gdb := mailPostgres(t)
cat := editorCatalog(t, editorWelcomeHTM)
locales := []string{"en", "pl"}
if err := SyncIndex(ctx, gdb, cat, locales); err != nil {
t.Fatalf("SyncIndex: %v", err)
}
type row struct {
ID int64
Code string
Source *string
Locale *string
Description string
Subject string
Body string
LayoutID *int64
Name string
ContentHTML string `gorm:"column:content_html"`
ContentText string `gorm:"column:content_text"`
IsCustom bool
FileHash *string
BaseHash *string
UpdatedText string
}
rows := func(table string) map[string]row {
t.Helper()
var list []row
if err := gdb.Raw(`SELECT *, updated_at::text AS updated_text FROM ` + table + ` ORDER BY code`).Scan(&list).Error; err != nil {
t.Fatal(err)
}
out := make(map[string]row, len(list))
for _, r := range list {
out[r.Code] = r
}
return out
}
templates, layouts, partials := rows(TemplatesTable), rows(LayoutsTable), rows(PartialsTable)
if len(templates) != 2 || len(layouts) != 4 || len(partials) != 7 {
t.Fatalf("index holds %d templates, %d layouts, %d partials", len(templates), len(layouts), len(partials))
}
file, _ := cat.Template(editorWelcome)
w := templates[editorWelcome]
if w.Source == nil || *w.Source != "acme.blog" || w.Locale != nil || w.Description != file.Description || w.Subject != file.Subject || w.Body != file.Body ||
w.IsCustom || w.FileHash == nil || *w.FileHash != file.Hash || w.BaseHash != nil {
t.Errorf("welcome row = %+v", w)
}
if w.LayoutID == nil || *w.LayoutID != layouts[editorBlogLayout].ID {
t.Errorf("welcome row layout_id = %v, want the blog layout row %d", w.LayoutID, layouts[editorBlogLayout].ID)
}
if en := templates["acme.blog::mail.welcome-en"]; en.Locale == nil || *en.Locale != "en" || en.LayoutID == nil || *en.LayoutID != layouts[defaultLayoutName].ID {
t.Errorf("welcome-en row = %+v", en)
}
blog, _ := cat.Layout(editorBlogLayout)
if l := layouts[editorBlogLayout]; l.Name != "Blog" || l.ContentHTML != blog.HTML || l.ContentText != blog.Text || l.IsCustom || *l.FileHash != blog.Hash || *l.Source != "acme.blog" {
t.Errorf("blog layout row = %+v", l)
}
if l := layouts[systemLayoutName]; l.Name != "System layout" || *l.Source != "postcard" {
t.Errorf("system layout row = %+v", l)
}
note, _ := cat.Partial(editorNote)
if p := partials[editorNote]; p.Name != "Note" || p.ContentHTML != note.HTML || p.ContentText != note.Text || *p.FileHash != note.Hash {
t.Errorf("note partial row = %+v", p)
}
// A second run changes nothing, IDs and timestamps included.
if err := SyncIndex(ctx, gdb, cat, locales); err != nil {
t.Fatal(err)
}
if again := rows(TemplatesTable); !reflect.DeepEqual(again, templates) {
t.Errorf("a second sync changed the template rows:\n%+v\n%+v", templates, again)
}
if again := rows(LayoutsTable); !reflect.DeepEqual(again, layouts) {
t.Error("a second sync changed the layout rows")
}
if again := rows(PartialsTable); !reflect.DeepEqual(again, partials) {
t.Error("a second sync changed the partial rows")
}
// An override, a database-only layout and a row of a template this
// catalog does not hold.
defaultID := layouts[defaultLayoutName].ID
stmts := []string{
`UPDATE ` + TemplatesTable + ` SET subject = 'Mine', body = 'My body.', layout_id = ` + itoa(defaultID) + `, is_custom = TRUE, base_hash = 'based-on', customized_at = NOW() WHERE code = '` + editorWelcome + `'`,
`UPDATE ` + PartialsTable + ` SET content_html = '<i>mine</i>', is_custom = TRUE, base_hash = 'based-on' WHERE code = '` + editorNote + `'`,
`UPDATE ` + LayoutsTable + ` SET content_html = '<p>mine {{ .Content }}</p>', is_custom = TRUE, base_hash = 'based-on' WHERE code = '` + editorBlogLayout + `'`,
`INSERT INTO ` + LayoutsTable + ` (code, source, name, content_html, content_text, is_custom) VALUES ('promo', NULL, 'Promo', '<p>{{ .Content }}</p>', '{{ .Content }}', TRUE)`,
`INSERT INTO ` + TemplatesTable + ` (code, source, subject, body) VALUES ('acme.old::mail.gone', 'acme.old', 'Old', 'Old.')`,
}
for _, stmt := range stmts {
if err := gdb.Exec(stmt).Error; err != nil {
t.Fatal(err)
}
}
promo := rows(LayoutsTable)["promo"]
// The plugin ships changed files: the override columns stay, the
// file columns follow the files.
changedSrc := strings.Replace(strings.Replace(editorWelcomeHTM, "Sent after registration", "Sent on sign-up", 1), "Hi **", "Hello **", 1)
changed := editorCatalog(t, changedSrc)
if err := SyncIndex(ctx, gdb, changed, locales); err != nil {
t.Fatal(err)
}
newFile, _ := changed.Template(editorWelcome)
after := rows(TemplatesTable)
w2 := after[editorWelcome]
if w2.ID != w.ID || w2.Subject != "Mine" || w2.Body != "My body." || *w2.LayoutID != defaultID || !w2.IsCustom || *w2.BaseHash != "based-on" {
t.Errorf("sync touched the override columns of a customized row: %+v", w2)
}
if *w2.FileHash != newFile.Hash || w2.Description != "Sent on sign-up" || *w2.FileHash == *w.FileHash {
t.Errorf("sync did not refresh the file columns of a customized row: %+v", w2)
}
if len(after) != 3 || after["acme.old::mail.gone"].Subject != "Old" {
t.Errorf("sync deleted or changed a row outside the catalog: %+v", after)
}
afterLayouts := rows(LayoutsTable)
if got := afterLayouts["promo"]; !reflect.DeepEqual(got, promo) {
t.Errorf("sync touched a database-only layout: %+v", got)
}
if l := afterLayouts[editorBlogLayout]; l.ContentHTML != "<p>mine {{ .Content }}</p>" || !l.IsCustom {
t.Errorf("sync touched a customized layout: %+v", l)
}
if p := rows(PartialsTable)[editorNote]; p.ContentHTML != "<i>mine</i>" || !p.IsCustom {
t.Errorf("sync touched a customized partial: %+v", p)
}
// A row that is not customized follows the file.
if err := gdb.Exec(`UPDATE ` + TemplatesTable + ` SET is_custom = FALSE WHERE code = '` + editorWelcome + `'`).Error; err != nil {
t.Fatal(err)
}
if err := SyncIndex(ctx, gdb, changed, locales); err != nil {
t.Fatal(err)
}
if w3 := rows(TemplatesTable)[editorWelcome]; w3.Subject != newFile.Subject || w3.Body != newFile.Body || *w3.LayoutID != layouts[editorBlogLayout].ID {
t.Errorf("a row that is not customized was not mirrored from the file: %+v", w3)
}
// Concurrent runs both succeed.
var wg sync.WaitGroup
errs := make([]error, 4)
for i := range errs {
wg.Add(1)
go func() {
defer wg.Done()
errs[i] = SyncIndex(ctx, gdb, cat, locales)
}()
}
wg.Wait()
for i, err := range errs {
if err != nil {
t.Errorf("concurrent SyncIndex %d: %v", i, err)
}
}
if got := rows(TemplatesTable); len(got) != 3 || got[editorWelcome].ID != w.ID {
t.Errorf("concurrent syncs changed the rows: %+v", got)
}
// Tables that are not migrated yet are an error for the caller to report.
empty := mailPostgres(t)
if err := empty.Exec(`DROP TABLE ` + TemplatesTable).Error; err != nil {
t.Fatal(err)
}
if err := SyncIndex(ctx, empty, cat, locales); err == nil {
t.Error("SyncIndex without the tables succeeded")
}
if err := SyncIndex(ctx, nil, cat, locales); err == nil {
t.Error("SyncIndex with a nil database succeeded")
}
})
t.Run("reset", func(t *testing.T) {
gdb := mailPostgres(t)
cat := editorCatalog(t, editorWelcomeHTM)
if err := SyncIndex(ctx, gdb, cat, nil); err != nil {
t.Fatal(err)
}
stmts := []string{
`UPDATE ` + TemplatesTable + ` SET subject = 'Mine', body = 'My body.', layout_id = (SELECT id FROM ` + LayoutsTable + ` WHERE code = '` + defaultLayoutName + `'), is_custom = TRUE, base_hash = 'based-on', customized_at = NOW() WHERE code = '` + editorWelcome + `'`,
`UPDATE ` + LayoutsTable + ` SET content_html = 'mine', content_text = 'mine', content_css = 'a{}', is_custom = TRUE, base_hash = 'based-on', customized_at = NOW() WHERE code = '` + editorBlogLayout + `'`,
`UPDATE ` + PartialsTable + ` SET content_html = 'mine', content_text = 'mine', is_custom = TRUE, base_hash = 'based-on', customized_at = NOW() WHERE code = '` + editorNote + `'`,
`INSERT INTO ` + LayoutsTable + ` (code, source, name, content_html, content_text, is_custom) VALUES ('promo', NULL, 'Promo', '<p>{{ .Content }}</p>', '{{ .Content }}', TRUE)`,
}
for _, stmt := range stmts {
if err := gdb.Exec(stmt).Error; err != nil {
t.Fatal(err)
}
}
type state struct {
Subject string
Body string
Layout string
ContentHTML string `gorm:"column:content_html"`
ContentText string `gorm:"column:content_text"`
ContentCSS string `gorm:"column:content_css"`
IsCustom bool
BaseHash *string
CustomizedAt *string
FileHash *string
}
read := func(query, code string) state {
t.Helper()
var s state
if err := gdb.Raw(query, code).Scan(&s).Error; err != nil {
t.Fatal(err)
}
return s
}
if err := ResetOverride(ctx, gdb, cat, KindTemplate, editorWelcome); err != nil {
t.Fatalf("ResetOverride template: %v", err)
}
file, _ := cat.Template(editorWelcome)
got := read(`SELECT t.subject, t.body, l.code AS layout, t.is_custom, t.base_hash, t.customized_at::text AS customized_at, t.file_hash
FROM `+TemplatesTable+` t LEFT JOIN `+LayoutsTable+` l ON l.id = t.layout_id WHERE t.code = ?`, editorWelcome)
if got.Subject != file.Subject || got.Body != file.Body || got.Layout != editorBlogLayout || got.IsCustom || got.BaseHash != nil || got.CustomizedAt != nil || *got.FileHash != file.Hash {
t.Errorf("template after reset = %+v", got)
}
if err := ResetOverride(ctx, gdb, cat, KindLayout, editorBlogLayout); err != nil {
t.Fatalf("ResetOverride layout: %v", err)
}
blog, _ := cat.Layout(editorBlogLayout)
got = read(`SELECT content_html, content_text, content_css, is_custom, base_hash, customized_at::text AS customized_at, file_hash FROM `+LayoutsTable+` WHERE code = ?`, editorBlogLayout)
if got.ContentHTML != blog.HTML || got.ContentText != blog.Text || got.ContentCSS != "" || got.IsCustom || got.BaseHash != nil || got.CustomizedAt != nil {
t.Errorf("layout after reset = %+v", got)
}
if err := ResetOverride(ctx, gdb, cat, KindPartial, editorNote); err != nil {
t.Fatalf("ResetOverride partial: %v", err)
}
note, _ := cat.Partial(editorNote)
got = read(`SELECT content_html, content_text, is_custom, base_hash, customized_at::text AS customized_at, file_hash FROM `+PartialsTable+` WHERE code = ?`, editorNote)
if got.ContentHTML != note.HTML || got.ContentText != note.Text || got.IsCustom || got.BaseHash != nil || got.CustomizedAt != nil {
t.Errorf("partial after reset = %+v", got)
}
// A database-only layout has no file version: it is deleted, never reset.
if err := ResetOverride(ctx, gdb, cat, KindLayout, "promo"); err == nil {
t.Error("ResetOverride of a database-only layout succeeded")
}
if got := read(`SELECT content_html, is_custom FROM `+LayoutsTable+` WHERE code = ?`, "promo"); got.ContentHTML != "<p>{{ .Content }}</p>" || !got.IsCustom {
t.Errorf("a refused reset changed the row: %+v", got)
}
for name, err := range map[string]error{
"unknown code": ResetOverride(ctx, gdb, cat, KindTemplate, "acme.blog::mail.gone"),
"brand": ResetOverride(ctx, gdb, cat, KindBrand, "1"),
"nil db": ResetOverride(ctx, nil, cat, KindTemplate, editorWelcome),
} {
if err == nil {
t.Errorf("ResetOverride (%s) succeeded", name)
}
}
})
t.Run("inline_flag", func(t *testing.T) {
gdb := mailPostgres(t)
const plain = "acme.shop::mail.layouts.plain"
cat := NewCatalog()
fsys := fstest.MapFS{
"views/mail/x.htm": {Data: []byte("subject = \"X\"\nlayout = \"plain\"\n==\nx\n")},
"views/mail/layouts/plain.htm": {Data: []byte("name = \"Plain\"\ndisable_auto_inline_css = true\n==\n{{ .Content }}\n==\n<div>{{ .Content }}</div>\n")},
}
if err := cat.Register("acme.shop", fsys, []string{"acme.shop::mail.x"}, map[string]string{"plain": plain}); err != nil {
t.Fatal(err)
}
flag := func(code string) bool {
t.Helper()
var v []bool
if err := gdb.Raw(`SELECT disable_auto_inline_css FROM `+LayoutsTable+` WHERE code = ?`, code).Scan(&v).Error; err != nil || len(v) != 1 {
t.Fatalf("flag of %s: %v %v", code, v, err)
}
return v[0]
}
sync := func() {
t.Helper()
if err := SyncIndex(ctx, gdb, cat, nil); err != nil {
t.Fatal(err)
}
}
// The index row mirrors the file's header key while not customized.
sync()
if !flag(plain) || flag(defaultLayoutName) {
t.Fatalf("after sync: plain %v, default %v", flag(plain), flag(defaultLayoutName))
}
if err := gdb.Exec(`UPDATE ` + LayoutsTable + ` SET disable_auto_inline_css = FALSE WHERE code = '` + plain + `'`).Error; err != nil {
t.Fatal(err)
}
sync()
if !flag(plain) {
t.Error("sync did not restore the file's value on a row that is not customized")
}
// A customized row keeps its own value, and the source reports it.
if err := gdb.Exec(`UPDATE ` + LayoutsTable + ` SET disable_auto_inline_css = FALSE, is_custom = TRUE, base_hash = file_hash WHERE code = '` + plain + `'`).Error; err != nil {
t.Fatal(err)
}
sync()
if flag(plain) {
t.Error("sync overwrote the value of a customized row")
}
ov, err := NewDBSource(appWith(t, gdb)).MailOverrides(ctx, "")
if err != nil || ov.Layouts[plain].DisableAutoInlineCSS {
t.Errorf("overrides = %+v, %v", ov.Layouts, err)
}
if err := gdb.Exec(`UPDATE ` + LayoutsTable + ` SET disable_auto_inline_css = TRUE WHERE code = '` + plain + `'`).Error; err != nil {
t.Fatal(err)
}
if ov, err = NewDBSource(appWith(t, gdb)).MailOverrides(ctx, ""); err != nil || !ov.Layouts[plain].DisableAutoInlineCSS {
t.Errorf("overrides = %+v, %v", ov.Layouts, err)
}
// Reset writes the file's value back.
if err := gdb.Exec(`UPDATE ` + LayoutsTable + ` SET disable_auto_inline_css = FALSE WHERE code = '` + plain + `'`).Error; err != nil {
t.Fatal(err)
}
if err := ResetOverride(ctx, gdb, cat, KindLayout, plain); err != nil {
t.Fatal(err)
}
if !flag(plain) {
t.Error("reset did not write the file's value back")
}
})
}
func with(d Draft, edit func(*Draft)) Draft { edit(&d); return d }
func itoa(n int64) string { return strconv.FormatInt(n, 10) }