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})
91 |> put_req_header("content-type", "application/json")
92 |> assign(:user, admin)
99 instance_address: "https://example.com",
100 pack_name: "test_pack",
105 |> text_response(200) == "ok"
107 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
108 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
111 |> assign(:user, admin)
112 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
113 |> response(200) == "ok"
115 refute File.exists?("#{@emoji_dir_path}/test_pack2")
117 # non-shared, downloaded from the fallback URL
122 |> put_req_header("content-type", "application/json")
123 |> assign(:user, admin)
130 instance_address: "https://example.com",
131 pack_name: "test_pack_nonshared",
132 as: "test_pack_nonshared2"
136 |> text_response(200) == "ok"
138 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
139 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
142 |> assign(:user, admin)
143 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
144 |> response(200) == "ok"
146 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
149 describe "updating pack metadata" do
151 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
152 original_content = File.read!(pack_file)
155 File.write!(pack_file, original_content)
159 admin: insert(:user, info: %{is_admin: true}),
160 pack_file: pack_file,
162 "license" => "Test license changed",
163 "homepage" => "https://pleroma.social",
164 "description" => "Test description",
165 "share-files" => false
169 test "for a pack without a fallback source", ctx do
173 |> assign(:user, ctx[:admin])
175 emoji_api_path(conn, :update_metadata, "test_pack"),
177 "new_data" => ctx[:new_data]
180 |> json_response(200) == ctx[:new_data]
182 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
185 test "for a pack with a fallback source", ctx do
189 url: "https://nonshared-pack"
191 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
194 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
199 "fallback-src-sha256",
200 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
206 |> assign(:user, ctx[:admin])
208 emoji_api_path(conn, :update_metadata, "test_pack"),
210 "new_data" => new_data
213 |> json_response(200) == new_data_with_sha
215 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
218 test "when the fallback source doesn't have all the files", ctx do
222 url: "https://nonshared-pack"
224 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
228 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
233 |> assign(:user, ctx[:admin])
235 emoji_api_path(conn, :update_metadata, "test_pack"),
237 "new_data" => new_data
240 |> text_response(:bad_request) =~ "does not have all"