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]),
16 clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
18 test "shared & non-shared pack information in list_packs is ok" do
20 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
22 assert Map.has_key?(resp, "test_pack")
24 pack = resp["test_pack"]
26 assert Map.has_key?(pack["pack"], "download-sha256")
27 assert pack["pack"]["can-download"]
29 assert pack["files"] == %{"blank" => "blank.png"}
33 assert Map.has_key?(resp, "test_pack_nonshared")
35 pack = resp["test_pack_nonshared"]
37 refute pack["pack"]["shared"]
38 refute pack["pack"]["can-download"]
41 test "listing remote packs" do
42 admin = insert(:user, is_admin: true)
43 %{conn: conn} = oauth_access(["admin:write"], user: admin)
47 |> get(emoji_api_path(conn, :list_packs))
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)
132 |> assign(:user, admin)
133 |> assign(:token, insert(:oauth_admin_token, user: admin, scopes: ["admin:write"]))
136 |> put_req_header("content-type", "application/json")
143 instance_address: "https://old-instance",
144 pack_name: "test_pack",
149 |> json_response(500))["error"] =~ "does not support"
152 |> put_req_header("content-type", "application/json")
159 instance_address: "https://example.com",
160 pack_name: "test_pack",
165 |> json_response(200) == "ok"
167 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
168 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
171 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
172 |> json_response(200) == "ok"
174 refute File.exists?("#{@emoji_dir_path}/test_pack2")
176 # non-shared, downloaded from the fallback URL
179 |> put_req_header("content-type", "application/json")
186 instance_address: "https://example.com",
187 pack_name: "test_pack_nonshared",
188 as: "test_pack_nonshared2"
192 |> json_response(200) == "ok"
194 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
195 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
198 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
199 |> json_response(200) == "ok"
201 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
204 describe "updating pack metadata" do
206 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
207 original_content = File.read!(pack_file)
210 File.write!(pack_file, original_content)
213 admin = insert(:user, is_admin: true)
214 %{conn: conn} = oauth_access(["admin:write"], user: admin)
219 pack_file: pack_file,
221 "license" => "Test license changed",
222 "homepage" => "https://pleroma.social",
223 "description" => "Test description",
224 "share-files" => false
228 test "for a pack without a fallback source", ctx do
233 emoji_api_path(conn, :update_metadata, "test_pack"),
235 "new_data" => ctx[:new_data]
238 |> json_response(200) == ctx[:new_data]
240 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
243 test "for a pack with a fallback source", ctx do
247 url: "https://nonshared-pack"
249 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
252 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
257 "fallback-src-sha256",
258 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
265 emoji_api_path(conn, :update_metadata, "test_pack"),
267 "new_data" => new_data
270 |> json_response(200) == new_data_with_sha
272 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
275 test "when the fallback source doesn't have all the files", ctx do
279 url: "https://nonshared-pack"
281 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
285 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
291 emoji_api_path(conn, :update_metadata, "test_pack"),
293 "new_data" => new_data
296 |> json_response(:bad_request))["error"] =~ "does not have all"
300 test "updating pack files" do
301 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
302 original_content = File.read!(pack_file)
305 File.write!(pack_file, original_content)
307 File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
308 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
309 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
312 admin = insert(:user, is_admin: true)
313 %{conn: conn} = oauth_access(["admin:write"], user: admin)
317 "shortcode" => "blank",
318 "filename" => "dir/blank.png",
319 "file" => %Plug.Upload{
320 filename: "blank.png",
321 path: "#{@emoji_dir_path}/test_pack/blank.png"
325 different_name = %{same_name | "shortcode" => "blank_2"}
328 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
329 |> json_response(:conflict))["error"] =~ "already exists"
332 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
333 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
335 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
338 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
339 "action" => "update",
340 "shortcode" => "blank_2",
341 "new_shortcode" => "blank_3",
342 "new_filename" => "dir_2/blank_3.png"
344 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
346 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
347 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
350 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
351 "action" => "remove",
352 "shortcode" => "blank_3"
354 |> json_response(200) == %{"blank" => "blank.png"}
356 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
361 url: "https://test-blank/blank_url.png"
363 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
366 # The name should be inferred from the URL ending
369 "shortcode" => "blank_url",
370 "file" => "https://test-blank/blank_url.png"
374 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
375 |> json_response(200) == %{
376 "blank" => "blank.png",
377 "blank_url" => "blank_url.png"
380 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
383 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
384 "action" => "remove",
385 "shortcode" => "blank_url"
387 |> json_response(200) == %{"blank" => "blank.png"}
389 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
392 test "creating and deleting a pack" do
394 File.rm_rf!("#{@emoji_dir_path}/test_created")
397 admin = insert(:user, is_admin: true)
398 %{conn: conn} = oauth_access(["admin:write"], user: admin)
401 |> put_req_header("content-type", "application/json")
409 |> json_response(200) == "ok"
411 assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
413 assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
419 |> delete(emoji_api_path(conn, :delete, "test_created"))
420 |> json_response(200) == "ok"
422 refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
425 test "filesystem import" do
427 File.rm!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt")
428 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
432 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
434 refute Map.has_key?(resp, "test_pack_for_import")
436 admin = insert(:user, is_admin: true)
437 %{conn: conn} = oauth_access(["admin:write"], user: admin)
440 |> post(emoji_api_path(conn, :import_from_fs))
441 |> json_response(200) == ["test_pack_for_import"]
443 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
444 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
446 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
447 refute File.exists?("#{@emoji_dir_path}/test_pack_for_import/pack.json")
449 emoji_txt_content = "blank, blank.png, Fun\n\nblank2, blank.png"
451 File.write!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
454 |> post(emoji_api_path(conn, :import_from_fs))
455 |> json_response(200) == ["test_pack_for_import"]
457 resp = build_conn() |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
459 assert resp["test_pack_for_import"]["files"] == %{
460 "blank" => "blank.png",
461 "blank2" => "blank.png"