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