Add tests for downloading from fallback url
[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 shared & unshared packs from another instance via download_from, deleting them" do
46 on_exit(fn ->
47 File.rm_rf!("test/instance_static/emoji/test_pack2")
48 File.rm_rf!("test/instance_static/emoji/test_pack_nonshared2")
49 end)
50
51 mock(fn
52 %{
53 method: :get,
54 url: "https://example.com/api/pleroma/emoji/packs/list"
55 } ->
56 conn = build_conn()
57
58 conn
59 |> get(emoji_api_path(conn, :list_packs))
60 |> json_response(200)
61 |> json()
62
63 %{
64 method: :get,
65 url: "https://example.com/api/pleroma/emoji/packs/download_shared/test_pack"
66 } ->
67 conn = build_conn()
68
69 conn
70 |> get(emoji_api_path(conn, :download_shared, "test_pack"))
71 |> response(200)
72 |> text()
73
74 %{
75 method: :get,
76 url: "https://nonshared-pack"
77 } ->
78 text(File.read!("test/instance_static/emoji/test_pack_nonshared/nonshared.zip"))
79 end)
80
81 admin = insert(:user, info: %{is_admin: true})
82
83 conn = build_conn()
84
85 assert conn
86 |> put_req_header("content-type", "application/json")
87 |> assign(:user, admin)
88 |> post(
89 emoji_api_path(
90 conn,
91 :download_from
92 ),
93 %{
94 instance_address: "https://example.com",
95 pack_name: "test_pack",
96 as: "test_pack2"
97 }
98 |> Jason.encode!()
99 )
100 |> text_response(200) == "ok"
101
102 assert File.exists?("test/instance_static/emoji/test_pack2/pack.json")
103 assert File.exists?("test/instance_static/emoji/test_pack2/blank.png")
104
105 assert conn
106 |> assign(:user, admin)
107 |> delete(emoji_api_path(conn, :delete, "test_pack2"))
108 |> response(200) == "ok"
109
110 refute File.exists?("test/instance_static/emoji/test_pack2")
111
112 # non-shared, downloaded from the fallback URL
113
114 conn = build_conn()
115
116 assert conn
117 |> put_req_header("content-type", "application/json")
118 |> assign(:user, admin)
119 |> post(
120 emoji_api_path(
121 conn,
122 :download_from
123 ),
124 %{
125 instance_address: "https://example.com",
126 pack_name: "test_pack_nonshared",
127 as: "test_pack_nonshared2"
128 }
129 |> Jason.encode!()
130 )
131 |> text_response(200) == "ok"
132
133 assert File.exists?("test/instance_static/emoji/test_pack_nonshared2/pack.json")
134 assert File.exists?("test/instance_static/emoji/test_pack_nonshared2/blank.png")
135
136 assert conn
137 |> assign(:user, admin)
138 |> delete(emoji_api_path(conn, :delete, "test_pack_nonshared2"))
139 |> response(200) == "ok"
140
141 refute File.exists?("test/instance_static/emoji/test_pack_nonshared2")
142 end
143 end