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
11 @emoji_path Path.join(
12 Pleroma.Config.get!([:instance, :static_dir]),
15 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
17 setup do: clear_config([:instance, :public], true)
20 admin = insert(:user, is_admin: true)
21 token = insert(:oauth_admin_token, user: admin)
25 |> assign(:user, admin)
26 |> assign(:token, token)
28 Pleroma.Emoji.reload()
29 {:ok, %{admin_conn: admin_conn}}
32 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/:name/files" do
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/test_pack/files", %{
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/test_pack/files", %{
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/test_pack/files", %{
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/test_pack/files", %{
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/test_pack/files", %{
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/test_pack/files", %{
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/test_pack/files", %{
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/not_loaded/files", %{
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 "remove file with not loaded pack", %{admin_conn: admin_conn} do
205 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=blank3")
206 |> json_response_and_validate_schema(:not_found) == %{
207 "error" => "pack \"not_loaded\" is not found"
211 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
213 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=")
214 |> json_response_and_validate_schema(:not_found) == %{
215 "error" => "pack \"not_loaded\" is not found"
219 test "update file with not loaded pack", %{admin_conn: admin_conn} do
221 |> put_req_header("content-type", "multipart/form-data")
222 |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
224 new_shortcode: "blank3",
225 new_filename: "dir_2/blank_3.png"
227 |> json_response_and_validate_schema(:not_found) == %{
228 "error" => "pack \"not_loaded\" is not found"
232 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
234 |> put_req_header("content-type", "multipart/form-data")
235 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
237 filename: "dir/blank.png",
239 filename: "blank.png",
240 path: "#{@emoji_path}/test_pack/blank.png"
243 |> json_response_and_validate_schema(200) == %{
244 "blank" => "blank.png",
245 "blank4" => "dir/blank.png",
246 "blank2" => "blank2.png"
249 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
252 |> put_req_header("content-type", "multipart/form-data")
253 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
255 new_shortcode: "blank3",
256 new_filename: "dir_2/blank_3.png"
258 |> json_response_and_validate_schema(200) == %{
259 "blank3" => "dir_2/blank_3.png",
260 "blank" => "blank.png",
261 "blank2" => "blank2.png"
264 refute File.exists?("#{@emoji_path}/test_pack/dir/")
265 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
268 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
269 |> json_response_and_validate_schema(200) == %{
270 "blank" => "blank.png",
271 "blank2" => "blank2.png"
274 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
276 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
279 test "new with shortcode from url", %{admin_conn: admin_conn} do
283 url: "https://test-blank/blank_url.png"
285 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
289 |> put_req_header("content-type", "multipart/form-data")
290 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
291 shortcode: "blank_url",
292 file: "https://test-blank/blank_url.png"
294 |> json_response_and_validate_schema(200) == %{
295 "blank_url" => "blank_url.png",
296 "blank" => "blank.png",
297 "blank2" => "blank2.png"
300 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
302 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
305 test "new without shortcode", %{admin_conn: admin_conn} do
306 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
309 |> put_req_header("content-type", "multipart/form-data")
310 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
312 filename: "shortcode.png",
313 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
316 |> json_response_and_validate_schema(200) == %{
317 "shortcode" => "shortcode.png",
318 "blank" => "blank.png",
319 "blank2" => "blank2.png"
323 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
325 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
326 |> json_response_and_validate_schema(:bad_request) == %{
327 "error" => "Emoji \"blank3\" does not exist"
331 test "update non existing emoji", %{admin_conn: admin_conn} do
333 |> put_req_header("content-type", "multipart/form-data")
334 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
336 new_shortcode: "blank4",
337 new_filename: "dir_2/blank_3.png"
339 |> json_response_and_validate_schema(:bad_request) == %{
340 "error" => "Emoji \"blank3\" does not exist"
344 test "update with empty shortcode", %{admin_conn: admin_conn} do
346 "error" => "Missing field: new_shortcode."
349 |> put_req_header("content-type", "multipart/form-data")
350 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
352 new_filename: "dir_2/blank_3.png"
354 |> json_response_and_validate_schema(:bad_request)