1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.EmojiAPIControllerTest do
6 use Pleroma.Web.ConnCase
11 @emoji_path Path.join(
12 Pleroma.Config.get!([:instance, :static_dir]),
15 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
18 admin = insert(:user, is_admin: true)
19 token = insert(:oauth_admin_token, user: admin)
23 |> assign(:user, admin)
24 |> assign(:token, token)
26 Pleroma.Emoji.reload()
27 {:ok, %{admin_conn: admin_conn}}
30 test "GET /api/pleroma/emoji/packs", %{conn: conn} do
31 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response(200)
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"]
39 non_shared = resp["test_pack_nonshared"]
40 assert non_shared["pack"]["share-files"] == false
41 assert non_shared["pack"]["can-download"] == false
44 describe "GET /api/pleroma/emoji/packs/remote" do
45 test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
48 |> get("/api/pleroma/emoji/packs")
52 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
53 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
55 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
56 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
58 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
63 |> get("/api/pleroma/emoji/packs/remote", %{
64 url: "https://example.com"
66 |> json_response(200) == resp
69 test "non shareable instance", %{admin_conn: admin_conn} do
71 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
72 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
74 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
75 json(%{metadata: %{features: []}})
79 |> get("/api/pleroma/emoji/packs/remote", %{url: "https://example.com"})
80 |> json_response(500) == %{
81 "error" => "The requested instance does not support sharing emoji packs"
86 describe "GET /api/pleroma/emoji/packs/:name/archive" do
87 test "download shared pack", %{conn: conn} do
90 |> get("/api/pleroma/emoji/packs/test_pack/archive")
93 {:ok, arch} = :zip.unzip(resp, [:memory])
95 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
96 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
99 test "non existing pack", %{conn: conn} do
101 |> get("/api/pleroma/emoji/packs/test_pack_for_import/archive")
102 |> json_response(:not_found) == %{
103 "error" => "Pack test_pack_for_import does not exist"
107 test "non downloadable pack", %{conn: conn} do
109 |> get("/api/pleroma/emoji/packs/test_pack_nonshared/archive")
110 |> json_response(:forbidden) == %{
112 "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
117 describe "POST /api/pleroma/emoji/packs/download" do
118 test "shared pack from remote and non shared from fallback-src", %{
119 admin_conn: admin_conn,
123 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
124 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
126 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
127 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
131 url: "https://example.com/api/pleroma/emoji/packs/test_pack"
134 |> get("/api/pleroma/emoji/packs/test_pack")
135 |> json_response(200)
140 url: "https://example.com/api/pleroma/emoji/packs/test_pack/archive"
143 |> get("/api/pleroma/emoji/packs/test_pack/archive")
149 url: "https://example.com/api/pleroma/emoji/packs/test_pack_nonshared"
152 |> get("/api/pleroma/emoji/packs/test_pack_nonshared")
153 |> json_response(200)
158 url: "https://nonshared-pack"
160 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
164 |> post("/api/pleroma/emoji/packs/download", %{
165 url: "https://example.com",
169 |> json_response(200) == "ok"
171 assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
172 assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
175 |> delete("/api/pleroma/emoji/packs/test_pack2")
176 |> json_response(200) == "ok"
178 refute File.exists?("#{@emoji_path}/test_pack2")
182 "/api/pleroma/emoji/packs/download",
184 url: "https://example.com",
185 name: "test_pack_nonshared",
186 as: "test_pack_nonshared2"
189 |> json_response(200) == "ok"
191 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
192 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
195 |> delete("/api/pleroma/emoji/packs/test_pack_nonshared2")
196 |> json_response(200) == "ok"
198 refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
201 test "nonshareable instance", %{admin_conn: admin_conn} do
203 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
204 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
206 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
207 json(%{metadata: %{features: []}})
212 "/api/pleroma/emoji/packs/download",
214 url: "https://old-instance",
219 |> json_response(500) == %{
220 "error" => "The requested instance does not support sharing emoji packs"
224 test "checksum fail", %{admin_conn: admin_conn} do
226 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
227 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
229 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
230 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
234 url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha"
238 body: Pleroma.Emoji.Pack.load_pack("pack_bad_sha") |> Jason.encode!()
243 url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha/archive"
247 body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
252 |> post("/api/pleroma/emoji/packs/download", %{
253 url: "https://example.com",
254 name: "pack_bad_sha",
257 |> json_response(:internal_server_error) == %{
258 "error" => "SHA256 for the pack doesn't match the one sent by the server"
262 test "other error", %{admin_conn: admin_conn} do
264 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
265 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
267 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
268 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
272 url: "https://example.com/api/pleroma/emoji/packs/test_pack"
276 body: Pleroma.Emoji.Pack.load_pack("test_pack") |> Jason.encode!()
281 |> post("/api/pleroma/emoji/packs/download", %{
282 url: "https://example.com",
286 |> json_response(:internal_server_error) == %{
288 "The pack was not set as shared and there is no fallback src to download from"
293 describe "PATCH /api/pleroma/emoji/packs/:name" do
295 pack_file = "#{@emoji_path}/test_pack/pack.json"
296 original_content = File.read!(pack_file)
299 File.write!(pack_file, original_content)
303 pack_file: pack_file,
305 "license" => "Test license changed",
306 "homepage" => "https://pleroma.social",
307 "description" => "Test description",
308 "share-files" => false
312 test "for a pack without a fallback source", ctx do
313 assert ctx[:admin_conn]
314 |> patch("/api/pleroma/emoji/packs/test_pack", %{"metadata" => ctx[:new_data]})
315 |> json_response(200) == ctx[:new_data]
317 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
320 test "for a pack with a fallback source", ctx do
324 url: "https://nonshared-pack"
326 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
329 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
334 "fallback-src-sha256",
335 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
338 assert ctx[:admin_conn]
339 |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
340 |> json_response(200) == new_data_with_sha
342 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
345 test "when the fallback source doesn't have all the files", ctx do
349 url: "https://nonshared-pack"
351 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
355 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
357 assert ctx[:admin_conn]
358 |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
359 |> json_response(:bad_request) == %{
360 "error" => "The fallback archive does not have all files specified in pack.json"
365 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/:name/files" do
367 pack_file = "#{@emoji_path}/test_pack/pack.json"
368 original_content = File.read!(pack_file)
371 File.write!(pack_file, original_content)
377 test "create shortcode exists", %{admin_conn: admin_conn} do
379 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
381 filename: "dir/blank.png",
383 filename: "blank.png",
384 path: "#{@emoji_path}/test_pack/blank.png"
387 |> json_response(:conflict) == %{
388 "error" => "An emoji with the \"blank\" shortcode already exists"
392 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
393 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
396 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
398 filename: "dir/blank.png",
400 filename: "blank.png",
401 path: "#{@emoji_path}/test_pack/blank.png"
404 |> json_response(200) == %{"blank" => "blank.png", "blank2" => "dir/blank.png"}
406 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
409 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
411 new_shortcode: "blank2",
412 new_filename: "dir_2/blank_3.png"
414 |> json_response(:conflict) == %{
416 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
420 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
421 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
424 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
426 filename: "dir/blank.png",
428 filename: "blank.png",
429 path: "#{@emoji_path}/test_pack/blank.png"
432 |> json_response(200) == %{"blank" => "blank.png", "blank2" => "dir/blank.png"}
434 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
437 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
439 new_shortcode: "blank3",
440 new_filename: "dir_2/blank_3.png",
443 |> json_response(200) == %{
444 "blank" => "blank.png",
445 "blank3" => "dir_2/blank_3.png"
448 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
451 test "with empty filename", %{admin_conn: admin_conn} do
453 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
457 filename: "blank.png",
458 path: "#{@emoji_path}/test_pack/blank.png"
461 |> json_response(:bad_request) == %{
462 "error" => "pack name, shortcode or filename cannot be empty"
466 test "add file with not loaded pack", %{admin_conn: admin_conn} do
468 |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
470 filename: "dir/blank.png",
472 filename: "blank.png",
473 path: "#{@emoji_path}/test_pack/blank.png"
476 |> json_response(:bad_request) == %{
477 "error" => "pack \"not_loaded\" is not found"
481 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
483 |> delete("/api/pleroma/emoji/packs/not_loaded/files", %{shortcode: "blank3"})
484 |> json_response(:bad_request) == %{"error" => "pack \"not_loaded\" is not found"}
487 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
489 |> delete("/api/pleroma/emoji/packs/not_loaded/files", %{shortcode: ""})
490 |> json_response(:bad_request) == %{
491 "error" => "pack name or shortcode cannot be empty"
495 test "update file with not loaded pack", %{admin_conn: admin_conn} do
497 |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
499 new_shortcode: "blank3",
500 new_filename: "dir_2/blank_3.png"
502 |> json_response(:bad_request) == %{"error" => "pack \"not_loaded\" is not found"}
505 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
507 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
509 filename: "dir/blank.png",
511 filename: "blank.png",
512 path: "#{@emoji_path}/test_pack/blank.png"
515 |> json_response(200) == %{"blank" => "blank.png", "blank4" => "dir/blank.png"}
517 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
520 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
522 new_shortcode: "blank3",
523 new_filename: "dir_2/blank_3.png"
525 |> json_response(200) == %{"blank3" => "dir_2/blank_3.png", "blank" => "blank.png"}
527 refute File.exists?("#{@emoji_path}/test_pack/dir/")
528 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
531 |> delete("/api/pleroma/emoji/packs/test_pack/files", %{shortcode: "blank3"})
532 |> json_response(200) == %{"blank" => "blank.png"}
534 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
536 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
539 test "new with shortcode from url", %{admin_conn: admin_conn} do
543 url: "https://test-blank/blank_url.png"
545 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
549 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
550 shortcode: "blank_url",
551 file: "https://test-blank/blank_url.png"
553 |> json_response(200) == %{
554 "blank_url" => "blank_url.png",
555 "blank" => "blank.png"
558 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
560 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
563 test "new without shortcode", %{admin_conn: admin_conn} do
564 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
567 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
569 filename: "shortcode.png",
570 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
573 |> json_response(200) == %{"shortcode" => "shortcode.png", "blank" => "blank.png"}
576 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
578 |> delete("/api/pleroma/emoji/packs/test_pack/files", %{shortcode: "blank2"})
579 |> json_response(:bad_request) == %{"error" => "Emoji \"blank2\" does not exist"}
582 test "update non existing emoji", %{admin_conn: admin_conn} do
584 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
586 new_shortcode: "blank3",
587 new_filename: "dir_2/blank_3.png"
589 |> json_response(:bad_request) == %{"error" => "Emoji \"blank2\" does not exist"}
592 test "update with empty shortcode", %{admin_conn: admin_conn} do
594 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
596 new_filename: "dir_2/blank_3.png"
598 |> json_response(:bad_request) == %{
599 "error" => "new_shortcode or new_filename cannot be empty"
604 describe "POST/DELETE /api/pleroma/emoji/packs/:name" do
605 test "creating and deleting a pack", %{admin_conn: admin_conn} do
607 |> post("/api/pleroma/emoji/packs/test_created")
608 |> json_response(200) == "ok"
610 assert File.exists?("#{@emoji_path}/test_created/pack.json")
612 assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
618 |> delete("/api/pleroma/emoji/packs/test_created")
619 |> json_response(200) == "ok"
621 refute File.exists?("#{@emoji_path}/test_created/pack.json")
624 test "if pack exists", %{admin_conn: admin_conn} do
625 path = Path.join(@emoji_path, "test_created")
627 pack_file = Jason.encode!(%{files: %{}, pack: %{}})
628 File.write!(Path.join(path, "pack.json"), pack_file)
631 |> post("/api/pleroma/emoji/packs/test_created")
632 |> json_response(:conflict) == %{
633 "error" => "A pack named \"test_created\" already exists"
636 on_exit(fn -> File.rm_rf(path) end)
639 test "with empty name", %{admin_conn: admin_conn} do
641 |> post("/api/pleroma/emoji/packs/ ")
642 |> json_response(:bad_request) == %{"error" => "pack name cannot be empty"}
646 test "deleting nonexisting pack", %{admin_conn: admin_conn} do
648 |> delete("/api/pleroma/emoji/packs/non_existing")
649 |> json_response(:not_found) == %{"error" => "Pack non_existing does not exist"}
652 test "deleting with empty name", %{admin_conn: admin_conn} do
654 |> delete("/api/pleroma/emoji/packs/ ")
655 |> json_response(:bad_request) == %{"error" => "pack name cannot be empty"}
658 test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
660 File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
661 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
664 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response(200)
666 refute Map.has_key?(resp, "test_pack_for_import")
669 |> get("/api/pleroma/emoji/packs/import")
670 |> json_response(200) == ["test_pack_for_import"]
672 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response(200)
673 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
675 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
676 refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
678 emoji_txt_content = """
679 blank, blank.png, Fun
681 foo, /emoji/test_pack_for_import/blank.png
685 File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
688 |> get("/api/pleroma/emoji/packs/import")
689 |> json_response(200) == ["test_pack_for_import"]
691 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response(200)
693 assert resp["test_pack_for_import"]["files"] == %{
694 "blank" => "blank.png",
695 "blank2" => "blank.png",
700 describe "GET /api/pleroma/emoji/packs/:name" do
701 test "shows pack.json", %{conn: conn} do
703 "files" => %{"blank" => "blank.png"},
705 "can-download" => true,
706 "description" => "Test description",
707 "download-sha256" => _,
708 "homepage" => "https://pleroma.social",
709 "license" => "Test license",
710 "share-files" => true
714 |> get("/api/pleroma/emoji/packs/test_pack")
715 |> json_response(200)
718 test "non existing pack", %{conn: conn} do
720 |> get("/api/pleroma/emoji/packs/non_existing")
721 |> json_response(:not_found) == %{"error" => "Pack non_existing does not exist"}
724 test "error name", %{conn: conn} do
726 |> get("/api/pleroma/emoji/packs/ ")
727 |> json_response(:bad_request) == %{"error" => "pack name cannot be empty"}