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