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`, "

Hi.

")}} 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(`

x

`)}} 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") } }