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, async: false
11 @emoji_path Path.join(
12 Pleroma.Config.get!([:instance, :static_dir]),
15 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
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/pleroma/emoji/packs when :public: false", %{conn: conn} do
33 Config.put([:instance, :public], false)
34 conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
37 test "GET /api/pleroma/emoji/packs", %{conn: conn} do
38 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
40 assert resp["count"] == 4
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"]
52 non_shared = resp["packs"]["test_pack_nonshared"]
53 assert non_shared["pack"]["share-files"] == false
54 assert non_shared["pack"]["can-download"] == false
58 |> get("/api/pleroma/emoji/packs?page_size=1")
59 |> json_response_and_validate_schema(200)
61 assert resp["count"] == 4
63 packs = Map.keys(resp["packs"])
65 assert length(packs) == 1
71 |> get("/api/pleroma/emoji/packs?page_size=1&page=2")
72 |> json_response_and_validate_schema(200)
74 assert resp["count"] == 4
75 packs = Map.keys(resp["packs"])
76 assert length(packs) == 1
81 |> get("/api/pleroma/emoji/packs?page_size=1&page=3")
82 |> json_response_and_validate_schema(200)
84 assert resp["count"] == 4
85 packs = Map.keys(resp["packs"])
86 assert length(packs) == 1
91 |> get("/api/pleroma/emoji/packs?page_size=1&page=4")
92 |> json_response_and_validate_schema(200)
94 assert resp["count"] == 4
95 packs = Map.keys(resp["packs"])
96 assert length(packs) == 1
98 assert [pack1, pack2, pack3, pack4] |> Enum.uniq() |> length() == 4
101 describe "GET /api/pleroma/emoji/packs/remote" do
102 test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
105 |> get("/api/pleroma/emoji/packs?page=2&page_size=1")
106 |> json_response_and_validate_schema(200)
109 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
110 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
112 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
113 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
115 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs?page=2&page_size=1"} ->
120 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com&page=2&page_size=1")
121 |> json_response_and_validate_schema(200) == resp
124 test "non shareable instance", %{admin_conn: admin_conn} do
126 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
127 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
129 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
130 json(%{metadata: %{features: []}})
134 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
135 |> json_response_and_validate_schema(500) == %{
136 "error" => "The requested instance does not support sharing emoji packs"
141 describe "GET /api/pleroma/emoji/packs/archive?name=:name" do
142 test "download shared pack", %{conn: conn} do
145 |> get("/api/pleroma/emoji/packs/archive?name=test_pack")
148 {:ok, arch} = :zip.unzip(resp, [:memory])
150 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
151 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
154 test "non existing pack", %{conn: conn} do
156 |> get("/api/pleroma/emoji/packs/archive?name=test_pack_for_import")
157 |> json_response_and_validate_schema(:not_found) == %{
158 "error" => "Pack test_pack_for_import does not exist"
162 test "non downloadable pack", %{conn: conn} do
164 |> get("/api/pleroma/emoji/packs/archive?name=test_pack_nonshared")
165 |> json_response_and_validate_schema(:forbidden) == %{
167 "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
172 describe "POST /api/pleroma/emoji/packs/download" do
173 test "shared pack from remote and non shared from fallback-src", %{
174 admin_conn: admin_conn,
178 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
179 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
181 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
182 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
186 url: "https://example.com/api/pleroma/emoji/pack?name=test_pack"
189 |> get("/api/pleroma/emoji/pack?name=test_pack")
190 |> json_response_and_validate_schema(200)
195 url: "https://example.com/api/pleroma/emoji/packs/archive?name=test_pack"
198 |> get("/api/pleroma/emoji/packs/archive?name=test_pack")
204 url: "https://example.com/api/pleroma/emoji/pack?name=test_pack_nonshared"
207 |> get("/api/pleroma/emoji/pack?name=test_pack_nonshared")
208 |> json_response_and_validate_schema(200)
213 url: "https://nonshared-pack"
215 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
219 |> put_req_header("content-type", "multipart/form-data")
220 |> post("/api/pleroma/emoji/packs/download", %{
221 url: "https://example.com",
225 |> json_response_and_validate_schema(200) == "ok"
227 assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
228 assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
231 |> delete("/api/pleroma/emoji/pack?name=test_pack2")
232 |> json_response_and_validate_schema(200) == "ok"
234 refute File.exists?("#{@emoji_path}/test_pack2")
237 |> put_req_header("content-type", "multipart/form-data")
239 "/api/pleroma/emoji/packs/download",
241 url: "https://example.com",
242 name: "test_pack_nonshared",
243 as: "test_pack_nonshared2"
246 |> json_response_and_validate_schema(200) == "ok"
248 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
249 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
252 |> delete("/api/pleroma/emoji/pack?name=test_pack_nonshared2")
253 |> json_response_and_validate_schema(200) == "ok"
255 refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
258 test "nonshareable instance", %{admin_conn: admin_conn} do
260 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
261 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
263 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
264 json(%{metadata: %{features: []}})
268 |> put_req_header("content-type", "multipart/form-data")
270 "/api/pleroma/emoji/packs/download",
272 url: "https://old-instance",
277 |> json_response_and_validate_schema(500) == %{
278 "error" => "The requested instance does not support sharing emoji packs"
282 test "checksum fail", %{admin_conn: admin_conn} do
284 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
285 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
287 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
288 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
292 url: "https://example.com/api/pleroma/emoji/pack?name=pack_bad_sha"
294 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
295 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
299 url: "https://example.com/api/pleroma/emoji/packs/archive?name=pack_bad_sha"
303 body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
308 |> put_req_header("content-type", "multipart/form-data")
309 |> post("/api/pleroma/emoji/packs/download", %{
310 url: "https://example.com",
311 name: "pack_bad_sha",
314 |> json_response_and_validate_schema(:internal_server_error) == %{
315 "error" => "SHA256 for the pack doesn't match the one sent by the server"
319 test "other error", %{admin_conn: admin_conn} do
321 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
322 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
324 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
325 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
329 url: "https://example.com/api/pleroma/emoji/pack?name=test_pack"
331 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
332 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
336 |> put_req_header("content-type", "multipart/form-data")
337 |> post("/api/pleroma/emoji/packs/download", %{
338 url: "https://example.com",
342 |> json_response_and_validate_schema(:internal_server_error) == %{
344 "The pack was not set as shared and there is no fallback src to download from"
349 describe "PATCH/update /api/pleroma/emoji/pack?name=:name" do
351 pack_file = "#{@emoji_path}/test_pack/pack.json"
352 original_content = File.read!(pack_file)
355 File.write!(pack_file, original_content)
359 pack_file: pack_file,
361 "license" => "Test license changed",
362 "homepage" => "https://pleroma.social",
363 "description" => "Test description",
364 "share-files" => false
368 test "returns error when file system not writable", %{admin_conn: conn} = ctx do
369 {:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path)
372 File.chmod!(@emoji_path, 0o400)
375 |> put_req_header("content-type", "multipart/form-data")
377 "/api/pleroma/emoji/pack?name=test_pack",
378 %{"metadata" => ctx[:new_data]}
380 |> json_response_and_validate_schema(500)
382 File.chmod!(@emoji_path, mode)
386 test "for a pack without a fallback source", ctx do
387 assert ctx[:admin_conn]
388 |> put_req_header("content-type", "multipart/form-data")
389 |> patch("/api/pleroma/emoji/pack?name=test_pack", %{
390 "metadata" => ctx[:new_data]
392 |> json_response_and_validate_schema(200) == ctx[:new_data]
394 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
397 test "for a pack with a fallback source", ctx do
401 url: "https://nonshared-pack"
403 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
406 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
411 "fallback-src-sha256",
412 "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
415 assert ctx[:admin_conn]
416 |> put_req_header("content-type", "multipart/form-data")
417 |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
418 |> json_response_and_validate_schema(200) == new_data_with_sha
420 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
423 test "when the fallback source doesn't have all the files", ctx do
427 url: "https://nonshared-pack"
429 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
433 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
435 assert ctx[:admin_conn]
436 |> put_req_header("content-type", "multipart/form-data")
437 |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
438 |> json_response_and_validate_schema(:bad_request) == %{
439 "error" => "The fallback archive does not have all files specified in pack.json"
444 describe "POST/DELETE /api/pleroma/emoji/pack?name=:name" do
445 test "returns error when file system not writable", %{admin_conn: admin_conn} do
446 {:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path)
449 File.chmod!(@emoji_path, 0o400)
452 |> post("/api/pleroma/emoji/pack?name=test_pack")
453 |> json_response_and_validate_schema(500) == %{
455 "Unexpected error occurred while creating pack. (POSIX error: Permission denied)"
458 File.chmod!(@emoji_path, mode)
462 test "returns an error on deletes pack when the file system is not writable", %{
463 admin_conn: admin_conn
465 {:ok, _pack} = Pleroma.Emoji.Pack.create("test_pack2")
466 {:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path)
469 File.chmod!(@emoji_path, 0o400)
472 |> delete("/api/pleroma/emoji/pack?name=test_pack")
473 |> json_response_and_validate_schema(500) == %{
474 "error" => "Couldn't delete the pack test_pack (POSIX error: Permission denied)"
477 File.chmod!(@emoji_path, mode)
478 File.rm_rf!(Path.join([@emoji_path, "test_pack2"]))
482 test "creating and deleting a pack", %{admin_conn: admin_conn} do
484 |> post("/api/pleroma/emoji/pack?name=test_created")
485 |> json_response_and_validate_schema(200) == "ok"
487 assert File.exists?("#{@emoji_path}/test_created/pack.json")
489 assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
496 |> delete("/api/pleroma/emoji/pack?name=test_created")
497 |> json_response_and_validate_schema(200) == "ok"
499 refute File.exists?("#{@emoji_path}/test_created/pack.json")
502 test "if pack exists", %{admin_conn: admin_conn} do
503 path = Path.join(@emoji_path, "test_created")
505 pack_file = Jason.encode!(%{files: %{}, pack: %{}})
506 File.write!(Path.join(path, "pack.json"), pack_file)
509 |> post("/api/pleroma/emoji/pack?name=test_created")
510 |> json_response_and_validate_schema(:conflict) == %{
511 "error" => "A pack named \"test_created\" already exists"
514 on_exit(fn -> File.rm_rf(path) end)
517 test "with empty name", %{admin_conn: admin_conn} do
519 |> post("/api/pleroma/emoji/pack?name= ")
520 |> json_response_and_validate_schema(:bad_request) == %{
521 "error" => "pack name cannot be empty"
526 test "deleting nonexisting pack", %{admin_conn: admin_conn} do
528 |> delete("/api/pleroma/emoji/pack?name=non_existing")
529 |> json_response_and_validate_schema(:not_found) == %{
530 "error" => "Pack non_existing does not exist"
534 test "deleting with empty name", %{admin_conn: admin_conn} do
536 |> delete("/api/pleroma/emoji/pack?name= ")
537 |> json_response_and_validate_schema(:bad_request) == %{
538 "error" => "pack name cannot be empty"
542 test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
544 File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
545 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
548 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
550 refute Map.has_key?(resp["packs"], "test_pack_for_import")
553 |> get("/api/pleroma/emoji/packs/import")
554 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
556 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
557 assert resp["packs"]["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
559 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
560 refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
562 emoji_txt_content = """
563 blank, blank.png, Fun
565 foo, /emoji/test_pack_for_import/blank.png
569 File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
572 |> get("/api/pleroma/emoji/packs/import")
573 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
575 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
577 assert resp["packs"]["test_pack_for_import"]["files"] == %{
578 "blank" => "blank.png",
579 "blank2" => "blank.png",
584 describe "GET /api/pleroma/emoji/pack?name=:name" do
585 test "shows pack.json", %{conn: conn} do
590 "can-download" => true,
591 "description" => "Test description",
592 "download-sha256" => _,
593 "homepage" => "https://pleroma.social",
594 "license" => "Test license",
595 "share-files" => true
599 |> get("/api/pleroma/emoji/pack?name=test_pack")
600 |> json_response_and_validate_schema(200)
602 assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
609 |> get("/api/pleroma/emoji/pack?name=test_pack&page_size=1")
610 |> json_response_and_validate_schema(200)
612 assert files |> Map.keys() |> length() == 1
619 |> get("/api/pleroma/emoji/pack?name=test_pack&page_size=1&page=2")
620 |> json_response_and_validate_schema(200)
622 assert files |> Map.keys() |> length() == 1
625 test "for pack name with special chars", %{conn: conn} do
630 "can-download" => true,
631 "description" => "Test description",
632 "download-sha256" => _,
633 "homepage" => "https://pleroma.social",
634 "license" => "Test license",
635 "share-files" => true
639 |> get("/api/pleroma/emoji/pack?name=blobs.gg")
640 |> json_response_and_validate_schema(200)
643 test "non existing pack", %{conn: conn} do
645 |> get("/api/pleroma/emoji/pack?name=non_existing")
646 |> json_response_and_validate_schema(:not_found) == %{
647 "error" => "Pack non_existing does not exist"
651 test "error name", %{conn: conn} do
653 |> get("/api/pleroma/emoji/pack?name= ")
654 |> json_response_and_validate_schema(:bad_request) == %{
655 "error" => "pack name cannot be empty"