package sanitize_test import ( "testing" "github.com/jhillyerd/inbucket/pkg/webui/sanitize" ) // TestHTMLPlainStrings test plain text passthrough func TestHTMLPlainStrings(t *testing.T) { testStrings := []string{ "", "plain string", "one < two", } for _, ts := range testStrings { t.Run(ts, func(t *testing.T) { got, err := sanitize.HTML(ts) if err != nil { t.Fatal(err) } if got != ts { t.Errorf("Got: %q, want: %q", got, ts) } }) } } // TestHTMLSimpleFormatting tests basic tags we should allow func TestHTMLSimpleFormatting(t *testing.T) { testStrings := []string{ "

paragraph

", "bold", "italic", "emphasis", "strong", "
text
", "
text
", } for _, ts := range testStrings { t.Run(ts, func(t *testing.T) { got, err := sanitize.HTML(ts) if err != nil { t.Fatal(err) } if got != ts { t.Errorf("Got: %q, want: %q", got, ts) } }) } } // TestHTMLScriptTags tests some strings with JavaScript func TestHTMLScriptTags(t *testing.T) { testCases := []struct { input, want string }{ { `safe`, `safe`, }, { `mysite`, `mysite`, }, } for _, tc := range testCases { t.Run(tc.input, func(t *testing.T) { got, err := sanitize.HTML(tc.input) if err != nil { t.Fatal(err) } if got != tc.want { t.Errorf("Got: %q, want: %q", got, tc.want) } }) } } func TestSanitizeStyleTags(t *testing.T) { testCases := []struct { name, input, want string }{ { "empty", ``, ``, }, { "open", `
`, `
`, }, { "open close", `
`, `
`, }, { "inner text", `
foo bar
`, `
foo bar
`, }, { "self close", `
`, `
`, }, { "open params", `
`, `
`, }, { "open params squote", `
`, `
`, }, { "open style", `
`, `
`, }, { "open style squote", `
`, `
`, }, { "open style mixed case", `
`, `
`, }, { "closed style", `
`, `
`, }, { "mixed case style", `
`, `
`, }, { "mixed case invalid style", `
`, `
`, }, { "mixed", `

some text

`, `

some text

`, }, { "invalid styles", `
`, `
`, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { got, err := sanitize.HTML(tc.input) if err != nil { t.Fatal(err) } if got != tc.want { t.Errorf("input: %s\ngot : %s\nwant: %s", tc.input, got, tc.want) } }) } }