fix(postcard): print the site name literally in the built-in layouts' footer
- the footer partial gains a text argument, printed as a paragraph through html/template and verbatim in the text part; body stays Markdown for callers that pass Markdown on purpose - the default and system layouts pass the copyright line as text, so *Acme*, Acme <Studio> and [x](url) appear as typed, as in the header (WR-01) - README partial table, layouts and Branding paragraphs; docs partials table and built-in layouts paragraph - TestMailSafetySmoke/literal_footer
This commit is contained in:
@@ -3,6 +3,7 @@ package postcard
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"html"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -41,6 +42,17 @@ func assertSiteName(t *testing.T, msg RenderedMessage, name string) {
|
||||
}
|
||||
}
|
||||
|
||||
// footerTable returns the footer partial's table of html.
|
||||
func footerTable(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)
|
||||
}
|
||||
table, _, _ := strings.Cut(html[tables[0].start:], "</table>")
|
||||
return table
|
||||
}
|
||||
|
||||
// TestMailSafetySmoke covers the 35-08 gap closure: prose in a site name or
|
||||
// a body is not a URL, encoded script URLs are refused, unsafe site names
|
||||
// never stop a send, the inliner never stops mail and the footer prints the
|
||||
@@ -203,4 +215,39 @@ func TestMailSafetySmoke(t *testing.T) {
|
||||
t.Errorf("log = %q, want the layout", out)
|
||||
}
|
||||
})
|
||||
t.Run("literal_footer", func(t *testing.T) {
|
||||
for _, name := range []string{"*Acme*", "Acme <Studio>", "[x](https://example.com)"} {
|
||||
for _, l := range safetyLayouts {
|
||||
msg := brandedSend(t, &Brand{SiteName: name}, "", l.template)
|
||||
escaped := html.EscapeString(name)
|
||||
if got, want := footerText(t, msg.HTML), copyright()+" "+escaped; got != want {
|
||||
t.Errorf("%s %q: footer = %q, want %q", l.layout, name, got, want)
|
||||
}
|
||||
footer := footerTable(t, msg.HTML)
|
||||
for _, bad := range []string{"<em", "<a ", "raw HTML omitted", "<!--"} {
|
||||
if strings.Contains(footer, bad) {
|
||||
t.Errorf("%s %q: the footer holds %q:\n%s", l.layout, name, bad, footer)
|
||||
}
|
||||
}
|
||||
cell, _ := headerCell(msg.HTML)
|
||||
if m := logoSpan.FindStringSubmatch(cell); m == nil || m[1] != escaped {
|
||||
t.Errorf("%s %q: header = %q, want %q", l.layout, name, cell, escaped)
|
||||
}
|
||||
if want := "-------------------\n" + copyright() + " " + name; !strings.HasSuffix(strings.TrimRight(msg.Text, "\n"), want) {
|
||||
t.Errorf("%s %q: text part does not end with %q:\n%s", l.layout, name, want, msg.Text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The footer partial called with body only still renders Markdown.
|
||||
m, drv := partialsMailer(t, Options{}, &Overrides{Template: &TemplateOverride{Subject: "S", Body: `{{ partial "footer" "body" "**Bold** text" }}`}})
|
||||
msg := sendTemplate(t, m, drv, "acme.blog::mail.welcome", inlineVars)
|
||||
tables := withClass(msg.HTML, "table", "footer")
|
||||
if len(tables) != 2 {
|
||||
t.Fatalf("%d footer tables, want the body's and the layout's:\n%s", len(tables), msg.HTML)
|
||||
}
|
||||
if body, _, _ := strings.Cut(msg.HTML[tables[0].start:], "</table>"); !strings.Contains(body, "<strong>Bold</strong> text") {
|
||||
t.Errorf("a footer body is not rendered as Markdown:\n%s", msg.HTML)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user