Merge branch 'develop' into refactor/discoverable_user_field
[akkoma] / test / pleroma / web / pleroma_api / controllers / emoji_file_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiFileControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Tesla.Mock
9 import Pleroma.Factory
10
11 @emoji_path Path.join(
12 Pleroma.Config.get!([:instance, :static_dir]),
13 "emoji"
14 )
15 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
16
17 setup do: clear_config([:instance, :public], true)
18
19 setup do
20 admin = insert(:user, is_admin: true)
21 token = insert(:oauth_admin_token, user: admin)
22
23 admin_conn =
24 build_conn()
25 |> assign(:user, admin)
26 |> assign(:token, token)
27
28 Pleroma.Emoji.reload()
29 {:ok, %{admin_conn: admin_conn}}
30 end
31
32 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/files?name=:name" do
33 setup do
34 pack_file = "#{@emoji_path}/test_pack/pack.json"
35 original_content = File.read!(pack_file)
36
37 on_exit(fn ->
38 File.write!(pack_file, original_content)
39 end)
40
41 :ok
42 end
43
44 test "upload zip file with emojies", %{admin_conn: admin_conn} do
45 on_exit(fn ->
46 [
47 "128px/a_trusted_friend-128.png",
48 "auroraborealis.png",
49 "1000px/baby_in_a_box.png",
50 "1000px/bear.png",
51 "128px/bear-128.png"
52 ]
53 |> Enum.each(fn path -> File.rm_rf!("#{@emoji_path}/test_pack/#{path}") end)
54 end)
55
56 resp =
57 admin_conn
58 |> put_req_header("content-type", "multipart/form-data")
59 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
60 file: %Plug.Upload{
61 content_type: "application/zip",
62 filename: "emojis.zip",
63 path: Path.absname("test/fixtures/emojis.zip")
64 }
65 })
66 |> json_response_and_validate_schema(200)
67
68 assert resp == %{
69 "a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
70 "auroraborealis" => "auroraborealis.png",
71 "baby_in_a_box" => "1000px/baby_in_a_box.png",
72 "bear" => "1000px/bear.png",
73 "bear-128" => "128px/bear-128.png",
74 "blank" => "blank.png",
75 "blank2" => "blank2.png"
76 }
77
78 Enum.each(Map.values(resp), fn path ->
79 assert File.exists?("#{@emoji_path}/test_pack/#{path}")
80 end)
81 end
82
83 test "create shortcode exists", %{admin_conn: admin_conn} do
84 assert admin_conn
85 |> put_req_header("content-type", "multipart/form-data")
86 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
87 shortcode: "blank",
88 filename: "dir/blank.png",
89 file: %Plug.Upload{
90 filename: "blank.png",
91 path: "#{@emoji_path}/test_pack/blank.png"
92 }
93 })
94 |> json_response_and_validate_schema(:conflict) == %{
95 "error" => "An emoji with the \"blank\" shortcode already exists"
96 }
97 end
98
99 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
100 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
101
102 assert admin_conn
103 |> put_req_header("content-type", "multipart/form-data")
104 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
105 shortcode: "blank3",
106 filename: "dir/blank.png",
107 file: %Plug.Upload{
108 filename: "blank.png",
109 path: "#{@emoji_path}/test_pack/blank.png"
110 }
111 })
112 |> json_response_and_validate_schema(200) == %{
113 "blank" => "blank.png",
114 "blank2" => "blank2.png",
115 "blank3" => "dir/blank.png"
116 }
117
118 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
119
120 assert admin_conn
121 |> put_req_header("content-type", "multipart/form-data")
122 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
123 shortcode: "blank",
124 new_shortcode: "blank2",
125 new_filename: "dir_2/blank_3.png"
126 })
127 |> json_response_and_validate_schema(:conflict) == %{
128 "error" =>
129 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
130 }
131 end
132
133 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
134 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
135
136 assert admin_conn
137 |> put_req_header("content-type", "multipart/form-data")
138 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
139 shortcode: "blank3",
140 filename: "dir/blank.png",
141 file: %Plug.Upload{
142 filename: "blank.png",
143 path: "#{@emoji_path}/test_pack/blank.png"
144 }
145 })
146 |> json_response_and_validate_schema(200) == %{
147 "blank" => "blank.png",
148 "blank2" => "blank2.png",
149 "blank3" => "dir/blank.png"
150 }
151
152 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
153
154 assert admin_conn
155 |> put_req_header("content-type", "multipart/form-data")
156 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
157 shortcode: "blank3",
158 new_shortcode: "blank4",
159 new_filename: "dir_2/blank_3.png",
160 force: true
161 })
162 |> json_response_and_validate_schema(200) == %{
163 "blank" => "blank.png",
164 "blank2" => "blank2.png",
165 "blank4" => "dir_2/blank_3.png"
166 }
167
168 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
169 end
170
171 test "with empty filename", %{admin_conn: admin_conn} do
172 assert admin_conn
173 |> put_req_header("content-type", "multipart/form-data")
174 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
175 shortcode: "blank2",
176 filename: "",
177 file: %Plug.Upload{
178 filename: "blank.png",
179 path: "#{@emoji_path}/test_pack/blank.png"
180 }
181 })
182 |> json_response_and_validate_schema(422) == %{
183 "error" => "pack name, shortcode or filename cannot be empty"
184 }
185 end
186
187 test "add file with not loaded pack", %{admin_conn: admin_conn} do
188 assert admin_conn
189 |> put_req_header("content-type", "multipart/form-data")
190 |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
191 shortcode: "blank3",
192 filename: "dir/blank.png",
193 file: %Plug.Upload{
194 filename: "blank.png",
195 path: "#{@emoji_path}/test_pack/blank.png"
196 }
197 })
198 |> json_response_and_validate_schema(:not_found) == %{
199 "error" => "pack \"not_loaded\" is not found"
200 }
201 end
202
203 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
204 assert admin_conn
205 |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=blank3")
206 |> json_response_and_validate_schema(:not_found) == %{
207 "error" => "pack \"not_loaded\" is not found"
208 }
209 end
210
211 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
212 assert admin_conn
213 |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=")
214 |> json_response_and_validate_schema(:not_found) == %{
215 "error" => "pack \"not_loaded\" is not found"
216 }
217 end
218
219 test "update file with not loaded pack", %{admin_conn: admin_conn} do
220 assert admin_conn
221 |> put_req_header("content-type", "multipart/form-data")
222 |> patch("/api/pleroma/emoji/packs/files?name=not_loaded", %{
223 shortcode: "blank4",
224 new_shortcode: "blank3",
225 new_filename: "dir_2/blank_3.png"
226 })
227 |> json_response_and_validate_schema(:not_found) == %{
228 "error" => "pack \"not_loaded\" is not found"
229 }
230 end
231
232 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
233 assert admin_conn
234 |> put_req_header("content-type", "multipart/form-data")
235 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
236 shortcode: "blank4",
237 filename: "dir/blank.png",
238 file: %Plug.Upload{
239 filename: "blank.png",
240 path: "#{@emoji_path}/test_pack/blank.png"
241 }
242 })
243 |> json_response_and_validate_schema(200) == %{
244 "blank" => "blank.png",
245 "blank4" => "dir/blank.png",
246 "blank2" => "blank2.png"
247 }
248
249 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
250
251 assert admin_conn
252 |> put_req_header("content-type", "multipart/form-data")
253 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
254 shortcode: "blank4",
255 new_shortcode: "blank3",
256 new_filename: "dir_2/blank_3.png"
257 })
258 |> json_response_and_validate_schema(200) == %{
259 "blank3" => "dir_2/blank_3.png",
260 "blank" => "blank.png",
261 "blank2" => "blank2.png"
262 }
263
264 refute File.exists?("#{@emoji_path}/test_pack/dir/")
265 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
266
267 assert admin_conn
268 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
269 |> json_response_and_validate_schema(200) == %{
270 "blank" => "blank.png",
271 "blank2" => "blank2.png"
272 }
273
274 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
275
276 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
277 end
278
279 test "new with shortcode from url", %{admin_conn: admin_conn} do
280 mock(fn
281 %{
282 method: :get,
283 url: "https://test-blank/blank_url.png"
284 } ->
285 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
286 end)
287
288 assert admin_conn
289 |> put_req_header("content-type", "multipart/form-data")
290 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
291 shortcode: "blank_url",
292 file: "https://test-blank/blank_url.png"
293 })
294 |> json_response_and_validate_schema(200) == %{
295 "blank_url" => "blank_url.png",
296 "blank" => "blank.png",
297 "blank2" => "blank2.png"
298 }
299
300 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
301
302 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
303 end
304
305 test "new without shortcode", %{admin_conn: admin_conn} do
306 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
307
308 assert admin_conn
309 |> put_req_header("content-type", "multipart/form-data")
310 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
311 file: %Plug.Upload{
312 filename: "shortcode.png",
313 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
314 }
315 })
316 |> json_response_and_validate_schema(200) == %{
317 "shortcode" => "shortcode.png",
318 "blank" => "blank.png",
319 "blank2" => "blank2.png"
320 }
321 end
322
323 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
324 assert admin_conn
325 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
326 |> json_response_and_validate_schema(:bad_request) == %{
327 "error" => "Emoji \"blank3\" does not exist"
328 }
329 end
330
331 test "update non existing emoji", %{admin_conn: admin_conn} do
332 assert admin_conn
333 |> put_req_header("content-type", "multipart/form-data")
334 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
335 shortcode: "blank3",
336 new_shortcode: "blank4",
337 new_filename: "dir_2/blank_3.png"
338 })
339 |> json_response_and_validate_schema(:bad_request) == %{
340 "error" => "Emoji \"blank3\" does not exist"
341 }
342 end
343
344 test "update with empty shortcode", %{admin_conn: admin_conn} do
345 assert %{
346 "error" => "Missing field: new_shortcode."
347 } =
348 admin_conn
349 |> put_req_header("content-type", "multipart/form-data")
350 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
351 shortcode: "blank",
352 new_filename: "dir_2/blank_3.png"
353 })
354 |> json_response_and_validate_schema(:bad_request)
355 end
356 end
357 end