1 defmodule Pleroma.Web.EmojiAPI.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")
59 url: "https://example.com/api/pleroma/emoji/packs/list"
64 |> get(emoji_api_path(conn, :list_packs))
70 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
75 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
81 url: "https://nonshared-pack"
83 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
86 admin = insert(:user, info: %{is_admin: true})
88 conn = build_conn() |> assign(:user, admin)
91 |> put_req_header("content-type", "application/json")
98 instance_address: "https://example.com",
99 pack_name: "test_pack",
104 |> json_response(200) == "ok"
106 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
107 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
110 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
111 |> json_response(200) == "ok"
113 refute File.exists?("#{@emoji_dir_path}/test_pack2")
115 # non-shared, downloaded from the fallback URL
117 conn = build_conn() |> assign(:user, admin)
120 |> put_req_header("content-type", "application/json")
127 instance_address: "https://example.com",
128 pack_name: "test_pack_nonshared",
129 as: "test_pack_nonshared2"
133 |> json_response(200) == "ok"
135 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
136 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
139 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
140 |> json_response(200) == "ok"
142 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
145 describe "updating pack metadata" do
147 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
148 original_content = File.read!(pack_file)
151 File.write!(pack_file, original_content)
155 admin: insert(:user, info: %{is_admin: true}),
156 pack_file: pack_file,
158 "license" => "Test license changed",
159 "homepage" => "https://pleroma.social",
160 "description" => "Test description",
161 "share-files" => false
165 test "for a pack without a fallback source", ctx do
169 |> assign(:user, ctx[:admin])
171 emoji_api_path(conn, :update_metadata, "test_pack"),
173 "new_data" => ctx[:new_data]
176 |> json_response(200) == ctx[:new_data]
178 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
181 test "for a pack with a fallback source", ctx do
185 url: "https://nonshared-pack"
187 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
190 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
195 "fallback-src-sha256",
196 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
202 |> assign(:user, ctx[:admin])
204 emoji_api_path(conn, :update_metadata, "test_pack"),
206 "new_data" => new_data
209 |> json_response(200) == new_data_with_sha
211 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
214 test "when the fallback source doesn't have all the files", ctx do
218 url: "https://nonshared-pack"
220 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
224 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
229 |> assign(:user, ctx[:admin])
231 emoji_api_path(conn, :update_metadata, "test_pack"),
233 "new_data" => new_data
236 |> json_response(:bad_request))["error"] =~ "does not have all"
240 test "updating pack files" do
241 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
242 original_content = File.read!(pack_file)
245 File.write!(pack_file, original_content)
247 File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
248 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
249 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
252 admin = insert(:user, info: %{is_admin: true})
258 "shortcode" => "blank",
259 "filename" => "dir/blank.png",
260 "file" => %Plug.Upload{
261 filename: "blank.png",
262 path: "#{@emoji_dir_path}/test_pack/blank.png"
266 different_name = %{same_name | "shortcode" => "blank_2"}
268 conn = conn |> assign(:user, admin)
271 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
272 |> json_response(:conflict))["error"] =~ "already exists"
275 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
276 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
278 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
281 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
282 "action" => "update",
283 "shortcode" => "blank_2",
284 "new_shortcode" => "blank_3",
285 "new_filename" => "dir_2/blank_3.png"
287 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
289 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
290 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
293 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
294 "action" => "remove",
295 "shortcode" => "blank_3"
297 |> json_response(200) == %{"blank" => "blank.png"}
299 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
304 url: "https://test-blank/blank_url.png"
306 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
309 # The name should be inferred from the URL ending
312 "shortcode" => "blank_url",
313 "file" => "https://test-blank/blank_url.png"
317 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
318 |> json_response(200) == %{
319 "blank" => "blank.png",
320 "blank_url" => "blank_url.png"
323 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
326 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
327 "action" => "remove",
328 "shortcode" => "blank_url"
330 |> json_response(200) == %{"blank" => "blank.png"}
332 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
335 test "creating and deleting a pack" do
337 File.rm_rf!("#{@emoji_dir_path}/test_created")
340 admin = insert(:user, info: %{is_admin: true})
342 conn = build_conn() |> assign(:user, admin)
345 |> put_req_header("content-type", "application/json")
353 |> json_response(200) == "ok"
355 assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
357 assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
363 |> delete(emoji_api_path(conn, :delete, "test_created"))
364 |> json_response(200) == "ok"
366 refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
369 test "filesystem import" do
371 File.rm!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt")
372 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
376 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
378 refute Map.has_key?(resp, "test_pack_for_import")
380 admin = insert(:user, info: %{is_admin: true})
383 |> assign(:user, admin)
384 |> post(emoji_api_path(conn, :import_from_fs))
385 |> json_response(200) == ["test_pack_for_import"]
387 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
388 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
390 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
391 refute File.exists?("#{@emoji_dir_path}/test_pack_for_import/pack.json")
393 emoji_txt_content = "blank, blank.png, Fun\n\nblank2, blank.png"
395 File.write!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
398 |> assign(:user, admin)
399 |> post(emoji_api_path(conn, :import_from_fs))
400 |> json_response(200) == ["test_pack_for_import"]
402 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
404 assert resp["test_pack_for_import"]["files"] == %{
405 "blank" => "blank.png",
406 "blank2" => "blank.png"