1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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/v1/pleroma/emoji/packs/files?name=:name" do
33 pack_file = "#{@emoji_path}/test_pack/pack.json"
34 original_content = File.read!(pack_file)
37 File.write!(pack_file, original_content)
43 test "upload zip file with emojies", %{admin_conn: admin_conn} do
46 "128px/a_trusted_friend-128.png",
48 "1000px/baby_in_a_box.png",
52 |> Enum.each(fn path -> File.rm_rf!("#{@emoji_path}/test_pack/#{path}") end)
57 |> put_req_header("content-type", "multipart/form-data")
58 |> post("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
60 content_type: "application/zip",
61 filename: "emojis.zip",
62 path: Path.absname("test/fixtures/emojis.zip")
65 |> json_response_and_validate_schema(200)
68 "a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
69 "auroraborealis" => "auroraborealis.png",
70 "baby_in_a_box" => "1000px/baby_in_a_box.png",
71 "bear" => "1000px/bear.png",
72 "bear-128" => "128px/bear-128.png",
73 "blank" => "blank.png",
74 "blank2" => "blank2.png"
77 Enum.each(Map.values(resp), fn path ->
78 assert File.exists?("#{@emoji_path}/test_pack/#{path}")
82 test "create shortcode exists", %{admin_conn: admin_conn} do
84 |> put_req_header("content-type", "multipart/form-data")
85 |> post("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
87 filename: "dir/blank.png",
89 filename: "blank.png",
90 path: "#{@emoji_path}/test_pack/blank.png"
93 |> json_response_and_validate_schema(:conflict) == %{
94 "error" => "An emoji with the \"blank\" shortcode already exists"
98 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
99 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
102 |> put_req_header("content-type", "multipart/form-data")
103 |> post("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
105 filename: "dir/blank.png",
107 filename: "blank.png",
108 path: "#{@emoji_path}/test_pack/blank.png"
111 |> json_response_and_validate_schema(200) == %{
112 "blank" => "blank.png",
113 "blank2" => "blank2.png",
114 "blank3" => "dir/blank.png"
117 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
120 |> put_req_header("content-type", "multipart/form-data")
121 |> patch("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
123 new_shortcode: "blank2",
124 new_filename: "dir_2/blank_3.png"
126 |> json_response_and_validate_schema(:conflict) == %{
128 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
132 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
133 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
136 |> put_req_header("content-type", "multipart/form-data")
137 |> post("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
139 filename: "dir/blank.png",
141 filename: "blank.png",
142 path: "#{@emoji_path}/test_pack/blank.png"
145 |> json_response_and_validate_schema(200) == %{
146 "blank" => "blank.png",
147 "blank2" => "blank2.png",
148 "blank3" => "dir/blank.png"
151 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
154 |> put_req_header("content-type", "multipart/form-data")
155 |> patch("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
157 new_shortcode: "blank4",
158 new_filename: "dir_2/blank_3.png",
161 |> json_response_and_validate_schema(200) == %{
162 "blank" => "blank.png",
163 "blank2" => "blank2.png",
164 "blank4" => "dir_2/blank_3.png"
167 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
170 test "with empty filename", %{admin_conn: admin_conn} do
172 |> put_req_header("content-type", "multipart/form-data")
173 |> post("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
177 filename: "blank.png",
178 path: "#{@emoji_path}/test_pack/blank.png"
181 |> json_response_and_validate_schema(422) == %{
182 "error" => "pack name, shortcode or filename cannot be empty"
186 test "add file with not loaded pack", %{admin_conn: admin_conn} do
188 |> put_req_header("content-type", "multipart/form-data")
189 |> post("/api/v1/pleroma/emoji/packs/files?name=not_loaded", %{
191 filename: "dir/blank.png",
193 filename: "blank.png",
194 path: "#{@emoji_path}/test_pack/blank.png"
197 |> json_response_and_validate_schema(:not_found) == %{
198 "error" => "pack \"not_loaded\" is not found"
202 test "returns an error on add file when file system is not writable", %{
203 admin_conn: admin_conn
205 pack_file = Path.join([@emoji_path, "not_loaded", "pack.json"])
208 {File, [:passthrough], [stat: fn ^pack_file -> {:error, :eacces} end]}
211 |> put_req_header("content-type", "multipart/form-data")
212 |> post("/api/v1/pleroma/emoji/packs/files?name=not_loaded", %{
214 filename: "dir/blank.png",
216 filename: "blank.png",
217 path: "#{@emoji_path}/test_pack/blank.png"
220 |> json_response_and_validate_schema(500) == %{
222 "Unexpected error occurred while adding file to pack. (POSIX error: Permission denied)"
227 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
229 |> delete("/api/v1/pleroma/emoji/packs/files?name=not_loaded&shortcode=blank3")
230 |> json_response_and_validate_schema(:not_found) == %{
231 "error" => "pack \"not_loaded\" is not found"
235 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
237 |> delete("/api/v1/pleroma/emoji/packs/files?name=not_loaded&shortcode=")
238 |> json_response_and_validate_schema(:not_found) == %{
239 "error" => "pack \"not_loaded\" is not found"
243 test "update file with not loaded pack", %{admin_conn: admin_conn} do
245 |> put_req_header("content-type", "multipart/form-data")
246 |> patch("/api/v1/pleroma/emoji/packs/files?name=not_loaded", %{
248 new_shortcode: "blank3",
249 new_filename: "dir_2/blank_3.png"
251 |> json_response_and_validate_schema(:not_found) == %{
252 "error" => "pack \"not_loaded\" is not found"
256 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
258 |> put_req_header("content-type", "multipart/form-data")
259 |> post("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
261 filename: "dir/blank.png",
263 filename: "blank.png",
264 path: "#{@emoji_path}/test_pack/blank.png"
267 |> json_response_and_validate_schema(200) == %{
268 "blank" => "blank.png",
269 "blank4" => "dir/blank.png",
270 "blank2" => "blank2.png"
273 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
276 |> put_req_header("content-type", "multipart/form-data")
277 |> patch("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
279 new_shortcode: "blank3",
280 new_filename: "dir_2/blank_3.png"
282 |> json_response_and_validate_schema(200) == %{
283 "blank3" => "dir_2/blank_3.png",
284 "blank" => "blank.png",
285 "blank2" => "blank2.png"
288 refute File.exists?("#{@emoji_path}/test_pack/dir/")
289 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
292 |> delete("/api/v1/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
293 |> json_response_and_validate_schema(200) == %{
294 "blank" => "blank.png",
295 "blank2" => "blank2.png"
298 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
300 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
303 test "new with shortcode from url", %{admin_conn: admin_conn} do
307 url: "https://test-blank/blank_url.png"
309 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
313 |> put_req_header("content-type", "multipart/form-data")
314 |> post("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
315 shortcode: "blank_url",
316 file: "https://test-blank/blank_url.png"
318 |> json_response_and_validate_schema(200) == %{
319 "blank_url" => "blank_url.png",
320 "blank" => "blank.png",
321 "blank2" => "blank2.png"
324 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
326 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
329 test "new without shortcode", %{admin_conn: admin_conn} do
330 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
333 |> put_req_header("content-type", "multipart/form-data")
334 |> post("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
336 filename: "shortcode.png",
337 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
340 |> json_response_and_validate_schema(200) == %{
341 "shortcode" => "shortcode.png",
342 "blank" => "blank.png",
343 "blank2" => "blank2.png"
347 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
349 |> delete("/api/v1/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
350 |> json_response_and_validate_schema(:bad_request) == %{
351 "error" => "Emoji \"blank3\" does not exist"
355 test "update non existing emoji", %{admin_conn: admin_conn} do
357 |> put_req_header("content-type", "multipart/form-data")
358 |> patch("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
360 new_shortcode: "blank4",
361 new_filename: "dir_2/blank_3.png"
363 |> json_response_and_validate_schema(:bad_request) == %{
364 "error" => "Emoji \"blank3\" does not exist"
368 test "update with empty shortcode", %{admin_conn: admin_conn} do
370 "error" => "Missing field: new_shortcode."
373 |> put_req_header("content-type", "multipart/form-data")
374 |> patch("/api/v1/pleroma/emoji/packs/files?name=test_pack", %{
376 new_filename: "dir_2/blank_3.png"
378 |> json_response_and_validate_schema(:bad_request)