test(postcard): locale fallback, built-in layouts, partials and CSS inlining

- TestTemplateBase, TestTemplateFor and TestSendLocaleFallback pin the
  <template>-<locale> resolution and the order the override source is asked
- TestEnsureLocaleRows (Postgres): rows only for non-default locales without
  files, idempotent, customized content kept while file_hash follows the
  base, base_code from SyncIndex, reset of a fallback row, migrations roll back
- TestBuiltinLayoutsLogo, TestHeaderFooterPartials, TestButtonPartial and
  TestSiteName cover the finished default, system and minimal layouts
- TestInlineCSS, TestInlineCSSOptOut and TestInlineFailure cover inlining,
  the per-layout opt-out and the fallback when the inliner fails
- check-phase35.sh requires the new names (GO_TESTS, DOCKER_TESTS)
This commit is contained in:
Jakub Zych
2026-11-13 21:31:44 +01:00
parent ae13e4dd41
commit a0dde086d2
5 changed files with 1044 additions and 0 deletions

View File

@@ -0,0 +1,245 @@
package postcard
import (
"bytes"
"context"
"errors"
"log/slog"
"strings"
"testing"
"testing/fstest"
"time"
)
const (
inlineBrandColour = "#445566"
inlineMedia = "@media only screen and (max-width: 600px) { .inner-body { width: 100% !important; } }"
)
var inlineVars = map[string]any{"Name": "Ada", "Link": "https://example.com/go"}
// defaultLayoutWithCSS is the built-in default layout as a database layout
// whose CSS is css.
func defaultLayoutWithCSS(css string, optOut bool) map[string]LayoutOverride {
def, _ := NewCatalog().Layout(defaultLayoutName)
return map[string]LayoutOverride{defaultLayoutName: {HTML: def.HTML, Text: def.Text, CSS: css, DisableAutoInlineCSS: optOut}}
}
func TestInlineCSS(t *testing.T) {
t.Parallel()
brand := &Brand{ButtonPrimaryBg: inlineBrandColour}
ov := &Overrides{Brand: brand, Layouts: defaultLayoutWithCSS(inlineMedia, false)}
m, drv := partialsMailer(t, Options{}, ov)
inlined := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
// The same message with inlining off: the mailer's no-op inliner seam.
plainMailer, plainDrv := partialsMailer(t, Options{}, ov)
plainMailer.(*mailer).inline = noInline
plain := sendTemplate(t, plainMailer, plainDrv, "acme.blog::mail.welcome", inlineVars)
t.Run("button_colour", func(t *testing.T) {
if style := buttonStyle(t, inlined.HTML); !strings.Contains(style, "background-color:"+inlineBrandColour) {
t.Errorf("button style = %q, want the brand primary colour inline", style)
}
if style := buttonStyle(t, plain.HTML); style != "" {
t.Errorf("without inlining the button has style %q", style)
}
})
t.Run("doctype", func(t *testing.T) {
const doctype = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
if !strings.HasPrefix(inlined.HTML, doctype) {
t.Errorf("DOCTYPE lost:\n%.200s", inlined.HTML)
}
if strings.Count(inlined.HTML, "<!DOCTYPE") != 1 {
t.Errorf("DOCTYPE repeated:\n%.300s", inlined.HTML)
}
})
t.Run("media_query", func(t *testing.T) {
css := styleBlock(t, inlined.HTML)
if !strings.Contains(css, "@media only screen and (max-width: 600px)") || !strings.Contains(css, ".inner-body") {
t.Errorf("the layout's media query is not in a style element:\n%s", css)
}
for _, tag := range withClass(inlined.HTML, "table", "inner-body") {
if strings.Contains(tag.attrs["style"], "100% !important") {
t.Errorf("a media-query rule reached an element: %q", tag.attrs["style"])
}
}
})
t.Run("text_unchanged", func(t *testing.T) {
if inlined.Text != plain.Text || inlined.Subject != plain.Subject {
t.Errorf("inlining changed the text part or the subject:\n%q\n%q", inlined.Text, plain.Text)
}
if inlined.HTML == plain.HTML {
t.Error("inlining changed nothing in the HTML")
}
if unstyled(inlined.HTML) == inlined.HTML {
t.Error("the inlined HTML has no style attributes")
}
})
t.Run("safety_check", func(t *testing.T) {
if err := validateHTML(inlined.HTML); err != nil {
t.Errorf("the inlined HTML fails the safety checks: %v", err)
}
// A layout override whose CSS holds a javascript: URL never passed
// save validation (Editor.Validate refuses it); injected as a raw
// fixture, it fails the safety check and the file version is sent.
plugin := acmeBlog{}
cat := NewCatalog()
if err := cat.Register(plugin.ID(), plugin.MailTemplatesFS(), plugin.MailTemplates(), plugin.MailLayouts()); err != nil {
t.Fatal(err)
}
unsafe := Overrides{Layouts: map[string]LayoutOverride{
"acme.blog::mail.layouts.blog": {
HTML: `<html><head><style>{{ .css }}</style></head><body><div class="blog-mail">{{ .Content }}</div></body></html>`,
Text: "{{ .Content }}",
CSS: `.blog-mail { background: url("javascript:alert(1)"); }`,
},
}}
assertFileFallback(t, cat, fileWelcome(t), Options{Overrides: fakeOverrides{ov: unsafe}}, true)
// The same CSS in an inlined style attribute is caught by the check
// that runs on the inliner's output.
if err := validateHTML(`<div style="background: url(&#34;javascript:alert(1)&#34;)">x</div>`); err == nil {
t.Error("the safety check accepts a javascript: URL in a style attribute")
}
})
}
func TestInlineCSSOptOut(t *testing.T) {
t.Parallel()
brand := &Brand{ButtonPrimaryBg: inlineBrandColour}
const buttonBody = "{{ partial \"button\" \"url\" \"https://example.com\" \"body\" \"Go\" }}\n"
const plainLayout = "<html><head><style>{{ .brandCss }}</style></head><body>{{ .Content }}</body></html>\n"
// optedOut checks a message whose layout kept its CSS in the style block.
optedOut := func(t *testing.T, html string) {
t.Helper()
if style := buttonStyle(t, html); style != "" {
t.Errorf("the button has an inlined style %q", style)
}
if css := styleBlock(t, html); !strings.Contains(css, inlineBrandColour) {
t.Errorf("the style block lost the brand CSS:\n%s", css)
}
}
inlinedButton := func(t *testing.T, html string) {
t.Helper()
if style := buttonStyle(t, html); !strings.Contains(style, inlineBrandColour) {
t.Errorf("the button carries no inlined brand colour: %q", style)
}
}
t.Run("file_header_key", func(t *testing.T) {
for _, value := range []string{"true", "TRUE", "1", "yes", "false", "0", ""} {
header := "name = \"Plain\"\n"
if value != "" {
header += "disable_auto_inline_css = " + value + "\n"
}
fsys := fstest.MapFS{
"views/mail/plain.htm": {Data: []byte("subject = \"Plain\"\nlayout = \"plain\"\n==\n" + buttonBody)},
"views/mail/layouts/plain.htm": {Data: []byte(header + "==\n{{ .Content }}\n==\n" + plainLayout)},
}
cat := NewCatalog()
if err := cat.Register("acme.shop", fsys, []string{"acme.shop::mail.plain"}, map[string]string{"plain": "acme.shop::mail.layouts.plain"}); err != nil {
t.Fatal(err)
}
want := value == "true" || value == "TRUE" || value == "1" || value == "yes"
if f, _ := cat.Layout("acme.shop::mail.layouts.plain"); f.DisableAutoInlineCSS != want {
t.Errorf("header %q: LayoutFile.DisableAutoInlineCSS = %v, want %v", value, f.DisableAutoInlineCSS, want)
}
drv := NewMemoryDriver()
msg := sendTemplate(t, NewMailer(cat, drv, Options{Overrides: fakeOverrides{ov: Overrides{Brand: brand}}}), drv, "acme.shop::mail.plain", nil)
if want {
optedOut(t, msg.HTML)
} else {
inlinedButton(t, msg.HTML)
}
}
for _, f := range NewCatalog().Layouts() {
if f.DisableAutoInlineCSS {
t.Errorf("built-in layout %s opts out of inlining", f.Name)
}
}
})
t.Run("override_column", func(t *testing.T) {
m, drv := partialsMailer(t, Options{}, &Overrides{Brand: brand, Layouts: defaultLayoutWithCSS("", true)})
optedOut(t, sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars).HTML)
m, drv = partialsMailer(t, Options{}, &Overrides{Brand: brand, Layouts: defaultLayoutWithCSS("", false)})
inlinedButton(t, sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars).HTML)
})
t.Run("layout_draft", func(t *testing.T) {
ed, _ := newEditor(t, &Overrides{Brand: brand})
draft := Draft{Kind: KindLayout, HTML: plainLayout, Text: "{{ .Content }}"}
prev, err := ed.Preview(context.Background(), draft)
if err != nil {
t.Fatal(err)
}
inlinedButton(t, prev.HTML)
draft.DisableAutoInlineCSS = true
if prev, err = ed.Preview(context.Background(), draft); err != nil {
t.Fatal(err)
}
optedOut(t, prev.HTML)
if derr := ed.Validate(context.Background(), draft); derr != nil {
t.Errorf("a layout draft with the opt-out does not validate: %v", derr)
}
})
}
func TestInlineFailure(t *testing.T) {
t.Parallel()
brand := &Brand{ButtonPrimaryBg: inlineBrandColour}
m, drv := partialsMailer(t, Options{CSS: inlineMedia}, &Overrides{Brand: brand})
mm := m.(*mailer)
var logs bytes.Buffer
mm.log = slog.New(slog.NewTextHandler(&logs, nil))
calls := 0
mm.inline = func(string) (string, error) { calls++; return "", errors.New("inliner broke") }
failed := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
if calls != 2 {
t.Fatalf("the inliner ran %d times, want 2", calls)
}
// The un-inlined HTML is sent: exactly what a no-op inliner produces.
ref, refDrv := partialsMailer(t, Options{CSS: inlineMedia}, &Overrides{Brand: brand})
ref.(*mailer).inline = noInline
want := sendTemplate(t, ref, refDrv, "acme.blog::mail.welcome", inlineVars)
if failed.HTML != want.HTML || failed.Text != want.Text {
t.Errorf("an inliner error did not send the un-inlined message:\n%s\nwant\n%s", failed.HTML, want.HTML)
}
if style := buttonStyle(t, failed.HTML); style != "" {
t.Errorf("the button has a style %q after an inliner error", style)
}
if css := styleBlock(t, failed.HTML); !strings.Contains(css, inlineBrandColour) || !strings.Contains(css, "@media") {
t.Errorf("the style block lost CSS after an inliner error:\n%s", css)
}
// One warning per layout per minute: two sends in the default layout
// warn once; the system layout is another key.
out := logs.String()
if n := strings.Count(out, "level=WARN"); n != 1 || !strings.Contains(out, "inliner broke") || !strings.Contains(out, "layout="+defaultLayoutName) {
t.Fatalf("log = %q, want one warning naming the error and the layout", out)
}
sendTemplate(t, m, drv, "acme.blog::mail.auto", nil)
if n := strings.Count(logs.String(), "level=WARN"); n != 2 || !strings.Contains(logs.String(), "layout="+systemLayoutName) {
t.Errorf("log = %q, want a second warning for the system layout", logs.String())
}
// The gate behind it repeats a key after warnInterval.
var g warnGate
now := time.Now()
if !g.allow("inline:"+defaultLayoutName, now) || g.allow("inline:"+defaultLayoutName, now.Add(warnInterval-time.Second)) {
t.Error("the warning gate did not hold a key for the interval")
}
if !g.allow("inline:"+defaultLayoutName, now.Add(warnInterval)) {
t.Error("the warning gate did not repeat a key after the interval")
}
// The real inliner takes ordinary HTML without an error.
if _, err := inlineCSS("<html><head><style>p{color:red}</style></head><body><p>x</p></body></html>"); err != nil {
t.Errorf("inlineCSS on plain HTML: %v", err)
}
}

View File

@@ -0,0 +1,393 @@
package postcard
import (
"regexp"
"strings"
"testing"
"testing/fstest"
)
// htmlTag is one opening tag of rendered HTML. Inlining re-serialises the
// HTML (attribute order, added style attributes), so tests read tags by
// attribute instead of comparing markup.
type htmlTag struct {
name string
attrs map[string]string
start, end int // the tag's byte range in the HTML
}
var (
openTagRe = regexp.MustCompile(`<([a-zA-Z][a-zA-Z0-9]*)(\s[^>]*)?>`)
tagAttrRe = regexp.MustCompile(`([a-zA-Z_:][-a-zA-Z0-9_:.]*)\s*=\s*"([^"]*)"`)
)
// htmlTags returns the opening tags called name, in document order.
func htmlTags(html, name string) []htmlTag {
var out []htmlTag
for _, m := range openTagRe.FindAllStringSubmatchIndex(html, -1) {
if !strings.EqualFold(html[m[2]:m[3]], name) {
continue
}
tag := htmlTag{name: name, attrs: map[string]string{}, start: m[0], end: m[1]}
if m[4] >= 0 {
for _, a := range tagAttrRe.FindAllStringSubmatch(html[m[4]:m[5]], -1) {
tag.attrs[strings.ToLower(a[1])] = a[2]
}
}
out = append(out, tag)
}
return out
}
func (h htmlTag) hasClass(class string) bool {
for _, c := range strings.Fields(h.attrs["class"]) {
if c == class {
return true
}
}
return false
}
// withClass returns the tags called name that carry class.
func withClass(html, name, class string) []htmlTag {
var out []htmlTag
for _, tag := range htmlTags(html, name) {
if tag.hasClass(class) {
out = append(out, tag)
}
}
return out
}
// headerCell returns the content of the built-in header partial's cell, and
// whether there is one.
func headerCell(html string) (string, bool) {
cells := withClass(html, "td", "header")
if len(cells) == 0 {
return "", false
}
rest := html[cells[0].end:]
content, _, _ := strings.Cut(rest, "</td>")
return content, true
}
var logoSpan = regexp.MustCompile(`<span[^>]*>([^<]*)</span>`)
const testLogo = "https://cdn.example.com/logo.png"
// builtinLayoutCases are the blog fixture's templates on the three built-in
// layouts, with a text the message body starts with.
var builtinLayoutCases = []struct {
layout, template, content string
header bool
}{
{"default", "acme.blog::mail.welcome", "Hi Ada", true},
{"system", "acme.blog::mail.auto", "Done.", true},
{"minimal", "acme.blog::mail.bare", "Done.", false},
}
// brandedSend sends template through the blog fixture with brand (nil: no
// override source) and mail.from from.
func brandedSend(t *testing.T, brand *Brand, from, template string) RenderedMessage {
t.Helper()
var ov *Overrides
if brand != nil {
ov = &Overrides{Brand: brand}
}
m, drv := partialsMailer(t, Options{From: from}, ov)
return sendTemplate(t, m, drv, template, map[string]any{"Name": "Ada", "Link": "https://example.com/go"})
}
func TestBuiltinLayoutsLogo(t *testing.T) {
t.Parallel()
brands := []struct {
name string
brand *Brand
from string
logo bool
alt string
title string // the site name the header shows without a logo
}{
{name: "logo_and_name", brand: &Brand{LogoURL: testLogo, SiteName: "Acme Mail"}, logo: true, alt: "Acme Mail"},
{name: "logo_only", brand: &Brand{LogoURL: testLogo}, from: "shop@example.com", logo: true, alt: ""},
{name: "name_only", brand: &Brand{SiteName: "Acme Mail"}, title: "Acme Mail"},
{name: "neither", brand: &Brand{}, from: "shop@example.com"},
{name: "no_branding", from: "shop@example.com"},
}
for _, b := range brands {
for _, l := range builtinLayoutCases {
t.Run(b.name+"/"+l.layout, func(t *testing.T) {
msg := brandedSend(t, b.brand, b.from, l.template)
html := msg.HTML
logos := withClass(html, "img", "logo")
cell, hasHeader := headerCell(html)
switch {
case b.logo:
if len(logos) != 1 {
t.Fatalf("%d logo images, want 1:\n%s", len(logos), html)
}
img := logos[0]
if img.attrs["src"] != testLogo {
t.Errorf("logo src = %q, want %q", img.attrs["src"], testLogo)
}
if alt, ok := img.attrs["alt"]; !ok || alt != b.alt {
t.Errorf("logo alt = %q (present %v), want %q", alt, ok, b.alt)
}
if l.header {
// In the header cell of the default and system layouts.
if !hasHeader || !strings.Contains(cell, `src="`+testLogo+`"`) {
t.Errorf("the logo is not in the header cell:\n%s", html)
}
} else {
// Above the message in the minimal layout, which has no header.
if hasHeader {
t.Errorf("the minimal layout has a header cell:\n%s", html)
}
if at := strings.Index(html, l.content); at < 0 || img.start > at {
t.Errorf("the logo is not above the message:\n%s", html)
}
}
default:
if len(logos) != 0 {
t.Errorf("a logo image without a Branding logo:\n%s", html)
}
}
switch {
case !l.header:
if hasHeader {
t.Errorf("the minimal layout has a header cell:\n%s", html)
}
case b.title != "":
m := logoSpan.FindStringSubmatch(cell)
if !hasHeader || m == nil || m[1] != b.title {
t.Errorf("the header does not show the site name %q:\n%s", b.title, html)
}
case !b.logo:
if hasHeader {
t.Errorf("a header row without a logo or a site name:\n%s", html)
}
}
})
}
}
}
// partialsFixture is a plugin whose templates call the header and footer
// partials directly, in a layout without CSS (nothing is inlined).
func partialsFixture(t *testing.T) (Mailer, *MemoryDriver) {
t.Helper()
fsys := fstest.MapFS{
"views/mail/layouts/plain.htm": {Data: []byte("name = \"Plain\"\n==\n{{ .Content }}\n==\n<table class=\"plain\">{{ .Content }}</table>\n")},
"views/mail/header.htm": {Data: []byte("subject = \"Header\"\nlayout = \"plain\"\n==\n{{ partial \"header\" \"logo\" .logo \"body\" .name \"url\" .url }}\n")},
"views/mail/footer.htm": {Data: []byte("subject = \"Footer\"\nlayout = \"plain\"\n==\n{{ partial \"footer\" \"body\" .body }}\n")},
}
cat := NewCatalog()
if err := cat.Register("acme.parts", fsys, []string{"acme.parts::mail.header", "acme.parts::mail.footer"}, map[string]string{"plain": "acme.parts::mail.layouts.plain"}); err != nil {
t.Fatal(err)
}
drv := NewMemoryDriver()
return NewMailer(cat, drv, Options{}), drv
}
var footerPara = regexp.MustCompile(`<p[^>]*>([^<]*)</p>`)
// footerText returns the paragraph of the footer partial's table.
func footerText(t *testing.T, html string) string {
t.Helper()
tables := withClass(html, "table", "footer")
if len(tables) != 1 {
t.Fatalf("%d footer tables, want 1:\n%s", len(tables), html)
}
cells := withClass(html[tables[0].end:], "td", "content-cell")
if len(cells) == 0 {
t.Fatalf("no content cell in the footer:\n%s", html)
}
m := footerPara.FindStringSubmatch(html[tables[0].end+cells[0].end:])
if m == nil {
t.Fatalf("no paragraph in the footer:\n%s", html)
}
return m[1]
}
func TestHeaderFooterPartials(t *testing.T) {
t.Parallel()
m, drv := partialsFixture(t)
header := []struct {
name, logo, url string
wantImg, wantA bool
}{
{name: "logo", logo: testLogo, wantImg: true},
{name: "name_only"},
{name: "url_with_logo", logo: testLogo, url: "https://acme.example/", wantImg: true, wantA: true},
{name: "url_with_name", url: "https://acme.example/", wantA: true},
}
for _, tc := range header {
t.Run("header/"+tc.name, func(t *testing.T) {
msg := sendTemplate(t, m, drv, "acme.parts::mail.header", map[string]any{"logo": tc.logo, "name": "Acme Mail", "url": tc.url})
cell, ok := headerCell(msg.HTML)
if !ok || !strings.Contains(msg.HTML, "<tr>") {
t.Fatalf("no header row:\n%s", msg.HTML)
}
imgs := htmlTags(cell, "img")
if tc.wantImg {
if len(imgs) != 1 || imgs[0].attrs["src"] != tc.logo || imgs[0].attrs["alt"] != "Acme Mail" || !imgs[0].hasClass("logo") {
t.Errorf("header image = %+v, want img.logo src %s alt Acme Mail:\n%s", imgs, tc.logo, cell)
}
if strings.Contains(cell, "<span") {
t.Errorf("a span next to the logo:\n%s", cell)
}
} else {
if len(imgs) != 0 {
t.Errorf("an image without a logo:\n%s", cell)
}
if !strings.Contains(cell, "Acme Mail") {
t.Errorf("the header does not show the name:\n%s", cell)
}
}
links := htmlTags(cell, "a")
if tc.wantA {
if len(links) != 1 || links[0].attrs["href"] != tc.url {
t.Fatalf("header link = %+v, want href %s:\n%s", links, tc.url, cell)
}
// The link wraps the logo or the name.
inner, _, _ := strings.Cut(cell[links[0].end:], "</a>")
if tc.wantImg && !strings.Contains(inner, "<img") || !tc.wantImg && inner != "Acme Mail" {
t.Errorf("the link does not wrap the content: %q", inner)
}
} else {
if len(links) != 0 {
t.Errorf("a link without a url:\n%s", cell)
}
if !tc.wantImg && !strings.Contains(cell, "<span>Acme Mail</span>") {
t.Errorf("the name is not in a span:\n%s", cell)
}
}
if strings.TrimSpace(msg.Text) != "Acme Mail" {
t.Errorf("header text = %q, want the name", msg.Text)
}
})
}
for _, body := range []string{"© 2031 Acme Mail", "© 2031"} {
t.Run("footer/"+body, func(t *testing.T) {
msg := sendTemplate(t, m, drv, "acme.parts::mail.footer", map[string]any{"body": body})
if got := footerText(t, msg.HTML); got != body {
t.Errorf("footer paragraph = %q, want %q", got, body)
}
if want := "-------------------\n" + body; strings.TrimSpace(msg.Text) != want {
t.Errorf("footer text = %q, want %q", msg.Text, want)
}
})
}
// The built-in layouts fill the footer with © year and the site name.
for _, l := range builtinLayoutCases[:2] {
for _, name := range []string{"Acme Mail", ""} {
t.Run("layout/"+l.layout+"/"+name, func(t *testing.T) {
msg := brandedSend(t, &Brand{SiteName: name}, "", l.template)
want := strings.TrimSpace(copyright() + " " + name)
if got := footerText(t, msg.HTML); got != want {
t.Errorf("footer = %q, want %q", got, want)
}
if !strings.HasSuffix(strings.TrimRight(msg.Text, "\n"), "-------------------\n"+want) {
t.Errorf("text part does not end with the footer %q:\n%s", want, msg.Text)
}
})
}
}
}
func TestButtonPartial(t *testing.T) {
t.Parallel()
const url = "https://example.com/activate?code=a1"
body := "Before.\n\n" +
"{{ partial \"button\" \"url\" .url \"body\" \"Go\" }}\n\n" +
"{{ partial \"button\" \"url\" .url \"body\" \"Yes\" \"type\" \"positive\" }}\n\n" +
"{{ partial \"button\" \"url\" .url \"body\" \"No\" \"type\" \"negative\" }}\n\nAfter.\n"
fsys := fstest.MapFS{}
for _, layout := range []string{"default", "system", "minimal"} {
fsys["views/mail/action_"+layout+".htm"] = &fstest.MapFile{Data: []byte("subject = \"Action\"\nlayout = \"" + layout + "\"\n==\n" + body)}
}
cat := NewCatalog()
names := []string{"acme.act::mail.action_default", "acme.act::mail.action_system", "acme.act::mail.action_minimal"}
if err := cat.Register("acme.act", fsys, names, nil); err != nil {
t.Fatal(err)
}
drv := NewMemoryDriver()
m := NewMailer(cat, drv, Options{})
want := []struct{ text, class string }{
{"Go", "button button-primary"},
{"Yes", "button button-positive"},
{"No", "button button-negative"},
}
for _, name := range names {
t.Run(name, func(t *testing.T) {
msg := sendTemplate(t, m, drv, name, map[string]any{"url": url})
var buttons []htmlTag
for _, a := range htmlTags(msg.HTML, "a") {
if a.hasClass("button") {
buttons = append(buttons, a)
}
}
if len(buttons) != len(want) {
t.Fatalf("%d buttons, want %d:\n%s", len(buttons), len(want), msg.HTML)
}
for i, w := range want {
b := buttons[i]
if b.attrs["class"] != w.class {
t.Errorf("button %d class = %q, want %q", i, b.attrs["class"], w.class)
}
// html/template escapes the query's = sign in the href.
if href := strings.ReplaceAll(b.attrs["href"], "&#61;", "="); href != url {
t.Errorf("button %d href = %q, want %q", i, b.attrs["href"], url)
}
if label, _, _ := strings.Cut(msg.HTML[b.end:], "</a>"); label != w.text {
t.Errorf("button %d label = %q, want %q", i, label, w.text)
}
if line := w.text + " <" + url + ">"; !strings.Contains(msg.Text, line) {
t.Errorf("text part lacks %q:\n%s", line, msg.Text)
}
}
if strings.Contains(msg.Text, "<a ") || strings.Contains(msg.Text, "MAILPARTIAL") {
t.Errorf("text part holds HTML or a partial token:\n%s", msg.Text)
}
if !strings.Contains(msg.Text, "Before.") || !strings.Contains(msg.Text, "After.") {
t.Errorf("text part lost the body:\n%s", msg.Text)
}
})
}
}
func TestSiteName(t *testing.T) {
t.Parallel()
cases := []struct {
name string
brand *Brand
from string
want string
}{
{"branding_wins", &Brand{SiteName: " Acme Mail "}, "Acme Shop <shop@example.com>", "Acme Mail"},
{"empty_uses_from", &Brand{}, "Acme Shop <shop@example.com>", "Acme Shop"},
{"blank_uses_from", &Brand{SiteName: " "}, `"Acme Shop" <shop@example.com>`, "Acme Shop"},
{"no_branding_uses_from", nil, "Acme Shop <shop@example.com>", "Acme Shop"},
{"from_without_name", nil, "shop@example.com", ""},
{"from_unparsable", &Brand{}, "not an address", ""},
{"from_empty", nil, "", ""},
}
for _, tc := range cases {
if got := siteName(tc.brand, tc.from); got != tc.want {
t.Errorf("%s: siteName(%+v, %q) = %q, want %q", tc.name, tc.brand, tc.from, got, tc.want)
}
}
// The mail.from fallback reaches the header and the footer of a send.
msg := brandedSend(t, nil, "Acme Shop <shop@example.com>", "acme.blog::mail.welcome")
cell, ok := headerCell(msg.HTML)
if m := logoSpan.FindStringSubmatch(cell); !ok || m == nil || m[1] != "Acme Shop" {
t.Errorf("header does not show the mail.from name:\n%s", msg.HTML)
}
if got := footerText(t, msg.HTML); got != copyright()+" Acme Shop" {
t.Errorf("footer = %q, want %q", got, copyright()+" Acme Shop")
}
}

View File

@@ -0,0 +1,193 @@
package postcard
import (
"context"
"testing"
"testing/fstest"
"time"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
// ensureCatalog is a catalog holding welcome (body welcomeBody) and
// welcome-en, the shape EnsureLocaleRows works on.
func ensureCatalog(t *testing.T, welcomeBody string) *Catalog {
t.Helper()
fsys := fstest.MapFS{
"views/mail/welcome.htm": {Data: []byte("subject = \"Welcome, {{ .name }}\"\ndescription = \"Sent after registration\"\n==\n" + welcomeBody + "\n")},
"views/mail/welcome-en.htm": {Data: []byte("subject = \"Welcome (en)\"\n==\nHello.\n")},
}
cat := NewCatalog()
if err := cat.Register("acme.blog", fsys, []string{localeWelcome, localeWelcome + "-en"}, nil); err != nil {
t.Fatal(err)
}
return cat
}
// templateRow is the part of a template index row TestEnsureLocaleRows reads.
type templateRow struct {
Code string
Locale *string
BaseCode *string
Description string
Subject string
Body string
LayoutID *int64
IsCustom bool
FileHash *string
BaseHash *string
CustomizedAt *time.Time
UpdatedAt time.Time
}
func templateRows(t *testing.T, gdb *gorm.DB) map[string]templateRow {
t.Helper()
var rows []templateRow
if err := gdb.Raw(`SELECT code, locale, base_code, description, subject, body, layout_id, is_custom, file_hash, base_hash, customized_at, updated_at
FROM ` + TemplatesTable + ` ORDER BY code`).Scan(&rows).Error; err != nil {
t.Fatal(err)
}
out := make(map[string]templateRow, len(rows))
for _, r := range rows {
out[r.Code] = r
}
return out
}
func derefOrNil(p *string) string {
if p == nil {
return "<nil>"
}
return *p
}
func TestEnsureLocaleRows(t *testing.T) {
gdb := mailPostgres(t)
ctx := context.Background()
locales := []string{"pl", "en", "de"}
de := localeWelcome + "-de"
cat := ensureCatalog(t, "Hi **{{ .name }}**.")
base, _ := cat.Template(localeWelcome)
// Only de gets a row: pl is the default locale (the base file is its
// version) and en ships a file.
if err := EnsureLocaleRows(ctx, gdb, cat, "pl", locales); err != nil {
t.Fatal(err)
}
rows := templateRows(t, gdb)
if len(rows) != 1 {
t.Fatalf("rows after EnsureLocaleRows = %v, want only %s", rows, de)
}
row, ok := rows[de]
if !ok {
t.Fatalf("no %s row: %v", de, rows)
}
if derefOrNil(row.Locale) != "de" || derefOrNil(row.BaseCode) != localeWelcome || derefOrNil(row.FileHash) != base.Hash ||
row.Subject != base.Subject || row.Body != base.Body || row.Description != base.Description || row.IsCustom || row.BaseHash != nil {
t.Errorf("%s row = %+v, want the base file's content and hash, locale de, base_code %s, not customized", de, row, localeWelcome)
}
var defaultLayout int64
if err := gdb.Raw(`SELECT COUNT(*) FROM `+LayoutsTable+` WHERE code = ?`, defaultLayoutName).Scan(&defaultLayout).Error; err != nil {
t.Fatal(err)
}
if defaultLayout == 0 && row.LayoutID != nil {
t.Errorf("layout_id = %d without a layout row", *row.LayoutID)
}
// A second run writes nothing.
time.Sleep(20 * time.Millisecond)
if err := EnsureLocaleRows(ctx, gdb, cat, "pl", locales); err != nil {
t.Fatal(err)
}
if again := templateRows(t, gdb)[de]; !again.UpdatedAt.Equal(row.UpdatedAt) {
t.Errorf("a second run rewrote %s: updated_at %v, was %v", de, again.UpdatedAt, row.UpdatedAt)
}
// bases limits the run to the listed templates.
if err := EnsureLocaleRows(ctx, gdb, cat, "pl", []string{"pl", "fr"}, "acme.blog::mail.other"); err != nil {
t.Fatal(err)
}
if _, ok := templateRows(t, gdb)[localeWelcome+"-fr"]; ok {
t.Error("a base outside bases got a fr row")
}
// SyncIndex writes base_code for the file-backed rows, and its layout
// rows give the fallback row a layout_id on the next run.
if err := SyncIndex(ctx, gdb, cat, locales); err != nil {
t.Fatal(err)
}
rows = templateRows(t, gdb)
if r := rows[localeWelcome+"-en"]; derefOrNil(r.BaseCode) != localeWelcome || derefOrNil(r.Locale) != "en" {
t.Errorf("welcome-en after SyncIndex = %+v, want base_code %s and locale en", r, localeWelcome)
}
if r := rows[localeWelcome]; derefOrNil(r.BaseCode) != localeWelcome || r.Locale != nil {
t.Errorf("welcome after SyncIndex = %+v, want base_code %s and no locale", r, localeWelcome)
}
// A customized fallback row keeps its content when the base file
// changes; file_hash follows the base, so the row shows as outdated.
if err := gdb.Exec(`UPDATE `+TemplatesTable+` SET subject = 'Willkommen', body = 'Hallo.', is_custom = TRUE, base_hash = file_hash, customized_at = NOW() WHERE code = ?`, de).Error; err != nil {
t.Fatal(err)
}
changed := ensureCatalog(t, "Hi **{{ .name }}**, the file changed.")
newBase, _ := changed.Template(localeWelcome)
if newBase.Hash == base.Hash {
t.Fatal("the changed base file has the same hash")
}
if err := EnsureLocaleRows(ctx, gdb, changed, "pl", locales); err != nil {
t.Fatal(err)
}
row = templateRows(t, gdb)[de]
if row.Subject != "Willkommen" || row.Body != "Hallo." || !row.IsCustom {
t.Errorf("customized %s lost its content: %+v", de, row)
}
if derefOrNil(row.FileHash) != newBase.Hash || derefOrNil(row.BaseHash) != base.Hash {
t.Errorf("%s file_hash = %s, base_hash = %s, want %s (the changed base) and %s", de, derefOrNil(row.FileHash), derefOrNil(row.BaseHash), newBase.Hash, base.Hash)
}
// Reset returns the fallback row to the base file's version.
if err := ResetOverride(ctx, gdb, changed, KindTemplate, de); err != nil {
t.Fatal(err)
}
row = templateRows(t, gdb)[de]
if row.Subject != newBase.Subject || row.Body != newBase.Body || row.IsCustom || row.BaseHash != nil || row.CustomizedAt != nil || derefOrNil(row.FileHash) != newBase.Hash {
t.Errorf("%s after reset = %+v, want the base file's version, not customized", de, row)
}
if err := ResetOverride(ctx, gdb, changed, KindTemplate, "acme.blog::mail.missing-de"); err == nil {
t.Error("reset of a code with no file version succeeded")
}
// Every migration rolls back and applies again, the 35 gap-closure ones
// (base_code, site_name, disable_auto_inline_css) included.
ids := make(map[string]bool, len(Migrations))
for _, m := range Migrations {
ids[m.ID] = true
}
for _, id := range []string{"202611130001_mail_template_base_code", "202611130002_mail_brand_site_name", "202611130003_mail_layout_disable_auto_inline_css"} {
if !ids[id] {
t.Errorf("migration %s is missing", id)
}
}
mig := gormigrate.New(gdb, gormigrate.DefaultOptions, Migrations)
for range Migrations {
if err := mig.RollbackLast(); err != nil {
t.Fatalf("rollback: %v", err)
}
}
var n int64
if err := gdb.Raw(`SELECT COUNT(*) FROM information_schema.tables WHERE table_name LIKE 'system_mail_%'`).Scan(&n).Error; err != nil {
t.Fatal(err)
}
if n != 0 {
t.Errorf("%d mail tables left after the rollback", n)
}
if err := mig.Migrate(); err != nil {
t.Fatalf("migrate again: %v", err)
}
if err := EnsureLocaleRows(ctx, gdb, cat, "pl", locales); err != nil {
t.Fatalf("EnsureLocaleRows after migrating again: %v", err)
}
if rows := templateRows(t, gdb); len(rows) != 1 || derefOrNil(rows[de].BaseCode) != localeWelcome {
t.Errorf("rows after migrating again = %v", rows)
}
}

View File

@@ -0,0 +1,202 @@
package postcard
import (
"context"
"reflect"
"sync"
"testing"
"testing/fstest"
)
// localeFS is a plugin whose welcome template ships a default-locale file
// and an English file, next to dashed names that are not locale variants.
func localeFS() fstest.MapFS {
tpl := func(subject string) *fstest.MapFile {
return &fstest.MapFile{Data: []byte("subject = \"" + subject + "\"\n==\n" + subject + " body for {{ .name }}.\n")}
}
return fstest.MapFS{
"views/mail/welcome.htm": tpl("Welcome"),
"views/mail/welcome-en.htm": tpl("Welcome EN"),
"views/mail/order.htm": tpl("Order"),
"views/mail/order-paid.htm": tpl("Order paid"),
"views/mail/cancel-notice.htm": tpl("Cancel notice"),
}
}
const (
localeWelcome = "acme.blog::mail.welcome"
localeOrder = "acme.blog::mail.order"
localePaid = "acme.blog::mail.order-paid"
)
// localeCatalog registers localeFS under acme.blog.
func localeCatalog(t *testing.T) *Catalog {
t.Helper()
cat := NewCatalog()
names := []string{localeWelcome, localeWelcome + "-en", localeOrder, localePaid, "acme.blog::mail.cancel-notice"}
if err := cat.Register("acme.blog", localeFS(), names, nil); err != nil {
t.Fatal(err)
}
return cat
}
// recordingOverrides is an OverrideSource that answers by template name and
// records the names it was asked for, in order.
type recordingOverrides struct {
mu sync.Mutex
by map[string]Overrides
asked []string
}
func (r *recordingOverrides) MailOverrides(_ context.Context, name string) (Overrides, error) {
r.mu.Lock()
defer r.mu.Unlock()
r.asked = append(r.asked, name)
return r.by[name], nil
}
func (r *recordingOverrides) names() []string {
r.mu.Lock()
defer r.mu.Unlock()
return append([]string(nil), r.asked...)
}
func TestTemplateBase(t *testing.T) {
t.Parallel()
cases := []struct {
name string
locales []string
want string
}{
{"acme.blog::mail.welcome-en", []string{"en", "pl"}, "acme.blog::mail.welcome"},
{"acme.blog::mail.welcome-pl", []string{"en", "pl"}, "acme.blog::mail.welcome"},
{"acme.blog::mail.welcome-EN", []string{"en", "pl"}, "acme.blog::mail.welcome"},
{"acme.blog::mail.welcome-pt-BR", []string{"pt-BR"}, "acme.blog::mail.welcome"},
// pt-BR wins over a shorter locale that also ends the name.
{"acme.blog::mail.welcome-pt-BR", []string{"BR", "pt-BR"}, "acme.blog::mail.welcome"},
{"acme.blog::mail.cancel-notice", []string{"en", "pl"}, "acme.blog::mail.cancel-notice"},
{"acme.blog::mail.welcome", []string{"en", "pl"}, "acme.blog::mail.welcome"},
// A locale that is not one of the application's is not stripped.
{"acme.blog::mail.welcome-de", []string{"en", "pl"}, "acme.blog::mail.welcome-de"},
{"welcome-en", []string{"en"}, "welcome-en"},
{"acme.blog::mail.welcome-en", nil, "acme.blog::mail.welcome-en"},
}
for _, tc := range cases {
if got := TemplateBase(tc.name, tc.locales); got != tc.want {
t.Errorf("TemplateBase(%q, %v) = %q, want %q", tc.name, tc.locales, got, tc.want)
}
}
}
func TestTemplateFor(t *testing.T) {
t.Parallel()
cat := localeCatalog(t)
cases := []struct {
name string
want string // the Name of the file returned; empty means not found
reason string
}{
{localeWelcome, localeWelcome, "own file"},
{localeWelcome + "-en", localeWelcome + "-en", "own file of a locale"},
{localeWelcome + "-de", localeWelcome, "locale without a file"},
{localeWelcome + "-pt-BR", localeWelcome, "language with a region subtag"},
{localeWelcome + "-zh_Hant", localeWelcome, "underscore subtag"},
{localePaid + "-en", localePaid, "longest registered base"},
{localeOrder + "-en", localeOrder, "shorter base of its own"},
{"acme.blog::mail.cancel-notice", "acme.blog::mail.cancel-notice", "dashed name with a file"},
{localeWelcome + "-now", "", "three letters are not a locale tag"},
{"acme.blog::mail.missing-de", "", "no base template"},
{"acme.shop::mail.welcome-de", "", "another plugin's namespace"},
{"welcome-de", "", "no namespace"},
{localeWelcome + "-", "", "empty tag"},
}
for _, tc := range cases {
f, ok := cat.TemplateFor(tc.name)
if tc.want == "" {
if ok {
t.Errorf("%s (%s): TemplateFor = %q, want not found", tc.name, tc.reason, f.Name)
}
continue
}
if !ok || f.Name != tc.want {
t.Errorf("%s (%s): TemplateFor = %q, %v, want %q", tc.name, tc.reason, f.Name, ok, tc.want)
continue
}
own, _ := cat.Template(tc.want)
if f.Hash != own.Hash || f.Subject != own.Subject {
t.Errorf("%s: file %+v is not %s's", tc.name, f, tc.want)
}
}
// Template keeps answering for registered names only.
if _, ok := cat.Template(localeWelcome + "-de"); ok {
t.Error("Template found a fallback-locale name")
}
}
func TestSendLocaleFallback(t *testing.T) {
t.Parallel()
cat := localeCatalog(t)
de, en := localeWelcome+"-de", localeWelcome+"-en"
deOverride := Overrides{Template: &TemplateOverride{Subject: "Willkommen {{ .name }}", Body: "Hallo."}}
baseOverride := Overrides{Template: &TemplateOverride{Subject: "Base {{ .name }}", Body: "Base body."}}
cases := []struct {
name string
template string
by map[string]Overrides
wantSub string
wantAsked []string
}{
{"own_customization", de, map[string]Overrides{de: deOverride, localeWelcome: baseOverride}, "Willkommen Ada", []string{de}},
{"base_customization", de, map[string]Overrides{localeWelcome: baseOverride}, "Base Ada", []string{de, localeWelcome}},
{"base_file", de, nil, "Welcome", []string{de, localeWelcome}},
{"file_backed_locale", en, map[string]Overrides{localeWelcome: baseOverride}, "Welcome EN", []string{en}},
{"file_backed_locale_customized", en, map[string]Overrides{en: {Template: &TemplateOverride{Subject: "Custom EN", Body: "x"}}}, "Custom EN", []string{en}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
src := &recordingOverrides{by: tc.by}
drv := NewMemoryDriver()
m := NewMailer(cat, drv, Options{Overrides: src})
sendFixture(t, m, tc.template, "")
got := lastMessage(t, drv)
if got.Subject != tc.wantSub {
t.Errorf("subject = %q, want %q", got.Subject, tc.wantSub)
}
if asked := src.names(); !reflect.DeepEqual(asked, tc.wantAsked) {
t.Errorf("override source asked for %v, want %v", asked, tc.wantAsked)
}
})
}
t.Run("file_versions_match", func(t *testing.T) {
// Without an override source a fallback name sends the base file
// exactly, and a file-backed locale its own file.
drv := NewMemoryDriver()
m := NewMailer(cat, drv, Options{})
sendFixture(t, m, localeWelcome, "")
base := lastMessage(t, drv)
sendFixture(t, m, de, "")
if got := lastMessage(t, drv); got.Subject != base.Subject || got.HTML != base.HTML || got.Text != base.Text {
t.Errorf("welcome-de = %+v, want the base file %+v", got, base)
}
sendFixture(t, m, en, "")
if got := lastMessage(t, drv); got.Subject != "Welcome EN" || got.Text == base.Text {
t.Errorf("welcome-en = %+v, want its own file", got)
}
})
t.Run("unknown_template", func(t *testing.T) {
src := &recordingOverrides{}
m := NewMailer(cat, NewMemoryDriver(), Options{Overrides: src})
for _, name := range []string{"acme.blog::mail.missing-de", localeWelcome + "-now"} {
err := m.Send(context.Background(), Message{Template: name, To: []string{"ada@example.com"}})
if err == nil || err.Error() != "postcard: unknown template "+name {
t.Errorf("Send(%s) error = %v, want postcard: unknown template %s", name, err, name)
}
}
if asked := src.names(); len(asked) != 0 {
t.Errorf("an unknown template asked the override source for %v", asked)
}
})
}