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