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 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, info: %{is_admin: true})
42 conn = build_conn() |> assign(:user, admin)
44 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
47 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
48 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
50 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
51 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
53 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
58 |> post(emoji_api_path(conn, :list_from), %{instance_address: "https://example.com"})
59 |> json_response(200) == resp
62 test "downloading a shared pack from download_shared" do
67 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
70 {:ok, arch} = :zip.unzip(resp, [:memory])
72 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
73 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
76 test "downloading shared & unshared packs from another instance via download_from, deleting them" do
78 File.rm_rf!("#{@emoji_dir_path}/test_pack2")
79 File.rm_rf!("#{@emoji_dir_path}/test_pack_nonshared2")
83 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
84 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
86 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
87 json(%{metadata: %{features: []}})
89 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
90 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
92 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
93 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
97 url: "https://example.com/api/pleroma/emoji/packs/list"
102 |> get(emoji_api_path(conn, :list_packs))
103 |> json_response(200)
108 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
113 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
119 url: "https://nonshared-pack"
121 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
124 admin = insert(:user, info: %{is_admin: true})
126 conn = build_conn() |> assign(:user, admin)
129 |> put_req_header("content-type", "application/json")
136 instance_address: "https://old-instance",
137 pack_name: "test_pack",
142 |> json_response(500))["error"] =~ "does not support"
145 |> put_req_header("content-type", "application/json")
152 instance_address: "https://example.com",
153 pack_name: "test_pack",
158 |> json_response(200) == "ok"
160 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
161 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
164 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
165 |> json_response(200) == "ok"
167 refute File.exists?("#{@emoji_dir_path}/test_pack2")
169 # non-shared, downloaded from the fallback URL
171 conn = build_conn() |> assign(:user, admin)
174 |> put_req_header("content-type", "application/json")
181 instance_address: "https://example.com",
182 pack_name: "test_pack_nonshared",
183 as: "test_pack_nonshared2"
187 |> json_response(200) == "ok"
189 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
190 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
193 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
194 |> json_response(200) == "ok"
196 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
199 describe "updating pack metadata" do
201 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
202 original_content = File.read!(pack_file)
205 File.write!(pack_file, original_content)
209 admin: insert(:user, info: %{is_admin: true}),
210 pack_file: pack_file,
212 "license" => "Test license changed",
213 "homepage" => "https://pleroma.social",
214 "description" => "Test description",
215 "share-files" => false
219 test "for a pack without a fallback source", ctx do
223 |> assign(:user, ctx[:admin])
225 emoji_api_path(conn, :update_metadata, "test_pack"),
227 "new_data" => ctx[:new_data]
230 |> json_response(200) == ctx[:new_data]
232 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
235 test "for a pack with a fallback source", ctx do
239 url: "https://nonshared-pack"
241 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
244 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
249 "fallback-src-sha256",
250 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
256 |> assign(:user, ctx[:admin])
258 emoji_api_path(conn, :update_metadata, "test_pack"),
260 "new_data" => new_data
263 |> json_response(200) == new_data_with_sha
265 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
268 test "when the fallback source doesn't have all the files", ctx do
272 url: "https://nonshared-pack"
274 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
278 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
283 |> assign(:user, ctx[:admin])
285 emoji_api_path(conn, :update_metadata, "test_pack"),
287 "new_data" => new_data
290 |> json_response(:bad_request))["error"] =~ "does not have all"
294 test "updating pack files" do
295 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
296 original_content = File.read!(pack_file)
299 File.write!(pack_file, original_content)
301 File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
302 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
303 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
306 admin = insert(:user, info: %{is_admin: true})
312 "shortcode" => "blank",
313 "filename" => "dir/blank.png",
314 "file" => %Plug.Upload{
315 filename: "blank.png",
316 path: "#{@emoji_dir_path}/test_pack/blank.png"
320 different_name = %{same_name | "shortcode" => "blank_2"}
322 conn = conn |> assign(:user, admin)
325 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
326 |> json_response(:conflict))["error"] =~ "already exists"
329 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
330 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
332 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
335 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
336 "action" => "update",
337 "shortcode" => "blank_2",
338 "new_shortcode" => "blank_3",
339 "new_filename" => "dir_2/blank_3.png"
341 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
343 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
344 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
347 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
348 "action" => "remove",
349 "shortcode" => "blank_3"
351 |> json_response(200) == %{"blank" => "blank.png"}
353 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
358 url: "https://test-blank/blank_url.png"
360 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
363 # The name should be inferred from the URL ending
366 "shortcode" => "blank_url",
367 "file" => "https://test-blank/blank_url.png"
371 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
372 |> json_response(200) == %{
373 "blank" => "blank.png",
374 "blank_url" => "blank_url.png"
377 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
380 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
381 "action" => "remove",
382 "shortcode" => "blank_url"
384 |> json_response(200) == %{"blank" => "blank.png"}
386 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
389 test "creating and deleting a pack" do
391 File.rm_rf!("#{@emoji_dir_path}/test_created")
394 admin = insert(:user, info: %{is_admin: true})
396 conn = build_conn() |> assign(: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, info: %{is_admin: true})
437 |> assign(: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 |> assign(:user, admin)
453 |> post(emoji_api_path(conn, :import_from_fs))
454 |> json_response(200) == ["test_pack_for_import"]
456 resp = 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"