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.EmojiAPIControllerTest do
6 use Pleroma.Web.ConnCase
11 @emoji_dir_path Path.join(
12 Pleroma.Config.get!([:instance, :static_dir]),
15 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
17 test "shared & non-shared pack information in list_packs is ok" do
19 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
21 assert Map.has_key?(resp, "test_pack")
23 pack = resp["test_pack"]
25 assert Map.has_key?(pack["pack"], "download-sha256")
26 assert pack["pack"]["can-download"]
28 assert pack["files"] == %{"blank" => "blank.png"}
32 assert Map.has_key?(resp, "test_pack_nonshared")
34 pack = resp["test_pack_nonshared"]
36 refute pack["pack"]["shared"]
37 refute pack["pack"]["can-download"]
40 test "listing remote packs" do
41 admin = insert(:user, is_admin: true)
42 %{conn: conn} = oauth_access(["admin:write"], user: admin)
46 |> get(emoji_api_path(conn, :list_packs))
50 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
51 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
53 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
54 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
56 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
61 |> post(emoji_api_path(conn, :list_from), %{instance_address: "https://example.com"})
62 |> json_response(200) == resp
65 test "downloading a shared pack from download_shared" do
70 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
73 {:ok, arch} = :zip.unzip(resp, [:memory])
75 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
76 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
79 test "downloading shared & unshared packs from another instance via download_from, deleting them" do
81 File.rm_rf!("#{@emoji_dir_path}/test_pack2")
82 File.rm_rf!("#{@emoji_dir_path}/test_pack_nonshared2")
86 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
87 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
89 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
90 json(%{metadata: %{features: []}})
92 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
93 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
95 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
96 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
100 url: "https://example.com/api/pleroma/emoji/packs/list"
105 |> get(emoji_api_path(conn, :list_packs))
106 |> json_response(200)
111 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
116 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
122 url: "https://nonshared-pack"
124 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
127 admin = insert(:user, is_admin: true)
131 |> assign(:user, admin)
132 |> assign(:token, insert(:oauth_admin_token, user: admin, scopes: ["admin:write"]))
135 |> put_req_header("content-type", "application/json")
142 instance_address: "https://old-instance",
143 pack_name: "test_pack",
148 |> json_response(500))["error"] =~ "does not support"
151 |> put_req_header("content-type", "application/json")
158 instance_address: "https://example.com",
159 pack_name: "test_pack",
164 |> json_response(200) == "ok"
166 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
167 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
170 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
171 |> json_response(200) == "ok"
173 refute File.exists?("#{@emoji_dir_path}/test_pack2")
175 # non-shared, downloaded from the fallback URL
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)
212 admin = insert(:user, is_admin: true)
213 %{conn: conn} = oauth_access(["admin:write"], user: admin)
218 pack_file: pack_file,
220 "license" => "Test license changed",
221 "homepage" => "https://pleroma.social",
222 "description" => "Test description",
223 "share-files" => false
227 test "for a pack without a fallback source", ctx do
232 emoji_api_path(conn, :update_metadata, "test_pack"),
234 "new_data" => ctx[:new_data]
237 |> json_response(200) == ctx[:new_data]
239 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
242 test "for a pack with a fallback source", ctx do
246 url: "https://nonshared-pack"
248 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
251 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
256 "fallback-src-sha256",
257 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
264 emoji_api_path(conn, :update_metadata, "test_pack"),
266 "new_data" => new_data
269 |> json_response(200) == new_data_with_sha
271 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
274 test "when the fallback source doesn't have all the files", ctx do
278 url: "https://nonshared-pack"
280 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
284 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
290 emoji_api_path(conn, :update_metadata, "test_pack"),
292 "new_data" => new_data
295 |> json_response(:bad_request))["error"] =~ "does not have all"
299 test "updating pack files" do
300 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
301 original_content = File.read!(pack_file)
304 File.write!(pack_file, original_content)
306 File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
307 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
308 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
311 admin = insert(:user, is_admin: true)
312 %{conn: conn} = oauth_access(["admin:write"], user: admin)
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"}
327 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
328 |> json_response(:conflict))["error"] =~ "already exists"
331 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
332 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
334 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
337 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
338 "action" => "update",
339 "shortcode" => "blank_2",
340 "new_shortcode" => "blank_3",
341 "new_filename" => "dir_2/blank_3.png"
343 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
345 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
346 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
349 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
350 "action" => "remove",
351 "shortcode" => "blank_3"
353 |> json_response(200) == %{"blank" => "blank.png"}
355 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
360 url: "https://test-blank/blank_url.png"
362 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
365 # The name should be inferred from the URL ending
368 "shortcode" => "blank_url",
369 "file" => "https://test-blank/blank_url.png"
373 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
374 |> json_response(200) == %{
375 "blank" => "blank.png",
376 "blank_url" => "blank_url.png"
379 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
382 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
383 "action" => "remove",
384 "shortcode" => "blank_url"
386 |> json_response(200) == %{"blank" => "blank.png"}
388 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
391 test "creating and deleting a pack" do
393 File.rm_rf!("#{@emoji_dir_path}/test_created")
396 admin = insert(:user, is_admin: true)
397 %{conn: conn} = oauth_access(["admin:write"], user: admin)
400 |> put_req_header("content-type", "application/json")
408 |> json_response(200) == "ok"
410 assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
412 assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
418 |> delete(emoji_api_path(conn, :delete, "test_created"))
419 |> json_response(200) == "ok"
421 refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
424 test "filesystem import" do
426 File.rm!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt")
427 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
431 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
433 refute Map.has_key?(resp, "test_pack_for_import")
435 admin = insert(:user, is_admin: true)
436 %{conn: conn} = oauth_access(["admin:write"], user: admin)
439 |> post(emoji_api_path(conn, :import_from_fs))
440 |> json_response(200) == ["test_pack_for_import"]
442 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
443 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
445 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
446 refute File.exists?("#{@emoji_dir_path}/test_pack_for_import/pack.json")
448 emoji_txt_content = "blank, blank.png, Fun\n\nblank2, blank.png"
450 File.write!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
453 |> post(emoji_api_path(conn, :import_from_fs))
454 |> json_response(200) == ["test_pack_for_import"]
456 resp = build_conn() |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
458 assert resp["test_pack_for_import"]["files"] == %{
459 "blank" => "blank.png",
460 "blank2" => "blank.png"