Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
package chat
import (
"encoding/json"
"fmt"
"testing"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/services/chat/events"
"github.com/owncast/owncast/utils"
)
// 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)
<img class="emoji" src="/img/emoji/bananadance.gif">`
expected := `<p>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>.
Here is an iframe</p>
blah blah blah
<p><a href="http://owncast.online" rel="nofollow noreferrer noopener" target="_blank">test link</a>
<img class="emoji" src="/img/emoji/bananadance.gif"></p>`
result := events.RenderAndSanitize(messageContent)
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 we block remote images in chat messages.
func TestBlockRemoteImages(t *testing.T) {
messageContent := `<img src="https://via.placeholder.com/img/emoji/350x150"> test `
expected := `<p> test </p>`
result := events.RenderAndSanitize(messageContent)
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) {
messageContent := `<img alt=":beerparrot:" title=":beerparrot:" src="/img/emoji/beerparrot.gif"> test `
expected := `<p><img alt=":beerparrot:" title=":beerparrot:" src="/img/emoji/beerparrot.gif"> test <img src="/img/emoji/beerparrot.gif"></p>`
result := events.RenderAndSanitize(messageContent)
if result != expected {
t.Errorf("message rendering/sanitation does not match expected. Got\n%s, \n\n want:\n%s", result, expected)
}
}
// 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"
result := events.RenderMarkdown(messageContent)
if result != expected {
t.Errorf("message rendering does not match expected. Got\n%s, \n\n want:\n%s", result, expected)
}
}
func TestSendSystemActionRendersTrustedHTML(t *testing.T) {
client := &Client{send: make(chan []byte, 1)}
service := &Service{clients: map[uint]*Client{1: client}}
if err := service.SendSystemAction(`<span class="trusted" style="color:red">Status</span>`, true); err != nil {
t.Fatal(err)
}
var payload struct {
Body string `json:"body"`
}
if err := json.Unmarshal(<-client.send, &payload); err != nil {
t.Fatal(err)
}
const want = "<p><span class=\"trusted\" style=\"color:red\">Status</span></p>\n"
if payload.Body != want {
t.Fatalf("action body = %q, want %q", payload.Body, want)
}
}
func TestSendSystemActionDoesNotReactivateSanitizedDisplayName(t *testing.T) {
client := &Client{send: make(chan []byte, 1)}
service := &Service{clients: map[uint]*Client{1: client}}
displayName := utils.MakeSafeStringOfLength(`<img src=x onerror=alert(1)>Alice`, config.MaxChatDisplayNameLength)
if err := service.SendSystemAction(fmt.Sprintf("**%s** has been removed from chat.", displayName), true); err != nil {
t.Fatal(err)
}
var payload struct {
Body string `json:"body"`
}
if err := json.Unmarshal(<-client.send, &payload); err != nil {
t.Fatal(err)
}
const want = "<p><strong>Alice</strong> has been removed from chat.</p>\n"
if payload.Body != want {
t.Fatalf("action body = %q, want %q", payload.Body, want)
}
}