Refactor User.post_register_action/1 emails
[akkoma] / test / tasks / emoji_test.exs
1 defmodule Mix.Tasks.Pleroma.EmojiTest do
2 use ExUnit.Case, async: true
3
4 import ExUnit.CaptureIO
5 import Tesla.Mock
6
7 alias Mix.Tasks.Pleroma.Emoji
8
9 describe "ls-packs" do
10 test "with default manifest as url" do
11 mock(fn
12 %{
13 method: :get,
14 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
15 } ->
16 %Tesla.Env{
17 status: 200,
18 body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
19 }
20 end)
21
22 capture_io(fn -> Emoji.run(["ls-packs"]) end) =~
23 "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
24 end
25
26 test "with passed manifest as file" do
27 capture_io(fn ->
28 Emoji.run(["ls-packs", "-m", "test/fixtures/emoji/packs/manifest.json"])
29 end) =~ "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip"
30 end
31 end
32
33 describe "get-packs" do
34 test "download pack from default manifest" do
35 mock(fn
36 %{
37 method: :get,
38 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
39 } ->
40 %Tesla.Env{
41 status: 200,
42 body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
43 }
44
45 %{
46 method: :get,
47 url: "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
48 } ->
49 %Tesla.Env{
50 status: 200,
51 body: File.read!("test/fixtures/emoji/packs/blank.png.zip")
52 }
53
54 %{
55 method: :get,
56 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/finmoji.json"
57 } ->
58 %Tesla.Env{
59 status: 200,
60 body: File.read!("test/fixtures/emoji/packs/finmoji.json")
61 }
62 end)
63
64 assert capture_io(fn -> Emoji.run(["get-packs", "finmoji"]) end) =~ "Writing pack.json for"
65
66 emoji_path =
67 Path.join(
68 Pleroma.Config.get!([:instance, :static_dir]),
69 "emoji"
70 )
71
72 assert File.exists?(Path.join([emoji_path, "finmoji", "pack.json"]))
73 on_exit(fn -> File.rm_rf!("test/instance_static/emoji/finmoji") end)
74 end
75
76 test "install local emoji pack" do
77 assert capture_io(fn ->
78 Emoji.run([
79 "get-packs",
80 "local",
81 "--manifest",
82 "test/instance_static/local_pack/manifest.json"
83 ])
84 end) =~ "Writing pack.json for"
85
86 on_exit(fn -> File.rm_rf!("test/instance_static/emoji/local") end)
87 end
88
89 test "pack not found" do
90 mock(fn
91 %{
92 method: :get,
93 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
94 } ->
95 %Tesla.Env{
96 status: 200,
97 body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
98 }
99 end)
100
101 assert capture_io(fn -> Emoji.run(["get-packs", "not_found"]) end) =~
102 "No pack named \"not_found\" found"
103 end
104
105 test "raise on bad sha256" do
106 mock(fn
107 %{
108 method: :get,
109 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip"
110 } ->
111 %Tesla.Env{
112 status: 200,
113 body: File.read!("test/fixtures/emoji/packs/blank.png.zip")
114 }
115 end)
116
117 assert_raise RuntimeError, ~r/^Bad SHA256 for blobs.gg/, fn ->
118 capture_io(fn ->
119 Emoji.run(["get-packs", "blobs.gg", "-m", "test/fixtures/emoji/packs/manifest.json"])
120 end)
121 end
122 end
123 end
124
125 describe "gen-pack" do
126 setup do
127 url = "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
128
129 mock(fn %{
130 method: :get,
131 url: ^url
132 } ->
133 %Tesla.Env{status: 200, body: File.read!("test/fixtures/emoji/packs/blank.png.zip")}
134 end)
135
136 {:ok, url: url}
137 end
138
139 test "with default extensions", %{url: url} do
140 name = "pack1"
141 pack_json = "#{name}.json"
142 files_json = "#{name}_file.json"
143 refute File.exists?(pack_json)
144 refute File.exists?(files_json)
145
146 captured =
147 capture_io(fn ->
148 Emoji.run([
149 "gen-pack",
150 url,
151 "--name",
152 name,
153 "--license",
154 "license",
155 "--homepage",
156 "homepage",
157 "--description",
158 "description",
159 "--files",
160 files_json,
161 "--extensions",
162 ".png .gif"
163 ])
164 end)
165
166 assert captured =~ "#{pack_json} has been created with the pack1 pack"
167 assert captured =~ "Using .png .gif extensions"
168
169 assert File.exists?(pack_json)
170 assert File.exists?(files_json)
171
172 on_exit(fn ->
173 File.rm!(pack_json)
174 File.rm!(files_json)
175 end)
176 end
177
178 test "with custom extensions and update existing files", %{url: url} do
179 name = "pack2"
180 pack_json = "#{name}.json"
181 files_json = "#{name}_file.json"
182 refute File.exists?(pack_json)
183 refute File.exists?(files_json)
184
185 captured =
186 capture_io(fn ->
187 Emoji.run([
188 "gen-pack",
189 url,
190 "--name",
191 name,
192 "--license",
193 "license",
194 "--homepage",
195 "homepage",
196 "--description",
197 "description",
198 "--files",
199 files_json,
200 "--extensions",
201 " .png .gif .jpeg "
202 ])
203 end)
204
205 assert captured =~ "#{pack_json} has been created with the pack2 pack"
206 assert captured =~ "Using .png .gif .jpeg extensions"
207
208 assert File.exists?(pack_json)
209 assert File.exists?(files_json)
210
211 captured =
212 capture_io(fn ->
213 Emoji.run([
214 "gen-pack",
215 url,
216 "--name",
217 name,
218 "--license",
219 "license",
220 "--homepage",
221 "homepage",
222 "--description",
223 "description",
224 "--files",
225 files_json,
226 "--extensions",
227 " .png .gif .jpeg "
228 ])
229 end)
230
231 assert captured =~ "#{pack_json} has been updated with the pack2 pack"
232
233 on_exit(fn ->
234 File.rm!(pack_json)
235 File.rm!(files_json)
236 end)
237 end
238 end
239 end