Merge branch 'chores/bump-copyright' into 'develop'
[akkoma] / test / pleroma / web / pleroma_api / controllers / emoji_file_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiFileControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Mock
9 import Tesla.Mock
10 import Pleroma.Factory
11
12 @emoji_path Path.join(
13 Pleroma.Config.get!([:instance, :static_dir]),
14 "emoji"
15 )
16 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
17
18 setup do: clear_config([:instance, :public], true)
19
20 setup do
21 admin = insert(:user, is_admin: true)
22 token = insert(:oauth_admin_token, user: admin)
23
24 admin_conn =
25 build_conn()
26 |> assign(:user, admin)
27 |> assign(:token, token)
28
29 Pleroma.Emoji.reload()
30 {:ok, %{admin_conn: admin_conn}}
31 end
32
33 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/files?name=:name" do
34 setup do
35 pack_file = "#{@emoji_path}/test_pack/pack.json"
36 original_content = File.read!(pack_file)
37
38 on_exit(fn ->
39 File.write!(pack_file, original_content)
40 end)
41
42 :ok
43 end
44
45 test "upload zip file with emojies", %{admin_conn: admin_conn} do
46 on_exit(fn ->
47 [
48 "128px/a_trusted_friend-128.png",
49 "auroraborealis.png",
50 "1000px/baby_in_a_box.png",
51 "1000px/bear.png",
52 "128px/bear-128.png"
53 ]
54 |> Enum.each(fn path -> File.rm_rf!("#{@emoji_path}/test_pack/#{path}") end)
55 end)
56
57 resp =
58 admin_conn
59 |> put_req_header("content-type", "multipart/form-data")
60 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
61 file: %Plug.Upload{
62 content_type: "application/zip",
63 filename: "emojis.zip",
64 path: Path.absname("test/fixtures/emojis.zip")
65 }
66 })
67 |> json_response_and_validate_schema(200)
68
69 assert resp == %{
70 "a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
71 "auroraborealis" => "auroraborealis.png",
72 "baby_in_a_box" => "1000px/baby_in_a_box.png",
73 "bear" => "1000px/bear.png",
74 "bear-128" => "128px/bear-128.png",
75 "blank" => "blank.png",
76 "blank2" => "blank2.png"
77 }
78
79 Enum.each(Map.values(resp), fn path ->
80 assert File.exists?("#{@emoji_path}/test_pack/#{path}")
81 end)
82 end
83
84 test "create shortcode exists", %{admin_conn: admin_conn} do
85 assert admin_conn
86 |> put_req_header("content-type", "multipart/form-data")
87 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
88 shortcode: "blank",
89 filename: "dir/blank.png",
90 file: %Plug.Upload{
91 filename: "blank.png",
92 path: "#{@emoji_path}/test_pack/blank.png"
93 }
94 })
95 |> json_response_and_validate_schema(:conflict) == %{
96 "error" => "An emoji with the \"blank\" shortcode already exists"
97 }
98 end
99
100 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
101 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
102
103 assert admin_conn
104 |> put_req_header("content-type", "multipart/form-data")
105 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
106 shortcode: "blank3",
107 filename: "dir/blank.png",
108 file: %Plug.Upload{
109 filename: "blank.png",
110 path: "#{@emoji_path}/test_pack/blank.png"
111 }
112 })
113 |> json_response_and_validate_schema(200) == %{
114 "blank" => "blank.png",
115 "blank2" => "blank2.png",
116 "blank3" => "dir/blank.png"
117 }
118
119 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
120
121 assert admin_conn
122 |> put_req_header("content-type", "multipart/form-data")
123 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
124 shortcode: "blank",
125 new_shortcode: "blank2",
126 new_filename: "dir_2/blank_3.png"
127 })
128 |> json_response_and_validate_schema(:conflict) == %{
129 "error" =>
130 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
131 }
132 end
133
134 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
135 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
136
137 assert admin_conn
138 |> put_req_header("content-type", "multipart/form-data")
139 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
140 shortcode: "blank3",
141 filename: "dir/blank.png",
142 file: %Plug.Upload{
143 filename: "blank.png",
144 path: "#{@emoji_path}/test_pack/blank.png"
145 }
146 })
147 |> json_response_and_validate_schema(200) == %{
148 "blank" => "blank.png",
149 "blank2" => "blank2.png",
150 "blank3" => "dir/blank.png"
151 }
152
153 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
154
155 assert admin_conn
156 |> put_req_header("content-type", "multipart/form-data")
157 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
158 shortcode: "blank3",
159 new_shortcode: "blank4",
160 new_filename: "dir_2/blank_3.png",
161 force: true
162 })
163 |> json_response_and_validate_schema(200) == %{
164 "blank" => "blank.png",
165 "blank2" => "blank2.png",
166 "blank4" => "dir_2/blank_3.png"
167 }
168
169 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
170 end
171
172 test "with empty filename", %{admin_conn: admin_conn} do
173 assert admin_conn
174 |> put_req_header("content-type", "multipart/form-data")
175 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
176 shortcode: "blank2",
177 filename: "",
178 file: %Plug.Upload{
179 filename: "blank.png",
180 path: "#{@emoji_path}/test_pack/blank.png"
181 }
182 })
183 |> json_response_and_validate_schema(422) == %{
184 "error" => "pack name, shortcode or filename cannot be empty"
185 }
186 end
187
188 test "add file with not loaded pack", %{admin_conn: admin_conn} do
189 assert admin_conn
190 |> put_req_header("content-type", "multipart/form-data")
191 |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
192 shortcode: "blank3",
193 filename: "dir/blank.png",
194 file: %Plug.Upload{
195 filename: "blank.png",
196 path: "#{@emoji_path}/test_pack/blank.png"
197 }
198 })
199 |> json_response_and_validate_schema(:not_found) == %{
200 "error" => "pack \"not_loaded\" is not found"
201 }
202 end
203
204 test "returns an error on add file when file system is not writable", %{
205 admin_conn: admin_conn
206 } do
207 pack_file = Path.join([@emoji_path, "not_loaded", "pack.json"])
208
209 with_mocks([
210 {File, [:passthrough], [stat: fn ^pack_file -> {:error, :eacces} end]}
211 ]) do
212 assert admin_conn
213 |> put_req_header("content-type", "multipart/form-data")
214 |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
215 shortcode: "blank3",
216 filename: "dir/blank.png",
217 file: %Plug.Upload{
218 filename: "blank.png",
219 path: "#{@emoji_path}/test_pack/blank.png"
220 }
221 })
222 |> json_response_and_validate_schema(500) == %{
223 "error" =>
224 "Unexpected error occurred while adding file to pack. (POSIX error: Permission denied)"
225 }
226 end
227 end
228
229 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
230 assert admin_conn
231 |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=blank3")
232 |> json_response_and_validate_schema(:not_found) == %{
233 "error" => "pack \"not_loaded\" is not found"
234 }
235 end
236
237 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
238 assert admin_conn
239 |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=")
240 |> json_response_and_validate_schema(:not_found) == %{
241 "error" => "pack \"not_loaded\" is not found"
242 }
243 end
244
245 test "update file with not loaded pack", %{admin_conn: admin_conn} do
246 assert admin_conn
247 |> put_req_header("content-type", "multipart/form-data")
248 |> patch("/api/pleroma/emoji/packs/files?name=not_loaded", %{
249 shortcode: "blank4",
250 new_shortcode: "blank3",
251 new_filename: "dir_2/blank_3.png"
252 })
253 |> json_response_and_validate_schema(:not_found) == %{
254 "error" => "pack \"not_loaded\" is not found"
255 }
256 end
257
258 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
259 assert admin_conn
260 |> put_req_header("content-type", "multipart/form-data")
261 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
262 shortcode: "blank4",
263 filename: "dir/blank.png",
264 file: %Plug.Upload{
265 filename: "blank.png",
266 path: "#{@emoji_path}/test_pack/blank.png"
267 }
268 })
269 |> json_response_and_validate_schema(200) == %{
270 "blank" => "blank.png",
271 "blank4" => "dir/blank.png",
272 "blank2" => "blank2.png"
273 }
274
275 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
276
277 assert admin_conn
278 |> put_req_header("content-type", "multipart/form-data")
279 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
280 shortcode: "blank4",
281 new_shortcode: "blank3",
282 new_filename: "dir_2/blank_3.png"
283 })
284 |> json_response_and_validate_schema(200) == %{
285 "blank3" => "dir_2/blank_3.png",
286 "blank" => "blank.png",
287 "blank2" => "blank2.png"
288 }
289
290 refute File.exists?("#{@emoji_path}/test_pack/dir/")
291 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
292
293 assert admin_conn
294 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
295 |> json_response_and_validate_schema(200) == %{
296 "blank" => "blank.png",
297 "blank2" => "blank2.png"
298 }
299
300 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
301
302 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
303 end
304
305 test "new with shortcode from url", %{admin_conn: admin_conn} do
306 mock(fn
307 %{
308 method: :get,
309 url: "https://test-blank/blank_url.png"
310 } ->
311 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
312 end)
313
314 assert admin_conn
315 |> put_req_header("content-type", "multipart/form-data")
316 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
317 shortcode: "blank_url",
318 file: "https://test-blank/blank_url.png"
319 })
320 |> json_response_and_validate_schema(200) == %{
321 "blank_url" => "blank_url.png",
322 "blank" => "blank.png",
323 "blank2" => "blank2.png"
324 }
325
326 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
327
328 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
329 end
330
331 test "new without shortcode", %{admin_conn: admin_conn} do
332 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
333
334 assert admin_conn
335 |> put_req_header("content-type", "multipart/form-data")
336 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
337 file: %Plug.Upload{
338 filename: "shortcode.png",
339 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
340 }
341 })
342 |> json_response_and_validate_schema(200) == %{
343 "shortcode" => "shortcode.png",
344 "blank" => "blank.png",
345 "blank2" => "blank2.png"
346 }
347 end
348
349 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
350 assert admin_conn
351 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
352 |> json_response_and_validate_schema(:bad_request) == %{
353 "error" => "Emoji \"blank3\" does not exist"
354 }
355 end
356
357 test "update non existing emoji", %{admin_conn: admin_conn} do
358 assert admin_conn
359 |> put_req_header("content-type", "multipart/form-data")
360 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
361 shortcode: "blank3",
362 new_shortcode: "blank4",
363 new_filename: "dir_2/blank_3.png"
364 })
365 |> json_response_and_validate_schema(:bad_request) == %{
366 "error" => "Emoji \"blank3\" does not exist"
367 }
368 end
369
370 test "update with empty shortcode", %{admin_conn: admin_conn} do
371 assert %{
372 "error" => "Missing field: new_shortcode."
373 } =
374 admin_conn
375 |> put_req_header("content-type", "multipart/form-data")
376 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
377 shortcode: "blank",
378 new_filename: "dir_2/blank_3.png"
379 })
380 |> json_response_and_validate_schema(:bad_request)
381 end
382 end
383 end