1 defmodule Pleroma.Web.PleromaAPI.EmojiAPIControllerTest do
2 use Pleroma.Web.ConnCase
8 @emoji_dir_path Path.join(
9 Pleroma.Config.get!([:instance, :static_dir]),
13 test "shared & non-shared pack information in list_packs is ok" do
15 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
17 assert Map.has_key?(resp, "test_pack")
19 pack = resp["test_pack"]
21 assert Map.has_key?(pack["pack"], "download-sha256")
22 assert pack["pack"]["can-download"]
24 assert pack["files"] == %{"blank" => "blank.png"}
28 assert Map.has_key?(resp, "test_pack_nonshared")
30 pack = resp["test_pack_nonshared"]
32 refute pack["pack"]["shared"]
33 refute pack["pack"]["can-download"]
36 test "downloading a shared pack from download_shared" do
41 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
44 {:ok, arch} = :zip.unzip(resp, [:memory])
46 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
47 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
50 test "downloading shared & unshared packs from another instance via download_from, deleting them" do
52 File.rm_rf!("#{@emoji_dir_path}/test_pack2")
53 File.rm_rf!("#{@emoji_dir_path}/test_pack_nonshared2")
57 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
58 json(%{metadata: %{features: []}})
60 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
61 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
65 url: "https://example.com/api/pleroma/emoji/packs/list"
70 |> get(emoji_api_path(conn, :list_packs))
76 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
81 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
87 url: "https://nonshared-pack"
89 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
92 admin = insert(:user, info: %{is_admin: true})
94 conn = build_conn() |> assign(:user, admin)
97 |> put_req_header("content-type", "application/json")
104 instance_address: "https://old-instance",
105 pack_name: "test_pack",
110 |> json_response(500))["error"] =~ "does not support"
113 |> put_req_header("content-type", "application/json")
120 instance_address: "https://example.com",
121 pack_name: "test_pack",
126 |> json_response(200) == "ok"
128 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
129 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
132 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
133 |> json_response(200) == "ok"
135 refute File.exists?("#{@emoji_dir_path}/test_pack2")
137 # non-shared, downloaded from the fallback URL
139 conn = build_conn() |> assign(:user, admin)
142 |> put_req_header("content-type", "application/json")
149 instance_address: "https://example.com",
150 pack_name: "test_pack_nonshared",
151 as: "test_pack_nonshared2"
155 |> json_response(200) == "ok"
157 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
158 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
161 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
162 |> json_response(200) == "ok"
164 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
167 describe "updating pack metadata" do
169 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
170 original_content = File.read!(pack_file)
173 File.write!(pack_file, original_content)
177 admin: insert(:user, info: %{is_admin: true}),
178 pack_file: pack_file,
180 "license" => "Test license changed",
181 "homepage" => "https://pleroma.social",
182 "description" => "Test description",
183 "share-files" => false
187 test "for a pack without a fallback source", ctx do
191 |> assign(:user, ctx[:admin])
193 emoji_api_path(conn, :update_metadata, "test_pack"),
195 "new_data" => ctx[:new_data]
198 |> json_response(200) == ctx[:new_data]
200 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
203 test "for a pack with a fallback source", ctx do
207 url: "https://nonshared-pack"
209 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
212 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
217 "fallback-src-sha256",
218 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
224 |> assign(:user, ctx[:admin])
226 emoji_api_path(conn, :update_metadata, "test_pack"),
228 "new_data" => new_data
231 |> json_response(200) == new_data_with_sha
233 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
236 test "when the fallback source doesn't have all the files", ctx do
240 url: "https://nonshared-pack"
242 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
246 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
251 |> assign(:user, ctx[:admin])
253 emoji_api_path(conn, :update_metadata, "test_pack"),
255 "new_data" => new_data
258 |> json_response(:bad_request))["error"] =~ "does not have all"
262 test "updating pack files" do
263 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
264 original_content = File.read!(pack_file)
267 File.write!(pack_file, original_content)
269 File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
270 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
271 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
274 admin = insert(:user, info: %{is_admin: true})
280 "shortcode" => "blank",
281 "filename" => "dir/blank.png",
282 "file" => %Plug.Upload{
283 filename: "blank.png",
284 path: "#{@emoji_dir_path}/test_pack/blank.png"
288 different_name = %{same_name | "shortcode" => "blank_2"}
290 conn = conn |> assign(:user, admin)
293 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
294 |> json_response(:conflict))["error"] =~ "already exists"
297 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
298 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
300 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
303 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
304 "action" => "update",
305 "shortcode" => "blank_2",
306 "new_shortcode" => "blank_3",
307 "new_filename" => "dir_2/blank_3.png"
309 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
311 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
312 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
315 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
316 "action" => "remove",
317 "shortcode" => "blank_3"
319 |> json_response(200) == %{"blank" => "blank.png"}
321 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
326 url: "https://test-blank/blank_url.png"
328 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
331 # The name should be inferred from the URL ending
334 "shortcode" => "blank_url",
335 "file" => "https://test-blank/blank_url.png"
339 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
340 |> json_response(200) == %{
341 "blank" => "blank.png",
342 "blank_url" => "blank_url.png"
345 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
348 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
349 "action" => "remove",
350 "shortcode" => "blank_url"
352 |> json_response(200) == %{"blank" => "blank.png"}
354 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
357 test "creating and deleting a pack" do
359 File.rm_rf!("#{@emoji_dir_path}/test_created")
362 admin = insert(:user, info: %{is_admin: true})
364 conn = build_conn() |> assign(:user, admin)
367 |> put_req_header("content-type", "application/json")
375 |> json_response(200) == "ok"
377 assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
379 assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
385 |> delete(emoji_api_path(conn, :delete, "test_created"))
386 |> json_response(200) == "ok"
388 refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
391 test "filesystem import" do
393 File.rm!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt")
394 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
398 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
400 refute Map.has_key?(resp, "test_pack_for_import")
402 admin = insert(:user, info: %{is_admin: true})
405 |> assign(:user, admin)
406 |> post(emoji_api_path(conn, :import_from_fs))
407 |> json_response(200) == ["test_pack_for_import"]
409 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
410 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
412 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
413 refute File.exists?("#{@emoji_dir_path}/test_pack_for_import/pack.json")
415 emoji_txt_content = "blank, blank.png, Fun\n\nblank2, blank.png"
417 File.write!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
420 |> assign(:user, admin)
421 |> post(emoji_api_path(conn, :import_from_fs))
422 |> json_response(200) == ["test_pack_for_import"]
424 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
426 assert resp["test_pack_for_import"]["files"] == %{
427 "blank" => "blank.png",
428 "blank2" => "blank.png"