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 TestQuestionAnsweredContent(t *testing.T) { t.Parallel() text, htmlBody := questionAnsweredContent("https://plumber.example/", QuestionAnswered{ ToName: ``, QuestionID: "question-123", QuestionTitle: `Leaky sink`, AnswerBody: "Replace the cartridge.\nThen test the handle. ", }) for _, want := range []string{ "Ask a Plumber First", "Shop response", "cid:answer-notification-mark", "https://plumber.example/questions/question-123", "white-space:pre-wrap", "<Sam & Pat>", "<b>Leaky sink</b>", "<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 ,`, `Your question "Leaky sink"`, "Replace the cartridge.\nThen test the handle.", "https://plumber.example/questions/question-123", } { if !strings.Contains(text, want) { t.Errorf("text missing %q", want) } } } func TestQuestionAnsweredContentUsesFallbackTitle(t *testing.T) { t.Parallel() text, htmlBody := questionAnsweredContent("https://plumber.example", QuestionAnswered{}) if !strings.Contains(text, `"your question"`) { t.Errorf("text missing fallback title") } if !strings.Contains(htmlBody, "a plumber replied to:

") || !strings.Contains(htmlBody, "“your question”") { t.Errorf("HTML missing fallback title") } }