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