added Pleroma.Web.PleromaAPI.EmojiFileController
[akkoma] / test / web / pleroma_api / controllers / emoji_file_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiFileControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Tesla.Mock
9 import Pleroma.Factory
10
11 @emoji_path Path.join(
12 Pleroma.Config.get!([:instance, :static_dir]),
13 "emoji"
14 )
15 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
16
17 setup do: clear_config([:instance, :public], true)
18
19 setup do
20 admin = insert(:user, is_admin: true)
21 token = insert(:oauth_admin_token, user: admin)
22
23 admin_conn =
24 build_conn()
25 |> assign(:user, admin)
26 |> assign(:token, token)
27
28 Pleroma.Emoji.reload()
29 {:ok, %{admin_conn: admin_conn}}
30 end
31
32 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/:name/files" do
33 setup do
34 pack_file = "#{@emoji_path}/test_pack/pack.json"
35 original_content = File.read!(pack_file)
36
37 on_exit(fn ->
38 File.write!(pack_file, original_content)
39 end)
40
41 :ok
42 end
43
44 test "create shortcode exists", %{admin_conn: admin_conn} do
45 assert admin_conn
46 |> put_req_header("content-type", "multipart/form-data")
47 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
48 shortcode: "blank",
49 filename: "dir/blank.png",
50 file: %Plug.Upload{
51 filename: "blank.png",
52 path: "#{@emoji_path}/test_pack/blank.png"
53 }
54 })
55 |> json_response_and_validate_schema(:conflict) == %{
56 "error" => "An emoji with the \"blank\" shortcode already exists"
57 }
58 end
59
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)
62
63 assert admin_conn
64 |> put_req_header("content-type", "multipart/form-data")
65 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
66 shortcode: "blank3",
67 filename: "dir/blank.png",
68 file: %Plug.Upload{
69 filename: "blank.png",
70 path: "#{@emoji_path}/test_pack/blank.png"
71 }
72 })
73 |> json_response_and_validate_schema(200) == %{
74 "blank" => "blank.png",
75 "blank2" => "blank2.png",
76 "blank3" => "dir/blank.png"
77 }
78
79 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
80
81 assert admin_conn
82 |> put_req_header("content-type", "multipart/form-data")
83 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
84 shortcode: "blank",
85 new_shortcode: "blank2",
86 new_filename: "dir_2/blank_3.png"
87 })
88 |> json_response_and_validate_schema(:conflict) == %{
89 "error" =>
90 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
91 }
92 end
93
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)
96
97 assert admin_conn
98 |> put_req_header("content-type", "multipart/form-data")
99 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
100 shortcode: "blank3",
101 filename: "dir/blank.png",
102 file: %Plug.Upload{
103 filename: "blank.png",
104 path: "#{@emoji_path}/test_pack/blank.png"
105 }
106 })
107 |> json_response_and_validate_schema(200) == %{
108 "blank" => "blank.png",
109 "blank2" => "blank2.png",
110 "blank3" => "dir/blank.png"
111 }
112
113 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
114
115 assert admin_conn
116 |> put_req_header("content-type", "multipart/form-data")
117 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
118 shortcode: "blank3",
119 new_shortcode: "blank4",
120 new_filename: "dir_2/blank_3.png",
121 force: true
122 })
123 |> json_response_and_validate_schema(200) == %{
124 "blank" => "blank.png",
125 "blank2" => "blank2.png",
126 "blank4" => "dir_2/blank_3.png"
127 }
128
129 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
130 end
131
132 test "with empty filename", %{admin_conn: admin_conn} do
133 assert admin_conn
134 |> put_req_header("content-type", "multipart/form-data")
135 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
136 shortcode: "blank2",
137 filename: "",
138 file: %Plug.Upload{
139 filename: "blank.png",
140 path: "#{@emoji_path}/test_pack/blank.png"
141 }
142 })
143 |> json_response_and_validate_schema(:bad_request) == %{
144 "error" => "pack name, shortcode or filename cannot be empty"
145 }
146 end
147
148 test "add file with not loaded pack", %{admin_conn: admin_conn} do
149 assert admin_conn
150 |> put_req_header("content-type", "multipart/form-data")
151 |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
152 shortcode: "blank3",
153 filename: "dir/blank.png",
154 file: %Plug.Upload{
155 filename: "blank.png",
156 path: "#{@emoji_path}/test_pack/blank.png"
157 }
158 })
159 |> json_response_and_validate_schema(:bad_request) == %{
160 "error" => "pack \"not_loaded\" is not found"
161 }
162 end
163
164 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
165 assert admin_conn
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"
169 }
170 end
171
172 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
173 assert admin_conn
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"
177 }
178 end
179
180 test "update file with not loaded pack", %{admin_conn: admin_conn} do
181 assert admin_conn
182 |> put_req_header("content-type", "multipart/form-data")
183 |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
184 shortcode: "blank4",
185 new_shortcode: "blank3",
186 new_filename: "dir_2/blank_3.png"
187 })
188 |> json_response_and_validate_schema(:bad_request) == %{
189 "error" => "pack \"not_loaded\" is not found"
190 }
191 end
192
193 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
194 assert admin_conn
195 |> put_req_header("content-type", "multipart/form-data")
196 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
197 shortcode: "blank4",
198 filename: "dir/blank.png",
199 file: %Plug.Upload{
200 filename: "blank.png",
201 path: "#{@emoji_path}/test_pack/blank.png"
202 }
203 })
204 |> json_response_and_validate_schema(200) == %{
205 "blank" => "blank.png",
206 "blank4" => "dir/blank.png",
207 "blank2" => "blank2.png"
208 }
209
210 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
211
212 assert admin_conn
213 |> put_req_header("content-type", "multipart/form-data")
214 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
215 shortcode: "blank4",
216 new_shortcode: "blank3",
217 new_filename: "dir_2/blank_3.png"
218 })
219 |> json_response_and_validate_schema(200) == %{
220 "blank3" => "dir_2/blank_3.png",
221 "blank" => "blank.png",
222 "blank2" => "blank2.png"
223 }
224
225 refute File.exists?("#{@emoji_path}/test_pack/dir/")
226 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
227
228 assert admin_conn
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"
233 }
234
235 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
236
237 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
238 end
239
240 test "new with shortcode from url", %{admin_conn: admin_conn} do
241 mock(fn
242 %{
243 method: :get,
244 url: "https://test-blank/blank_url.png"
245 } ->
246 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
247 end)
248
249 assert admin_conn
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"
254 })
255 |> json_response_and_validate_schema(200) == %{
256 "blank_url" => "blank_url.png",
257 "blank" => "blank.png",
258 "blank2" => "blank2.png"
259 }
260
261 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
262
263 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
264 end
265
266 test "new without shortcode", %{admin_conn: admin_conn} do
267 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
268
269 assert admin_conn
270 |> put_req_header("content-type", "multipart/form-data")
271 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
272 file: %Plug.Upload{
273 filename: "shortcode.png",
274 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
275 }
276 })
277 |> json_response_and_validate_schema(200) == %{
278 "shortcode" => "shortcode.png",
279 "blank" => "blank.png",
280 "blank2" => "blank2.png"
281 }
282 end
283
284 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
285 assert admin_conn
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"
289 }
290 end
291
292 test "update non existing emoji", %{admin_conn: admin_conn} do
293 assert admin_conn
294 |> put_req_header("content-type", "multipart/form-data")
295 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
296 shortcode: "blank3",
297 new_shortcode: "blank4",
298 new_filename: "dir_2/blank_3.png"
299 })
300 |> json_response_and_validate_schema(:bad_request) == %{
301 "error" => "Emoji \"blank3\" does not exist"
302 }
303 end
304
305 test "update with empty shortcode", %{admin_conn: admin_conn} do
306 assert %{
307 "error" => "Missing field: new_shortcode."
308 } =
309 admin_conn
310 |> put_req_header("content-type", "multipart/form-data")
311 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
312 shortcode: "blank",
313 new_filename: "dir_2/blank_3.png"
314 })
315 |> json_response_and_validate_schema(:bad_request)
316 end
317 end
318 end