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
45 |> get(emoji_api_path(conn, :list_packs))
49 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
50 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
52 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
53 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
55 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
60 |> post(emoji_api_path(conn, :list_from), %{instance_address: "https://example.com"})
61 |> json_response(200) == resp
64 test "downloading a shared pack from download_shared" do
69 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
72 {:ok, arch} = :zip.unzip(resp, [:memory])
74 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
75 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
78 test "downloading shared & unshared packs from another instance, deleting them" do
80 File.rm_rf!("#{@emoji_dir_path}/test_pack2")
81 File.rm_rf!("#{@emoji_dir_path}/test_pack_nonshared2")
85 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
86 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
88 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
89 json(%{metadata: %{features: []}})
91 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
92 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
94 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
95 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
99 url: "https://example.com/api/pleroma/emoji/packs/list"
104 |> get(emoji_api_path(conn, :list_packs))
105 |> json_response(200)
110 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
115 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
121 url: "https://nonshared-pack"
123 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
126 admin = insert(:user, is_admin: true)
130 |> assign(:user, admin)
131 |> assign(:token, insert(:oauth_admin_token, user: admin, scopes: ["admin:write"]))
134 |> put_req_header("content-type", "application/json")
141 instance_address: "https://old-instance",
142 pack_name: "test_pack",
147 |> json_response(500))["error"] =~ "does not support"
150 |> put_req_header("content-type", "application/json")
157 instance_address: "https://example.com",
158 pack_name: "test_pack",
163 |> json_response(200) == "ok"
165 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
166 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
169 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
170 |> json_response(200) == "ok"
172 refute File.exists?("#{@emoji_dir_path}/test_pack2")
174 # non-shared, downloaded from the fallback URL
177 |> put_req_header("content-type", "application/json")
184 instance_address: "https://example.com",
185 pack_name: "test_pack_nonshared",
186 as: "test_pack_nonshared2"
190 |> json_response(200) == "ok"
192 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
193 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
196 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
197 |> json_response(200) == "ok"
199 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
202 describe "updating pack metadata" do
204 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
205 original_content = File.read!(pack_file)
208 File.write!(pack_file, original_content)
211 admin = insert(:user, is_admin: true)
212 %{conn: conn} = oauth_access(["admin:write"], user: admin)
217 pack_file: pack_file,
219 "license" => "Test license changed",
220 "homepage" => "https://pleroma.social",
221 "description" => "Test description",
222 "share-files" => false
226 test "for a pack without a fallback source", ctx do
231 emoji_api_path(conn, :update_metadata, "test_pack"),
233 "new_data" => ctx[:new_data]
236 |> json_response(200) == ctx[:new_data]
238 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
241 test "for a pack with a fallback source", ctx do
245 url: "https://nonshared-pack"
247 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
250 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
255 "fallback-src-sha256",
256 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
263 emoji_api_path(conn, :update_metadata, "test_pack"),
265 "new_data" => new_data
268 |> json_response(200) == new_data_with_sha
270 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
273 test "when the fallback source doesn't have all the files", ctx do
277 url: "https://nonshared-pack"
279 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
283 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
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)
311 %{conn: conn} = oauth_access(["admin:write"], user: admin)
315 "shortcode" => "blank",
316 "filename" => "dir/blank.png",
317 "file" => %Plug.Upload{
318 filename: "blank.png",
319 path: "#{@emoji_dir_path}/test_pack/blank.png"
323 different_name = %{same_name | "shortcode" => "blank_2"}
326 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
327 |> json_response(:conflict))["error"] =~ "already exists"
330 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
331 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
333 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
336 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
337 "action" => "update",
338 "shortcode" => "blank_2",
339 "new_shortcode" => "blank_3",
340 "new_filename" => "dir_2/blank_3.png"
342 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
344 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
345 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
348 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
349 "action" => "remove",
350 "shortcode" => "blank_3"
352 |> json_response(200) == %{"blank" => "blank.png"}
354 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
359 url: "https://test-blank/blank_url.png"
361 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
364 # The name should be inferred from the URL ending
367 "shortcode" => "blank_url",
368 "file" => "https://test-blank/blank_url.png"
372 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
373 |> json_response(200) == %{
374 "blank" => "blank.png",
375 "blank_url" => "blank_url.png"
378 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
381 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
382 "action" => "remove",
383 "shortcode" => "blank_url"
385 |> json_response(200) == %{"blank" => "blank.png"}
387 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
390 test "creating and deleting a pack" do
392 File.rm_rf!("#{@emoji_dir_path}/test_created")
395 admin = insert(:user, is_admin: true)
396 %{conn: conn} = oauth_access(["admin:write"], user: admin)
399 |> put_req_header("content-type", "application/json")
407 |> json_response(200) == "ok"
409 assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
411 assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
417 |> delete(emoji_api_path(conn, :delete, "test_created"))
418 |> json_response(200) == "ok"
420 refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
423 test "filesystem import" do
425 File.rm!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt")
426 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
430 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
432 refute Map.has_key?(resp, "test_pack_for_import")
434 admin = insert(:user, is_admin: true)
435 %{conn: conn} = oauth_access(["admin:write"], user: admin)
438 |> post(emoji_api_path(conn, :import_from_fs))
439 |> json_response(200) == ["test_pack_for_import"]
441 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
442 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
444 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
445 refute File.exists?("#{@emoji_dir_path}/test_pack_for_import/pack.json")
447 emoji_txt_content = "blank, blank.png, Fun\n\nblank2, blank.png"
449 File.write!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
452 |> post(emoji_api_path(conn, :import_from_fs))
453 |> json_response(200) == ["test_pack_for_import"]
455 resp = build_conn() |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
457 assert resp["test_pack_for_import"]["files"] == %{
458 "blank" => "blank.png",
459 "blank2" => "blank.png"