feat: announcement email with one-click unsubscribe headers
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func sampleAnnouncement() Announcement {
|
||||
return Announcement{
|
||||
CategoryLabel: "New features",
|
||||
Subject: "New in Vantage: scheduled workflows",
|
||||
BodyMarkdown: "Workflows can now run **on a schedule**.\n\n<script>alert(1)</script>\n\n- cron syntax\n- time zones",
|
||||
UnsubscribeURL: "https://api.example/email/unsubscribe?c=features&t=abc",
|
||||
PreferencesURL: "https://hq.example/email-preferences?t=abc",
|
||||
PostalAddress: "Hostxtra Ltd, 1 High Street, Leeds",
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderAnnouncement(t *testing.T) {
|
||||
subject, html, text, err := RenderAnnouncement(sampleAnnouncement(), "https://hq.example")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if subject != "New in Vantage: scheduled workflows" {
|
||||
t.Errorf("subject = %q", subject)
|
||||
}
|
||||
if !strings.Contains(html, "<strong>on a schedule</strong>") {
|
||||
t.Error("markdown not rendered to html")
|
||||
}
|
||||
if strings.Contains(html, "<script>") {
|
||||
t.Error("raw html from markdown reached the email")
|
||||
}
|
||||
for label, body := range map[string]string{"html": html, "text": text} {
|
||||
for _, want := range []string{"email/unsubscribe?c=features", "email-preferences?t=abc", "New features", "Hostxtra Ltd"} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("%s body missing %q", label, want)
|
||||
}
|
||||
}
|
||||
if strings.Contains(body, "—") {
|
||||
t.Errorf("%s body contains an em dash", label)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(text, "**on a schedule**") {
|
||||
t.Error("text part should carry the markdown source")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderAnnouncementWithoutPostalAddress(t *testing.T) {
|
||||
a := sampleAnnouncement()
|
||||
a.PostalAddress = ""
|
||||
_, html, _, err := RenderAnnouncement(a, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(html, "<no value>") {
|
||||
t.Error("empty postal address rendered as <no value>")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendAnnouncementRefusesListsAndMissingLink(t *testing.T) {
|
||||
s := Sender{Host: "smtp.invalid", From: "updates@example.com"}
|
||||
if err := s.SendAnnouncement("a@example.com, b@example.com", sampleAnnouncement()); err == nil {
|
||||
t.Error("a comma-separated To must be refused")
|
||||
}
|
||||
a := sampleAnnouncement()
|
||||
a.UnsubscribeURL = ""
|
||||
if err := s.SendAnnouncement("a@example.com", a); err == nil {
|
||||
t.Error("an announcement without an unsubscribe URL must be refused")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user