CI / test (pull_request) Successful in 6m16s
Reference https://www.askaplumberfirst.com for production email links and configuration examples.
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
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: `<Sam & Pat>`,
|
|
RootID: "question-123",
|
|
RootTitle: `<b>Leaky sink</b>`,
|
|
ReplyID: "reply-456",
|
|
ReplyBody: "Replace the cartridge.\nThen test the handle. <script>alert('x')</script>",
|
|
ReplyAuthorName: `<Jo & Co>`,
|
|
})
|
|
|
|
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{
|
|
"<Sam & Pat>",
|
|
"<b>Leaky sink</b>",
|
|
"<Jo & Co>",
|
|
"<script>alert('x')</script>",
|
|
} {
|
|
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 <Sam & Pat>,`,
|
|
`<Jo & Co> replied in "<b>Leaky sink</b>"`,
|
|
"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:</p>") ||
|
|
!strings.Contains(htmlBody, "“your conversation”") {
|
|
t.Errorf("HTML missing fallbacks")
|
|
}
|
|
}
|