Change YAML to JSON
[akkoma] / test / web / emoji_api_controller_test.exs
1 defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest do
2 use Pleroma.Web.ConnCase
3
4 import Tesla.Mock
5
6 import Pleroma.Factory
7
8 test "shared & non-shared pack information in list_packs is ok" do
9 conn = build_conn()
10 resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
11
12 assert Map.has_key?(resp, "test_pack")
13
14 pack = resp["test_pack"]
15
16 assert Map.has_key?(pack["pack"], "download-sha256")
17 assert pack["pack"]["can-download"]
18
19 assert pack["files"] == %{"blank" => "blank.png"}
20
21 # Non-shared pack
22
23 assert Map.has_key?(resp, "test_pack_nonshared")
24
25 pack = resp["test_pack_nonshared"]
26
27 refute pack["pack"]["shared"]
28 refute pack["pack"]["can-download"]
29 end
30
31 test "downloading a shared pack from download_shared" do
32 conn = build_conn()
33
34 resp =
35 conn
36 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
37 |> response(200)
38
39 {:ok, arch} = :zip.unzip(resp, [:memory])
40
41 assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
42 assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
43 end
44
45 test "downloading a shared pack from another instance via download_from, deleting it" do
46 on_exit(fn ->
47 File.rm_rf!("test/instance_static/emoji/test_pack2")
48 end)
49
50 mock(fn
51 %{
52 method: :get,
53 url: "https://example.com/api/pleroma/emoji/packs/list"
54 } ->
55 conn = build_conn()
56
57 conn
58 |> get(emoji_api_path(conn, :list_packs))
59 |> json_response(200)
60 |> json()
61
62 %{
63 method: :get,
64 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
65 } ->
66 conn = build_conn()
67
68 conn
69 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
70 |> response(200)
71 |> text()
72 end)
73
74 admin = insert(:user, info: %{is_admin: true})
75
76 conn = build_conn()
77
78 assert conn
79 |> put_req_header("content-type", "application/json")
80 |> assign(:user, admin)
81 |> post(
82 emoji_api_path(
83 conn,
84 :download_from
85 ),
86 %{
87 instance_address: "https://example.com",
88 pack_name: "test_pack",
89 as: "test_pack2"
90 }
91 |> Jason.encode!()
92 )
93 |> text_response(200) == "ok"
94
95 assert File.exists?("test/instance_static/emoji/test_pack2/pack.json")
96 assert File.exists?("test/instance_static/emoji/test_pack2/blank.png")
97
98 assert conn
99 |> assign(:user, admin)
100 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
101 |> response(200) == "ok"
102
103 refute File.exists?("test/instance_static/emoji/test_pack2")
104 end
105 end