total rebase
[anni] / test / mix / tasks / pleroma / emoji_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 Mix.Tasks.Pleroma.EmojiTest do
6   use ExUnit.Case, async: true
7
8   import ExUnit.CaptureIO
9   import Tesla.Mock
10
11   alias Mix.Tasks.Pleroma.Emoji
12
13   describe "ls-packs" do
14     test "with default manifest as url" do
15       mock(fn
16         %{
17           method: :get,
18           url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
19         } ->
20           %Tesla.Env{
21             status: 200,
22             body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
23           }
24       end)
25
26       capture_io(fn -> Emoji.run(["ls-packs"]) end) =~
27         "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
28     end
29
30     test "with passed manifest as file" do
31       capture_io(fn ->
32         Emoji.run(["ls-packs", "-m", "test/fixtures/emoji/packs/manifest.json"])
33       end) =~ "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip"
34     end
35   end
36
37   describe "get-packs" do
38     test "download pack from default manifest" do
39       mock(fn
40         %{
41           method: :get,
42           url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
43         } ->
44           %Tesla.Env{
45             status: 200,
46             body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
47           }
48
49         %{
50           method: :get,
51           url: "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
52         } ->
53           %Tesla.Env{
54             status: 200,
55             body: File.read!("test/fixtures/emoji/packs/blank.png.zip")
56           }
57
58         %{
59           method: :get,
60           url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/finmoji.json"
61         } ->
62           %Tesla.Env{
63             status: 200,
64             body: File.read!("test/fixtures/emoji/packs/finmoji.json")
65           }
66       end)
67
68       assert capture_io(fn -> Emoji.run(["get-packs", "finmoji"]) end) =~ "Writing pack.json for"
69
70       emoji_path =
71         Path.join(
72           Pleroma.Config.get!([:instance, :static_dir]),
73           "emoji"
74         )
75
76       assert File.exists?(Path.join([emoji_path, "finmoji", "pack.json"]))
77       on_exit(fn -> File.rm_rf!("test/instance_static/emoji/finmoji") end)
78     end
79
80     test "install local emoji pack" do
81       assert capture_io(fn ->
82                Emoji.run([
83                  "get-packs",
84                  "local",
85                  "--manifest",
86                  "test/instance_static/local_pack/manifest.json"
87                ])
88              end) =~ "Writing pack.json for"
89
90       on_exit(fn -> File.rm_rf!("test/instance_static/emoji/local") end)
91     end
92
93     test "pack not found" do
94       mock(fn
95         %{
96           method: :get,
97           url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
98         } ->
99           %Tesla.Env{
100             status: 200,
101             body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
102           }
103       end)
104
105       assert capture_io(fn -> Emoji.run(["get-packs", "not_found"]) end) =~
106                "No pack named \"not_found\" found"
107     end
108
109     test "raise on bad sha256" do
110       mock(fn
111         %{
112           method: :get,
113           url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip"
114         } ->
115           %Tesla.Env{
116             status: 200,
117             body: File.read!("test/fixtures/emoji/packs/blank.png.zip")
118           }
119       end)
120
121       assert_raise RuntimeError, ~r/^Bad SHA256 for blobs.gg/, fn ->
122         capture_io(fn ->
123           Emoji.run(["get-packs", "blobs.gg", "-m", "test/fixtures/emoji/packs/manifest.json"])
124         end)
125       end
126     end
127   end
128
129   describe "gen-pack" do
130     setup do
131       url = "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
132
133       mock(fn %{
134                 method: :get,
135                 url: ^url
136               } ->
137         %Tesla.Env{status: 200, body: File.read!("test/fixtures/emoji/packs/blank.png.zip")}
138       end)
139
140       {:ok, url: url}
141     end
142
143     test "with default extensions", %{url: url} do
144       name = "pack1"
145       pack_json = "#{name}.json"
146       files_json = "#{name}_file.json"
147       refute File.exists?(pack_json)
148       refute File.exists?(files_json)
149
150       captured =
151         capture_io(fn ->
152           Emoji.run([
153             "gen-pack",
154             url,
155             "--name",
156             name,
157             "--license",
158             "license",
159             "--homepage",
160             "homepage",
161             "--description",
162             "description",
163             "--files",
164             files_json,
165             "--extensions",
166             ".png .gif"
167           ])
168         end)
169
170       assert captured =~ "#{pack_json} has been created with the pack1 pack"
171       assert captured =~ "Using .png .gif extensions"
172
173       assert File.exists?(pack_json)
174       assert File.exists?(files_json)
175
176       on_exit(fn ->
177         File.rm!(pack_json)
178         File.rm!(files_json)
179       end)
180     end
181
182     test "with custom extensions and update existing files", %{url: url} do
183       name = "pack2"
184       pack_json = "#{name}.json"
185       files_json = "#{name}_file.json"
186       refute File.exists?(pack_json)
187       refute File.exists?(files_json)
188
189       captured =
190         capture_io(fn ->
191           Emoji.run([
192             "gen-pack",
193             url,
194             "--name",
195             name,
196             "--license",
197             "license",
198             "--homepage",
199             "homepage",
200             "--description",
201             "description",
202             "--files",
203             files_json,
204             "--extensions",
205             " .png   .gif    .jpeg "
206           ])
207         end)
208
209       assert captured =~ "#{pack_json} has been created with the pack2 pack"
210       assert captured =~ "Using .png .gif .jpeg extensions"
211
212       assert File.exists?(pack_json)
213       assert File.exists?(files_json)
214
215       captured =
216         capture_io(fn ->
217           Emoji.run([
218             "gen-pack",
219             url,
220             "--name",
221             name,
222             "--license",
223             "license",
224             "--homepage",
225             "homepage",
226             "--description",
227             "description",
228             "--files",
229             files_json,
230             "--extensions",
231             " .png   .gif    .jpeg "
232           ])
233         end)
234
235       assert captured =~ "#{pack_json} has been updated with the pack2 pack"
236
237       on_exit(fn ->
238         File.rm!(pack_json)
239         File.rm!(files_json)
240       end)
241     end
242   end
243 end