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/.well-known/nodeinfo"} ->
58 json([%{href: "https://old-instance/nodeinfo/2.1.json"}])
60 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
61 json(%{metadata: %{features: []}})
63 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
64 json([%{href: "https://example.com/nodeinfo/2.1.json"}])
66 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
67 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
71 url: "https://example.com/api/pleroma/emoji/packs/list"
76 |> get(emoji_api_path(conn, :list_packs))
82 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
87 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
93 url: "https://nonshared-pack"
95 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
98 admin = insert(:user, info: %{is_admin: true})
100 conn = build_conn() |> assign(:user, admin)
103 |> put_req_header("content-type", "application/json")
110 instance_address: "https://old-instance",
111 pack_name: "test_pack",
116 |> json_response(500))["error"] =~ "does not support"
119 |> put_req_header("content-type", "application/json")
126 instance_address: "https://example.com",
127 pack_name: "test_pack",
132 |> json_response(200) == "ok"
134 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
135 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
138 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
139 |> json_response(200) == "ok"
141 refute File.exists?("#{@emoji_dir_path}/test_pack2")
143 # non-shared, downloaded from the fallback URL
145 conn = build_conn() |> assign(:user, admin)
148 |> put_req_header("content-type", "application/json")
155 instance_address: "https://example.com",
156 pack_name: "test_pack_nonshared",
157 as: "test_pack_nonshared2"
161 |> json_response(200) == "ok"
163 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
164 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
167 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
168 |> json_response(200) == "ok"
170 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
173 describe "updating pack metadata" do
175 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
176 original_content = File.read!(pack_file)
179 File.write!(pack_file, original_content)
183 admin: insert(:user, info: %{is_admin: true}),
184 pack_file: pack_file,
186 "license" => "Test license changed",
187 "homepage" => "https://pleroma.social",
188 "description" => "Test description",
189 "share-files" => false
193 test "for a pack without a fallback source", ctx do
197 |> assign(:user, ctx[:admin])
199 emoji_api_path(conn, :update_metadata, "test_pack"),
201 "new_data" => ctx[:new_data]
204 |> json_response(200) == ctx[:new_data]
206 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
209 test "for a pack with a fallback source", ctx do
213 url: "https://nonshared-pack"
215 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
218 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
223 "fallback-src-sha256",
224 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
230 |> assign(:user, ctx[:admin])
232 emoji_api_path(conn, :update_metadata, "test_pack"),
234 "new_data" => new_data
237 |> json_response(200) == new_data_with_sha
239 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
242 test "when the fallback source doesn't have all the files", ctx do
246 url: "https://nonshared-pack"
248 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
252 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
257 |> assign(:user, ctx[:admin])
259 emoji_api_path(conn, :update_metadata, "test_pack"),
261 "new_data" => new_data
264 |> json_response(:bad_request))["error"] =~ "does not have all"
268 test "updating pack files" do
269 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
270 original_content = File.read!(pack_file)
273 File.write!(pack_file, original_content)
275 File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
276 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
277 File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
280 admin = insert(:user, info: %{is_admin: true})
286 "shortcode" => "blank",
287 "filename" => "dir/blank.png",
288 "file" => %Plug.Upload{
289 filename: "blank.png",
290 path: "#{@emoji_dir_path}/test_pack/blank.png"
294 different_name = %{same_name | "shortcode" => "blank_2"}
296 conn = conn |> assign(:user, admin)
299 |> post(emoji_api_path(conn, :update_file, "test_pack"), same_name)
300 |> json_response(:conflict))["error"] =~ "already exists"
303 |> post(emoji_api_path(conn, :update_file, "test_pack"), different_name)
304 |> json_response(200) == %{"blank" => "blank.png", "blank_2" => "dir/blank.png"}
306 assert File.exists?("#{@emoji_dir_path}/test_pack/dir/blank.png")
309 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
310 "action" => "update",
311 "shortcode" => "blank_2",
312 "new_shortcode" => "blank_3",
313 "new_filename" => "dir_2/blank_3.png"
315 |> json_response(200) == %{"blank" => "blank.png", "blank_3" => "dir_2/blank_3.png"}
317 refute File.exists?("#{@emoji_dir_path}/test_pack/dir/")
318 assert File.exists?("#{@emoji_dir_path}/test_pack/dir_2/blank_3.png")
321 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
322 "action" => "remove",
323 "shortcode" => "blank_3"
325 |> json_response(200) == %{"blank" => "blank.png"}
327 refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
332 url: "https://test-blank/blank_url.png"
334 text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
337 # The name should be inferred from the URL ending
340 "shortcode" => "blank_url",
341 "file" => "https://test-blank/blank_url.png"
345 |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
346 |> json_response(200) == %{
347 "blank" => "blank.png",
348 "blank_url" => "blank_url.png"
351 assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
354 |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
355 "action" => "remove",
356 "shortcode" => "blank_url"
358 |> json_response(200) == %{"blank" => "blank.png"}
360 refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
363 test "creating and deleting a pack" do
365 File.rm_rf!("#{@emoji_dir_path}/test_created")
368 admin = insert(:user, info: %{is_admin: true})
370 conn = build_conn() |> assign(:user, admin)
373 |> put_req_header("content-type", "application/json")
381 |> json_response(200) == "ok"
383 assert File.exists?("#{@emoji_dir_path}/test_created/pack.json")
385 assert Jason.decode!(File.read!("#{@emoji_dir_path}/test_created/pack.json")) == %{
391 |> delete(emoji_api_path(conn, :delete, "test_created"))
392 |> json_response(200) == "ok"
394 refute File.exists?("#{@emoji_dir_path}/test_created/pack.json")
397 test "filesystem import" do
399 File.rm!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt")
400 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
404 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
406 refute Map.has_key?(resp, "test_pack_for_import")
408 admin = insert(:user, info: %{is_admin: true})
411 |> assign(:user, admin)
412 |> post(emoji_api_path(conn, :import_from_fs))
413 |> json_response(200) == ["test_pack_for_import"]
415 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
416 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
418 File.rm!("#{@emoji_dir_path}/test_pack_for_import/pack.json")
419 refute File.exists?("#{@emoji_dir_path}/test_pack_for_import/pack.json")
421 emoji_txt_content = "blank, blank.png, Fun\n\nblank2, blank.png"
423 File.write!("#{@emoji_dir_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
426 |> assign(:user, admin)
427 |> post(emoji_api_path(conn, :import_from_fs))
428 |> json_response(200) == ["test_pack_for_import"]
430 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
432 assert resp["test_pack_for_import"]["files"] == %{
433 "blank" => "blank.png",
434 "blank2" => "blank.png"