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