Add tests for pack metadata updating
[akkoma] / test / web / emoji_api_controller_test.exs
1 defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest do
2 use Pleroma.Web.ConnCase
3
4 import Tesla.Mock
5
6 import Pleroma.Factory
7
8 @emoji_dir_path Path.join(
9 Pleroma.Config.get!([:instance, :static_dir]),
10 "emoji"
11 )
12
13 test "shared & non-shared pack information in list_packs is ok" do
14 conn = build_conn()
15 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
16
17 assert Map.has_key?(resp, "test_pack")
18
19 pack = resp["test_pack"]
20
21 assert Map.has_key?(pack["pack"], "download-sha256")
22 assert pack["pack"]["can-download"]
23
24 assert pack["files"] == %{"blank" => "blank.png"}
25
26 # Non-shared pack
27
28 assert Map.has_key?(resp, "test_pack_nonshared")
29
30 pack = resp["test_pack_nonshared"]
31
32 refute pack["pack"]["shared"]
33 refute pack["pack"]["can-download"]
34 end
35
36 test "downloading a shared pack from download_shared" do
37 conn = build_conn()
38
39 resp =
40 conn
41 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
42 |> response(200)
43
44 {:ok, arch} = :zip.unzip(resp, [:memory])
45
46 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
47 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
48 end
49
50 test "downloading shared & unshared packs from another instance via download_from, deleting them" do
51 on_exit(fn ->
52 File.rm_rf!("#{@emoji_dir_path}/test_pack2")
53 File.rm_rf!("#{@emoji_dir_path}/test_pack_nonshared2")
54 end)
55
56 mock(fn
57 %{
58 method: :get,
59 url: "https://example.com/api/pleroma/emoji/packs/list"
60 } ->
61 conn = build_conn()
62
63 conn
64 |> get(emoji_api_path(conn, :list_packs))
65 |> json_response(200)
66 |> json()
67
68 %{
69 method: :get,
70 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
71 } ->
72 conn = build_conn()
73
74 conn
75 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
76 |> response(200)
77 |> text()
78
79 %{
80 method: :get,
81 url: "https://nonshared-pack"
82 } ->
83 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
84 end)
85
86 admin = insert(:user, info: %{is_admin: true})
87
88 conn = build_conn()
89
90 assert conn
91 |> put_req_header("content-type", "application/json")
92 |> assign(:user, admin)
93 |> post(
94 emoji_api_path(
95 conn,
96 :download_from
97 ),
98 %{
99 instance_address: "https://example.com",
100 pack_name: "test_pack",
101 as: "test_pack2"
102 }
103 |> Jason.encode!()
104 )
105 |> text_response(200) == "ok"
106
107 assert File.exists?("#{@emoji_dir_path}/test_pack2/pack.json")
108 assert File.exists?("#{@emoji_dir_path}/test_pack2/blank.png")
109
110 assert conn
111 |> assign(:user, admin)
112 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
113 |> response(200) == "ok"
114
115 refute File.exists?("#{@emoji_dir_path}/test_pack2")
116
117 # non-shared, downloaded from the fallback URL
118
119 conn = build_conn()
120
121 assert conn
122 |> put_req_header("content-type", "application/json")
123 |> assign(:user, admin)
124 |> post(
125 emoji_api_path(
126 conn,
127 :download_from
128 ),
129 %{
130 instance_address: "https://example.com",
131 pack_name: "test_pack_nonshared",
132 as: "test_pack_nonshared2"
133 }
134 |> Jason.encode!()
135 )
136 |> text_response(200) == "ok"
137
138 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/pack.json")
139 assert File.exists?("#{@emoji_dir_path}/test_pack_nonshared2/blank.png")
140
141 assert conn
142 |> assign(:user, admin)
143 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
144 |> response(200) == "ok"
145
146 refute File.exists?("#{@emoji_dir_path}/test_pack_nonshared2")
147 end
148
149 describe "updating pack metadata" do
150 setup do
151 pack_file = "#{@emoji_dir_path}/test_pack/pack.json"
152 original_content = File.read!(pack_file)
153
154 on_exit(fn ->
155 File.write!(pack_file, original_content)
156 end)
157
158 {:ok,
159 admin: insert(:user, info: %{is_admin: true}),
160 pack_file: pack_file,
161 new_data: %{
162 "license" => "Test license changed",
163 "homepage" => "https://pleroma.social",
164 "description" => "Test description",
165 "share-files" => false
166 }}
167 end
168
169 test "for a pack without a fallback source", ctx do
170 conn = build_conn()
171
172 assert conn
173 |> assign(:user, ctx[:admin])
174 |> post(
175 emoji_api_path(conn, :update_metadata, "test_pack"),
176 %{
177 "new_data" => ctx[:new_data]
178 }
179 )
180 |> json_response(200) == ctx[:new_data]
181
182 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
183 end
184
185 test "for a pack with a fallback source", ctx do
186 mock(fn
187 %{
188 method: :get,
189 url: "https://nonshared-pack"
190 } ->
191 text(File.read!("#{@emoji_dir_path}/test_pack_nonshared/nonshared.zip"))
192 end)
193
194 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
195
196 new_data_with_sha =
197 Map.put(
198 new_data,
199 "fallback-src-sha256",
200 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
201 )
202
203 conn = build_conn()
204
205 assert conn
206 |> assign(:user, ctx[:admin])
207 |> post(
208 emoji_api_path(conn, :update_metadata, "test_pack"),
209 %{
210 "new_data" => new_data
211 }
212 )
213 |> json_response(200) == new_data_with_sha
214
215 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
216 end
217
218 test "when the fallback source doesn't have all the files", ctx do
219 mock(fn
220 %{
221 method: :get,
222 url: "https://nonshared-pack"
223 } ->
224 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
225 text(empty_arch)
226 end)
227
228 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
229
230 conn = build_conn()
231
232 assert conn
233 |> assign(:user, ctx[:admin])
234 |> post(
235 emoji_api_path(conn, :update_metadata, "test_pack"),
236 %{
237 "new_data" => new_data
238 }
239 )
240 |> text_response(:bad_request) =~ "does not have all"
241 end
242 end
243 end