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