00001abfcdb32793ad9f637ebe83689b22e76f2c
[anni] / test / pleroma / emoji / pack_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Emoji.PackTest do
6   use Pleroma.DataCase
7   alias Pleroma.Emoji.Pack
8
9   @emoji_path Path.join(
10                 Pleroma.Config.get!([:instance, :static_dir]),
11                 "emoji"
12               )
13
14   setup do
15     pack_path = Path.join(@emoji_path, "dump_pack")
16     File.mkdir(pack_path)
17
18     File.write!(Path.join(pack_path, "pack.json"), """
19     {
20     "files": { },
21     "pack": {
22     "description": "Dump pack", "homepage": "https://pleroma.social",
23     "license": "Test license", "share-files": true
24     }}
25     """)
26
27     {:ok, pack} = Pleroma.Emoji.Pack.load_pack("dump_pack")
28
29     on_exit(fn ->
30       File.rm_rf!(pack_path)
31     end)
32
33     {:ok, pack: pack}
34   end
35
36   describe "add_file/4" do
37     test "add emojies from zip file", %{pack: pack} do
38       file = %Plug.Upload{
39         content_type: "application/zip",
40         filename: "emojis.zip",
41         path: Path.absname("test/fixtures/emojis.zip")
42       }
43
44       {:ok, updated_pack} = Pack.add_file(pack, nil, nil, file)
45
46       assert updated_pack.files == %{
47                "a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
48                "auroraborealis" => "auroraborealis.png",
49                "baby_in_a_box" => "1000px/baby_in_a_box.png",
50                "bear" => "1000px/bear.png",
51                "bear-128" => "128px/bear-128.png"
52              }
53
54       assert updated_pack.files_count == 5
55     end
56   end
57
58   test "returns error when zip file is bad", %{pack: pack} do
59     file = %Plug.Upload{
60       content_type: "application/zip",
61       filename: "emojis.zip",
62       path: Path.absname("test/instance_static/emoji/test_pack/blank.png")
63     }
64
65     assert Pack.add_file(pack, nil, nil, file) == {:error, :einval}
66   end
67
68   test "returns pack when zip file is empty", %{pack: pack} do
69     file = %Plug.Upload{
70       content_type: "application/zip",
71       filename: "emojis.zip",
72       path: Path.absname("test/fixtures/empty.zip")
73     }
74
75     {:ok, updated_pack} = Pack.add_file(pack, nil, nil, file)
76     assert updated_pack == pack
77   end
78
79   test "add emoji file", %{pack: pack} do
80     file = %Plug.Upload{
81       filename: "blank.png",
82       path: "#{@emoji_path}/test_pack/blank.png"
83     }
84
85     {:ok, updated_pack} = Pack.add_file(pack, "test_blank", "test_blank.png", file)
86
87     assert updated_pack.files == %{
88              "test_blank" => "test_blank.png"
89            }
90
91     assert updated_pack.files_count == 1
92   end
93
94   test "load_pack/1 ignores path traversal in a forged pack name", %{pack: pack} do
95     assert {:ok, ^pack} = Pack.load_pack("../../../../../dump_pack")
96   end
97 end