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.EmojiPackControllerTest 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_and_validate_schema(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
45 |> get("/api/pleroma/emoji/packs?page_size=1")
46 |> json_response_and_validate_schema(200)
48 [pack1] = Map.keys(resp)
52 |> get("/api/pleroma/emoji/packs?page_size=1&page=2")
53 |> json_response_and_validate_schema(200)
55 [pack2] = Map.keys(resp)
59 |> get("/api/pleroma/emoji/packs?page_size=1&page=3")
60 |> json_response_and_validate_schema(200)
62 [pack3] = Map.keys(resp)
63 assert [pack1, pack2, pack3] |> Enum.uniq() |> length() == 3
66 describe "GET /api/pleroma/emoji/packs/remote" do
67 test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
70 |> get("/api/pleroma/emoji/packs")
71 |> json_response_and_validate_schema(200)
74 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
75 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
77 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
78 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
80 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
85 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
86 |> json_response_and_validate_schema(200) == resp
89 test "non shareable instance", %{admin_conn: admin_conn} do
91 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
92 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
94 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
95 json(%{metadata: %{features: []}})
99 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
100 |> json_response_and_validate_schema(500) == %{
101 "error" => "The requested instance does not support sharing emoji packs"
106 describe "GET /api/pleroma/emoji/packs/:name/archive" do
107 test "download shared pack", %{conn: conn} do
110 |> get("/api/pleroma/emoji/packs/test_pack/archive")
113 {:ok, arch} = :zip.unzip(resp, [:memory])
115 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
116 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
119 test "non existing pack", %{conn: conn} do
121 |> get("/api/pleroma/emoji/packs/test_pack_for_import/archive")
122 |> json_response_and_validate_schema(:not_found) == %{
123 "error" => "Pack test_pack_for_import does not exist"
127 test "non downloadable pack", %{conn: conn} do
129 |> get("/api/pleroma/emoji/packs/test_pack_nonshared/archive")
130 |> json_response_and_validate_schema(:forbidden) == %{
132 "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
137 describe "POST /api/pleroma/emoji/packs/download" do
138 test "shared pack from remote and non shared from fallback-src", %{
139 admin_conn: admin_conn,
143 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
144 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
146 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
147 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
151 url: "https://example.com/api/pleroma/emoji/packs/test_pack"
154 |> get("/api/pleroma/emoji/packs/test_pack")
155 |> json_response_and_validate_schema(200)
160 url: "https://example.com/api/pleroma/emoji/packs/test_pack/archive"
163 |> get("/api/pleroma/emoji/packs/test_pack/archive")
169 url: "https://example.com/api/pleroma/emoji/packs/test_pack_nonshared"
172 |> get("/api/pleroma/emoji/packs/test_pack_nonshared")
173 |> json_response_and_validate_schema(200)
178 url: "https://nonshared-pack"
180 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
184 |> put_req_header("content-type", "multipart/form-data")
185 |> post("/api/pleroma/emoji/packs/download", %{
186 url: "https://example.com",
190 |> json_response_and_validate_schema(200) == "ok"
192 assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
193 assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
196 |> delete("/api/pleroma/emoji/packs/test_pack2")
197 |> json_response_and_validate_schema(200) == "ok"
199 refute File.exists?("#{@emoji_path}/test_pack2")
202 |> put_req_header("content-type", "multipart/form-data")
204 "/api/pleroma/emoji/packs/download",
206 url: "https://example.com",
207 name: "test_pack_nonshared",
208 as: "test_pack_nonshared2"
211 |> json_response_and_validate_schema(200) == "ok"
213 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
214 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
217 |> delete("/api/pleroma/emoji/packs/test_pack_nonshared2")
218 |> json_response_and_validate_schema(200) == "ok"
220 refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
223 test "nonshareable instance", %{admin_conn: admin_conn} do
225 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
226 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
228 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
229 json(%{metadata: %{features: []}})
233 |> put_req_header("content-type", "multipart/form-data")
235 "/api/pleroma/emoji/packs/download",
237 url: "https://old-instance",
242 |> json_response_and_validate_schema(500) == %{
243 "error" => "The requested instance does not support sharing emoji packs"
247 test "checksum fail", %{admin_conn: admin_conn} do
249 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
250 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
252 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
253 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
257 url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha"
259 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
260 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
264 url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha/archive"
268 body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
273 |> put_req_header("content-type", "multipart/form-data")
274 |> post("/api/pleroma/emoji/packs/download", %{
275 url: "https://example.com",
276 name: "pack_bad_sha",
279 |> json_response_and_validate_schema(:internal_server_error) == %{
280 "error" => "SHA256 for the pack doesn't match the one sent by the server"
284 test "other error", %{admin_conn: admin_conn} do
286 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
287 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
289 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
290 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
294 url: "https://example.com/api/pleroma/emoji/packs/test_pack"
296 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
297 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
301 |> put_req_header("content-type", "multipart/form-data")
302 |> post("/api/pleroma/emoji/packs/download", %{
303 url: "https://example.com",
307 |> json_response_and_validate_schema(:internal_server_error) == %{
309 "The pack was not set as shared and there is no fallback src to download from"
314 describe "PATCH /api/pleroma/emoji/packs/:name" do
316 pack_file = "#{@emoji_path}/test_pack/pack.json"
317 original_content = File.read!(pack_file)
320 File.write!(pack_file, original_content)
324 pack_file: pack_file,
326 "license" => "Test license changed",
327 "homepage" => "https://pleroma.social",
328 "description" => "Test description",
329 "share-files" => false
333 test "for a pack without a fallback source", ctx do
334 assert ctx[:admin_conn]
335 |> put_req_header("content-type", "multipart/form-data")
336 |> patch("/api/pleroma/emoji/packs/test_pack", %{"metadata" => ctx[:new_data]})
337 |> json_response_and_validate_schema(200) == ctx[:new_data]
339 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
342 test "for a pack with a fallback source", ctx do
346 url: "https://nonshared-pack"
348 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
351 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
356 "fallback-src-sha256",
357 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
360 assert ctx[:admin_conn]
361 |> put_req_header("content-type", "multipart/form-data")
362 |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
363 |> json_response_and_validate_schema(200) == new_data_with_sha
365 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
368 test "when the fallback source doesn't have all the files", ctx do
372 url: "https://nonshared-pack"
374 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
378 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
380 assert ctx[:admin_conn]
381 |> put_req_header("content-type", "multipart/form-data")
382 |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
383 |> json_response_and_validate_schema(:bad_request) == %{
384 "error" => "The fallback archive does not have all files specified in pack.json"
389 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/:name/files" do
391 pack_file = "#{@emoji_path}/test_pack/pack.json"
392 original_content = File.read!(pack_file)
395 File.write!(pack_file, original_content)
401 test "create shortcode exists", %{admin_conn: admin_conn} do
403 |> put_req_header("content-type", "multipart/form-data")
404 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
406 filename: "dir/blank.png",
408 filename: "blank.png",
409 path: "#{@emoji_path}/test_pack/blank.png"
412 |> json_response_and_validate_schema(:conflict) == %{
413 "error" => "An emoji with the \"blank\" shortcode already exists"
417 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
418 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
421 |> put_req_header("content-type", "multipart/form-data")
422 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
424 filename: "dir/blank.png",
426 filename: "blank.png",
427 path: "#{@emoji_path}/test_pack/blank.png"
430 |> json_response_and_validate_schema(200) == %{
431 "blank" => "blank.png",
432 "blank2" => "dir/blank.png"
435 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
438 |> put_req_header("content-type", "multipart/form-data")
439 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
441 new_shortcode: "blank2",
442 new_filename: "dir_2/blank_3.png"
444 |> json_response_and_validate_schema(:conflict) == %{
446 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
450 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
451 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
454 |> put_req_header("content-type", "multipart/form-data")
455 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
457 filename: "dir/blank.png",
459 filename: "blank.png",
460 path: "#{@emoji_path}/test_pack/blank.png"
463 |> json_response_and_validate_schema(200) == %{
464 "blank" => "blank.png",
465 "blank2" => "dir/blank.png"
468 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
471 |> put_req_header("content-type", "multipart/form-data")
472 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
474 new_shortcode: "blank3",
475 new_filename: "dir_2/blank_3.png",
478 |> json_response_and_validate_schema(200) == %{
479 "blank" => "blank.png",
480 "blank3" => "dir_2/blank_3.png"
483 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
486 test "with empty filename", %{admin_conn: admin_conn} do
488 |> put_req_header("content-type", "multipart/form-data")
489 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
493 filename: "blank.png",
494 path: "#{@emoji_path}/test_pack/blank.png"
497 |> json_response_and_validate_schema(:bad_request) == %{
498 "error" => "pack name, shortcode or filename cannot be empty"
502 test "add file with not loaded pack", %{admin_conn: admin_conn} do
504 |> put_req_header("content-type", "multipart/form-data")
505 |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
507 filename: "dir/blank.png",
509 filename: "blank.png",
510 path: "#{@emoji_path}/test_pack/blank.png"
513 |> json_response_and_validate_schema(:bad_request) == %{
514 "error" => "pack \"not_loaded\" is not found"
518 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
520 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=blank3")
521 |> json_response_and_validate_schema(:bad_request) == %{
522 "error" => "pack \"not_loaded\" is not found"
526 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
528 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=")
529 |> json_response_and_validate_schema(:bad_request) == %{
530 "error" => "pack name or shortcode cannot be empty"
534 test "update file with not loaded pack", %{admin_conn: admin_conn} do
536 |> put_req_header("content-type", "multipart/form-data")
537 |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
539 new_shortcode: "blank3",
540 new_filename: "dir_2/blank_3.png"
542 |> json_response_and_validate_schema(:bad_request) == %{
543 "error" => "pack \"not_loaded\" is not found"
547 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
549 |> put_req_header("content-type", "multipart/form-data")
550 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
552 filename: "dir/blank.png",
554 filename: "blank.png",
555 path: "#{@emoji_path}/test_pack/blank.png"
558 |> json_response_and_validate_schema(200) == %{
559 "blank" => "blank.png",
560 "blank4" => "dir/blank.png"
563 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
566 |> put_req_header("content-type", "multipart/form-data")
567 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
569 new_shortcode: "blank3",
570 new_filename: "dir_2/blank_3.png"
572 |> json_response_and_validate_schema(200) == %{
573 "blank3" => "dir_2/blank_3.png",
574 "blank" => "blank.png"
577 refute File.exists?("#{@emoji_path}/test_pack/dir/")
578 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
581 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
582 |> json_response_and_validate_schema(200) == %{"blank" => "blank.png"}
584 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
586 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
589 test "new with shortcode from url", %{admin_conn: admin_conn} do
593 url: "https://test-blank/blank_url.png"
595 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
599 |> put_req_header("content-type", "multipart/form-data")
600 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
601 shortcode: "blank_url",
602 file: "https://test-blank/blank_url.png"
604 |> json_response_and_validate_schema(200) == %{
605 "blank_url" => "blank_url.png",
606 "blank" => "blank.png"
609 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
611 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
614 test "new without shortcode", %{admin_conn: admin_conn} do
615 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
618 |> put_req_header("content-type", "multipart/form-data")
619 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
621 filename: "shortcode.png",
622 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
625 |> json_response_and_validate_schema(200) == %{
626 "shortcode" => "shortcode.png",
627 "blank" => "blank.png"
631 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
633 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank2")
634 |> json_response_and_validate_schema(:bad_request) == %{
635 "error" => "Emoji \"blank2\" does not exist"
639 test "update non existing emoji", %{admin_conn: admin_conn} do
641 |> put_req_header("content-type", "multipart/form-data")
642 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
644 new_shortcode: "blank3",
645 new_filename: "dir_2/blank_3.png"
647 |> json_response_and_validate_schema(:bad_request) == %{
648 "error" => "Emoji \"blank2\" does not exist"
652 test "update with empty shortcode", %{admin_conn: admin_conn} do
654 "error" => "Missing field: new_shortcode."
657 |> put_req_header("content-type", "multipart/form-data")
658 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
660 new_filename: "dir_2/blank_3.png"
662 |> json_response_and_validate_schema(:bad_request)
666 describe "POST/DELETE /api/pleroma/emoji/packs/:name" do
667 test "creating and deleting a pack", %{admin_conn: admin_conn} do
669 |> post("/api/pleroma/emoji/packs/test_created")
670 |> json_response_and_validate_schema(200) == "ok"
672 assert File.exists?("#{@emoji_path}/test_created/pack.json")
674 assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
680 |> delete("/api/pleroma/emoji/packs/test_created")
681 |> json_response_and_validate_schema(200) == "ok"
683 refute File.exists?("#{@emoji_path}/test_created/pack.json")
686 test "if pack exists", %{admin_conn: admin_conn} do
687 path = Path.join(@emoji_path, "test_created")
689 pack_file = Jason.encode!(%{files: %{}, pack: %{}})
690 File.write!(Path.join(path, "pack.json"), pack_file)
693 |> post("/api/pleroma/emoji/packs/test_created")
694 |> json_response_and_validate_schema(:conflict) == %{
695 "error" => "A pack named \"test_created\" already exists"
698 on_exit(fn -> File.rm_rf(path) end)
701 test "with empty name", %{admin_conn: admin_conn} do
703 |> post("/api/pleroma/emoji/packs/ ")
704 |> json_response_and_validate_schema(:bad_request) == %{
705 "error" => "pack name cannot be empty"
710 test "deleting nonexisting pack", %{admin_conn: admin_conn} do
712 |> delete("/api/pleroma/emoji/packs/non_existing")
713 |> json_response_and_validate_schema(:not_found) == %{
714 "error" => "Pack non_existing does not exist"
718 test "deleting with empty name", %{admin_conn: admin_conn} do
720 |> delete("/api/pleroma/emoji/packs/ ")
721 |> json_response_and_validate_schema(:bad_request) == %{
722 "error" => "pack name cannot be empty"
726 test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
728 File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
729 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
732 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
734 refute Map.has_key?(resp, "test_pack_for_import")
737 |> get("/api/pleroma/emoji/packs/import")
738 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
740 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
741 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
743 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
744 refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
746 emoji_txt_content = """
747 blank, blank.png, Fun
749 foo, /emoji/test_pack_for_import/blank.png
753 File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
756 |> get("/api/pleroma/emoji/packs/import")
757 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
759 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
761 assert resp["test_pack_for_import"]["files"] == %{
762 "blank" => "blank.png",
763 "blank2" => "blank.png",
768 describe "GET /api/pleroma/emoji/packs/:name" do
769 test "shows pack.json", %{conn: conn} do
771 "files" => %{"blank" => "blank.png"},
773 "can-download" => true,
774 "description" => "Test description",
775 "download-sha256" => _,
776 "homepage" => "https://pleroma.social",
777 "license" => "Test license",
778 "share-files" => true
782 |> get("/api/pleroma/emoji/packs/test_pack")
783 |> json_response_and_validate_schema(200)
786 test "non existing pack", %{conn: conn} do
788 |> get("/api/pleroma/emoji/packs/non_existing")
789 |> json_response_and_validate_schema(:not_found) == %{
790 "error" => "Pack non_existing does not exist"
794 test "error name", %{conn: conn} do
796 |> get("/api/pleroma/emoji/packs/ ")
797 |> json_response_and_validate_schema(:bad_request) == %{
798 "error" => "pack name cannot be empty"