1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.EmojiFileControllerTest do
6 use Pleroma.Web.ConnCase, async: false
10 import Pleroma.Factory
12 @emoji_path Path.join(
13 Pleroma.Config.get!([:instance, :static_dir]),
16 setup do: clear_config([:instance, :public], true)
19 admin = insert(:user, is_admin: true)
20 token = insert(:oauth_admin_token, user: admin)
24 |> assign(:user, admin)
25 |> assign(:token, token)
27 Pleroma.Emoji.reload()
28 {:ok, %{admin_conn: admin_conn}}
31 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/files?name=:name" do
33 clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
34 pack_file = "#{@emoji_path}/test_pack/pack.json"
35 original_content = File.read!(pack_file)
38 File.write!(pack_file, original_content)
44 test "upload zip file with emojies", %{admin_conn: admin_conn} do
47 "128px/a_trusted_friend-128.png",
49 "1000px/baby_in_a_box.png",
53 |> Enum.each(fn path -> File.rm_rf!("#{@emoji_path}/test_pack/#{path}") end)
58 |> put_req_header("content-type", "multipart/form-data")
59 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
61 content_type: "application/zip",
62 filename: "emojis.zip",
63 path: Path.absname("test/fixtures/emojis.zip")
66 |> json_response_and_validate_schema(200)
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"
78 Enum.each(Map.values(resp), fn path ->
79 assert File.exists?("#{@emoji_path}/test_pack/#{path}")
83 test "create shortcode exists", %{admin_conn: admin_conn} do
85 |> put_req_header("content-type", "multipart/form-data")
86 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
88 filename: "dir/blank.png",
90 filename: "blank.png",
91 path: "#{@emoji_path}/test_pack/blank.png"
94 |> json_response_and_validate_schema(:conflict) == %{
95 "error" => "An emoji with the \"blank\" shortcode already exists"
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)
103 |> put_req_header("content-type", "multipart/form-data")
104 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
106 filename: "dir/blank.png",
108 filename: "blank.png",
109 path: "#{@emoji_path}/test_pack/blank.png"
112 |> json_response_and_validate_schema(200) == %{
113 "blank" => "blank.png",
114 "blank2" => "blank2.png",
115 "blank3" => "dir/blank.png"
118 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
121 |> put_req_header("content-type", "multipart/form-data")
122 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
124 new_shortcode: "blank2",
125 new_filename: "dir_2/blank_3.png"
127 |> json_response_and_validate_schema(:conflict) == %{
129 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
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)
137 |> put_req_header("content-type", "multipart/form-data")
138 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
140 filename: "dir/blank.png",
142 filename: "blank.png",
143 path: "#{@emoji_path}/test_pack/blank.png"
146 |> json_response_and_validate_schema(200) == %{
147 "blank" => "blank.png",
148 "blank2" => "blank2.png",
149 "blank3" => "dir/blank.png"
152 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
155 |> put_req_header("content-type", "multipart/form-data")
156 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
158 new_shortcode: "blank4",
159 new_filename: "dir_2/blank_3.png",
162 |> json_response_and_validate_schema(200) == %{
163 "blank" => "blank.png",
164 "blank2" => "blank2.png",
165 "blank4" => "dir_2/blank_3.png"
168 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
171 test "with empty filename", %{admin_conn: admin_conn} do
173 |> put_req_header("content-type", "multipart/form-data")
174 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
178 filename: "blank.png",
179 path: "#{@emoji_path}/test_pack/blank.png"
182 |> json_response_and_validate_schema(422) == %{
183 "error" => "pack name, shortcode or filename cannot be empty"
187 test "add file with not loaded pack", %{admin_conn: admin_conn} do
189 |> put_req_header("content-type", "multipart/form-data")
190 |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
192 filename: "dir/blank.png",
194 filename: "blank.png",
195 path: "#{@emoji_path}/test_pack/blank.png"
198 |> json_response_and_validate_schema(:not_found) == %{
199 "error" => "pack \"not_loaded\" is not found"
203 test "returns an error on add file when file system is not writable", %{
204 admin_conn: admin_conn
206 pack_file = Path.join([@emoji_path, "not_loaded", "pack.json"])
209 {File, [:passthrough], [stat: fn ^pack_file -> {:error, :eacces} end]}
212 |> put_req_header("content-type", "multipart/form-data")
213 |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
215 filename: "dir/blank.png",
217 filename: "blank.png",
218 path: "#{@emoji_path}/test_pack/blank.png"
221 |> json_response_and_validate_schema(500) == %{
223 "Unexpected error occurred while adding file to pack. (POSIX error: Permission denied)"
228 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
230 |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=blank3")
231 |> json_response_and_validate_schema(:not_found) == %{
232 "error" => "pack \"not_loaded\" is not found"
236 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
238 |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=")
239 |> json_response_and_validate_schema(:not_found) == %{
240 "error" => "pack \"not_loaded\" is not found"
244 test "update file with not loaded pack", %{admin_conn: admin_conn} do
246 |> put_req_header("content-type", "multipart/form-data")
247 |> patch("/api/pleroma/emoji/packs/files?name=not_loaded", %{
249 new_shortcode: "blank3",
250 new_filename: "dir_2/blank_3.png"
252 |> json_response_and_validate_schema(:not_found) == %{
253 "error" => "pack \"not_loaded\" is not found"
257 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
259 |> put_req_header("content-type", "multipart/form-data")
260 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
262 filename: "dir/blank.png",
264 filename: "blank.png",
265 path: "#{@emoji_path}/test_pack/blank.png"
268 |> json_response_and_validate_schema(200) == %{
269 "blank" => "blank.png",
270 "blank4" => "dir/blank.png",
271 "blank2" => "blank2.png"
274 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
277 |> put_req_header("content-type", "multipart/form-data")
278 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
280 new_shortcode: "blank3",
281 new_filename: "dir_2/blank_3.png"
283 |> json_response_and_validate_schema(200) == %{
284 "blank3" => "dir_2/blank_3.png",
285 "blank" => "blank.png",
286 "blank2" => "blank2.png"
289 refute File.exists?("#{@emoji_path}/test_pack/dir/")
290 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
293 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
294 |> json_response_and_validate_schema(200) == %{
295 "blank" => "blank.png",
296 "blank2" => "blank2.png"
299 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
301 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
304 test "new with shortcode from url", %{admin_conn: admin_conn} do
308 url: "https://test-blank/blank_url.png"
310 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
314 |> put_req_header("content-type", "multipart/form-data")
315 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
316 shortcode: "blank_url",
317 file: "https://test-blank/blank_url.png"
319 |> json_response_and_validate_schema(200) == %{
320 "blank_url" => "blank_url.png",
321 "blank" => "blank.png",
322 "blank2" => "blank2.png"
325 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
327 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
330 test "new without shortcode", %{admin_conn: admin_conn} do
331 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
334 |> put_req_header("content-type", "multipart/form-data")
335 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
337 filename: "shortcode.png",
338 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
341 |> json_response_and_validate_schema(200) == %{
342 "shortcode" => "shortcode.png",
343 "blank" => "blank.png",
344 "blank2" => "blank2.png"
348 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
350 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
351 |> json_response_and_validate_schema(:bad_request) == %{
352 "error" => "Emoji \"blank3\" does not exist"
356 test "update non existing emoji", %{admin_conn: admin_conn} do
358 |> put_req_header("content-type", "multipart/form-data")
359 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
361 new_shortcode: "blank4",
362 new_filename: "dir_2/blank_3.png"
364 |> json_response_and_validate_schema(:bad_request) == %{
365 "error" => "Emoji \"blank3\" does not exist"
369 test "update with empty shortcode", %{admin_conn: admin_conn} do
371 "error" => "Missing field: new_shortcode."
374 |> put_req_header("content-type", "multipart/form-data")
375 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
377 new_filename: "dir_2/blank_3.png"
379 |> json_response_and_validate_schema(:bad_request)
382 test "it requires privileged role :emoji_manage_emoji", %{admin_conn: admin_conn} do
383 clear_config([:instance, :admin_privileges], [])
386 |> put_req_header("content-type", "multipart/form-data")
387 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
389 filename: "shortcode.png",
390 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
393 |> json_response(:forbidden)
396 |> put_req_header("content-type", "multipart/form-data")
397 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
399 new_filename: "dir_2/blank_3.png"
401 |> json_response(:forbidden)
404 |> put_req_header("content-type", "multipart/form-data")
405 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
406 |> json_response(:forbidden)