giant massive dep upgrade and dialyxir-found error emporium (#371)
[akkoma] / test / pleroma / web / pleroma_api / controllers / emoji_pack_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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, async: false
7
8 import Mock
9 import Tesla.Mock
10 import Pleroma.Factory
11
12 @emoji_path Path.join(
13 Pleroma.Config.get!([:instance, :static_dir]),
14 "emoji"
15 )
16
17 setup do: clear_config([:instance, :public], true)
18
19 setup do
20 admin = insert(:user, is_admin: true)
21 token = insert(:oauth_admin_token, user: admin)
22
23 admin_conn =
24 build_conn()
25 |> assign(:user, admin)
26 |> assign(:token, token)
27
28 Pleroma.Emoji.reload()
29 {:ok, %{admin_conn: admin_conn}}
30 end
31
32 test "GET /api/v1/pleroma/emoji/packs when :public: false", %{conn: conn} do
33 clear_config([:instance, :public], false)
34 conn |> get("/api/v1/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
35 end
36
37 test "GET /api/v1/pleroma/emoji/packs", %{conn: conn} do
38 resp =
39 conn
40 |> get("/api/v1/pleroma/emoji/packs")
41 |> json_response_and_validate_schema(200)
42
43 assert resp["count"] == 4
44
45 assert resp["packs"]
46 |> Map.keys()
47 |> length() == 4
48
49 shared = resp["packs"]["test_pack"]
50 assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
51 assert Map.has_key?(shared["pack"], "download-sha256")
52 assert shared["pack"]["can-download"]
53 assert shared["pack"]["share-files"]
54
55 non_shared = resp["packs"]["test_pack_nonshared"]
56 assert non_shared["pack"]["share-files"] == false
57 assert non_shared["pack"]["can-download"] == false
58
59 resp =
60 conn
61 |> get("/api/v1/pleroma/emoji/packs?page_size=1")
62 |> json_response_and_validate_schema(200)
63
64 assert resp["count"] == 4
65
66 packs = Map.keys(resp["packs"])
67
68 assert length(packs) == 1
69
70 [pack1] = packs
71
72 resp =
73 conn
74 |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=2")
75 |> json_response_and_validate_schema(200)
76
77 assert resp["count"] == 4
78 packs = Map.keys(resp["packs"])
79 assert length(packs) == 1
80 [pack2] = packs
81
82 resp =
83 conn
84 |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=3")
85 |> json_response_and_validate_schema(200)
86
87 assert resp["count"] == 4
88 packs = Map.keys(resp["packs"])
89 assert length(packs) == 1
90 [pack3] = packs
91
92 resp =
93 conn
94 |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=4")
95 |> json_response_and_validate_schema(200)
96
97 assert resp["count"] == 4
98 packs = Map.keys(resp["packs"])
99 assert length(packs) == 1
100 [pack4] = packs
101 assert [pack1, pack2, pack3, pack4] |> Enum.uniq() |> length() == 4
102 end
103
104 describe "GET /api/v1/pleroma/emoji/packs/remote" do
105 test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
106 resp =
107 conn
108 |> get("/api/v1/pleroma/emoji/packs?page=2&page_size=1")
109 |> json_response_and_validate_schema(200)
110
111 mock(fn
112 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
113 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
114
115 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
116 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
117
118 %{
119 method: :get,
120 url: "https://example.com/api/v1/pleroma/emoji/packs?page=2&page_size=1"
121 } ->
122 json(resp)
123 end)
124
125 assert admin_conn
126 |> get(
127 "/api/v1/pleroma/emoji/packs/remote?url=https://example.com&page=2&page_size=1"
128 )
129 |> json_response_and_validate_schema(200) == resp
130 end
131
132 test "non shareable instance", %{admin_conn: admin_conn} do
133 mock(fn
134 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
135 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
136
137 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
138 json(%{metadata: %{features: []}})
139 end)
140
141 assert admin_conn
142 |> get("/api/v1/pleroma/emoji/packs/remote?url=https://example.com")
143 |> json_response_and_validate_schema(500) == %{
144 "error" => "The requested instance does not support sharing emoji packs"
145 }
146 end
147 end
148
149 describe "GET /api/v1/pleroma/emoji/packs/archive?name=:name" do
150 test "download shared pack", %{conn: conn} do
151 resp =
152 conn
153 |> get("/api/v1/pleroma/emoji/packs/archive?name=test_pack")
154 |> response(200)
155
156 {:ok, arch} = :zip.unzip(resp, [:memory])
157
158 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
159 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
160 end
161
162 test "non existing pack", %{conn: conn} do
163 assert conn
164 |> get("/api/v1/pleroma/emoji/packs/archive?name=test_pack_for_import")
165 |> json_response_and_validate_schema(:not_found) == %{
166 "error" => "Pack test_pack_for_import does not exist"
167 }
168 end
169
170 test "non downloadable pack", %{conn: conn} do
171 assert conn
172 |> get("/api/v1/pleroma/emoji/packs/archive?name=test_pack_nonshared")
173 |> json_response_and_validate_schema(:forbidden) == %{
174 "error" =>
175 "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
176 }
177 end
178 end
179
180 describe "POST /api/v1/pleroma/emoji/packs/download" do
181 test "shared pack from remote and non shared from fallback-src", %{
182 admin_conn: admin_conn,
183 conn: conn
184 } do
185 mock(fn
186 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
187 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
188
189 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
190 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
191
192 %{
193 method: :get,
194 url: "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack"
195 } ->
196 conn
197 |> get("/api/v1/pleroma/emoji/pack?name=test_pack")
198 |> json_response_and_validate_schema(200)
199 |> json()
200
201 %{
202 method: :get,
203 url: "https://example.com/api/v1/pleroma/emoji/packs/archive?name=test_pack"
204 } ->
205 conn
206 |> get("/api/v1/pleroma/emoji/packs/archive?name=test_pack")
207 |> response(200)
208 |> text()
209
210 %{
211 method: :get,
212 url: "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack_nonshared"
213 } ->
214 conn
215 |> get("/api/v1/pleroma/emoji/pack?name=test_pack_nonshared")
216 |> json_response_and_validate_schema(200)
217 |> json()
218
219 %{
220 method: :get,
221 url: "https://nonshared-pack"
222 } ->
223 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
224 end)
225
226 assert admin_conn
227 |> put_req_header("content-type", "multipart/form-data")
228 |> post("/api/v1/pleroma/emoji/packs/download", %{
229 url: "https://example.com",
230 name: "test_pack",
231 as: "test_pack2"
232 })
233 |> json_response_and_validate_schema(200) == "ok"
234
235 assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
236 assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
237
238 assert admin_conn
239 |> delete("/api/v1/pleroma/emoji/pack?name=test_pack2")
240 |> json_response_and_validate_schema(200) == "ok"
241
242 refute File.exists?("#{@emoji_path}/test_pack2")
243
244 assert admin_conn
245 |> put_req_header("content-type", "multipart/form-data")
246 |> post(
247 "/api/v1/pleroma/emoji/packs/download",
248 %{
249 url: "https://example.com",
250 name: "test_pack_nonshared",
251 as: "test_pack_nonshared2"
252 }
253 )
254 |> json_response_and_validate_schema(200) == "ok"
255
256 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
257 assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
258
259 assert admin_conn
260 |> delete("/api/v1/pleroma/emoji/pack?name=test_pack_nonshared2")
261 |> json_response_and_validate_schema(200) == "ok"
262
263 refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
264 end
265
266 test "nonshareable instance", %{admin_conn: admin_conn} do
267 mock(fn
268 %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
269 json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
270
271 %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
272 json(%{metadata: %{features: []}})
273 end)
274
275 assert admin_conn
276 |> put_req_header("content-type", "multipart/form-data")
277 |> post(
278 "/api/v1/pleroma/emoji/packs/download",
279 %{
280 url: "https://old-instance",
281 name: "test_pack",
282 as: "test_pack2"
283 }
284 )
285 |> json_response_and_validate_schema(500) == %{
286 "error" => "The requested instance does not support sharing emoji packs"
287 }
288 end
289
290 test "checksum fail", %{admin_conn: admin_conn} do
291 mock(fn
292 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
293 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
294
295 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
296 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
297
298 %{
299 method: :get,
300 url: "https://example.com/api/v1/pleroma/emoji/pack?name=pack_bad_sha"
301 } ->
302 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
303 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
304
305 %{
306 method: :get,
307 url: "https://example.com/api/v1/pleroma/emoji/packs/archive?name=pack_bad_sha"
308 } ->
309 %Tesla.Env{
310 status: 200,
311 body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
312 }
313 end)
314
315 assert admin_conn
316 |> put_req_header("content-type", "multipart/form-data")
317 |> post("/api/v1/pleroma/emoji/packs/download", %{
318 url: "https://example.com",
319 name: "pack_bad_sha",
320 as: "pack_bad_sha2"
321 })
322 |> json_response_and_validate_schema(:internal_server_error) == %{
323 "error" => "SHA256 for the pack doesn't match the one sent by the server"
324 }
325 end
326
327 test "other error", %{admin_conn: admin_conn} do
328 mock(fn
329 %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
330 json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
331
332 %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
333 json(%{metadata: %{features: ["shareable_emoji_packs"]}})
334
335 %{
336 method: :get,
337 url: "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack"
338 } ->
339 {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
340 %Tesla.Env{status: 200, body: Jason.encode!(pack)}
341 end)
342
343 assert admin_conn
344 |> put_req_header("content-type", "multipart/form-data")
345 |> post("/api/v1/pleroma/emoji/packs/download", %{
346 url: "https://example.com",
347 name: "test_pack",
348 as: "test_pack2"
349 })
350 |> json_response_and_validate_schema(:internal_server_error) == %{
351 "error" =>
352 "The pack was not set as shared and there is no fallback src to download from"
353 }
354 end
355 end
356
357 describe "PATCH/update /api/v1/pleroma/emoji/pack?name=:name" do
358 setup do
359 pack_file = "#{@emoji_path}/test_pack/pack.json"
360 original_content = File.read!(pack_file)
361
362 on_exit(fn ->
363 File.write!(pack_file, original_content)
364 end)
365
366 {:ok,
367 pack_file: pack_file,
368 new_data: %{
369 "license" => "Test license changed",
370 "homepage" => "https://pleroma.social",
371 "description" => "Test description",
372 "share-files" => false
373 }}
374 end
375
376 test "returns error when file system not writable", %{admin_conn: conn} = ctx do
377 with_mocks([
378 {File, [:passthrough], [stat: fn _ -> {:error, :eacces} end]}
379 ]) do
380 assert conn
381 |> put_req_header("content-type", "multipart/form-data")
382 |> patch(
383 "/api/v1/pleroma/emoji/pack?name=test_pack",
384 %{"metadata" => ctx[:new_data]}
385 )
386 |> json_response_and_validate_schema(500)
387 end
388 end
389
390 test "for a pack without a fallback source", ctx do
391 assert ctx[:admin_conn]
392 |> put_req_header("content-type", "multipart/form-data")
393 |> patch("/api/v1/pleroma/emoji/pack?name=test_pack", %{
394 "metadata" => ctx[:new_data]
395 })
396 |> json_response_and_validate_schema(200) == ctx[:new_data]
397
398 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
399 end
400
401 test "for a pack with a fallback source", ctx do
402 mock(fn
403 %{
404 method: :get,
405 url: "https://nonshared-pack"
406 } ->
407 text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
408 end)
409
410 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
411
412 new_data_with_sha =
413 Map.put(
414 new_data,
415 "fallback-src-sha256",
416 "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
417 )
418
419 assert ctx[:admin_conn]
420 |> put_req_header("content-type", "multipart/form-data")
421 |> patch("/api/v1/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
422 |> json_response_and_validate_schema(200) == new_data_with_sha
423
424 assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
425 end
426
427 test "when the fallback source doesn't have all the files", ctx do
428 mock(fn
429 %{
430 method: :get,
431 url: "https://nonshared-pack"
432 } ->
433 {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
434 text(empty_arch)
435 end)
436
437 new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
438
439 assert ctx[:admin_conn]
440 |> put_req_header("content-type", "multipart/form-data")
441 |> patch("/api/v1/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
442 |> json_response_and_validate_schema(:bad_request) == %{
443 "error" => "The fallback archive does not have all files specified in pack.json"
444 }
445 end
446 end
447
448 describe "POST/DELETE /api/v1/pleroma/emoji/pack?name=:name" do
449 test "returns an error on creates pack when file system not writable", %{
450 admin_conn: admin_conn
451 } do
452 path_pack = Path.join(@emoji_path, "test_pack")
453
454 with_mocks([
455 {File, [:passthrough], [mkdir: fn ^path_pack -> {:error, :eacces} end]}
456 ]) do
457 assert admin_conn
458 |> post("/api/v1/pleroma/emoji/pack?name=test_pack")
459 |> json_response_and_validate_schema(500) == %{
460 "error" =>
461 "Unexpected error occurred while creating pack. (POSIX error: Permission denied)"
462 }
463 end
464 end
465
466 test "returns an error on deletes pack when the file system is not writable", %{
467 admin_conn: admin_conn
468 } do
469 path_pack = Path.join(@emoji_path, "test_emoji_pack")
470
471 try do
472 {:ok, _pack} = Pleroma.Emoji.Pack.create("test_emoji_pack")
473
474 with_mocks([
475 {File, [:passthrough], [rm_rf: fn ^path_pack -> {:error, :eacces, path_pack} end]}
476 ]) do
477 assert admin_conn
478 |> delete("/api/v1/pleroma/emoji/pack?name=test_emoji_pack")
479 |> json_response_and_validate_schema(500) == %{
480 "error" =>
481 "Couldn't delete the `test_emoji_pack` pack (POSIX error: Permission denied)"
482 }
483 end
484 after
485 File.rm_rf(path_pack)
486 end
487 end
488
489 test "creating and deleting a pack", %{admin_conn: admin_conn} do
490 assert admin_conn
491 |> post("/api/v1/pleroma/emoji/pack?name=test_created")
492 |> json_response_and_validate_schema(200) == "ok"
493
494 assert File.exists?("#{@emoji_path}/test_created/pack.json")
495
496 assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
497 "pack" => %{},
498 "files" => %{},
499 "files_count" => 0
500 }
501
502 assert admin_conn
503 |> delete("/api/v1/pleroma/emoji/pack?name=test_created")
504 |> json_response_and_validate_schema(200) == "ok"
505
506 refute File.exists?("#{@emoji_path}/test_created/pack.json")
507 end
508
509 test "if pack exists", %{admin_conn: admin_conn} do
510 path = Path.join(@emoji_path, "test_created")
511 File.mkdir(path)
512 pack_file = Jason.encode!(%{files: %{}, pack: %{}})
513 File.write!(Path.join(path, "pack.json"), pack_file)
514
515 assert admin_conn
516 |> post("/api/v1/pleroma/emoji/pack?name=test_created")
517 |> json_response_and_validate_schema(:conflict) == %{
518 "error" => "A pack named \"test_created\" already exists"
519 }
520
521 on_exit(fn -> File.rm_rf(path) end)
522 end
523
524 test "with empty name", %{admin_conn: admin_conn} do
525 assert admin_conn
526 |> post("/api/v1/pleroma/emoji/pack?name= ")
527 |> json_response_and_validate_schema(:bad_request) == %{
528 "error" => "pack name cannot be empty"
529 }
530 end
531 end
532
533 test "deleting nonexisting pack", %{admin_conn: admin_conn} do
534 assert admin_conn
535 |> delete("/api/v1/pleroma/emoji/pack?name=non_existing")
536 |> json_response_and_validate_schema(:not_found) == %{
537 "error" => "Pack non_existing does not exist"
538 }
539 end
540
541 test "deleting with empty name", %{admin_conn: admin_conn} do
542 assert admin_conn
543 |> delete("/api/v1/pleroma/emoji/pack?name= ")
544 |> json_response_and_validate_schema(:bad_request) == %{
545 "error" => "pack name cannot be empty"
546 }
547 end
548
549 test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
550 on_exit(fn ->
551 File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
552 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
553 end)
554
555 resp =
556 conn
557 |> get("/api/v1/pleroma/emoji/packs")
558 |> json_response_and_validate_schema(200)
559
560 refute Map.has_key?(resp["packs"], "test_pack_for_import")
561
562 assert admin_conn
563 |> get("/api/v1/pleroma/emoji/packs/import")
564 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
565
566 resp =
567 conn
568 |> get("/api/v1/pleroma/emoji/packs")
569 |> json_response_and_validate_schema(200)
570
571 assert resp["packs"]["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
572
573 File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
574 refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
575
576 emoji_txt_content = """
577 blank, blank.png, Fun
578 blank2, blank.png
579 foo, /emoji/test_pack_for_import/blank.png
580 bar
581 """
582
583 File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
584
585 assert admin_conn
586 |> get("/api/v1/pleroma/emoji/packs/import")
587 |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
588
589 resp =
590 conn
591 |> get("/api/v1/pleroma/emoji/packs")
592 |> json_response_and_validate_schema(200)
593
594 assert resp["packs"]["test_pack_for_import"]["files"] == %{
595 "blank" => "blank.png",
596 "blank2" => "blank.png",
597 "foo" => "blank.png"
598 }
599 end
600
601 describe "GET /api/v1/pleroma/emoji/pack?name=:name" do
602 test "shows pack.json", %{conn: conn} do
603 assert %{
604 "files" => files,
605 "files_count" => 2,
606 "pack" => %{
607 "can-download" => true,
608 "description" => "Test description",
609 "download-sha256" => _,
610 "homepage" => "https://pleroma.social",
611 "license" => "Test license",
612 "share-files" => true
613 }
614 } =
615 conn
616 |> get("/api/v1/pleroma/emoji/pack?name=test_pack")
617 |> json_response_and_validate_schema(200)
618
619 assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
620
621 assert %{
622 "files" => files,
623 "files_count" => 2
624 } =
625 conn
626 |> get("/api/v1/pleroma/emoji/pack?name=test_pack&page_size=1")
627 |> json_response_and_validate_schema(200)
628
629 assert files |> Map.keys() |> length() == 1
630
631 assert %{
632 "files" => files,
633 "files_count" => 2
634 } =
635 conn
636 |> get("/api/v1/pleroma/emoji/pack?name=test_pack&page_size=1&page=2")
637 |> json_response_and_validate_schema(200)
638
639 assert files |> Map.keys() |> length() == 1
640 end
641
642 test "for pack name with special chars", %{conn: conn} do
643 assert %{
644 "files" => _files,
645 "files_count" => 1,
646 "pack" => %{
647 "can-download" => true,
648 "description" => "Test description",
649 "download-sha256" => _,
650 "homepage" => "https://pleroma.social",
651 "license" => "Test license",
652 "share-files" => true
653 }
654 } =
655 conn
656 |> get("/api/v1/pleroma/emoji/pack?name=blobs.gg")
657 |> json_response_and_validate_schema(200)
658 end
659
660 test "non existing pack", %{conn: conn} do
661 assert conn
662 |> get("/api/v1/pleroma/emoji/pack?name=non_existing")
663 |> json_response_and_validate_schema(:not_found) == %{
664 "error" => "Pack non_existing does not exist"
665 }
666 end
667
668 test "error name", %{conn: conn} do
669 assert conn
670 |> get("/api/v1/pleroma/emoji/pack?name= ")
671 |> json_response_and_validate_schema(:bad_request) == %{
672 "error" => "pack name cannot be empty"
673 }
674 end
675 end
676 end