1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
10 import Pleroma.Factory
12 @emoji_dir_path Path.join(
13 Pleroma.Config.get!([:instance, :static_dir]),
17 clear_config([:auth, :enforce_oauth_admin_scope_usage]) do
18 Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], false)
21 test "shared & non-shared pack information in list_packs is ok" do
23 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
25 assert Map.has_key?(resp, "test_pack")
27 pack = resp["test_pack"]
29 assert Map.has_key?(pack["pack"], "download-sha256")
30 assert pack["pack"]["can-download"]
32 assert pack["files"] == %{"blank" => "blank.png"}
36 assert Map.has_key?(resp, "test_pack_nonshared")
38 pack = resp["test_pack_nonshared"]
40 refute pack["pack"]["shared"]
41 refute pack["pack"]["can-download"]
44 test "listing remote packs" do
45 admin = insert(:user, is_admin: true)
46 conn = build_conn() |> assign(:user, admin)
48 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
51 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
52 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
54 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
55 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
57 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
62 |> post(emoji_api_path(conn, :list_from), %{instance_address: "https://example.com"})
63 |> json_response(200) == resp
66 test "downloading a shared pack from download_shared" do
71 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
74 {:ok, arch} = :zip.unzip(resp, [:memory])
76 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
77 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
80 test "downloading shared & unshared packs from another instance via download_from, deleting them" do
82 File.rm_rf!("#{@emoji_dir_path}/test_pack2")
83 File.rm_rf!("#{@emoji_dir_path}/test_pack_nonshared2")
87 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
88 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
90 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
91 json(%{metadata: %{features: []}})
93 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
94 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
96 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
97 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
101 url: "https://example.com/api/pleroma/emoji/packs/list"
106 |> get(emoji_api_path(conn, :list_packs))
107 |> json_response(200)
112 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
117 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
123 url: "https://nonshared-pack"
125 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
128 admin = insert(:user, is_admin: true)
130 conn = build_conn() |> assign(:user, admin)
133 |> put_req_header("content-type", "application/json")
140 instance_address: "https://old-instance",
141 pack_name: "test_pack",
146 |> json_response(500))["error"] =~ "does not support"
149 |> put_req_header("content-type", "application/json")
156 instance_address: "https://example.com",
157 pack_name: "test_pack",
162 |> json_response(200) == "ok"
164 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
165 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
168 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
169 |> json_response(200) == "ok"
171 refute File.exists?("#{@emoji_dir_path}/test_pack2")
173 # non-shared, downloaded from the fallback URL
175 conn = build_conn() |> assign(:user, admin)
178 |> put_req_header("content-type", "application/json")
185 instance_address: "https://example.com",
186 pack_name: "test_pack_nonshared",
187 as: "test_pack_nonshared2"
191 |> json_response(200) == "ok"
193 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
194 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
197 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
198 |> json_response(200) == "ok"
200 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
203 describe "updating pack metadata" do
205 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
206 original_content = File.read!(pack_file)
209 File.write!(pack_file, original_content)
213 admin: insert(:user, is_admin: true),
214 pack_file: pack_file,
216 "license" => "Test license changed",
217 "homepage" => "https://pleroma.social",
218 "description" => "Test description",
219 "share-files" => false
223 test "for a pack without a fallback source", ctx do
227 |> assign(:user, ctx[:admin])
229 emoji_api_path(conn, :update_metadata, "test_pack"),
231 "new_data" => ctx[:new_data]
234 |> json_response(200) == ctx[:new_data]
236 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
239 test "for a pack with a fallback source", ctx do
243 url: "https://nonshared-pack"
245 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
248 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
253 "fallback-src-sha256",
254 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
260 |> assign(:user, ctx[:admin])
262 emoji_api_path(conn, :update_metadata, "test_pack"),
264 "new_data" => new_data
267 |> json_response(200) == new_data_with_sha
269 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
272 test "when the fallback source doesn't have all the files", ctx do
276 url: "https://nonshared-pack"
278 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
282 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
287 |> assign(:user, ctx[:admin])
289 emoji_api_path(conn, :update_metadata, "test_pack"),
291 "new_data" => new_data
294 |> json_response(:bad_request))["error"] =~ "does not have all"
298 test "updating pack files" do
299 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
300 original_content = File.read!(pack_file)
303 File.write!(pack_file, original_content)
305 File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
306 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
307 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
310 admin = insert(:user, is_admin: true)
316 "shortcode" => "blank",
317 "filename" => "dir/blank.png",
318 "file" => %Plug.Upload{
319 filename: "blank.png",
320 path: "#{@emoji_dir_path}/test_pack/blank.png"
324 different_name = %{same_name | "shortcode" => "blank_2"}
326 conn = conn |> assign(:user, admin)
329 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
330 |> json_response(:conflict))["error"] =~ "already exists"
333 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
334 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
336 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
339 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
340 "action" => "update",
341 "shortcode" => "blank_2",
342 "new_shortcode" => "blank_3",
343 "new_filename" => "dir_2/blank_3.png"
345 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
347 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
348 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
351 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
352 "action" => "remove",
353 "shortcode" => "blank_3"
355 |> json_response(200) == %{"blank" => "blank.png"}
357 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
362 url: "https://test-blank/blank_url.png"
364 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
367 # The name should be inferred from the URL ending
370 "shortcode" => "blank_url",
371 "file" => "https://test-blank/blank_url.png"
375 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
376 |> json_response(200) == %{
377 "blank" => "blank.png",
378 "blank_url" => "blank_url.png"
381 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
384 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
385 "action" => "remove",
386 "shortcode" => "blank_url"
388 |> json_response(200) == %{"blank" => "blank.png"}
390 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
393 test "creating and deleting a pack" do
395 File.rm_rf!("#{@emoji_dir_path}/test_created")
398 admin = insert(:user, is_admin: true)
400 conn = build_conn() |> assign(:user, admin)
403 |> put_req_header("content-type", "application/json")
411 |> json_response(200) == "ok"
413 assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
415 assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
421 |> delete(emoji_api_path(conn, :delete, "test_created"))
422 |> json_response(200) == "ok"
424 refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
427 test "filesystem import" do
429 File.rm!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt")
430 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
434 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
436 refute Map.has_key?(resp, "test_pack_for_import")
438 admin = insert(:user, is_admin: true)
441 |> assign(:user, admin)
442 |> post(emoji_api_path(conn, :import_from_fs))
443 |> json_response(200) == ["test_pack_for_import"]
445 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
446 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
448 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
449 refute File.exists?("#{@emoji_dir_path}/test_pack_for_import/pack.json")
451 emoji_txt_content = "blank, blank.png, Fun\n\nblank2, blank.png"
453 File.write!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
456 |> assign(:user, admin)
457 |> post(emoji_api_path(conn, :import_from_fs))
458 |> json_response(200) == ["test_pack_for_import"]
460 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
462 assert resp["test_pack_for_import"]["files"] == %{
463 "blank" => "blank.png",
464 "blank2" => "blank.png"