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: conn} = oauth_access(["admin:write"], user: admin)
50 |> get(emoji_api_path(conn, :list_packs))
54 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
55 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
57 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
58 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
60 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
65 |> post(emoji_api_path(conn, :list_from), %{instance_address: "https://example.com"})
66 |> json_response(200) == resp
69 test "downloading a shared pack from download_shared" do
74 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
77 {:ok, arch} = :zip.unzip(resp, [:memory])
79 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
80 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
83 test "downloading shared & unshared packs from another instance via download_from, deleting them" do
85 File.rm_rf!("#{@emoji_dir_path}/test_pack2")
86 File.rm_rf!("#{@emoji_dir_path}/test_pack_nonshared2")
90 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
91 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
93 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
94 json(%{metadata: %{features: []}})
96 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
97 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
99 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
100 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
104 url: "https://example.com/api/pleroma/emoji/packs/list"
109 |> get(emoji_api_path(conn, :list_packs))
110 |> json_response(200)
115 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
120 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
126 url: "https://nonshared-pack"
128 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
131 admin = insert(:user, is_admin: true)
135 |> assign(:user, admin)
136 |> assign(:token, insert(:oauth_admin_token, user: admin, scopes: ["admin:write"]))
139 |> put_req_header("content-type", "application/json")
146 instance_address: "https://old-instance",
147 pack_name: "test_pack",
152 |> json_response(500))["error"] =~ "does not support"
155 |> put_req_header("content-type", "application/json")
162 instance_address: "https://example.com",
163 pack_name: "test_pack",
168 |> json_response(200) == "ok"
170 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
171 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
174 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
175 |> json_response(200) == "ok"
177 refute File.exists?("#{@emoji_dir_path}/test_pack2")
179 # non-shared, downloaded from the fallback URL
182 |> put_req_header("content-type", "application/json")
189 instance_address: "https://example.com",
190 pack_name: "test_pack_nonshared",
191 as: "test_pack_nonshared2"
195 |> json_response(200) == "ok"
197 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
198 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
201 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
202 |> json_response(200) == "ok"
204 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
207 describe "updating pack metadata" do
209 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
210 original_content = File.read!(pack_file)
213 File.write!(pack_file, original_content)
216 admin = insert(:user, is_admin: true)
217 %{conn: conn} = oauth_access(["admin:write"], user: admin)
222 pack_file: pack_file,
224 "license" => "Test license changed",
225 "homepage" => "https://pleroma.social",
226 "description" => "Test description",
227 "share-files" => false
231 test "for a pack without a fallback source", ctx do
236 emoji_api_path(conn, :update_metadata, "test_pack"),
238 "new_data" => ctx[:new_data]
241 |> json_response(200) == ctx[:new_data]
243 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
246 test "for a pack with a fallback source", ctx do
250 url: "https://nonshared-pack"
252 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
255 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
260 "fallback-src-sha256",
261 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
268 emoji_api_path(conn, :update_metadata, "test_pack"),
270 "new_data" => new_data
273 |> json_response(200) == new_data_with_sha
275 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
278 test "when the fallback source doesn't have all the files", ctx do
282 url: "https://nonshared-pack"
284 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
288 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
294 emoji_api_path(conn, :update_metadata, "test_pack"),
296 "new_data" => new_data
299 |> json_response(:bad_request))["error"] =~ "does not have all"
303 test "updating pack files" do
304 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
305 original_content = File.read!(pack_file)
308 File.write!(pack_file, original_content)
310 File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
311 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
312 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
315 admin = insert(:user, is_admin: true)
316 %{conn: conn} = oauth_access(["admin:write"], user: admin)
320 "shortcode" => "blank",
321 "filename" => "dir/blank.png",
322 "file" => %Plug.Upload{
323 filename: "blank.png",
324 path: "#{@emoji_dir_path}/test_pack/blank.png"
328 different_name = %{same_name | "shortcode" => "blank_2"}
331 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
332 |> json_response(:conflict))["error"] =~ "already exists"
335 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
336 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
338 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
341 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
342 "action" => "update",
343 "shortcode" => "blank_2",
344 "new_shortcode" => "blank_3",
345 "new_filename" => "dir_2/blank_3.png"
347 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
349 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
350 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
353 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
354 "action" => "remove",
355 "shortcode" => "blank_3"
357 |> json_response(200) == %{"blank" => "blank.png"}
359 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
364 url: "https://test-blank/blank_url.png"
366 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
369 # The name should be inferred from the URL ending
372 "shortcode" => "blank_url",
373 "file" => "https://test-blank/blank_url.png"
377 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
378 |> json_response(200) == %{
379 "blank" => "blank.png",
380 "blank_url" => "blank_url.png"
383 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
386 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
387 "action" => "remove",
388 "shortcode" => "blank_url"
390 |> json_response(200) == %{"blank" => "blank.png"}
392 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
395 test "creating and deleting a pack" do
397 File.rm_rf!("#{@emoji_dir_path}/test_created")
400 admin = insert(:user, is_admin: true)
401 %{conn: conn} = oauth_access(["admin:write"], user: admin)
404 |> put_req_header("content-type", "application/json")
412 |> json_response(200) == "ok"
414 assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
416 assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
422 |> delete(emoji_api_path(conn, :delete, "test_created"))
423 |> json_response(200) == "ok"
425 refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
428 test "filesystem import" do
430 File.rm!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt")
431 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
435 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
437 refute Map.has_key?(resp, "test_pack_for_import")
439 admin = insert(:user, is_admin: true)
440 %{conn: conn} = oauth_access(["admin:write"], user: admin)
443 |> post(emoji_api_path(conn, :import_from_fs))
444 |> json_response(200) == ["test_pack_for_import"]
446 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
447 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
449 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
450 refute File.exists?("#{@emoji_dir_path}/test_pack_for_import/pack.json")
452 emoji_txt_content = "blank, blank.png, Fun\n\nblank2, blank.png"
454 File.write!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
457 |> post(emoji_api_path(conn, :import_from_fs))
458 |> json_response(200) == ["test_pack_for_import"]
460 resp = build_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"