1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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, async: false
10 import Pleroma.Factory
12 @emoji_path Path.join(
13 Pleroma.Config.get!([:instance, :static_dir]),
17 setup do: clear_config([:instance, :public], true)
20 admin = insert(:user, is_admin: true)
21 token = insert(:oauth_admin_token, user: admin)
25 |> assign(:user, admin)
26 |> assign(:token, token)
28 Pleroma.Emoji.reload()
29 {:ok, %{admin_conn: admin_conn}}
32 test "GET /api/v1/pleroma/emoji/packs when :public: false", %{conn: conn} do
33 clear_config([:instance, :public], false)
34 conn |> get("/api/v1/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
37 test "GET /api/v1/pleroma/emoji/packs", %{conn: conn} do
40 |> get("/api/v1/pleroma/emoji/packs")
41 |> json_response_and_validate_schema(200)
43 assert resp["count"] == 5
49 shared = resp["packs"]["test_pack"]
50 assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
51 assert Map.has_key?(shared["pack"], "download-sha256")
52 assert shared["pack"]["can-download"]
53 assert shared["pack"]["share-files"]
55 non_shared = resp["packs"]["test_pack_nonshared"]
56 assert non_shared["pack"]["share-files"] == false
57 assert non_shared["pack"]["can-download"] == false
61 |> get("/api/v1/pleroma/emoji/packs?page_size=1")
62 |> json_response_and_validate_schema(200)
64 assert resp["count"] == 5
66 packs = Map.keys(resp["packs"])
68 assert length(packs) == 1
74 |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=2")
75 |> json_response_and_validate_schema(200)
77 assert resp["count"] == 5
78 packs = Map.keys(resp["packs"])
79 assert length(packs) == 1
84 |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=3")
85 |> json_response_and_validate_schema(200)
87 assert resp["count"] == 5
88 packs = Map.keys(resp["packs"])
89 assert length(packs) == 1
94 |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=4")
95 |> json_response_and_validate_schema(200)
97 assert resp["count"] == 5
98 packs = Map.keys(resp["packs"])
99 assert length(packs) == 1
101 assert [pack1, pack2, pack3, pack4] |> Enum.uniq() |> length() == 4
104 describe "GET /api/v1/pleroma/emoji/packs/remote" do
105 test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
108 |> get("/api/v1/pleroma/emoji/packs?page=2&page_size=1")
109 |> json_response_and_validate_schema(200)
112 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
113 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
115 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
116 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
120 url: "https://example.com/api/v1/pleroma/emoji/packs?page=2&page_size=1"
127 "/api/v1/pleroma/emoji/packs/remote?url=https://example.com&page=2&page_size=1"
129 |> json_response_and_validate_schema(200) == resp
132 test "non shareable instance", %{admin_conn: admin_conn} do
134 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
135 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
137 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
138 json(%{metadata: %{features: []}})
142 |> get("/api/v1/pleroma/emoji/packs/remote?url=https://example.com")
143 |> json_response_and_validate_schema(500) == %{
144 "error" => "The requested instance does not support sharing emoji packs"
149 describe "GET /api/v1/pleroma/emoji/packs/archive?name=:name" do
150 test "download shared pack", %{conn: conn} do
153 |> get("/api/v1/pleroma/emoji/packs/archive?name=test_pack")
156 {:ok, arch} = :zip.unzip(resp, [:memory])
158 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
159 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
162 test "non existing pack", %{conn: conn} do
164 |> get("/api/v1/pleroma/emoji/packs/archive?name=test_pack_for_import")
165 |> json_response_and_validate_schema(:not_found) == %{
166 "error" => "Pack test_pack_for_import does not exist"
170 test "non downloadable pack", %{conn: conn} do
172 |> get("/api/v1/pleroma/emoji/packs/archive?name=test_pack_nonshared")
173 |> json_response_and_validate_schema(:forbidden) == %{
175 "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
180 describe "POST /api/v1/pleroma/emoji/packs/download" do
181 test "shared pack from remote and non shared from fallback-src", %{
182 admin_conn: admin_conn,
186 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
187 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
189 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
190 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
194 url: "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack"
197 |> get("/api/v1/pleroma/emoji/pack?name=test_pack")
198 |> json_response_and_validate_schema(200)
203 url: "https://example.com/api/v1/pleroma/emoji/packs/archive?name=test_pack"
206 |> get("/api/v1/pleroma/emoji/packs/archive?name=test_pack")
212 url: "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack_nonshared"
215 |> get("/api/v1/pleroma/emoji/pack?name=test_pack_nonshared")
216 |> json_response_and_validate_schema(200)
221 url: "https://nonshared-pack"
223 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
227 url: "https://example.com/api/v1/pleroma/emoji/pack?name=test%20with%20spaces"
230 |> get("/api/v1/pleroma/emoji/pack?name=test%20with%20spaces")
231 |> json_response_and_validate_schema(200)
236 url: "https://example.com/api/v1/pleroma/emoji/packs/archive?name=test%20with%20spaces"
239 |> get("/api/v1/pleroma/emoji/packs/archive?name=test%20with%20spaces")
245 |> put_req_header("content-type", "multipart/form-data")
246 |> post("/api/v1/pleroma/emoji/packs/download", %{
247 url: "https://example.com",
251 |> json_response_and_validate_schema(200) == "ok"
253 assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
254 assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
257 |> delete("/api/v1/pleroma/emoji/pack?name=test_pack2")
258 |> json_response_and_validate_schema(200) == "ok"
260 refute File.exists?("#{@emoji_path}/test_pack2")
263 |> put_req_header("content-type", "multipart/form-data")
265 "/api/v1/pleroma/emoji/packs/download",
267 url: "https://example.com",
268 name: "test_pack_nonshared",
269 as: "test_pack_nonshared2"
272 |> json_response_and_validate_schema(200) == "ok"
274 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
275 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
278 |> delete("/api/v1/pleroma/emoji/pack?name=test_pack_nonshared2")
279 |> json_response_and_validate_schema(200) == "ok"
281 refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
284 |> put_req_header("content-type", "multipart/form-data")
285 |> post("/api/v1/pleroma/emoji/packs/download", %{
286 url: "https://example.com",
287 name: "test with spaces",
288 as: "test with spaces"
290 |> json_response_and_validate_schema(200) == "ok"
292 assert File.exists?("#{@emoji_path}/test with spaces/pack.json")
293 assert File.exists?("#{@emoji_path}/test with spaces/blank.png")
296 test "nonshareable instance", %{admin_conn: admin_conn} do
298 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
299 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
301 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
302 json(%{metadata: %{features: []}})
306 |> put_req_header("content-type", "multipart/form-data")
308 "/api/v1/pleroma/emoji/packs/download",
310 url: "https://old-instance",
315 |> json_response_and_validate_schema(500) == %{
316 "error" => "The requested instance does not support sharing emoji packs"
320 test "checksum fail", %{admin_conn: admin_conn} do
322 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
323 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
325 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
326 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
330 url: "https://example.com/api/v1/pleroma/emoji/pack?name=pack_bad_sha"
332 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
333 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
337 url: "https://example.com/api/v1/pleroma/emoji/packs/archive?name=pack_bad_sha"
341 body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
346 |> put_req_header("content-type", "multipart/form-data")
347 |> post("/api/v1/pleroma/emoji/packs/download", %{
348 url: "https://example.com",
349 name: "pack_bad_sha",
352 |> json_response_and_validate_schema(:internal_server_error) == %{
353 "error" => "SHA256 for the pack doesn't match the one sent by the server"
357 test "other error", %{admin_conn: admin_conn} do
359 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
360 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
362 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
363 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
367 url: "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack"
369 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
370 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
374 |> put_req_header("content-type", "multipart/form-data")
375 |> post("/api/v1/pleroma/emoji/packs/download", %{
376 url: "https://example.com",
380 |> json_response_and_validate_schema(:internal_server_error) == %{
382 "The pack was not set as shared and there is no fallback src to download from"
387 describe "PATCH/update /api/v1/pleroma/emoji/pack?name=:name" do
389 pack_file = "#{@emoji_path}/test_pack/pack.json"
390 original_content = File.read!(pack_file)
393 File.write!(pack_file, original_content)
397 pack_file: pack_file,
399 "license" => "Test license changed",
400 "homepage" => "https://pleroma.social",
401 "description" => "Test description",
402 "share-files" => false
406 test "returns error when file system not writable", %{admin_conn: conn} = ctx do
408 {File, [:passthrough], [stat: fn _ -> {:error, :eacces} end]}
411 |> put_req_header("content-type", "multipart/form-data")
413 "/api/v1/pleroma/emoji/pack?name=test_pack",
414 %{"metadata" => ctx[:new_data]}
416 |> json_response_and_validate_schema(500)
420 test "for a pack without a fallback source", ctx do
421 assert ctx[:admin_conn]
422 |> put_req_header("content-type", "multipart/form-data")
423 |> patch("/api/v1/pleroma/emoji/pack?name=test_pack", %{
424 "metadata" => ctx[:new_data]
426 |> json_response_and_validate_schema(200) == ctx[:new_data]
428 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
431 test "for a pack with a fallback source", ctx do
435 url: "https://nonshared-pack"
437 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
440 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
445 "fallback-src-sha256",
446 "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
449 assert ctx[:admin_conn]
450 |> put_req_header("content-type", "multipart/form-data")
451 |> patch("/api/v1/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
452 |> json_response_and_validate_schema(200) == new_data_with_sha
454 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
457 test "when the fallback source doesn't have all the files", ctx do
461 url: "https://nonshared-pack"
463 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
467 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
469 assert ctx[:admin_conn]
470 |> put_req_header("content-type", "multipart/form-data")
471 |> patch("/api/v1/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
472 |> json_response_and_validate_schema(:bad_request) == %{
473 "error" => "The fallback archive does not have all files specified in pack.json"
478 describe "POST/DELETE /api/v1/pleroma/emoji/pack?name=:name" do
479 test "returns an error on creates pack when file system not writable", %{
480 admin_conn: admin_conn
482 path_pack = Path.join(@emoji_path, "test_pack")
485 {File, [:passthrough], [mkdir: fn ^path_pack -> {:error, :eacces} end]}
488 |> post("/api/v1/pleroma/emoji/pack?name=test_pack")
489 |> json_response_and_validate_schema(500) == %{
491 "Unexpected error occurred while creating pack. (POSIX error: Permission denied)"
496 test "returns an error on deletes pack when the file system is not writable", %{
497 admin_conn: admin_conn
499 path_pack = Path.join(@emoji_path, "test_emoji_pack")
502 {:ok, _pack} = Pleroma.Emoji.Pack.create("test_emoji_pack")
505 {File, [:passthrough], [rm_rf: fn ^path_pack -> {:error, :eacces, path_pack} end]}
508 |> delete("/api/v1/pleroma/emoji/pack?name=test_emoji_pack")
509 |> json_response_and_validate_schema(500) == %{
511 "Couldn't delete the `test_emoji_pack` pack (POSIX error: Permission denied)"
515 File.rm_rf(path_pack)
519 test "creating and deleting a pack", %{admin_conn: admin_conn} do
521 |> post("/api/v1/pleroma/emoji/pack?name=test_created")
522 |> json_response_and_validate_schema(200) == "ok"
524 assert File.exists?("#{@emoji_path}/test_created/pack.json")
526 assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
533 |> delete("/api/v1/pleroma/emoji/pack?name=test_created")
534 |> json_response_and_validate_schema(200) == "ok"
536 refute File.exists?("#{@emoji_path}/test_created/pack.json")
539 test "if pack exists", %{admin_conn: admin_conn} do
540 path = Path.join(@emoji_path, "test_created")
542 pack_file = Jason.encode!(%{files: %{}, pack: %{}})
543 File.write!(Path.join(path, "pack.json"), pack_file)
546 |> post("/api/v1/pleroma/emoji/pack?name=test_created")
547 |> json_response_and_validate_schema(:conflict) == %{
548 "error" => "A pack named \"test_created\" already exists"
551 on_exit(fn -> File.rm_rf(path) end)
554 test "with empty name", %{admin_conn: admin_conn} do
556 |> post("/api/v1/pleroma/emoji/pack?name= ")
557 |> json_response_and_validate_schema(:bad_request) == %{
558 "error" => "pack name cannot be empty"
563 test "deleting nonexisting pack", %{admin_conn: admin_conn} do
565 |> delete("/api/v1/pleroma/emoji/pack?name=non_existing")
566 |> json_response_and_validate_schema(:not_found) == %{
567 "error" => "Pack non_existing does not exist"
571 test "deleting with empty name", %{admin_conn: admin_conn} do
573 |> delete("/api/v1/pleroma/emoji/pack?name= ")
574 |> json_response_and_validate_schema(:bad_request) == %{
575 "error" => "pack name cannot be empty"
579 test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
581 File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
582 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
587 |> get("/api/v1/pleroma/emoji/packs")
588 |> json_response_and_validate_schema(200)
590 refute Map.has_key?(resp["packs"], "test_pack_for_import")
593 |> get("/api/v1/pleroma/emoji/packs/import")
594 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
598 |> get("/api/v1/pleroma/emoji/packs")
599 |> json_response_and_validate_schema(200)
601 assert resp["packs"]["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
603 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
604 refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
606 emoji_txt_content = """
607 blank, blank.png, Fun
609 foo, /emoji/test_pack_for_import/blank.png
613 File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
616 |> get("/api/v1/pleroma/emoji/packs/import")
617 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
621 |> get("/api/v1/pleroma/emoji/packs")
622 |> json_response_and_validate_schema(200)
624 assert resp["packs"]["test_pack_for_import"]["files"] == %{
625 "blank" => "blank.png",
626 "blank2" => "blank.png",
631 describe "GET /api/v1/pleroma/emoji/pack?name=:name" do
632 test "shows pack.json", %{conn: conn} do
637 "can-download" => true,
638 "description" => "Test description",
639 "download-sha256" => _,
640 "homepage" => "https://pleroma.social",
641 "license" => "Test license",
642 "share-files" => true
646 |> get("/api/v1/pleroma/emoji/pack?name=test_pack")
647 |> json_response_and_validate_schema(200)
649 assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
656 |> get("/api/v1/pleroma/emoji/pack?name=test_pack&page_size=1")
657 |> json_response_and_validate_schema(200)
659 assert files |> Map.keys() |> length() == 1
666 |> get("/api/v1/pleroma/emoji/pack?name=test_pack&page_size=1&page=2")
667 |> json_response_and_validate_schema(200)
669 assert files |> Map.keys() |> length() == 1
672 test "for pack name with special chars", %{conn: conn} do
677 "can-download" => true,
678 "description" => "Test description",
679 "download-sha256" => _,
680 "homepage" => "https://pleroma.social",
681 "license" => "Test license",
682 "share-files" => true
686 |> get("/api/v1/pleroma/emoji/pack?name=blobs.gg")
687 |> json_response_and_validate_schema(200)
690 test "non existing pack", %{conn: conn} do
692 |> get("/api/v1/pleroma/emoji/pack?name=non_existing")
693 |> json_response_and_validate_schema(:not_found) == %{
694 "error" => "Pack non_existing does not exist"
698 test "error name", %{conn: conn} do
700 |> get("/api/v1/pleroma/emoji/pack?name= ")
701 |> json_response_and_validate_schema(:bad_request) == %{
702 "error" => "pack name cannot be empty"