emoji packs pagination
[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"}
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 "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
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: "blank2",
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" => "dir/blank.png"
433 }
434
435 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
436
437 assert admin_conn
438 |> put_req_header("content-type", "multipart/form-data")
439 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
440 shortcode: "blank",
441 new_shortcode: "blank2",
442 new_filename: "dir_2/blank_3.png"
443 })
444 |> json_response_and_validate_schema(:conflict) == %{
445 "error" =>
446 "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
447 }
448 end
449
450 test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
451 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
452
453 assert admin_conn
454 |> put_req_header("content-type", "multipart/form-data")
455 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
456 shortcode: "blank2",
457 filename: "dir/blank.png",
458 file: %Plug.Upload{
459 filename: "blank.png",
460 path: "#{@emoji_path}/test_pack/blank.png"
461 }
462 })
463 |> json_response_and_validate_schema(200) == %{
464 "blank" => "blank.png",
465 "blank2" => "dir/blank.png"
466 }
467
468 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
469
470 assert admin_conn
471 |> put_req_header("content-type", "multipart/form-data")
472 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
473 shortcode: "blank2",
474 new_shortcode: "blank3",
475 new_filename: "dir_2/blank_3.png",
476 force: true
477 })
478 |> json_response_and_validate_schema(200) == %{
479 "blank" => "blank.png",
480 "blank3" => "dir_2/blank_3.png"
481 }
482
483 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
484 end
485
486 test "with empty filename", %{admin_conn: admin_conn} do
487 assert admin_conn
488 |> put_req_header("content-type", "multipart/form-data")
489 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
490 shortcode: "blank2",
491 filename: "",
492 file: %Plug.Upload{
493 filename: "blank.png",
494 path: "#{@emoji_path}/test_pack/blank.png"
495 }
496 })
497 |> json_response_and_validate_schema(:bad_request) == %{
498 "error" => "pack name, shortcode or filename cannot be empty"
499 }
500 end
501
502 test "add file with not loaded pack", %{admin_conn: admin_conn} do
503 assert admin_conn
504 |> put_req_header("content-type", "multipart/form-data")
505 |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
506 shortcode: "blank2",
507 filename: "dir/blank.png",
508 file: %Plug.Upload{
509 filename: "blank.png",
510 path: "#{@emoji_path}/test_pack/blank.png"
511 }
512 })
513 |> json_response_and_validate_schema(:bad_request) == %{
514 "error" => "pack \"not_loaded\" is not found"
515 }
516 end
517
518 test "remove file with not loaded pack", %{admin_conn: admin_conn} do
519 assert admin_conn
520 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=blank3")
521 |> json_response_and_validate_schema(:bad_request) == %{
522 "error" => "pack \"not_loaded\" is not found"
523 }
524 end
525
526 test "remove file with empty shortcode", %{admin_conn: admin_conn} do
527 assert admin_conn
528 |> delete("/api/pleroma/emoji/packs/not_loaded/files?shortcode=")
529 |> json_response_and_validate_schema(:bad_request) == %{
530 "error" => "pack name or shortcode cannot be empty"
531 }
532 end
533
534 test "update file with not loaded pack", %{admin_conn: admin_conn} do
535 assert admin_conn
536 |> put_req_header("content-type", "multipart/form-data")
537 |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
538 shortcode: "blank4",
539 new_shortcode: "blank3",
540 new_filename: "dir_2/blank_3.png"
541 })
542 |> json_response_and_validate_schema(:bad_request) == %{
543 "error" => "pack \"not_loaded\" is not found"
544 }
545 end
546
547 test "new with shortcode as file with update", %{admin_conn: admin_conn} do
548 assert admin_conn
549 |> put_req_header("content-type", "multipart/form-data")
550 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
551 shortcode: "blank4",
552 filename: "dir/blank.png",
553 file: %Plug.Upload{
554 filename: "blank.png",
555 path: "#{@emoji_path}/test_pack/blank.png"
556 }
557 })
558 |> json_response_and_validate_schema(200) == %{
559 "blank" => "blank.png",
560 "blank4" => "dir/blank.png"
561 }
562
563 assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
564
565 assert admin_conn
566 |> put_req_header("content-type", "multipart/form-data")
567 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
568 shortcode: "blank4",
569 new_shortcode: "blank3",
570 new_filename: "dir_2/blank_3.png"
571 })
572 |> json_response_and_validate_schema(200) == %{
573 "blank3" => "dir_2/blank_3.png",
574 "blank" => "blank.png"
575 }
576
577 refute File.exists?("#{@emoji_path}/test_pack/dir/")
578 assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
579
580 assert admin_conn
581 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank3")
582 |> json_response_and_validate_schema(200) == %{"blank" => "blank.png"}
583
584 refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
585
586 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
587 end
588
589 test "new with shortcode from url", %{admin_conn: admin_conn} do
590 mock(fn
591 %{
592 method: :get,
593 url: "https://test-blank/blank_url.png"
594 } ->
595 text(File.read!("#{@emoji_path}/test_pack/blank.png"))
596 end)
597
598 assert admin_conn
599 |> put_req_header("content-type", "multipart/form-data")
600 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
601 shortcode: "blank_url",
602 file: "https://test-blank/blank_url.png"
603 })
604 |> json_response_and_validate_schema(200) == %{
605 "blank_url" => "blank_url.png",
606 "blank" => "blank.png"
607 }
608
609 assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
610
611 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
612 end
613
614 test "new without shortcode", %{admin_conn: admin_conn} do
615 on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
616
617 assert admin_conn
618 |> put_req_header("content-type", "multipart/form-data")
619 |> post("/api/pleroma/emoji/packs/test_pack/files", %{
620 file: %Plug.Upload{
621 filename: "shortcode.png",
622 path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
623 }
624 })
625 |> json_response_and_validate_schema(200) == %{
626 "shortcode" => "shortcode.png",
627 "blank" => "blank.png"
628 }
629 end
630
631 test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
632 assert admin_conn
633 |> delete("/api/pleroma/emoji/packs/test_pack/files?shortcode=blank2")
634 |> json_response_and_validate_schema(:bad_request) == %{
635 "error" => "Emoji \"blank2\" does not exist"
636 }
637 end
638
639 test "update non existing emoji", %{admin_conn: admin_conn} do
640 assert admin_conn
641 |> put_req_header("content-type", "multipart/form-data")
642 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
643 shortcode: "blank2",
644 new_shortcode: "blank3",
645 new_filename: "dir_2/blank_3.png"
646 })
647 |> json_response_and_validate_schema(:bad_request) == %{
648 "error" => "Emoji \"blank2\" does not exist"
649 }
650 end
651
652 test "update with empty shortcode", %{admin_conn: admin_conn} do
653 assert %{
654 "error" => "Missing field: new_shortcode."
655 } =
656 admin_conn
657 |> put_req_header("content-type", "multipart/form-data")
658 |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
659 shortcode: "blank",
660 new_filename: "dir_2/blank_3.png"
661 })
662 |> json_response_and_validate_schema(:bad_request)
663 end
664 end
665
666 describe "POST/DELETE /api/pleroma/emoji/packs/:name" do
667 test "creating and deleting a pack", %{admin_conn: admin_conn} do
668 assert admin_conn
669 |> post("/api/pleroma/emoji/packs/test_created")
670 |> json_response_and_validate_schema(200) == "ok"
671
672 assert File.exists?("#{@emoji_path}/test_created/pack.json")
673
674 assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
675 "pack" => %{},
676 "files" => %{}
677 }
678
679 assert admin_conn
680 |> delete("/api/pleroma/emoji/packs/test_created")
681 |> json_response_and_validate_schema(200) == "ok"
682
683 refute File.exists?("#{@emoji_path}/test_created/pack.json")
684 end
685
686 test "if pack exists", %{admin_conn: admin_conn} do
687 path = Path.join(@emoji_path, "test_created")
688 File.mkdir(path)
689 pack_file = Jason.encode!(%{files: %{}, pack: %{}})
690 File.write!(Path.join(path, "pack.json"), pack_file)
691
692 assert admin_conn
693 |> post("/api/pleroma/emoji/packs/test_created")
694 |> json_response_and_validate_schema(:conflict) == %{
695 "error" => "A pack named \"test_created\" already exists"
696 }
697
698 on_exit(fn -> File.rm_rf(path) end)
699 end
700
701 test "with empty name", %{admin_conn: admin_conn} do
702 assert admin_conn
703 |> post("/api/pleroma/emoji/packs/ ")
704 |> json_response_and_validate_schema(:bad_request) == %{
705 "error" => "pack name cannot be empty"
706 }
707 end
708 end
709
710 test "deleting nonexisting pack", %{admin_conn: admin_conn} do
711 assert admin_conn
712 |> delete("/api/pleroma/emoji/packs/non_existing")
713 |> json_response_and_validate_schema(:not_found) == %{
714 "error" => "Pack non_existing does not exist"
715 }
716 end
717
718 test "deleting with empty name", %{admin_conn: admin_conn} do
719 assert admin_conn
720 |> delete("/api/pleroma/emoji/packs/ ")
721 |> json_response_and_validate_schema(:bad_request) == %{
722 "error" => "pack name cannot be empty"
723 }
724 end
725
726 test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
727 on_exit(fn ->
728 File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
729 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
730 end)
731
732 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
733
734 refute Map.has_key?(resp, "test_pack_for_import")
735
736 assert admin_conn
737 |> get("/api/pleroma/emoji/packs/import")
738 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
739
740 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
741 assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
742
743 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
744 refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
745
746 emoji_txt_content = """
747 blank, blank.png, Fun
748 blank2, blank.png
749 foo, /emoji/test_pack_for_import/blank.png
750 bar
751 """
752
753 File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
754
755 assert admin_conn
756 |> get("/api/pleroma/emoji/packs/import")
757 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
758
759 resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
760
761 assert resp["test_pack_for_import"]["files"] == %{
762 "blank" => "blank.png",
763 "blank2" => "blank.png",
764 "foo" => "blank.png"
765 }
766 end
767
768 describe "GET /api/pleroma/emoji/packs/:name" do
769 test "shows pack.json", %{conn: conn} do
770 assert %{
771 "files" => %{"blank" => "blank.png"},
772 "pack" => %{
773 "can-download" => true,
774 "description" => "Test description",
775 "download-sha256" => _,
776 "homepage" => "https://pleroma.social",
777 "license" => "Test license",
778 "share-files" => true
779 }
780 } =
781 conn
782 |> get("/api/pleroma/emoji/packs/test_pack")
783 |> json_response_and_validate_schema(200)
784 end
785
786 test "non existing pack", %{conn: conn} do
787 assert conn
788 |> get("/api/pleroma/emoji/packs/non_existing")
789 |> json_response_and_validate_schema(:not_found) == %{
790 "error" => "Pack non_existing does not exist"
791 }
792 end
793
794 test "error name", %{conn: conn} do
795 assert conn
796 |> get("/api/pleroma/emoji/packs/ ")
797 |> json_response_and_validate_schema(:bad_request) == %{
798 "error" => "pack name cannot be empty"
799 }
800 end
801 end
802 end