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
10 import Pleroma.Factory
12 @emoji_path Path.join(
13 Pleroma.Config.get!([:instance, :static_dir]),
16 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
18 setup do: clear_config([:instance, :public], true)
21 admin = insert(:user, is_admin: true)
22 token = insert(:oauth_admin_token, user: admin)
26 |> assign(:user, admin)
27 |> assign(:token, token)
29 Pleroma.Emoji.reload()
30 {:ok, %{admin_conn: admin_conn}}
33 test "GET /api/pleroma/emoji/packs when :public: false", %{conn: conn} do
34 Config.put([:instance, :public], false)
35 conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
38 test "GET /api/pleroma/emoji/packs", %{conn: conn} do
39 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
41 assert resp["count"] == 4
47 shared = resp["packs"]["test_pack"]
48 assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
49 assert Map.has_key?(shared["pack"], "download-sha256")
50 assert shared["pack"]["can-download"]
51 assert shared["pack"]["share-files"]
53 non_shared = resp["packs"]["test_pack_nonshared"]
54 assert non_shared["pack"]["share-files"] == false
55 assert non_shared["pack"]["can-download"] == false
59 |> get("/api/pleroma/emoji/packs?page_size=1")
60 |> json_response_and_validate_schema(200)
62 assert resp["count"] == 4
64 packs = Map.keys(resp["packs"])
66 assert length(packs) == 1
72 |> get("/api/pleroma/emoji/packs?page_size=1&page=2")
73 |> json_response_and_validate_schema(200)
75 assert resp["count"] == 4
76 packs = Map.keys(resp["packs"])
77 assert length(packs) == 1
82 |> get("/api/pleroma/emoji/packs?page_size=1&page=3")
83 |> json_response_and_validate_schema(200)
85 assert resp["count"] == 4
86 packs = Map.keys(resp["packs"])
87 assert length(packs) == 1
92 |> get("/api/pleroma/emoji/packs?page_size=1&page=4")
93 |> json_response_and_validate_schema(200)
95 assert resp["count"] == 4
96 packs = Map.keys(resp["packs"])
97 assert length(packs) == 1
99 assert [pack1, pack2, pack3, pack4] |> Enum.uniq() |> length() == 4
102 describe "GET /api/pleroma/emoji/packs/remote" do
103 test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
106 |> get("/api/pleroma/emoji/packs?page=2&page_size=1")
107 |> json_response_and_validate_schema(200)
110 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
111 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
113 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
114 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
116 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs?page=2&page_size=1"} ->
121 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com&page=2&page_size=1")
122 |> json_response_and_validate_schema(200) == resp
125 test "non shareable instance", %{admin_conn: admin_conn} do
127 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
128 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
130 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
131 json(%{metadata: %{features: []}})
135 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
136 |> json_response_and_validate_schema(500) == %{
137 "error" => "The requested instance does not support sharing emoji packs"
142 describe "GET /api/pleroma/emoji/packs/archive?name=:name" do
143 test "download shared pack", %{conn: conn} do
146 |> get("/api/pleroma/emoji/packs/archive?name=test_pack")
149 {:ok, arch} = :zip.unzip(resp, [:memory])
151 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
152 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
155 test "non existing pack", %{conn: conn} do
157 |> get("/api/pleroma/emoji/packs/archive?name=test_pack_for_import")
158 |> json_response_and_validate_schema(:not_found) == %{
159 "error" => "Pack test_pack_for_import does not exist"
163 test "non downloadable pack", %{conn: conn} do
165 |> get("/api/pleroma/emoji/packs/archive?name=test_pack_nonshared")
166 |> json_response_and_validate_schema(:forbidden) == %{
168 "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
173 describe "POST /api/pleroma/emoji/packs/download" do
174 test "shared pack from remote and non shared from fallback-src", %{
175 admin_conn: admin_conn,
179 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
180 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
182 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
183 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
187 url: "https://example.com/api/pleroma/emoji/pack?name=test_pack"
190 |> get("/api/pleroma/emoji/pack?name=test_pack")
191 |> json_response_and_validate_schema(200)
196 url: "https://example.com/api/pleroma/emoji/packs/archive?name=test_pack"
199 |> get("/api/pleroma/emoji/packs/archive?name=test_pack")
205 url: "https://example.com/api/pleroma/emoji/pack?name=test_pack_nonshared"
208 |> get("/api/pleroma/emoji/pack?name=test_pack_nonshared")
209 |> json_response_and_validate_schema(200)
214 url: "https://nonshared-pack"
216 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
220 |> put_req_header("content-type", "multipart/form-data")
221 |> post("/api/pleroma/emoji/packs/download", %{
222 url: "https://example.com",
226 |> json_response_and_validate_schema(200) == "ok"
228 assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
229 assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
232 |> delete("/api/pleroma/emoji/pack?name=test_pack2")
233 |> json_response_and_validate_schema(200) == "ok"
235 refute File.exists?("#{@emoji_path}/test_pack2")
238 |> put_req_header("content-type", "multipart/form-data")
240 "/api/pleroma/emoji/packs/download",
242 url: "https://example.com",
243 name: "test_pack_nonshared",
244 as: "test_pack_nonshared2"
247 |> json_response_and_validate_schema(200) == "ok"
249 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
250 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
253 |> delete("/api/pleroma/emoji/pack?name=test_pack_nonshared2")
254 |> json_response_and_validate_schema(200) == "ok"
256 refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
259 test "nonshareable instance", %{admin_conn: admin_conn} do
261 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
262 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
264 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
265 json(%{metadata: %{features: []}})
269 |> put_req_header("content-type", "multipart/form-data")
271 "/api/pleroma/emoji/packs/download",
273 url: "https://old-instance",
278 |> json_response_and_validate_schema(500) == %{
279 "error" => "The requested instance does not support sharing emoji packs"
283 test "checksum fail", %{admin_conn: admin_conn} do
285 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
286 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
288 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
289 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
293 url: "https://example.com/api/pleroma/emoji/pack?name=pack_bad_sha"
295 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
296 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
300 url: "https://example.com/api/pleroma/emoji/packs/archive?name=pack_bad_sha"
304 body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
309 |> put_req_header("content-type", "multipart/form-data")
310 |> post("/api/pleroma/emoji/packs/download", %{
311 url: "https://example.com",
312 name: "pack_bad_sha",
315 |> json_response_and_validate_schema(:internal_server_error) == %{
316 "error" => "SHA256 for the pack doesn't match the one sent by the server"
320 test "other error", %{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/pleroma/emoji/pack?name=test_pack"
332 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
333 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
337 |> put_req_header("content-type", "multipart/form-data")
338 |> post("/api/pleroma/emoji/packs/download", %{
339 url: "https://example.com",
343 |> json_response_and_validate_schema(:internal_server_error) == %{
345 "The pack was not set as shared and there is no fallback src to download from"
350 describe "PATCH/update /api/pleroma/emoji/pack?name=:name" do
352 pack_file = "#{@emoji_path}/test_pack/pack.json"
353 original_content = File.read!(pack_file)
356 File.write!(pack_file, original_content)
360 pack_file: pack_file,
362 "license" => "Test license changed",
363 "homepage" => "https://pleroma.social",
364 "description" => "Test description",
365 "share-files" => false
369 test "returns error when file system not writable", %{admin_conn: conn} = ctx do
371 {File, [:passthrough], [stat: fn _ -> {:error, :eacces} end]}
374 |> put_req_header("content-type", "multipart/form-data")
376 "/api/pleroma/emoji/pack?name=test_pack",
377 %{"metadata" => ctx[:new_data]}
379 |> json_response_and_validate_schema(500)
383 test "for a pack without a fallback source", ctx do
384 assert ctx[:admin_conn]
385 |> put_req_header("content-type", "multipart/form-data")
386 |> patch("/api/pleroma/emoji/pack?name=test_pack", %{
387 "metadata" => ctx[:new_data]
389 |> json_response_and_validate_schema(200) == ctx[:new_data]
391 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
394 test "for a pack with a fallback source", ctx do
398 url: "https://nonshared-pack"
400 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
403 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
408 "fallback-src-sha256",
409 "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
412 assert ctx[:admin_conn]
413 |> put_req_header("content-type", "multipart/form-data")
414 |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
415 |> json_response_and_validate_schema(200) == new_data_with_sha
417 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
420 test "when the fallback source doesn't have all the files", ctx do
424 url: "https://nonshared-pack"
426 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
430 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
432 assert ctx[:admin_conn]
433 |> put_req_header("content-type", "multipart/form-data")
434 |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
435 |> json_response_and_validate_schema(:bad_request) == %{
436 "error" => "The fallback archive does not have all files specified in pack.json"
441 describe "POST/DELETE /api/pleroma/emoji/pack?name=:name" do
442 test "returns an error on creates pack when file system not writable", %{
443 admin_conn: admin_conn
445 path_pack = Path.join(@emoji_path, "test_pack")
448 {File, [:passthrough], [mkdir: fn ^path_pack -> {:error, :eacces} end]}
451 |> post("/api/pleroma/emoji/pack?name=test_pack")
452 |> json_response_and_validate_schema(500) == %{
454 "Unexpected error occurred while creating pack. (POSIX error: Permission denied)"
459 test "returns an error on deletes pack when the file system is not writable", %{
460 admin_conn: admin_conn
462 path_pack = Path.join(@emoji_path, "test_emoji_pack")
465 {:ok, _pack} = Pleroma.Emoji.Pack.create("test_emoji_pack")
468 {File, [:passthrough], [rm_rf: fn ^path_pack -> {:error, :eacces, path_pack} end]}
471 |> delete("/api/pleroma/emoji/pack?name=test_emoji_pack")
472 |> json_response_and_validate_schema(500) == %{
474 "Couldn't delete the pack test_emoji_pack (POSIX error: Permission denied)"
478 File.rm_rf(path_pack)
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"