First
[anni] / test / pleroma / web / pleroma_api / controllers / emoji_pack_controller_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 Pleroma.Web.PleromaAPI.EmojiPackControllerTest 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
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     clear_config([: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"] == 4
41
42     assert resp["packs"]
43            |> Map.keys()
44            |> length() == 4
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"] == 4
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"] == 4
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"] == 4
85     packs = Map.keys(resp["packs"])
86     assert length(packs) == 1
87     [pack3] = packs
88
89     resp =
90       conn
91       |> get("/api/pleroma/emoji/packs?page_size=1&page=4")
92       |> json_response_and_validate_schema(200)
93
94     assert resp["count"] == 4
95     packs = Map.keys(resp["packs"])
96     assert length(packs) == 1
97     [pack4] = packs
98     assert [pack1, pack2, pack3, pack4] |> Enum.uniq() |> length() == 4
99   end
100
101   describe "GET /api/pleroma/emoji/packs/remote" do
102     setup do
103       clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
104     end
105
106     test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
107       resp =
108         conn
109         |> get("/api/pleroma/emoji/packs?page=2&page_size=1")
110         |> json_response_and_validate_schema(200)
111
112       mock(fn
113         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
114           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
115
116         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
117           json(%{metadata: %{features: ["shareable_emoji_packs"]}})
118
119         %{method: :get, url: "https://example.com/api/pleroma/emoji/packs?page=2&page_size=1"} ->
120           json(resp)
121       end)
122
123       assert admin_conn
124              |> get("/api/pleroma/emoji/packs/remote?url=https://example.com&page=2&page_size=1")
125              |> json_response_and_validate_schema(200) == resp
126     end
127
128     test "non shareable instance", %{admin_conn: admin_conn} do
129       mock(fn
130         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
131           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
132
133         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
134           json(%{metadata: %{features: []}})
135       end)
136
137       assert admin_conn
138              |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
139              |> json_response_and_validate_schema(500) == %{
140                "error" => "The requested instance does not support sharing emoji packs"
141              }
142     end
143
144     test "it requires privileged role :emoji_manage_emoji", %{admin_conn: admin_conn} do
145       clear_config([:instance, :admin_privileges], [])
146
147       assert admin_conn
148              |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
149              |> json_response(:forbidden)
150     end
151   end
152
153   describe "GET /api/pleroma/emoji/packs/archive?name=:name" do
154     test "download shared pack", %{conn: conn} do
155       resp =
156         conn
157         |> get("/api/pleroma/emoji/packs/archive?name=test_pack")
158         |> response(200)
159
160       {:ok, arch} = :zip.unzip(resp, [:memory])
161
162       assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
163       assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
164     end
165
166     test "non existing pack", %{conn: conn} do
167       assert conn
168              |> get("/api/pleroma/emoji/packs/archive?name=test_pack_for_import")
169              |> json_response_and_validate_schema(:not_found) == %{
170                "error" => "Pack test_pack_for_import does not exist"
171              }
172     end
173
174     test "non downloadable pack", %{conn: conn} do
175       assert conn
176              |> get("/api/pleroma/emoji/packs/archive?name=test_pack_nonshared")
177              |> json_response_and_validate_schema(:forbidden) == %{
178                "error" =>
179                  "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
180              }
181     end
182   end
183
184   describe "POST /api/pleroma/emoji/packs/download" do
185     setup do
186       clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
187     end
188
189     test "shared pack from remote and non shared from fallback-src", %{
190       admin_conn: admin_conn,
191       conn: conn
192     } do
193       mock(fn
194         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
195           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
196
197         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
198           json(%{metadata: %{features: ["shareable_emoji_packs"]}})
199
200         %{
201           method: :get,
202           url: "https://example.com/api/pleroma/emoji/pack?name=test_pack"
203         } ->
204           conn
205           |> get("/api/pleroma/emoji/pack?name=test_pack")
206           |> json_response_and_validate_schema(200)
207           |> json()
208
209         %{
210           method: :get,
211           url: "https://example.com/api/pleroma/emoji/packs/archive?name=test_pack"
212         } ->
213           conn
214           |> get("/api/pleroma/emoji/packs/archive?name=test_pack")
215           |> response(200)
216           |> text()
217
218         %{
219           method: :get,
220           url: "https://example.com/api/pleroma/emoji/pack?name=test_pack_nonshared"
221         } ->
222           conn
223           |> get("/api/pleroma/emoji/pack?name=test_pack_nonshared")
224           |> json_response_and_validate_schema(200)
225           |> json()
226
227         %{
228           method: :get,
229           url: "https://nonshared-pack"
230         } ->
231           text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
232       end)
233
234       assert admin_conn
235              |> put_req_header("content-type", "multipart/form-data")
236              |> post("/api/pleroma/emoji/packs/download", %{
237                url: "https://example.com",
238                name: "test_pack",
239                as: "test_pack2"
240              })
241              |> json_response_and_validate_schema(200) == "ok"
242
243       assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
244       assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
245
246       assert admin_conn
247              |> delete("/api/pleroma/emoji/pack?name=test_pack2")
248              |> json_response_and_validate_schema(200) == "ok"
249
250       refute File.exists?("#{@emoji_path}/test_pack2")
251
252       assert admin_conn
253              |> put_req_header("content-type", "multipart/form-data")
254              |> post(
255                "/api/pleroma/emoji/packs/download",
256                %{
257                  url: "https://example.com",
258                  name: "test_pack_nonshared",
259                  as: "test_pack_nonshared2"
260                }
261              )
262              |> json_response_and_validate_schema(200) == "ok"
263
264       assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
265       assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
266
267       assert admin_conn
268              |> delete("/api/pleroma/emoji/pack?name=test_pack_nonshared2")
269              |> json_response_and_validate_schema(200) == "ok"
270
271       refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
272     end
273
274     test "nonshareable instance", %{admin_conn: admin_conn} do
275       mock(fn
276         %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
277           json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
278
279         %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
280           json(%{metadata: %{features: []}})
281       end)
282
283       assert admin_conn
284              |> put_req_header("content-type", "multipart/form-data")
285              |> post(
286                "/api/pleroma/emoji/packs/download",
287                %{
288                  url: "https://old-instance",
289                  name: "test_pack",
290                  as: "test_pack2"
291                }
292              )
293              |> json_response_and_validate_schema(500) == %{
294                "error" => "The requested instance does not support sharing emoji packs"
295              }
296     end
297
298     test "checksum fail", %{admin_conn: admin_conn} do
299       mock(fn
300         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
301           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
302
303         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
304           json(%{metadata: %{features: ["shareable_emoji_packs"]}})
305
306         %{
307           method: :get,
308           url: "https://example.com/api/pleroma/emoji/pack?name=pack_bad_sha"
309         } ->
310           {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
311           %Tesla.Env{status: 200, body: Jason.encode!(pack)}
312
313         %{
314           method: :get,
315           url: "https://example.com/api/pleroma/emoji/packs/archive?name=pack_bad_sha"
316         } ->
317           %Tesla.Env{
318             status: 200,
319             body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
320           }
321       end)
322
323       assert admin_conn
324              |> put_req_header("content-type", "multipart/form-data")
325              |> post("/api/pleroma/emoji/packs/download", %{
326                url: "https://example.com",
327                name: "pack_bad_sha",
328                as: "pack_bad_sha2"
329              })
330              |> json_response_and_validate_schema(:internal_server_error) == %{
331                "error" => "SHA256 for the pack doesn't match the one sent by the server"
332              }
333     end
334
335     test "other error", %{admin_conn: admin_conn} do
336       mock(fn
337         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
338           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
339
340         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
341           json(%{metadata: %{features: ["shareable_emoji_packs"]}})
342
343         %{
344           method: :get,
345           url: "https://example.com/api/pleroma/emoji/pack?name=test_pack"
346         } ->
347           {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
348           %Tesla.Env{status: 200, body: Jason.encode!(pack)}
349       end)
350
351       assert admin_conn
352              |> put_req_header("content-type", "multipart/form-data")
353              |> post("/api/pleroma/emoji/packs/download", %{
354                url: "https://example.com",
355                name: "test_pack",
356                as: "test_pack2"
357              })
358              |> json_response_and_validate_schema(:internal_server_error) == %{
359                "error" =>
360                  "The pack was not set as shared and there is no fallback src to download from"
361              }
362     end
363
364     test "it requires privileged role :emoji_manage_emoji", %{admin_conn: conn} do
365       clear_config([:instance, :admin_privileges], [])
366
367       assert conn
368              |> put_req_header("content-type", "multipart/form-data")
369              |> post("/api/pleroma/emoji/packs/download", %{
370                url: "https://example.com",
371                name: "test_pack",
372                as: "test_pack2"
373              })
374              |> json_response(:forbidden)
375     end
376   end
377
378   describe "PATCH/update /api/pleroma/emoji/pack?name=:name" do
379     setup do
380       clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
381       pack_file = "#{@emoji_path}/test_pack/pack.json"
382       original_content = File.read!(pack_file)
383
384       on_exit(fn ->
385         File.write!(pack_file, original_content)
386       end)
387
388       {:ok,
389        pack_file: pack_file,
390        new_data: %{
391          "license" => "Test license changed",
392          "homepage" => "https://pleroma.social",
393          "description" => "Test description",
394          "share-files" => false
395        }}
396     end
397
398     test "returns error when file system not writable", %{admin_conn: conn} = ctx do
399       with_mocks([
400         {File, [:passthrough], [stat: fn _ -> {:error, :eacces} end]}
401       ]) do
402         assert conn
403                |> put_req_header("content-type", "multipart/form-data")
404                |> patch(
405                  "/api/pleroma/emoji/pack?name=test_pack",
406                  %{"metadata" => ctx[:new_data]}
407                )
408                |> json_response_and_validate_schema(500)
409       end
410     end
411
412     test "for a pack without a fallback source", ctx do
413       assert ctx[:admin_conn]
414              |> put_req_header("content-type", "multipart/form-data")
415              |> patch("/api/pleroma/emoji/pack?name=test_pack", %{
416                "metadata" => ctx[:new_data]
417              })
418              |> json_response_and_validate_schema(200) == ctx[:new_data]
419
420       assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
421     end
422
423     test "for a pack with a fallback source", ctx do
424       mock(fn
425         %{
426           method: :get,
427           url: "https://nonshared-pack"
428         } ->
429           text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
430       end)
431
432       new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
433
434       new_data_with_sha =
435         Map.put(
436           new_data,
437           "fallback-src-sha256",
438           "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
439         )
440
441       assert ctx[:admin_conn]
442              |> put_req_header("content-type", "multipart/form-data")
443              |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
444              |> json_response_and_validate_schema(200) == new_data_with_sha
445
446       assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
447     end
448
449     test "when the fallback source doesn't have all the files", ctx do
450       mock(fn
451         %{
452           method: :get,
453           url: "https://nonshared-pack"
454         } ->
455           {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
456           text(empty_arch)
457       end)
458
459       new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
460
461       assert ctx[:admin_conn]
462              |> put_req_header("content-type", "multipart/form-data")
463              |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
464              |> json_response_and_validate_schema(:bad_request) == %{
465                "error" => "The fallback archive does not have all files specified in pack.json"
466              }
467     end
468
469     test "it requires privileged role :emoji_manage_emoji", %{
470       admin_conn: conn,
471       new_data: new_data
472     } do
473       clear_config([:instance, :admin_privileges], [])
474
475       assert conn
476              |> put_req_header("content-type", "multipart/form-data")
477              |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
478              |> json_response(:forbidden)
479     end
480   end
481
482   describe "POST/DELETE /api/pleroma/emoji/pack?name=:name" do
483     setup do
484       clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
485     end
486
487     test "returns an error on creates pack when file system not writable", %{
488       admin_conn: admin_conn
489     } do
490       path_pack = Path.join(@emoji_path, "test_pack")
491
492       with_mocks([
493         {File, [:passthrough], [mkdir: fn ^path_pack -> {:error, :eacces} end]}
494       ]) do
495         assert admin_conn
496                |> post("/api/pleroma/emoji/pack?name=test_pack")
497                |> json_response_and_validate_schema(500) == %{
498                  "error" =>
499                    "Unexpected error occurred while creating pack. (POSIX error: Permission denied)"
500                }
501       end
502     end
503
504     test "returns an error on deletes pack when the file system is not writable", %{
505       admin_conn: admin_conn
506     } do
507       path_pack = Path.join(@emoji_path, "test_emoji_pack")
508
509       try do
510         {:ok, _pack} = Pleroma.Emoji.Pack.create("test_emoji_pack")
511
512         with_mocks([
513           {File, [:passthrough], [rm_rf: fn ^path_pack -> {:error, :eacces, path_pack} end]}
514         ]) do
515           assert admin_conn
516                  |> delete("/api/pleroma/emoji/pack?name=test_emoji_pack")
517                  |> json_response_and_validate_schema(500) == %{
518                    "error" =>
519                      "Couldn't delete the `test_emoji_pack` pack (POSIX error: Permission denied)"
520                  }
521         end
522       after
523         File.rm_rf(path_pack)
524       end
525     end
526
527     test "creating and deleting a pack", %{admin_conn: admin_conn} do
528       assert admin_conn
529              |> post("/api/pleroma/emoji/pack?name=test_created")
530              |> json_response_and_validate_schema(200) == "ok"
531
532       assert File.exists?("#{@emoji_path}/test_created/pack.json")
533
534       assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
535                "pack" => %{},
536                "files" => %{},
537                "files_count" => 0
538              }
539
540       assert admin_conn
541              |> delete("/api/pleroma/emoji/pack?name=test_created")
542              |> json_response_and_validate_schema(200) == "ok"
543
544       refute File.exists?("#{@emoji_path}/test_created/pack.json")
545     end
546
547     test "if pack exists", %{admin_conn: admin_conn} do
548       path = Path.join(@emoji_path, "test_created")
549       File.mkdir(path)
550       pack_file = Jason.encode!(%{files: %{}, pack: %{}})
551       File.write!(Path.join(path, "pack.json"), pack_file)
552
553       assert admin_conn
554              |> post("/api/pleroma/emoji/pack?name=test_created")
555              |> json_response_and_validate_schema(:conflict) == %{
556                "error" => "A pack named \"test_created\" already exists"
557              }
558
559       on_exit(fn -> File.rm_rf(path) end)
560     end
561
562     test "with empty name", %{admin_conn: admin_conn} do
563       assert admin_conn
564              |> post("/api/pleroma/emoji/pack?name= ")
565              |> json_response_and_validate_schema(:bad_request) == %{
566                "error" => "pack name cannot be empty"
567              }
568     end
569
570     test "it requires privileged role :emoji_manage_emoji", %{admin_conn: admin_conn} do
571       clear_config([:instance, :admin_privileges], [])
572
573       assert admin_conn
574              |> post("/api/pleroma/emoji/pack?name= ")
575              |> json_response(:forbidden)
576
577       assert admin_conn
578              |> delete("/api/pleroma/emoji/pack?name= ")
579              |> json_response(:forbidden)
580     end
581   end
582
583   test "deleting nonexisting pack", %{admin_conn: admin_conn} do
584     assert admin_conn
585            |> delete("/api/pleroma/emoji/pack?name=non_existing")
586            |> json_response_and_validate_schema(:not_found) == %{
587              "error" => "Pack non_existing does not exist"
588            }
589   end
590
591   test "deleting with empty name", %{admin_conn: admin_conn} do
592     assert admin_conn
593            |> delete("/api/pleroma/emoji/pack?name= ")
594            |> json_response_and_validate_schema(:bad_request) == %{
595              "error" => "pack name cannot be empty"
596            }
597   end
598
599   test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
600     on_exit(fn ->
601       File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
602       File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
603     end)
604
605     resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
606
607     refute Map.has_key?(resp["packs"], "test_pack_for_import")
608
609     assert admin_conn
610            |> get("/api/pleroma/emoji/packs/import")
611            |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
612
613     resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
614     assert resp["packs"]["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
615
616     File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
617     refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
618
619     emoji_txt_content = """
620     blank, blank.png, Fun
621     blank2, blank.png
622     foo, /emoji/test_pack_for_import/blank.png
623     bar
624     """
625
626     File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
627
628     assert admin_conn
629            |> get("/api/pleroma/emoji/packs/import")
630            |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
631
632     resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
633
634     assert resp["packs"]["test_pack_for_import"]["files"] == %{
635              "blank" => "blank.png",
636              "blank2" => "blank.png",
637              "foo" => "blank.png"
638            }
639
640     clear_config([:instance, :admin_privileges], [])
641
642     assert admin_conn
643            |> get("/api/pleroma/emoji/packs/import")
644            |> json_response(:forbidden)
645   end
646
647   describe "GET /api/pleroma/emoji/pack?name=:name" do
648     test "shows pack.json", %{conn: conn} do
649       assert %{
650                "files" => files,
651                "files_count" => 2,
652                "pack" => %{
653                  "can-download" => true,
654                  "description" => "Test description",
655                  "download-sha256" => _,
656                  "homepage" => "https://pleroma.social",
657                  "license" => "Test license",
658                  "share-files" => true
659                }
660              } =
661                conn
662                |> get("/api/pleroma/emoji/pack?name=test_pack")
663                |> json_response_and_validate_schema(200)
664
665       assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
666
667       assert %{
668                "files" => files,
669                "files_count" => 2
670              } =
671                conn
672                |> get("/api/pleroma/emoji/pack?name=test_pack&page_size=1")
673                |> json_response_and_validate_schema(200)
674
675       assert files |> Map.keys() |> length() == 1
676
677       assert %{
678                "files" => files,
679                "files_count" => 2
680              } =
681                conn
682                |> get("/api/pleroma/emoji/pack?name=test_pack&page_size=1&page=2")
683                |> json_response_and_validate_schema(200)
684
685       assert files |> Map.keys() |> length() == 1
686     end
687
688     test "for pack name with special chars", %{conn: conn} do
689       assert %{
690                "files" => _files,
691                "files_count" => 1,
692                "pack" => %{
693                  "can-download" => true,
694                  "description" => "Test description",
695                  "download-sha256" => _,
696                  "homepage" => "https://pleroma.social",
697                  "license" => "Test license",
698                  "share-files" => true
699                }
700              } =
701                conn
702                |> get("/api/pleroma/emoji/pack?name=blobs.gg")
703                |> json_response_and_validate_schema(200)
704     end
705
706     test "non existing pack", %{conn: conn} do
707       assert conn
708              |> get("/api/pleroma/emoji/pack?name=non_existing")
709              |> json_response_and_validate_schema(:not_found) == %{
710                "error" => "Pack non_existing does not exist"
711              }
712     end
713
714     test "error name", %{conn: conn} do
715       assert conn
716              |> get("/api/pleroma/emoji/pack?name= ")
717              |> json_response_and_validate_schema(:bad_request) == %{
718                "error" => "pack name cannot be empty"
719              }
720     end
721   end
722 end