package sanitize import ( "html/template" "testing" "github.com/stretchr/testify/assert" ) func TestMarkdownToHTML(t *testing.T) { assert := assert.New(t) tests := []struct { Test string Result template.HTML }{ {"", ""}, {"> lll", "
\n

lll

\n
\n"}, {"> lll > lol", "
\n

lll > lol

\n
\n"}, // Limit number of blockquotes {"> lll", "
\n

lll

\n
\n"}, {"\n", ""}, {"lol", "

lol

\n"}, // keep HTML tags {"[b]lol[/b]", "

[b]lol[/b]

\n"}, // keep BBCode tags {"**[b]lol[/b]**", "

[b]lol[/b]

\n"}, // Render Markdown } for _, test := range tests { assert.Equal(test.Result, MarkdownToHTML(test.Test), "Should be equal") } } func TestParseBBCodes(t *testing.T) { assert := assert.New(t) tests := []struct { Test string Result string }{ {"", ""}, {">", ">"}, // keep escaped html {"lol", "lol"}, // keep html tags {"ddd\nddd", "ddd\nddd"}, // keep html tags {"[b]lol[/b]", "lol"}, // Convert bbcodes {"[u][b]lol[/u]", "lol"}, // Close unclosed tags } for _, test := range tests { assert.Equal(test.Result, ParseBBCodes(test.Test), "Should be equal") } assert.Contains(ParseBBCodes("[url=http://kk.cc/]lol[/url]"), "rel=\"nofollow\"") // rel="nofollow" for urls } func TestRepairHTMLTags(t *testing.T) { assert := assert.New(t) tests := []struct { Test string Result string }{ {"", ""}, {">", ">"}, // keep escaped html {"lol", "lol"}, // keep html tags {"lol", "lol"}, // close unclosed tags encapsulated {"lol", "lol"}, // close unclosed tags non encapsulated {"lol", "lol"}, // close unclosed tags non encaptsulated + remove useless end tags {"
lol
", "
lol
"}, // close unclosed tags + remove useless end tags encaptsulated } for _, test := range tests { assert.Equal(test.Result, repairHTMLTags(test.Test), "Should be equal") } } func TestSanitize(t *testing.T) { assert := assert.New(t) tests := []struct { Test string Result string }{ {"", ""}, {"[b]lol[/b]", "lol"}, // Should convert bbcodes {"ddd\nddd", "ddd\nddd"}, {">", ">"}, // keep escaped html {"lol", "lol"}, // keep html tags {"lol", "lol"}, // close unclosed tags encapsulated {"lol", "lol"}, // close unclosed tags non encapsulated {"lol", "lol"}, // close unclosed tags non encaptsulated + remove useless end tags {"
lol
", "lol"}, // close unclosed tags + remove useless end tags encaptsulated and remove div tag {"Hello World", "Hello World"}, // Remove css XSS {"XSS", "XSS"}, // Remove javascript xss {"", ""}, // We allow img and linl {"", ""}, // But not allow datauri img by default {"