package mail import ( "bytes" "strings" "testing" ) func TestEmbeddedMarkIsPNG(t *testing.T) { t.Parallel() if !bytes.HasPrefix(markPNG, []byte("\x89PNG\r\n\x1a\n")) { t.Fatal("embedded mark is not PNG data") } } func TestPostReplyContent(t *testing.T) { t.Parallel() text, htmlBody := postReplyContent("https://www.askaplumberfirst.com/", PostReply{ ToName: ``, RootID: "question-123", RootTitle: `Leaky sink`, ReplyID: "reply-456", ReplyBody: "Replace the cartridge.\nThen test the handle. ", ReplyAuthorName: ``, }) for _, want := range []string{ "Ask a Plumber First", "New reply", "cid:reply-notification-mark", "https://www.askaplumberfirst.com/questions/question-123#post-reply-456", "white-space:pre-wrap", "<Sam & Pat>", "<b>Leaky sink</b>", "<Jo & Co>", "<script>alert('x')</script>", } { if !strings.Contains(htmlBody, want) { t.Errorf("HTML missing %q", want) } } for _, unsafe := range []string{ "", "Leaky sink", "", "", } { if strings.Contains(htmlBody, unsafe) { t.Errorf("HTML contains unescaped content %q", unsafe) } } if strings.Contains(htmlBody, "{{") { t.Error("HTML contains an unresolved template token") } for _, want := range []string{ `Hi ,`, ` replied in "Leaky sink"`, "Replace the cartridge.\nThen test the handle.", "https://www.askaplumberfirst.com/questions/question-123#post-reply-456", } { if !strings.Contains(text, want) { t.Errorf("text missing %q", want) } } } func TestPostReplyContentUsesFallbacks(t *testing.T) { t.Parallel() text, htmlBody := postReplyContent("https://www.askaplumberfirst.com", PostReply{}) if !strings.Contains(text, `Someone replied in "your conversation"`) { t.Errorf("text missing fallback title") } if !strings.Contains(htmlBody, "Someone replied in:

") || !strings.Contains(htmlBody, "“your conversation”") { t.Errorf("HTML missing fallbacks") } }