1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
10 import Pleroma.Factory
12 @emoji_path Path.join(
13 Pleroma.Config.get!([:instance, :static_dir]),
16 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
18 setup do: clear_config([:instance, :public], true)
21 admin = insert(:user, is_admin: true)
22 token = insert(:oauth_admin_token, user: admin)
26 |> assign(:user, admin)
27 |> assign(:token, token)
29 Pleroma.Emoji.reload()
30 {:ok, %{admin_conn: admin_conn}}
33 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/files?name=:name" do
35 pack_file = "#{@emoji_path}/test_pack/pack.json"
36 original_content = File.read!(pack_file)
39 File.write!(pack_file, original_content)
45 test "upload zip file with emojies", %{admin_conn: admin_conn} do
48 "128px/a_trusted_friend-128.png",
50 "1000px/baby_in_a_box.png",
54 |> Enum.each(fn path -> File.rm_rf!("#{@emoji_path}/test_pack/#{path}") end)
59 |> put_req_header("content-type", "multipart/form-data")
60 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
62 content_type: "application/zip",
63 filename: "emojis.zip",
64 path: Path.absname("test/fixtures/emojis.zip")
67 |> json_response_and_validate_schema(200)
70 "a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
71 "auroraborealis" => "auroraborealis.png",
72 "baby_in_a_box" => "1000px/baby_in_a_box.png",
73 "bear" => "1000px/bear.png",
74 "bear-128" => "128px/bear-128.png",
75 "blank" => "blank.png",
76 "blank2" => "blank2.png"
79 Enum.each(Map.values(resp), fn path ->
80 assert File.exists?("#{@emoji_path}/test_pack/#{path}")
84 test "create shortcode exists", %{admin_conn: admin_conn} do
86 |> put_req_header("content-type", "multipart/form-data")
87 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
89 filename: "dir/blank.png",
91 filename: "blank.png",
92 path: "#{@emoji_path}/test_pack/blank.png"
95 |> json_response_and_validate_schema(:conflict) == %{
96 "error" => "An emoji with the \"blank\" shortcode already exists"
100 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
101 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
104 |> put_req_header("content-type", "multipart/form-data")
105 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
107 filename: "dir/blank.png",
109 filename: "blank.png",
110 path: "#{@emoji_path}/test_pack/blank.png"
113 |> json_response_and_validate_schema(200) == %{
114 "blank" => "blank.png",
115 "blank2" => "blank2.png",
116 "blank3" => "dir/blank.png"
119 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
122 |> put_req_header("content-type", "multipart/form-data")
123 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
125 new_shortcode: "blank2",
126 new_filename: "dir_2/blank_3.png"
128 |> json_response_and_validate_schema(:conflict) == %{
130 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
134 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
135 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
138 |> put_req_header("content-type", "multipart/form-data")
139 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
141 filename: "dir/blank.png",
143 filename: "blank.png",
144 path: "#{@emoji_path}/test_pack/blank.png"
147 |> json_response_and_validate_schema(200) == %{
148 "blank" => "blank.png",
149 "blank2" => "blank2.png",
150 "blank3" => "dir/blank.png"
153 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
156 |> put_req_header("content-type", "multipart/form-data")
157 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
159 new_shortcode: "blank4",
160 new_filename: "dir_2/blank_3.png",
163 |> json_response_and_validate_schema(200) == %{
164 "blank" => "blank.png",
165 "blank2" => "blank2.png",
166 "blank4" => "dir_2/blank_3.png"
169 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
172 test "with empty filename", %{admin_conn: admin_conn} do
174 |> put_req_header("content-type", "multipart/form-data")
175 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
179 filename: "blank.png",
180 path: "#{@emoji_path}/test_pack/blank.png"
183 |> json_response_and_validate_schema(422) == %{
184 "error" => "pack name, shortcode or filename cannot be empty"
188 test "add file with not loaded pack", %{admin_conn: admin_conn} do
190 |> put_req_header("content-type", "multipart/form-data")
191 |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
193 filename: "dir/blank.png",
195 filename: "blank.png",
196 path: "#{@emoji_path}/test_pack/blank.png"
199 |> json_response_and_validate_schema(:not_found) == %{
200 "error" => "pack \"not_loaded\" is not found"
204 test "returns an error on add file when file system is not writable", %{
205 admin_conn: admin_conn
207 pack_file = Path.join([@emoji_path, "not_loaded", "pack.json"])
210 {File, [:passthrough], [stat: fn ^pack_file -> {:error, :eacces} end]}
213 |> put_req_header("content-type", "multipart/form-data")
214 |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
216 filename: "dir/blank.png",
218 filename: "blank.png",
219 path: "#{@emoji_path}/test_pack/blank.png"
222 |> json_response_and_validate_schema(500) == %{
224 "Unexpected error occurred while adding file to pack. (POSIX error: Permission denied)"
229 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
231 |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=blank3")
232 |> json_response_and_validate_schema(:not_found) == %{
233 "error" => "pack \"not_loaded\" is not found"
237 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
239 |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=")
240 |> json_response_and_validate_schema(:not_found) == %{
241 "error" => "pack \"not_loaded\" is not found"
245 test "update file with not loaded pack", %{admin_conn: admin_conn} do
247 |> put_req_header("content-type", "multipart/form-data")
248 |> patch("/api/pleroma/emoji/packs/files?name=not_loaded", %{
250 new_shortcode: "blank3",
251 new_filename: "dir_2/blank_3.png"
253 |> json_response_and_validate_schema(:not_found) == %{
254 "error" => "pack \"not_loaded\" is not found"
258 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
260 |> put_req_header("content-type", "multipart/form-data")
261 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
263 filename: "dir/blank.png",
265 filename: "blank.png",
266 path: "#{@emoji_path}/test_pack/blank.png"
269 |> json_response_and_validate_schema(200) == %{
270 "blank" => "blank.png",
271 "blank4" => "dir/blank.png",
272 "blank2" => "blank2.png"
275 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
278 |> put_req_header("content-type", "multipart/form-data")
279 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
281 new_shortcode: "blank3",
282 new_filename: "dir_2/blank_3.png"
284 |> json_response_and_validate_schema(200) == %{
285 "blank3" => "dir_2/blank_3.png",
286 "blank" => "blank.png",
287 "blank2" => "blank2.png"
290 refute File.exists?("#{@emoji_path}/test_pack/dir/")
291 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
294 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
295 |> json_response_and_validate_schema(200) == %{
296 "blank" => "blank.png",
297 "blank2" => "blank2.png"
300 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
302 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
305 test "new with shortcode from url", %{admin_conn: admin_conn} do
309 url: "https://test-blank/blank_url.png"
311 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
315 |> put_req_header("content-type", "multipart/form-data")
316 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
317 shortcode: "blank_url",
318 file: "https://test-blank/blank_url.png"
320 |> json_response_and_validate_schema(200) == %{
321 "blank_url" => "blank_url.png",
322 "blank" => "blank.png",
323 "blank2" => "blank2.png"
326 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
328 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
331 test "new without shortcode", %{admin_conn: admin_conn} do
332 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
335 |> put_req_header("content-type", "multipart/form-data")
336 |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
338 filename: "shortcode.png",
339 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
342 |> json_response_and_validate_schema(200) == %{
343 "shortcode" => "shortcode.png",
344 "blank" => "blank.png",
345 "blank2" => "blank2.png"
349 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
351 |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
352 |> json_response_and_validate_schema(:bad_request) == %{
353 "error" => "Emoji \"blank3\" does not exist"
357 test "update non existing emoji", %{admin_conn: admin_conn} do
359 |> put_req_header("content-type", "multipart/form-data")
360 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
362 new_shortcode: "blank4",
363 new_filename: "dir_2/blank_3.png"
365 |> json_response_and_validate_schema(:bad_request) == %{
366 "error" => "Emoji \"blank3\" does not exist"
370 test "update with empty shortcode", %{admin_conn: admin_conn} do
372 "error" => "Missing field: new_shortcode."
375 |> put_req_header("content-type", "multipart/form-data")
376 |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
378 new_filename: "dir_2/blank_3.png"
380 |> json_response_and_validate_schema(:bad_request)