package email import ( "strings" "testing" ) func TestSanitizeHTML(t *testing.T) { cid := map[string]string{"logo@x": "data:image/png;base64,AAAA"} tests := []struct { name string input string allowRemote bool contains []string absent []string remote bool }{ { name: "script and handlers removed", input: `

Hi

`, absent: []string{"onclick", "onmouseover", "abcde`, contains: []string{`href="https://ok.test/"`, `target="_blank"`, `rel="noopener noreferrer nofollow"`, `href="mailto:a@b.c"`}, absent: []string{"javascript", "/system/x"}, }, { name: "remote image blocked by default", input: ``, contains: []string{`data-remote-src="https://t.test/p.gif"`}, absent: []string{` src="https://t.test/p.gif"`}, remote: true, }, { name: "remote image allowed", input: ``, allowRemote: true, contains: []string{`src="https://t.test/p.gif"`}, remote: true, }, { name: "cid image resolved", input: ``, contains: []string{`src="data:image/png;base64,AAAA"`}, }, { name: "style urls and imports blocked", input: `
x
`, contains: []string{"color:red", "background:none", "background-image:none"}, absent: []string{"@import", "t.test", "evil.test"}, remote: true, }, { name: "frames, forms and meta removed", input: `
`, absent: []string{"

Inside

`, contains: []string{"

Inside

", "Label"}, }, { name: "svg and comments removed", input: `ok`, absent: []string{"p{color:red}

x

`, absent: []string{"behavior"}, }, { name: "legacy css expressions neutralised", input: `
x
`, absent: []string{"expression(", "behavior:"}, contains: []string{"blocked-"}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { result := sanitizeHTML(test.input, sanitizeOptions{allowRemote: test.allowRemote, cid: cid}) for _, want := range test.contains { if !strings.Contains(result.html, want) { t.Errorf("missing %q in %s", want, result.html) } } for _, unwanted := range test.absent { if strings.Contains(result.html, unwanted) { t.Errorf("unexpected %q in %s", unwanted, result.html) } } if result.hasRemote != test.remote { t.Errorf("hasRemote = %v, want %v", result.hasRemote, test.remote) } }) } } func TestHTMLToText(t *testing.T) { tests := []struct { name string input string withLinks bool want string }{ {"paragraphs", "

Hello

World

", false, "Hello\n\nWorld"}, {"line breaks and entities", "Tom & Jerry
next line", false, "Tom & Jerry\nnext line"}, {"head and style skipped", "TBody", false, "Body"}, {"lists", "
  • One
  • Two
", false, "- One\n- Two"}, {"links appended", `Click`, true, "Click (https://a.test/x)"}, {"bare link not duplicated", `https://a.test/`, true, "https://a.test/"}, {"preformatted", "
a  b\n c
", false, "a b\n c"}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := htmlToText(test.input, test.withLinks); got != test.want { t.Errorf("htmlToText = %q, want %q", got, test.want) } }) } } func TestPreviewText(t *testing.T) { text := "Thanks!\n> quoted line\n> another\nSee you" if got := previewText(text, 100); got != "Thanks! See you" { t.Errorf("previewText = %q", got) } if got := previewText(strings.Repeat("a", 50), 10); got != strings.Repeat("a", 10)+"…" { t.Errorf("truncation = %q", got) } } func TestUnflowText(t *testing.T) { flowed := "This is a long \r\nline that wraps.\r\n Stuffed\r\n-- \r\nSig" want := "This is a long line that wraps.\nStuffed\n-- \nSig" if got := unflowText(flowed, false); got != want { t.Errorf("unflowText = %q, want %q", got, want) } if got := unflowText("ab \r\ncd", true); got != "abcd" { t.Errorf("delsp unflow = %q", got) } }