f6239cae56260c123e23155f8d5ad78ec49a0b5e
[akkoma] / test / web / pleroma_api / controllers / emoji_pack_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.EmojiPackControllerTest 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
18 admin = insert(:user, is_admin: true)
19 token = insert(:oauth_admin_token, user: admin)
20
21 admin_conn =
22 build_conn()
23 |> assign(:user, admin)
24 |> assign(:token, token)
25
26 Pleroma.Emoji.reload()
27 {:ok, %{admin_conn: admin_conn}}
28 end
29
30 test "GET /api/pleroma/emoji/packs", %{conn: conn} do
31 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
32
33 shared = resp["test_pack"]
34 assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
35 assert Map.has_key?(shared["pack"], "download-sha256")
36 assert shared["pack"]["can-download"]
37 assert shared["pack"]["share-files"]
38
39 non_shared = resp["test_pack_nonshared"]
40 assert non_shared["pack"]["share-files"] == false
41 assert non_shared["pack"]["can-download"] == false
42
43 resp =
44 conn
45 |> get("/api/pleroma/emoji/packs?page_size=1")
46 |> json_response_and_validate_schema(200)
47
48 [pack1] = Map.keys(resp)
49
50 resp =
51 conn
52 |> get("/api/pleroma/emoji/packs?page_size=1&page=2")
53 |> json_response_and_validate_schema(200)
54
55 [pack2] = Map.keys(resp)
56
57 resp =
58 conn
59 |> get("/api/pleroma/emoji/packs?page_size=1&page=3")
60 |> json_response_and_validate_schema(200)
61
62 [pack3] = Map.keys(resp)
63 assert [pack1, pack2, pack3] |> Enum.uniq() |> length() == 3
64 end
65
66 describe "GET /api/pleroma/emoji/packs/remote" do
67 test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
68 resp =
69 conn
70 |> get("/api/pleroma/emoji/packs")
71 |> json_response_and_validate_schema(200)
72
73 mock(fn
74 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
75 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
76
77 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
78 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
79
80 %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
81 json(resp)
82 end)
83
84 assert admin_conn
85 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
86 |> json_response_and_validate_schema(200) == resp
87 end
88
89 test "non shareable instance", %{admin_conn: admin_conn} do
90 mock(fn
91 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
92 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
93
94 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
95 json(%{metadata: %{features: []}})
96 end)
97
98 assert admin_conn
99 |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
100 |> json_response_and_validate_schema(500) == %{
101 "error" => "The requested instance does not support sharing emoji packs"
102 }
103 end
104 end
105
106 describe "GET /api/pleroma/emoji/packs/:name/archive" do
107 test "download shared pack", %{conn: conn} do
108 resp =
109 conn
110 |> get("/api/pleroma/emoji/packs/test_pack/archive")
111 |> response(200)
112
113 {:ok, arch} = :zip.unzip(resp, [:memory])
114
115 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
116 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
117 end
118
119 test "non existing pack", %{conn: conn} do
120 assert conn
121 |> get("/api/pleroma/emoji/packs/test_pack_for_import/archive")
122 |> json_response_and_validate_schema(:not_found) == %{
123 "error" => "Pack test_pack_for_import does not exist"
124 }
125 end
126
127 test "non downloadable pack", %{conn: conn} do
128 assert conn
129 |> get("/api/pleroma/emoji/packs/test_pack_nonshared/archive")
130 |> json_response_and_validate_schema(:forbidden) == %{
131 "error" =>
132 "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
133 }
134 end
135 end
136
137 describe "POST /api/pleroma/emoji/packs/download" do
138 test "shared pack from remote and non shared from fallback-src", %{
139 admin_conn: admin_conn,
140 conn: conn
141 } do
142 mock(fn
143 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
144 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
145
146 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
147 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
148
149 %{
150 method: :get,
151 url: "https://example.com/api/pleroma/emoji/packs/test_pack"
152 } ->
153 conn
154 |> get("/api/pleroma/emoji/packs/test_pack")
155 |> json_response_and_validate_schema(200)
156 |> json()
157
158 %{
159 method: :get,
160 url: "https://example.com/api/pleroma/emoji/packs/test_pack/archive"
161 } ->
162 conn
163 |> get("/api/pleroma/emoji/packs/test_pack/archive")
164 |> response(200)
165 |> text()
166
167 %{
168 method: :get,
169 url: "https://example.com/api/pleroma/emoji/packs/test_pack_nonshared"
170 } ->
171 conn
172 |> get("/api/pleroma/emoji/packs/test_pack_nonshared")
173 |> json_response_and_validate_schema(200)
174 |> json()
175
176 %{
177 method: :get,
178 url: "https://nonshared-pack"
179 } ->
180 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
181 end)
182
183 assert admin_conn
184 |> put_req_header("content-type", "multipart/form-data")
185 |> post("/api/pleroma/emoji/packs/download", %{
186 url: "https://example.com",
187 name: "test_pack",
188 as: "test_pack2"
189 })
190 |> json_response_and_validate_schema(200) == "ok"
191
192 assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
193 assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
194
195 assert admin_conn
196 |> delete("/api/pleroma/emoji/packs/test_pack2")
197 |> json_response_and_validate_schema(200) == "ok"
198
199 refute File.exists?("#{@emoji_path}/test_pack2")
200
201 assert admin_conn
202 |> put_req_header("content-type", "multipart/form-data")
203 |> post(
204 "/api/pleroma/emoji/packs/download",
205 %{
206 url: "https://example.com",
207 name: "test_pack_nonshared",
208 as: "test_pack_nonshared2"
209 }
210 )
211 |> json_response_and_validate_schema(200) == "ok"
212
213 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
214 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
215
216 assert admin_conn
217 |> delete("/api/pleroma/emoji/packs/test_pack_nonshared2")
218 |> json_response_and_validate_schema(200) == "ok"
219
220 refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
221 end
222
223 test "nonshareable instance", %{admin_conn: admin_conn} do
224 mock(fn
225 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
226 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
227
228 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
229 json(%{metadata: %{features: []}})
230 end)
231
232 assert admin_conn
233 |> put_req_header("content-type", "multipart/form-data")
234 |> post(
235 "/api/pleroma/emoji/packs/download",
236 %{
237 url: "https://old-instance",
238 name: "test_pack",
239 as: "test_pack2"
240 }
241 )
242 |> json_response_and_validate_schema(500) == %{
243 "error" => "The requested instance does not support sharing emoji packs"
244 }
245 end
246
247 test "checksum fail", %{admin_conn: admin_conn} do
248 mock(fn
249 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
250 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
251
252 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
253 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
254
255 %{
256 method: :get,
257 url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha"
258 } ->
259 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
260 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
261
262 %{
263 method: :get,
264 url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha/archive"
265 } ->
266 %Tesla.Env{
267 status: 200,
268 body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
269 }
270 end)
271
272 assert admin_conn
273 |> put_req_header("content-type", "multipart/form-data")
274 |> post("/api/pleroma/emoji/packs/download", %{
275 url: "https://example.com",
276 name: "pack_bad_sha",
277 as: "pack_bad_sha2"
278 })
279 |> json_response_and_validate_schema(:internal_server_error) == %{
280 "error" => "SHA256 for the pack doesn't match the one sent by the server"
281 }
282 end
283
284 test "other error", %{admin_conn: admin_conn} do
285 mock(fn
286 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
287 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
288
289 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
290 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
291
292 %{
293 method: :get,
294 url: "https://example.com/api/pleroma/emoji/packs/test_pack"
295 } ->
296 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
297 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
298 end)
299
300 assert admin_conn
301 |> put_req_header("content-type", "multipart/form-data")
302 |> post("/api/pleroma/emoji/packs/download", %{
303 url: "https://example.com",
304 name: "test_pack",
305 as: "test_pack2"
306 })
307 |> json_response_and_validate_schema(:internal_server_error) == %{
308 "error" =>
309 "The pack was not set as shared and there is no fallback src to download from"
310 }
311 end
312 end
313
314 describe "PATCH /api/pleroma/emoji/packs/:name" do
315 setup do
316 pack_file = "#{@emoji_path}/test_pack/pack.json"
317 original_content = File.read!(pack_file)
318
319 on_exit(fn ->
320 File.write!(pack_file, original_content)
321 end)
322
323 {:ok,
324 pack_file: pack_file,
325 new_data: %{
326 "license" => "Test license changed",
327 "homepage" => "https://pleroma.social",
328 "description" => "Test description",
329 "share-files" => false
330 }}
331 end
332
333 test "for a pack without a fallback source", ctx do
334 assert ctx[:admin_conn]
335 |> put_req_header("content-type", "multipart/form-data")
336 |> patch("/api/pleroma/emoji/packs/test_pack", %{"metadata" => ctx[:new_data]})
337 |> json_response_and_validate_schema(200) == ctx[:new_data]
338
339 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
340 end
341
342 test "for a pack with a fallback source", ctx do
343 mock(fn
344 %{
345 method: :get,
346 url: "https://nonshared-pack"
347 } ->
348 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
349 end)
350
351 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
352
353 new_data_with_sha =
354 Map.put(
355 new_data,
356 "fallback-src-sha256",
357 "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
358 )
359
360 assert ctx[:admin_conn]
361 |> put_req_header("content-type", "multipart/form-data")
362 |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
363 |> json_response_and_validate_schema(200) == new_data_with_sha
364
365 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
366 end
367
368 test "when the fallback source doesn't have all the files", ctx do
369 mock(fn
370 %{
371 method: :get,
372 url: "https://nonshared-pack"
373 } ->
374 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
375 text(empty_arch)
376 end)
377
378 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
379
380 assert ctx[:admin_conn]
381 |> put_req_header("content-type", "multipart/form-data")
382 |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
383 |> json_response_and_validate_schema(:bad_request) == %{
384 "error" => "The fallback archive does not have all files specified in pack.json"
385 }
386 end
387 end
388
389 describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/:name/files" do
390 setup do
391 pack_file = "#{@emoji_path}/test_pack/pack.json"
392 original_content = File.read!(pack_file)
393
394 on_exit(fn ->
395 File.write!(pack_file, original_content)
396 end)
397
398 :ok
399 end
400
401 test "create shortcode exists", %{admin_conn: admin_conn} do
402 assert admin_conn
403 |> put_req_header("content-type", "multipart/form-data")
404 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
405 shortcode: "blank",
406 filename: "dir/blank.png",
407 file: %Plug.Upload{
408 filename: "blank.png",
409 path: "#{@emoji_path}/test_pack/blank.png"
410 }
411 })
412 |> json_response_and_validate_schema(:conflict) == %{
413 "error" => "An emoji with the \"blank\" shortcode already exists"
414 }
415 end
416
417 test "don't rewrite old emoji", %{admin_conn: admin_conn} do
418 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
419
420 assert admin_conn
421 |> put_req_header("content-type", "multipart/form-data")
422 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
423 shortcode: "blank3",
424 filename: "dir/blank.png",
425 file: %Plug.Upload{
426 filename: "blank.png",
427 path: "#{@emoji_path}/test_pack/blank.png"
428 }
429 })
430 |> json_response_and_validate_schema(200) == %{
431 "blank" => "blank.png",
432 "blank2" => "blank2.png",
433 "blank3" => "dir/blank.png"
434 }
435
436 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
437
438 assert admin_conn
439 |> put_req_header("content-type", "multipart/form-data")
440 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
441 shortcode: "blank",
442 new_shortcode: "blank2",
443 new_filename: "dir_2/blank_3.png"
444 })
445 |> json_response_and_validate_schema(:conflict) == %{
446 "error" =>
447 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
448 }
449 end
450
451 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
452 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
453
454 assert admin_conn
455 |> put_req_header("content-type", "multipart/form-data")
456 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
457 shortcode: "blank3",
458 filename: "dir/blank.png",
459 file: %Plug.Upload{
460 filename: "blank.png",
461 path: "#{@emoji_path}/test_pack/blank.png"
462 }
463 })
464 |> json_response_and_validate_schema(200) == %{
465 "blank" => "blank.png",
466 "blank2" => "blank2.png",
467 "blank3" => "dir/blank.png"
468 }
469
470 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
471
472 assert admin_conn
473 |> put_req_header("content-type", "multipart/form-data")
474 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
475 shortcode: "blank3",
476 new_shortcode: "blank4",
477 new_filename: "dir_2/blank_3.png",
478 force: true
479 })
480 |> json_response_and_validate_schema(200) == %{
481 "blank" => "blank.png",
482 "blank2" => "blank2.png",
483 "blank4" => "dir_2/blank_3.png"
484 }
485
486 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
487 end
488
489 test "with empty filename", %{admin_conn: admin_conn} do
490 assert admin_conn
491 |> put_req_header("content-type", "multipart/form-data")
492 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
493 shortcode: "blank2",
494 filename: "",
495 file: %Plug.Upload{
496 filename: "blank.png",
497 path: "#{@emoji_path}/test_pack/blank.png"
498 }
499 })
500 |> json_response_and_validate_schema(:bad_request) == %{
501 "error" => "pack name, shortcode or filename cannot be empty"
502 }
503 end
504
505 test "add file with not loaded pack", %{admin_conn: admin_conn} do
506 assert admin_conn
507 |> put_req_header("content-type", "multipart/form-data")
508 |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
509 shortcode: "blank3",
510 filename: "dir/blank.png",
511 file: %Plug.Upload{
512 filename: "blank.png",
513 path: "#{@emoji_path}/test_pack/blank.png"
514 }
515 })
516 |> json_response_and_validate_schema(:bad_request) == %{
517 "error" => "pack \"not_loaded\" is not found"
518 }
519 end
520
521 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
522 assert admin_conn
523 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=blank3")
524 |> json_response_and_validate_schema(:bad_request) == %{
525 "error" => "pack \"not_loaded\" is not found"
526 }
527 end
528
529 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
530 assert admin_conn
531 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=")
532 |> json_response_and_validate_schema(:bad_request) == %{
533 "error" => "pack name or shortcode cannot be empty"
534 }
535 end
536
537 test "update file with not loaded pack", %{admin_conn: admin_conn} do
538 assert admin_conn
539 |> put_req_header("content-type", "multipart/form-data")
540 |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
541 shortcode: "blank4",
542 new_shortcode: "blank3",
543 new_filename: "dir_2/blank_3.png"
544 })
545 |> json_response_and_validate_schema(:bad_request) == %{
546 "error" => "pack \"not_loaded\" is not found"
547 }
548 end
549
550 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
551 assert admin_conn
552 |> put_req_header("content-type", "multipart/form-data")
553 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
554 shortcode: "blank4",
555 filename: "dir/blank.png",
556 file: %Plug.Upload{
557 filename: "blank.png",
558 path: "#{@emoji_path}/test_pack/blank.png"
559 }
560 })
561 |> json_response_and_validate_schema(200) == %{
562 "blank" => "blank.png",
563 "blank4" => "dir/blank.png",
564 "blank2" => "blank2.png"
565 }
566
567 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
568
569 assert admin_conn
570 |> put_req_header("content-type", "multipart/form-data")
571 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
572 shortcode: "blank4",
573 new_shortcode: "blank3",
574 new_filename: "dir_2/blank_3.png"
575 })
576 |> json_response_and_validate_schema(200) == %{
577 "blank3" => "dir_2/blank_3.png",
578 "blank" => "blank.png",
579 "blank2" => "blank2.png"
580 }
581
582 refute File.exists?("#{@emoji_path}/test_pack/dir/")
583 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
584
585 assert admin_conn
586 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
587 |> json_response_and_validate_schema(200) == %{
588 "blank" => "blank.png",
589 "blank2" => "blank2.png"
590 }
591
592 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
593
594 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
595 end
596
597 test "new with shortcode from url", %{admin_conn: admin_conn} do
598 mock(fn
599 %{
600 method: :get,
601 url: "https://test-blank/blank_url.png"
602 } ->
603 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
604 end)
605
606 assert admin_conn
607 |> put_req_header("content-type", "multipart/form-data")
608 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
609 shortcode: "blank_url",
610 file: "https://test-blank/blank_url.png"
611 })
612 |> json_response_and_validate_schema(200) == %{
613 "blank_url" => "blank_url.png",
614 "blank" => "blank.png",
615 "blank2" => "blank2.png"
616 }
617
618 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
619
620 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
621 end
622
623 test "new without shortcode", %{admin_conn: admin_conn} do
624 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
625
626 assert admin_conn
627 |> put_req_header("content-type", "multipart/form-data")
628 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
629 file: %Plug.Upload{
630 filename: "shortcode.png",
631 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
632 }
633 })
634 |> json_response_and_validate_schema(200) == %{
635 "shortcode" => "shortcode.png",
636 "blank" => "blank.png",
637 "blank2" => "blank2.png"
638 }
639 end
640
641 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
642 assert admin_conn
643 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
644 |> json_response_and_validate_schema(:bad_request) == %{
645 "error" => "Emoji \"blank3\" does not exist"
646 }
647 end
648
649 test "update non existing emoji", %{admin_conn: admin_conn} do
650 assert admin_conn
651 |> put_req_header("content-type", "multipart/form-data")
652 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
653 shortcode: "blank3",
654 new_shortcode: "blank4",
655 new_filename: "dir_2/blank_3.png"
656 })
657 |> json_response_and_validate_schema(:bad_request) == %{
658 "error" => "Emoji \"blank3\" does not exist"
659 }
660 end
661
662 test "update with empty shortcode", %{admin_conn: admin_conn} do
663 assert %{
664 "error" => "Missing field: new_shortcode."
665 } =
666 admin_conn
667 |> put_req_header("content-type", "multipart/form-data")
668 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
669 shortcode: "blank",
670 new_filename: "dir_2/blank_3.png"
671 })
672 |> json_response_and_validate_schema(:bad_request)
673 end
674 end
675
676 describe "POST/DELETE /api/pleroma/emoji/packs/:name" do
677 test "creating and deleting a pack", %{admin_conn: admin_conn} do
678 assert admin_conn
679 |> post("/api/pleroma/emoji/packs/test_created")
680 |> json_response_and_validate_schema(200) == "ok"
681
682 assert File.exists?("#{@emoji_path}/test_created/pack.json")
683
684 assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
685 "pack" => %{},
686 "files" => %{}
687 }
688
689 assert admin_conn
690 |> delete("/api/pleroma/emoji/packs/test_created")
691 |> json_response_and_validate_schema(200) == "ok"
692
693 refute File.exists?("#{@emoji_path}/test_created/pack.json")
694 end
695
696 test "if pack exists", %{admin_conn: admin_conn} do
697 path = Path.join(@emoji_path, "test_created")
698 File.mkdir(path)
699 pack_file = Jason.encode!(%{files: %{}, pack: %{}})
700 File.write!(Path.join(path, "pack.json"), pack_file)
701
702 assert admin_conn
703 |> post("/api/pleroma/emoji/packs/test_created")
704 |> json_response_and_validate_schema(:conflict) == %{
705 "error" => "A pack named \"test_created\" already exists"
706 }
707
708 on_exit(fn -> File.rm_rf(path) end)
709 end
710
711 test "with empty name", %{admin_conn: admin_conn} do
712 assert admin_conn
713 |> post("/api/pleroma/emoji/packs/ ")
714 |> json_response_and_validate_schema(:bad_request) == %{
715 "error" => "pack name cannot be empty"
716 }
717 end
718 end
719
720 test "deleting nonexisting pack", %{admin_conn: admin_conn} do
721 assert admin_conn
722 |> delete("/api/pleroma/emoji/packs/non_existing")
723 |> json_response_and_validate_schema(:not_found) == %{
724 "error" => "Pack non_existing does not exist"
725 }
726 end
727
728 test "deleting with empty name", %{admin_conn: admin_conn} do
729 assert admin_conn
730 |> delete("/api/pleroma/emoji/packs/ ")
731 |> json_response_and_validate_schema(:bad_request) == %{
732 "error" => "pack name cannot be empty"
733 }
734 end
735
736 test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
737 on_exit(fn ->
738 File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
739 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
740 end)
741
742 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
743
744 refute Map.has_key?(resp, "test_pack_for_import")
745
746 assert admin_conn
747 |> get("/api/pleroma/emoji/packs/import")
748 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
749
750 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
751 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
752
753 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
754 refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
755
756 emoji_txt_content = """
757 blank, blank.png, Fun
758 blank2, blank.png
759 foo, /emoji/test_pack_for_import/blank.png
760 bar
761 """
762
763 File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
764
765 assert admin_conn
766 |> get("/api/pleroma/emoji/packs/import")
767 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
768
769 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
770
771 assert resp["test_pack_for_import"]["files"] == %{
772 "blank" => "blank.png",
773 "blank2" => "blank.png",
774 "foo" => "blank.png"
775 }
776 end
777
778 describe "GET /api/pleroma/emoji/packs/:name" do
779 test "shows pack.json", %{conn: conn} do
780 assert %{
781 "files" => files,
782 "pack" => %{
783 "can-download" => true,
784 "description" => "Test description",
785 "download-sha256" => _,
786 "homepage" => "https://pleroma.social",
787 "license" => "Test license",
788 "share-files" => true
789 }
790 } =
791 conn
792 |> get("/api/pleroma/emoji/packs/test_pack")
793 |> json_response_and_validate_schema(200)
794
795 assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
796
797 assert %{
798 "files" => files
799 } =
800 conn
801 |> get("/api/pleroma/emoji/packs/test_pack?page_size=1")
802 |> json_response_and_validate_schema(200)
803
804 assert files |> Map.keys() |> length() == 1
805
806 assert %{
807 "files" => files
808 } =
809 conn
810 |> get("/api/pleroma/emoji/packs/test_pack?page_size=1&page=2")
811 |> json_response_and_validate_schema(200)
812
813 assert files |> Map.keys() |> length() == 1
814 end
815
816 test "non existing pack", %{conn: conn} do
817 assert conn
818 |> get("/api/pleroma/emoji/packs/non_existing")
819 |> json_response_and_validate_schema(:not_found) == %{
820 "error" => "Pack non_existing does not exist"
821 }
822 end
823
824 test "error name", %{conn: conn} do
825 assert conn
826 |> get("/api/pleroma/emoji/packs/ ")
827 |> json_response_and_validate_schema(:bad_request) == %{
828 "error" => "pack name cannot be empty"
829 }
830 end
831 end
832 end