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 "create shortcode exists", %{admin_conn: admin_conn} do
46 |> put_req_header("content-type", "multipart/form-data")
47 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
49 filename: "dir/blank.png",
51 filename: "blank.png",
52 path: "#{@emoji_path}/test_pack/blank.png"
55 |> json_response_and_validate_schema(:conflict) == %{
56 "error" => "An emoji with the \"blank\" shortcode already exists"
60 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
61 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
64 |> put_req_header("content-type", "multipart/form-data")
65 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
67 filename: "dir/blank.png",
69 filename: "blank.png",
70 path: "#{@emoji_path}/test_pack/blank.png"
73 |> json_response_and_validate_schema(200) == %{
74 "blank" => "blank.png",
75 "blank2" => "blank2.png",
76 "blank3" => "dir/blank.png"
79 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
82 |> put_req_header("content-type", "multipart/form-data")
83 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
85 new_shortcode: "blank2",
86 new_filename: "dir_2/blank_3.png"
88 |> json_response_and_validate_schema(:conflict) == %{
90 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
94 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
95 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
98 |> put_req_header("content-type", "multipart/form-data")
99 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
101 filename: "dir/blank.png",
103 filename: "blank.png",
104 path: "#{@emoji_path}/test_pack/blank.png"
107 |> json_response_and_validate_schema(200) == %{
108 "blank" => "blank.png",
109 "blank2" => "blank2.png",
110 "blank3" => "dir/blank.png"
113 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
116 |> put_req_header("content-type", "multipart/form-data")
117 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
119 new_shortcode: "blank4",
120 new_filename: "dir_2/blank_3.png",
123 |> json_response_and_validate_schema(200) == %{
124 "blank" => "blank.png",
125 "blank2" => "blank2.png",
126 "blank4" => "dir_2/blank_3.png"
129 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
132 test "with empty filename", %{admin_conn: admin_conn} do
134 |> put_req_header("content-type", "multipart/form-data")
135 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
139 filename: "blank.png",
140 path: "#{@emoji_path}/test_pack/blank.png"
143 |> json_response_and_validate_schema(:bad_request) == %{
144 "error" => "pack name, shortcode or filename cannot be empty"
148 test "add file with not loaded pack", %{admin_conn: admin_conn} do
150 |> put_req_header("content-type", "multipart/form-data")
151 |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
153 filename: "dir/blank.png",
155 filename: "blank.png",
156 path: "#{@emoji_path}/test_pack/blank.png"
159 |> json_response_and_validate_schema(:bad_request) == %{
160 "error" => "pack \"not_loaded\" is not found"
164 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
166 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=blank3")
167 |> json_response_and_validate_schema(:bad_request) == %{
168 "error" => "pack \"not_loaded\" is not found"
172 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
174 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=")
175 |> json_response_and_validate_schema(:bad_request) == %{
176 "error" => "pack name or shortcode cannot be empty"
180 test "update file with not loaded pack", %{admin_conn: admin_conn} do
182 |> put_req_header("content-type", "multipart/form-data")
183 |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
185 new_shortcode: "blank3",
186 new_filename: "dir_2/blank_3.png"
188 |> json_response_and_validate_schema(:bad_request) == %{
189 "error" => "pack \"not_loaded\" is not found"
193 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
195 |> put_req_header("content-type", "multipart/form-data")
196 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
198 filename: "dir/blank.png",
200 filename: "blank.png",
201 path: "#{@emoji_path}/test_pack/blank.png"
204 |> json_response_and_validate_schema(200) == %{
205 "blank" => "blank.png",
206 "blank4" => "dir/blank.png",
207 "blank2" => "blank2.png"
210 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
213 |> put_req_header("content-type", "multipart/form-data")
214 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
216 new_shortcode: "blank3",
217 new_filename: "dir_2/blank_3.png"
219 |> json_response_and_validate_schema(200) == %{
220 "blank3" => "dir_2/blank_3.png",
221 "blank" => "blank.png",
222 "blank2" => "blank2.png"
225 refute File.exists?("#{@emoji_path}/test_pack/dir/")
226 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
229 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
230 |> json_response_and_validate_schema(200) == %{
231 "blank" => "blank.png",
232 "blank2" => "blank2.png"
235 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
237 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
240 test "new with shortcode from url", %{admin_conn: admin_conn} do
244 url: "https://test-blank/blank_url.png"
246 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
250 |> put_req_header("content-type", "multipart/form-data")
251 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
252 shortcode: "blank_url",
253 file: "https://test-blank/blank_url.png"
255 |> json_response_and_validate_schema(200) == %{
256 "blank_url" => "blank_url.png",
257 "blank" => "blank.png",
258 "blank2" => "blank2.png"
261 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
263 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
266 test "new without shortcode", %{admin_conn: admin_conn} do
267 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
270 |> put_req_header("content-type", "multipart/form-data")
271 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
273 filename: "shortcode.png",
274 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
277 |> json_response_and_validate_schema(200) == %{
278 "shortcode" => "shortcode.png",
279 "blank" => "blank.png",
280 "blank2" => "blank2.png"
284 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
286 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
287 |> json_response_and_validate_schema(:bad_request) == %{
288 "error" => "Emoji \"blank3\" does not exist"
292 test "update non existing emoji", %{admin_conn: admin_conn} do
294 |> put_req_header("content-type", "multipart/form-data")
295 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
297 new_shortcode: "blank4",
298 new_filename: "dir_2/blank_3.png"
300 |> json_response_and_validate_schema(:bad_request) == %{
301 "error" => "Emoji \"blank3\" does not exist"
305 test "update with empty shortcode", %{admin_conn: admin_conn} do
307 "error" => "Missing field: new_shortcode."
310 |> put_req_header("content-type", "multipart/form-data")
311 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
313 new_filename: "dir_2/blank_3.png"
315 |> json_response_and_validate_schema(:bad_request)