2020-10-14 02:45:52 +03:00
package chat
import (
"testing"
2021-07-20 05:22:29 +03:00
"github.com/owncast/owncast/core/chat/events"
2020-10-14 02:45:52 +03:00
)
// Test a bunch of arbitrary markup and markdown to make sure we get sanitized
// and fully rendered HTML out of it.
func TestRenderAndSanitize ( t * testing . T ) {
messageContent := `
Test one two three ! I go to http : //yahoo.com and search for _sports_ and **answers**.
Here is an iframe < iframe src = "http://yahoo.com" > < / iframe >
# # blah blah blah
[ test link ] ( http : //owncast.online)
2021-03-09 10:20:15 +03:00
< img class = "emoji" alt = "bananadance.gif" width = "600px" src = "/img/emoji/bananadance.gif" >
2020-10-14 02:45:52 +03:00
< script src = "http://hackers.org/hack.js" > < / script >
`
2021-08-19 04:08:17 +03:00
expected := ` Test one two three ! I go to < a href = "http://yahoo.com" rel = "nofollow noreferrer noopener" target = "_blank" > http : //yahoo.com</a> and search for <em>sports</em> and <strong>answers</strong>.
2021-07-28 01:26:27 +03:00
Here is an iframe
2020-10-14 02:45:52 +03:00
blah blah blah
2021-08-19 04:08:17 +03:00
< a href = "http://owncast.online" rel = "nofollow noreferrer noopener" target = "_blank" > test link < / a >
2021-07-28 01:26:27 +03:00
< img class = "emoji" src = "/img/emoji/bananadance.gif" > `
2020-10-14 02:45:52 +03:00
2021-07-20 05:22:29 +03:00
result := events . RenderAndSanitize ( messageContent )
2020-10-14 02:45:52 +03:00
if result != expected {
t . Errorf ( "message rendering/sanitation does not match expected. Got\n%s, \n\n want:\n%s" , result , expected )
}
2021-03-09 10:20:15 +03:00
}
// Test to make sure we block remote images in chat messages.
func TestBlockRemoteImages ( t * testing . T ) {
2021-07-23 21:00:04 +03:00
messageContent := ` <img src="https://via.placeholder.com/img/emoji/350x150"> test ![](https://via.placeholder.com/img/emoji/350x150) `
2021-07-28 01:26:27 +03:00
expected := ` test `
2021-07-20 05:22:29 +03:00
result := events . RenderAndSanitize ( messageContent )
2020-10-14 02:45:52 +03:00
2021-03-09 10:20:15 +03:00
if result != expected {
t . Errorf ( "message rendering/sanitation does not match expected. Got\n%s, \n\n want:\n%s" , result , expected )
}
}
// Test to make sure emoji images are allowed in chat messages.
func TestAllowEmojiImages ( t * testing . T ) {
2021-07-23 21:01:30 +03:00
messageContent := ` <img alt=":beerparrot:" title=":beerparrot:" src="/img/emoji/beerparrot.gif"> test ![](/img/emoji/beerparrot.gif) `
2021-07-28 01:26:27 +03:00
expected := ` <img alt=":beerparrot:" title=":beerparrot:" src="/img/emoji/beerparrot.gif"> test <img src="/img/emoji/beerparrot.gif"> `
2021-07-20 05:22:29 +03:00
result := events . RenderAndSanitize ( messageContent )
2021-03-09 10:20:15 +03:00
if result != expected {
t . Errorf ( "message rendering/sanitation does not match expected. Got\n%s, \n\n want:\n%s" , result , expected )
}
2020-10-14 02:45:52 +03:00
}
2021-03-12 11:43:10 +03:00
// Test to verify we can pass raw html and render markdown.
func TestAllowHTML ( t * testing . T ) {
messageContent := ` <img src="/img/emoji/beerparrot.gif"><ul><li>**test thing**</li></ul> `
expected := "<p><img src=\"/img/emoji/beerparrot.gif\"><ul><li><strong>test thing</strong></li></ul></p>\n"
2021-07-20 05:22:29 +03:00
result := events . RenderMarkdown ( messageContent )
2021-03-12 11:43:10 +03:00
if result != expected {
t . Errorf ( "message rendering does not match expected. Got\n%s, \n\n want:\n%s" , result , expected )
}
}