Add missing libmagic for image upload
[akkoma] / test / web / pleroma_api / controllers / emoji_pack_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Tesla.Mock
9 import Pleroma.Factory
10
11 @emoji_path Path.join(
12 Pleroma.Config.get!([:instance, :static_dir]),
13 "emoji"
14 )
15 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
16
17 setup do: clear_config([:instance, :public], true)
18
19 setup do
20 admin = insert(:user, is_admin: true)
21 token = insert(:oauth_admin_token, user: admin)
22
23 admin_conn =
24 build_conn()
25 |> assign(:user, admin)
26 |> assign(:token, token)
27
28 Pleroma.Emoji.reload()
29 {:ok, %{admin_conn: admin_conn}}
30 end
31
32 test "GET /api/pleroma/emoji/packs when :public: false", %{conn: conn} do
33 Config.put([:instance, :public], false)
34 conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
35 end
36
37 test "GET /api/pleroma/emoji/packs", %{conn: conn} do
38 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
39
40 assert resp["count"] == 3
41
42 assert resp["packs"]
43 |> Map.keys()
44 |> length() == 3
45
46 shared = resp["packs"]["test_pack"]
47 assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
48 assert Map.has_key?(shared["pack"], "download-sha256")
49 assert shared["pack"]["can-download"]
50 assert shared["pack"]["share-files"]
51
52 non_shared = resp["packs"]["test_pack_nonshared"]
53 assert non_shared["pack"]["share-files"] == false
54 assert non_shared["pack"]["can-download"] == false
55
56 resp =
57 conn
58 |> get("/api/pleroma/emoji/packs?page_size=1")
59 |> json_response_and_validate_schema(200)
60
61 assert resp["count"] == 3
62
63 packs = Map.keys(resp["packs"])
64
65 assert length(packs) == 1
66
67 [pack1] = packs
68
69 resp =
70 conn
71 |> get("/api/pleroma/emoji/packs?page_size=1&page=2")
72 |> json_response_and_validate_schema(200)
73
74 assert resp["count"] == 3
75 packs = Map.keys(resp["packs"])
76 assert length(packs) == 1
77 [pack2] = packs
78
79 resp =
80 conn
81 |> get("/api/pleroma/emoji/packs?page_size=1&page=3")
82 |> json_response_and_validate_schema(200)
83
84 assert resp["count"] == 3
85 packs = Map.keys(resp["packs"])
86 assert length(packs) == 1
87 [pack3] = packs
88 assert [pack1, pack2, pack3] |> Enum.uniq() |> length() == 3
89 end
90
91 describe "GET /api/pleroma/emoji/packs/remote" do
92 test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
93 resp =
94 conn
95 |> get("/api/pleroma/emoji/packs")
96 |> json_response_and_validate_schema(200)
97
98 mock(fn
99 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
100 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
101
102 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
103 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
104
105 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
106 json(resp)
107 end)
108
109 assert admin_conn
110 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
111 |> json_response_and_validate_schema(200) == resp
112 end
113
114 test "non shareable instance", %{admin_conn: admin_conn} do
115 mock(fn
116 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
117 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
118
119 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
120 json(%{metadata: %{features: []}})
121 end)
122
123 assert admin_conn
124 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
125 |> json_response_and_validate_schema(500) == %{
126 "error" => "The requested instance does not support sharing emoji packs"
127 }
128 end
129 end
130
131 describe "GET /api/pleroma/emoji/packs/:name/archive" do
132 test "download shared pack", %{conn: conn} do
133 resp =
134 conn
135 |> get("/api/pleroma/emoji/packs/test_pack/archive")
136 |> response(200)
137
138 {:ok, arch} = :zip.unzip(resp, [:memory])
139
140 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
141 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
142 end
143
144 test "non existing pack", %{conn: conn} do
145 assert conn
146 |> get("/api/pleroma/emoji/packs/test_pack_for_import/archive")
147 |> json_response_and_validate_schema(:not_found) == %{
148 "error" => "Pack test_pack_for_import does not exist"
149 }
150 end
151
152 test "non downloadable pack", %{conn: conn} do
153 assert conn
154 |> get("/api/pleroma/emoji/packs/test_pack_nonshared/archive")
155 |> json_response_and_validate_schema(:forbidden) == %{
156 "error" =>
157 "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
158 }
159 end
160 end
161
162 describe "POST /api/pleroma/emoji/packs/download" do
163 test "shared pack from remote and non shared from fallback-src", %{
164 admin_conn: admin_conn,
165 conn: conn
166 } do
167 mock(fn
168 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
169 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
170
171 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
172 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
173
174 %{
175 method: :get,
176 url: "https://example.com/api/pleroma/emoji/packs/test_pack"
177 } ->
178 conn
179 |> get("/api/pleroma/emoji/packs/test_pack")
180 |> json_response_and_validate_schema(200)
181 |> json()
182
183 %{
184 method: :get,
185 url: "https://example.com/api/pleroma/emoji/packs/test_pack/archive"
186 } ->
187 conn
188 |> get("/api/pleroma/emoji/packs/test_pack/archive")
189 |> response(200)
190 |> text()
191
192 %{
193 method: :get,
194 url: "https://example.com/api/pleroma/emoji/packs/test_pack_nonshared"
195 } ->
196 conn
197 |> get("/api/pleroma/emoji/packs/test_pack_nonshared")
198 |> json_response_and_validate_schema(200)
199 |> json()
200
201 %{
202 method: :get,
203 url: "https://nonshared-pack"
204 } ->
205 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
206 end)
207
208 assert admin_conn
209 |> put_req_header("content-type", "multipart/form-data")
210 |> post("/api/pleroma/emoji/packs/download", %{
211 url: "https://example.com",
212 name: "test_pack",
213 as: "test_pack2"
214 })
215 |> json_response_and_validate_schema(200) == "ok"
216
217 assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
218 assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
219
220 assert admin_conn
221 |> delete("/api/pleroma/emoji/packs/test_pack2")
222 |> json_response_and_validate_schema(200) == "ok"
223
224 refute File.exists?("#{@emoji_path}/test_pack2")
225
226 assert admin_conn
227 |> put_req_header("content-type", "multipart/form-data")
228 |> post(
229 "/api/pleroma/emoji/packs/download",
230 %{
231 url: "https://example.com",
232 name: "test_pack_nonshared",
233 as: "test_pack_nonshared2"
234 }
235 )
236 |> json_response_and_validate_schema(200) == "ok"
237
238 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
239 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
240
241 assert admin_conn
242 |> delete("/api/pleroma/emoji/packs/test_pack_nonshared2")
243 |> json_response_and_validate_schema(200) == "ok"
244
245 refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
246 end
247
248 test "nonshareable instance", %{admin_conn: admin_conn} do
249 mock(fn
250 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
251 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
252
253 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
254 json(%{metadata: %{features: []}})
255 end)
256
257 assert admin_conn
258 |> put_req_header("content-type", "multipart/form-data")
259 |> post(
260 "/api/pleroma/emoji/packs/download",
261 %{
262 url: "https://old-instance",
263 name: "test_pack",
264 as: "test_pack2"
265 }
266 )
267 |> json_response_and_validate_schema(500) == %{
268 "error" => "The requested instance does not support sharing emoji packs"
269 }
270 end
271
272 test "checksum fail", %{admin_conn: admin_conn} do
273 mock(fn
274 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
275 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
276
277 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
278 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
279
280 %{
281 method: :get,
282 url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha"
283 } ->
284 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
285 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
286
287 %{
288 method: :get,
289 url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha/archive"
290 } ->
291 %Tesla.Env{
292 status: 200,
293 body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
294 }
295 end)
296
297 assert admin_conn
298 |> put_req_header("content-type", "multipart/form-data")
299 |> post("/api/pleroma/emoji/packs/download", %{
300 url: "https://example.com",
301 name: "pack_bad_sha",
302 as: "pack_bad_sha2"
303 })
304 |> json_response_and_validate_schema(:internal_server_error) == %{
305 "error" => "SHA256 for the pack doesn't match the one sent by the server"
306 }
307 end
308
309 test "other error", %{admin_conn: admin_conn} do
310 mock(fn
311 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
312 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
313
314 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
315 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
316
317 %{
318 method: :get,
319 url: "https://example.com/api/pleroma/emoji/packs/test_pack"
320 } ->
321 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
322 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
323 end)
324
325 assert admin_conn
326 |> put_req_header("content-type", "multipart/form-data")
327 |> post("/api/pleroma/emoji/packs/download", %{
328 url: "https://example.com",
329 name: "test_pack",
330 as: "test_pack2"
331 })
332 |> json_response_and_validate_schema(:internal_server_error) == %{
333 "error" =>
334 "The pack was not set as shared and there is no fallback src to download from"
335 }
336 end
337 end
338
339 describe "PATCH /api/pleroma/emoji/packs/:name" do
340 setup do
341 pack_file = "#{@emoji_path}/test_pack/pack.json"
342 original_content = File.read!(pack_file)
343
344 on_exit(fn ->
345 File.write!(pack_file, original_content)
346 end)
347
348 {:ok,
349 pack_file: pack_file,
350 new_data: %{
351 "license" => "Test license changed",
352 "homepage" => "https://pleroma.social",
353 "description" => "Test description",
354 "share-files" => false
355 }}
356 end
357
358 test "for a pack without a fallback source", ctx do
359 assert ctx[:admin_conn]
360 |> put_req_header("content-type", "multipart/form-data")
361 |> patch("/api/pleroma/emoji/packs/test_pack", %{"metadata" => ctx[:new_data]})
362 |> json_response_and_validate_schema(200) == ctx[:new_data]
363
364 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
365 end
366
367 test "for a pack with a fallback source", ctx do
368 mock(fn
369 %{
370 method: :get,
371 url: "https://nonshared-pack"
372 } ->
373 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
374 end)
375
376 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
377
378 new_data_with_sha =
379 Map.put(
380 new_data,
381 "fallback-src-sha256",
382 "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
383 )
384
385 assert ctx[:admin_conn]
386 |> put_req_header("content-type", "multipart/form-data")
387 |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
388 |> json_response_and_validate_schema(200) == new_data_with_sha
389
390 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
391 end
392
393 test "when the fallback source doesn't have all the files", ctx do
394 mock(fn
395 %{
396 method: :get,
397 url: "https://nonshared-pack"
398 } ->
399 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
400 text(empty_arch)
401 end)
402
403 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
404
405 assert ctx[:admin_conn]
406 |> put_req_header("content-type", "multipart/form-data")
407 |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
408 |> json_response_and_validate_schema(:bad_request) == %{
409 "error" => "The fallback archive does not have all files specified in pack.json"
410 }
411 end
412 end
413
414 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/:name/files" do
415 setup do
416 pack_file = "#{@emoji_path}/test_pack/pack.json"
417 original_content = File.read!(pack_file)
418
419 on_exit(fn ->
420 File.write!(pack_file, original_content)
421 end)
422
423 :ok
424 end
425
426 test "create shortcode exists", %{admin_conn: admin_conn} do
427 assert admin_conn
428 |> put_req_header("content-type", "multipart/form-data")
429 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
430 shortcode: "blank",
431 filename: "dir/blank.png",
432 file: %Plug.Upload{
433 filename: "blank.png",
434 path: "#{@emoji_path}/test_pack/blank.png"
435 }
436 })
437 |> json_response_and_validate_schema(:conflict) == %{
438 "error" => "An emoji with the \"blank\" shortcode already exists"
439 }
440 end
441
442 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
443 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
444
445 assert admin_conn
446 |> put_req_header("content-type", "multipart/form-data")
447 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
448 shortcode: "blank3",
449 filename: "dir/blank.png",
450 file: %Plug.Upload{
451 filename: "blank.png",
452 path: "#{@emoji_path}/test_pack/blank.png"
453 }
454 })
455 |> json_response_and_validate_schema(200) == %{
456 "blank" => "blank.png",
457 "blank2" => "blank2.png",
458 "blank3" => "dir/blank.png"
459 }
460
461 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
462
463 assert admin_conn
464 |> put_req_header("content-type", "multipart/form-data")
465 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
466 shortcode: "blank",
467 new_shortcode: "blank2",
468 new_filename: "dir_2/blank_3.png"
469 })
470 |> json_response_and_validate_schema(:conflict) == %{
471 "error" =>
472 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
473 }
474 end
475
476 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
477 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
478
479 assert admin_conn
480 |> put_req_header("content-type", "multipart/form-data")
481 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
482 shortcode: "blank3",
483 filename: "dir/blank.png",
484 file: %Plug.Upload{
485 filename: "blank.png",
486 path: "#{@emoji_path}/test_pack/blank.png"
487 }
488 })
489 |> json_response_and_validate_schema(200) == %{
490 "blank" => "blank.png",
491 "blank2" => "blank2.png",
492 "blank3" => "dir/blank.png"
493 }
494
495 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
496
497 assert admin_conn
498 |> put_req_header("content-type", "multipart/form-data")
499 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
500 shortcode: "blank3",
501 new_shortcode: "blank4",
502 new_filename: "dir_2/blank_3.png",
503 force: true
504 })
505 |> json_response_and_validate_schema(200) == %{
506 "blank" => "blank.png",
507 "blank2" => "blank2.png",
508 "blank4" => "dir_2/blank_3.png"
509 }
510
511 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
512 end
513
514 test "with empty filename", %{admin_conn: admin_conn} do
515 assert admin_conn
516 |> put_req_header("content-type", "multipart/form-data")
517 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
518 shortcode: "blank2",
519 filename: "",
520 file: %Plug.Upload{
521 filename: "blank.png",
522 path: "#{@emoji_path}/test_pack/blank.png"
523 }
524 })
525 |> json_response_and_validate_schema(:bad_request) == %{
526 "error" => "pack name, shortcode or filename cannot be empty"
527 }
528 end
529
530 test "add file with not loaded pack", %{admin_conn: admin_conn} do
531 assert admin_conn
532 |> put_req_header("content-type", "multipart/form-data")
533 |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
534 shortcode: "blank3",
535 filename: "dir/blank.png",
536 file: %Plug.Upload{
537 filename: "blank.png",
538 path: "#{@emoji_path}/test_pack/blank.png"
539 }
540 })
541 |> json_response_and_validate_schema(:bad_request) == %{
542 "error" => "pack \"not_loaded\" is not found"
543 }
544 end
545
546 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
547 assert admin_conn
548 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=blank3")
549 |> json_response_and_validate_schema(:bad_request) == %{
550 "error" => "pack \"not_loaded\" is not found"
551 }
552 end
553
554 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
555 assert admin_conn
556 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=")
557 |> json_response_and_validate_schema(:bad_request) == %{
558 "error" => "pack name or shortcode cannot be empty"
559 }
560 end
561
562 test "update file with not loaded pack", %{admin_conn: admin_conn} do
563 assert admin_conn
564 |> put_req_header("content-type", "multipart/form-data")
565 |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
566 shortcode: "blank4",
567 new_shortcode: "blank3",
568 new_filename: "dir_2/blank_3.png"
569 })
570 |> json_response_and_validate_schema(:bad_request) == %{
571 "error" => "pack \"not_loaded\" is not found"
572 }
573 end
574
575 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
576 assert admin_conn
577 |> put_req_header("content-type", "multipart/form-data")
578 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
579 shortcode: "blank4",
580 filename: "dir/blank.png",
581 file: %Plug.Upload{
582 filename: "blank.png",
583 path: "#{@emoji_path}/test_pack/blank.png"
584 }
585 })
586 |> json_response_and_validate_schema(200) == %{
587 "blank" => "blank.png",
588 "blank4" => "dir/blank.png",
589 "blank2" => "blank2.png"
590 }
591
592 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
593
594 assert admin_conn
595 |> put_req_header("content-type", "multipart/form-data")
596 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
597 shortcode: "blank4",
598 new_shortcode: "blank3",
599 new_filename: "dir_2/blank_3.png"
600 })
601 |> json_response_and_validate_schema(200) == %{
602 "blank3" => "dir_2/blank_3.png",
603 "blank" => "blank.png",
604 "blank2" => "blank2.png"
605 }
606
607 refute File.exists?("#{@emoji_path}/test_pack/dir/")
608 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
609
610 assert admin_conn
611 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
612 |> json_response_and_validate_schema(200) == %{
613 "blank" => "blank.png",
614 "blank2" => "blank2.png"
615 }
616
617 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
618
619 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
620 end
621
622 test "new with shortcode from url", %{admin_conn: admin_conn} do
623 mock(fn
624 %{
625 method: :get,
626 url: "https://test-blank/blank_url.png"
627 } ->
628 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
629 end)
630
631 assert admin_conn
632 |> put_req_header("content-type", "multipart/form-data")
633 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
634 shortcode: "blank_url",
635 file: "https://test-blank/blank_url.png"
636 })
637 |> json_response_and_validate_schema(200) == %{
638 "blank_url" => "blank_url.png",
639 "blank" => "blank.png",
640 "blank2" => "blank2.png"
641 }
642
643 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
644
645 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
646 end
647
648 test "new without shortcode", %{admin_conn: admin_conn} do
649 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
650
651 assert admin_conn
652 |> put_req_header("content-type", "multipart/form-data")
653 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
654 file: %Plug.Upload{
655 filename: "shortcode.png",
656 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
657 }
658 })
659 |> json_response_and_validate_schema(200) == %{
660 "shortcode" => "shortcode.png",
661 "blank" => "blank.png",
662 "blank2" => "blank2.png"
663 }
664 end
665
666 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
667 assert admin_conn
668 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
669 |> json_response_and_validate_schema(:bad_request) == %{
670 "error" => "Emoji \"blank3\" does not exist"
671 }
672 end
673
674 test "update non existing emoji", %{admin_conn: admin_conn} do
675 assert admin_conn
676 |> put_req_header("content-type", "multipart/form-data")
677 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
678 shortcode: "blank3",
679 new_shortcode: "blank4",
680 new_filename: "dir_2/blank_3.png"
681 })
682 |> json_response_and_validate_schema(:bad_request) == %{
683 "error" => "Emoji \"blank3\" does not exist"
684 }
685 end
686
687 test "update with empty shortcode", %{admin_conn: admin_conn} do
688 assert %{
689 "error" => "Missing field: new_shortcode."
690 } =
691 admin_conn
692 |> put_req_header("content-type", "multipart/form-data")
693 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
694 shortcode: "blank",
695 new_filename: "dir_2/blank_3.png"
696 })
697 |> json_response_and_validate_schema(:bad_request)
698 end
699 end
700
701 describe "POST/DELETE /api/pleroma/emoji/packs/:name" do
702 test "creating and deleting a pack", %{admin_conn: admin_conn} do
703 assert admin_conn
704 |> post("/api/pleroma/emoji/packs/test_created")
705 |> json_response_and_validate_schema(200) == "ok"
706
707 assert File.exists?("#{@emoji_path}/test_created/pack.json")
708
709 assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
710 "pack" => %{},
711 "files" => %{},
712 "files_count" => 0
713 }
714
715 assert admin_conn
716 |> delete("/api/pleroma/emoji/packs/test_created")
717 |> json_response_and_validate_schema(200) == "ok"
718
719 refute File.exists?("#{@emoji_path}/test_created/pack.json")
720 end
721
722 test "if pack exists", %{admin_conn: admin_conn} do
723 path = Path.join(@emoji_path, "test_created")
724 File.mkdir(path)
725 pack_file = Jason.encode!(%{files: %{}, pack: %{}})
726 File.write!(Path.join(path, "pack.json"), pack_file)
727
728 assert admin_conn
729 |> post("/api/pleroma/emoji/packs/test_created")
730 |> json_response_and_validate_schema(:conflict) == %{
731 "error" => "A pack named \"test_created\" already exists"
732 }
733
734 on_exit(fn -> File.rm_rf(path) end)
735 end
736
737 test "with empty name", %{admin_conn: admin_conn} do
738 assert admin_conn
739 |> post("/api/pleroma/emoji/packs/ ")
740 |> json_response_and_validate_schema(:bad_request) == %{
741 "error" => "pack name cannot be empty"
742 }
743 end
744 end
745
746 test "deleting nonexisting pack", %{admin_conn: admin_conn} do
747 assert admin_conn
748 |> delete("/api/pleroma/emoji/packs/non_existing")
749 |> json_response_and_validate_schema(:not_found) == %{
750 "error" => "Pack non_existing does not exist"
751 }
752 end
753
754 test "deleting with empty name", %{admin_conn: admin_conn} do
755 assert admin_conn
756 |> delete("/api/pleroma/emoji/packs/ ")
757 |> json_response_and_validate_schema(:bad_request) == %{
758 "error" => "pack name cannot be empty"
759 }
760 end
761
762 test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
763 on_exit(fn ->
764 File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
765 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
766 end)
767
768 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
769
770 refute Map.has_key?(resp["packs"], "test_pack_for_import")
771
772 assert admin_conn
773 |> get("/api/pleroma/emoji/packs/import")
774 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
775
776 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
777 assert resp["packs"]["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
778
779 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
780 refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
781
782 emoji_txt_content = """
783 blank, blank.png, Fun
784 blank2, blank.png
785 foo, /emoji/test_pack_for_import/blank.png
786 bar
787 """
788
789 File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
790
791 assert admin_conn
792 |> get("/api/pleroma/emoji/packs/import")
793 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
794
795 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
796
797 assert resp["packs"]["test_pack_for_import"]["files"] == %{
798 "blank" => "blank.png",
799 "blank2" => "blank.png",
800 "foo" => "blank.png"
801 }
802 end
803
804 describe "GET /api/pleroma/emoji/packs/:name" do
805 test "shows pack.json", %{conn: conn} do
806 assert %{
807 "files" => files,
808 "files_count" => 2,
809 "pack" => %{
810 "can-download" => true,
811 "description" => "Test description",
812 "download-sha256" => _,
813 "homepage" => "https://pleroma.social",
814 "license" => "Test license",
815 "share-files" => true
816 }
817 } =
818 conn
819 |> get("/api/pleroma/emoji/packs/test_pack")
820 |> json_response_and_validate_schema(200)
821
822 assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
823
824 assert %{
825 "files" => files,
826 "files_count" => 2
827 } =
828 conn
829 |> get("/api/pleroma/emoji/packs/test_pack?page_size=1")
830 |> json_response_and_validate_schema(200)
831
832 assert files |> Map.keys() |> length() == 1
833
834 assert %{
835 "files" => files,
836 "files_count" => 2
837 } =
838 conn
839 |> get("/api/pleroma/emoji/packs/test_pack?page_size=1&page=2")
840 |> json_response_and_validate_schema(200)
841
842 assert files |> Map.keys() |> length() == 1
843 end
844
845 test "non existing pack", %{conn: conn} do
846 assert conn
847 |> get("/api/pleroma/emoji/packs/non_existing")
848 |> json_response_and_validate_schema(:not_found) == %{
849 "error" => "Pack non_existing does not exist"
850 }
851 end
852
853 test "error name", %{conn: conn} do
854 assert conn
855 |> get("/api/pleroma/emoji/packs/ ")
856 |> json_response_and_validate_schema(:bad_request) == %{
857 "error" => "pack name cannot be empty"
858 }
859 end
860 end
861 end