The password reset, the new-music and activity summaries and the SMTP
test message each drew their own page; the test message had no layout
at all. They now share internal/mailer's layout: the Minstrel name at
the top, the email's heading and message on a card in the design
system's dark theme, and a footer saying which server sent it and why,
signed "— Minstrel", with the notification-settings link on the
summaries. A plain-text body in the same frame goes alongside.
An email supplies only its content templates and a Frame (heading,
server, reason). Links, buttons and quiet text take named styles from
the layout ({{style "link"}}), so colours change in one place; an
unknown style name fails the render. The SMTP test message is a
template now.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
package mailer
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
// Every email Minstrel sends goes out in the one layout: the Minstrel name,
|
|
// its heading on the card, and a footer naming the sending server and why.
|
|
func TestEveryEmailIsFramedByTheLayout(t *testing.T) {
|
|
reset := func() (string, string, error) {
|
|
return RenderResetEmail(ResetEmailVars{
|
|
Username: "alice", ResetURL: "https://music.example/reset/abc", ExpiryHours: 1, Server: "https://music.example",
|
|
})
|
|
}
|
|
smtpTest := func() (string, string, error) { return RenderSMTPTestEmail("https://music.example/") }
|
|
|
|
for name, render := range map[string]func() (string, string, error){"reset": reset, "smtp test": smtpTest} {
|
|
text, html, err := render()
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
for _, want := range []string{
|
|
`">Minstrel</td>`,
|
|
"<h1 style=",
|
|
"Sent by the Minstrel server at https://music.example because ",
|
|
"— Minstrel",
|
|
} {
|
|
if !strings.Contains(html, want) {
|
|
t.Errorf("%s html is missing %q", name, want)
|
|
}
|
|
}
|
|
if !strings.Contains(text, "Sent by the Minstrel server at https://music.example because ") ||
|
|
!strings.HasSuffix(strings.TrimSpace(text), "— Minstrel") {
|
|
t.Errorf("%s text lacks the layout's footer:\n%s", name, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFrameWithoutAPublicAddress(t *testing.T) {
|
|
content := fstest.MapFS{"c.txt": {Data: []byte("Hi.")}, "c.html": {Data: []byte("<p>Hi.</p>")}}
|
|
text, html, err := Render(content, "c.txt", "c.html", nil, Frame{
|
|
Heading: "Hello", Reason: "you asked", NotificationSettings: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, body := range []string{text, html} {
|
|
if !strings.Contains(body, "Sent by your Minstrel server because you asked.") {
|
|
t.Errorf("footer should name the server generically:\n%s", body)
|
|
}
|
|
if !strings.Contains(body, "under Settings → Notifications") || strings.Contains(body, "http") {
|
|
t.Errorf("with no address, the settings line says where to look, without a link:\n%s", body)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A misspelt style name fails the render rather than shipping an unstyled email.
|
|
func TestUnknownStyleFailsTheRender(t *testing.T) {
|
|
content := fstest.MapFS{"c.txt": {Data: []byte("x")}, "c.html": {Data: []byte(`<p style="{{style "lnik"}}">x</p>`)}}
|
|
if _, _, err := Render(content, "c.txt", "c.html", nil, Frame{Heading: "h", Reason: "r"}); err == nil {
|
|
t.Fatal("want an error for an unknown style")
|
|
}
|
|
}
|