Merge branch 'restart-fix-for-mix-tasks' into 'develop'
[akkoma] / test / web / twitter_api / twitter_api_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.TwitterAPI.TwitterAPITest do
6 use Pleroma.DataCase
7 alias Pleroma.Repo
8 alias Pleroma.Tests.ObanHelpers
9 alias Pleroma.User
10 alias Pleroma.UserInviteToken
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.TwitterAPI.TwitterAPI
13
14 setup_all do
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
16 :ok
17 end
18
19 test "it registers a new user and returns the user." do
20 data = %{
21 "nickname" => "lain",
22 "email" => "lain@wired.jp",
23 "fullname" => "lain iwakura",
24 "password" => "bear",
25 "confirm" => "bear"
26 }
27
28 {:ok, user} = TwitterAPI.register_user(data)
29
30 fetched_user = User.get_cached_by_nickname("lain")
31
32 assert AccountView.render("show.json", %{user: user}) ==
33 AccountView.render("show.json", %{user: fetched_user})
34 end
35
36 test "it registers a new user with empty string in bio and returns the user." do
37 data = %{
38 "nickname" => "lain",
39 "email" => "lain@wired.jp",
40 "fullname" => "lain iwakura",
41 "bio" => "",
42 "password" => "bear",
43 "confirm" => "bear"
44 }
45
46 {:ok, user} = TwitterAPI.register_user(data)
47
48 fetched_user = User.get_cached_by_nickname("lain")
49
50 assert AccountView.render("show.json", %{user: user}) ==
51 AccountView.render("show.json", %{user: fetched_user})
52 end
53
54 test "it sends confirmation email if :account_activation_required is specified in instance config" do
55 setting = Pleroma.Config.get([:instance, :account_activation_required])
56
57 unless setting do
58 Pleroma.Config.put([:instance, :account_activation_required], true)
59 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
60 end
61
62 data = %{
63 "nickname" => "lain",
64 "email" => "lain@wired.jp",
65 "fullname" => "lain iwakura",
66 "bio" => "",
67 "password" => "bear",
68 "confirm" => "bear"
69 }
70
71 {:ok, user} = TwitterAPI.register_user(data)
72 ObanHelpers.perform_all()
73
74 assert user.confirmation_pending
75
76 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
77
78 notify_email = Pleroma.Config.get([:instance, :notify_email])
79 instance_name = Pleroma.Config.get([:instance, :name])
80
81 Swoosh.TestAssertions.assert_email_sent(
82 from: {instance_name, notify_email},
83 to: {user.name, user.email},
84 html_body: email.html_body
85 )
86 end
87
88 test "it registers a new user and parses mentions in the bio" do
89 data1 = %{
90 "nickname" => "john",
91 "email" => "john@gmail.com",
92 "fullname" => "John Doe",
93 "bio" => "test",
94 "password" => "bear",
95 "confirm" => "bear"
96 }
97
98 {:ok, user1} = TwitterAPI.register_user(data1)
99
100 data2 = %{
101 "nickname" => "lain",
102 "email" => "lain@wired.jp",
103 "fullname" => "lain iwakura",
104 "bio" => "@john test",
105 "password" => "bear",
106 "confirm" => "bear"
107 }
108
109 {:ok, user2} = TwitterAPI.register_user(data2)
110
111 expected_text =
112 ~s(<span class="h-card"><a data-user="#{user1.id}" class="u-url mention" href="#{
113 user1.ap_id
114 }" rel="ugc">@<span>john</span></a></span> test)
115
116 assert user2.bio == expected_text
117 end
118
119 describe "register with one time token" do
120 clear_config([:instance, :registrations_open]) do
121 Pleroma.Config.put([:instance, :registrations_open], false)
122 end
123
124 test "returns user on success" do
125 {:ok, invite} = UserInviteToken.create_invite()
126
127 data = %{
128 "nickname" => "vinny",
129 "email" => "pasta@pizza.vs",
130 "fullname" => "Vinny Vinesauce",
131 "bio" => "streamer",
132 "password" => "hiptofbees",
133 "confirm" => "hiptofbees",
134 "token" => invite.token
135 }
136
137 {:ok, user} = TwitterAPI.register_user(data)
138
139 fetched_user = User.get_cached_by_nickname("vinny")
140 invite = Repo.get_by(UserInviteToken, token: invite.token)
141
142 assert invite.used == true
143
144 assert AccountView.render("show.json", %{user: user}) ==
145 AccountView.render("show.json", %{user: fetched_user})
146 end
147
148 test "returns error on invalid token" do
149 data = %{
150 "nickname" => "GrimReaper",
151 "email" => "death@reapers.afterlife",
152 "fullname" => "Reaper Grim",
153 "bio" => "Your time has come",
154 "password" => "scythe",
155 "confirm" => "scythe",
156 "token" => "DudeLetMeInImAFairy"
157 }
158
159 {:error, msg} = TwitterAPI.register_user(data)
160
161 assert msg == "Invalid token"
162 refute User.get_cached_by_nickname("GrimReaper")
163 end
164
165 test "returns error on expired token" do
166 {:ok, invite} = UserInviteToken.create_invite()
167 UserInviteToken.update_invite!(invite, used: true)
168
169 data = %{
170 "nickname" => "GrimReaper",
171 "email" => "death@reapers.afterlife",
172 "fullname" => "Reaper Grim",
173 "bio" => "Your time has come",
174 "password" => "scythe",
175 "confirm" => "scythe",
176 "token" => invite.token
177 }
178
179 {:error, msg} = TwitterAPI.register_user(data)
180
181 assert msg == "Expired token"
182 refute User.get_cached_by_nickname("GrimReaper")
183 end
184 end
185
186 describe "registers with date limited token" do
187 clear_config([:instance, :registrations_open]) do
188 Pleroma.Config.put([:instance, :registrations_open], false)
189 end
190
191 setup do
192 data = %{
193 "nickname" => "vinny",
194 "email" => "pasta@pizza.vs",
195 "fullname" => "Vinny Vinesauce",
196 "bio" => "streamer",
197 "password" => "hiptofbees",
198 "confirm" => "hiptofbees"
199 }
200
201 check_fn = fn invite ->
202 data = Map.put(data, "token", invite.token)
203 {:ok, user} = TwitterAPI.register_user(data)
204 fetched_user = User.get_cached_by_nickname("vinny")
205
206 assert AccountView.render("show.json", %{user: user}) ==
207 AccountView.render("show.json", %{user: fetched_user})
208 end
209
210 {:ok, data: data, check_fn: check_fn}
211 end
212
213 test "returns user on success", %{check_fn: check_fn} do
214 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
215
216 check_fn.(invite)
217
218 invite = Repo.get_by(UserInviteToken, token: invite.token)
219
220 refute invite.used
221 end
222
223 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
224 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
225
226 check_fn.(invite)
227
228 invite = Repo.get_by(UserInviteToken, token: invite.token)
229
230 refute invite.used
231 end
232
233 test "returns an error on overdue date", %{data: data} do
234 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
235
236 data = Map.put(data, "token", invite.token)
237
238 {:error, msg} = TwitterAPI.register_user(data)
239
240 assert msg == "Expired token"
241 refute User.get_cached_by_nickname("vinny")
242 invite = Repo.get_by(UserInviteToken, token: invite.token)
243
244 refute invite.used
245 end
246 end
247
248 describe "registers with reusable token" do
249 clear_config([:instance, :registrations_open]) do
250 Pleroma.Config.put([:instance, :registrations_open], false)
251 end
252
253 test "returns user on success, after him registration fails" do
254 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
255
256 UserInviteToken.update_invite!(invite, uses: 99)
257
258 data = %{
259 "nickname" => "vinny",
260 "email" => "pasta@pizza.vs",
261 "fullname" => "Vinny Vinesauce",
262 "bio" => "streamer",
263 "password" => "hiptofbees",
264 "confirm" => "hiptofbees",
265 "token" => invite.token
266 }
267
268 {:ok, user} = TwitterAPI.register_user(data)
269 fetched_user = User.get_cached_by_nickname("vinny")
270 invite = Repo.get_by(UserInviteToken, token: invite.token)
271
272 assert invite.used == true
273
274 assert AccountView.render("show.json", %{user: user}) ==
275 AccountView.render("show.json", %{user: fetched_user})
276
277 data = %{
278 "nickname" => "GrimReaper",
279 "email" => "death@reapers.afterlife",
280 "fullname" => "Reaper Grim",
281 "bio" => "Your time has come",
282 "password" => "scythe",
283 "confirm" => "scythe",
284 "token" => invite.token
285 }
286
287 {:error, msg} = TwitterAPI.register_user(data)
288
289 assert msg == "Expired token"
290 refute User.get_cached_by_nickname("GrimReaper")
291 end
292 end
293
294 describe "registers with reusable date limited token" do
295 clear_config([:instance, :registrations_open]) do
296 Pleroma.Config.put([:instance, :registrations_open], false)
297 end
298
299 test "returns user on success" do
300 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
301
302 data = %{
303 "nickname" => "vinny",
304 "email" => "pasta@pizza.vs",
305 "fullname" => "Vinny Vinesauce",
306 "bio" => "streamer",
307 "password" => "hiptofbees",
308 "confirm" => "hiptofbees",
309 "token" => invite.token
310 }
311
312 {:ok, user} = TwitterAPI.register_user(data)
313 fetched_user = User.get_cached_by_nickname("vinny")
314 invite = Repo.get_by(UserInviteToken, token: invite.token)
315
316 refute invite.used
317
318 assert AccountView.render("show.json", %{user: user}) ==
319 AccountView.render("show.json", %{user: fetched_user})
320 end
321
322 test "error after max uses" do
323 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
324
325 UserInviteToken.update_invite!(invite, uses: 99)
326
327 data = %{
328 "nickname" => "vinny",
329 "email" => "pasta@pizza.vs",
330 "fullname" => "Vinny Vinesauce",
331 "bio" => "streamer",
332 "password" => "hiptofbees",
333 "confirm" => "hiptofbees",
334 "token" => invite.token
335 }
336
337 {:ok, user} = TwitterAPI.register_user(data)
338 fetched_user = User.get_cached_by_nickname("vinny")
339 invite = Repo.get_by(UserInviteToken, token: invite.token)
340 assert invite.used == true
341
342 assert AccountView.render("show.json", %{user: user}) ==
343 AccountView.render("show.json", %{user: fetched_user})
344
345 data = %{
346 "nickname" => "GrimReaper",
347 "email" => "death@reapers.afterlife",
348 "fullname" => "Reaper Grim",
349 "bio" => "Your time has come",
350 "password" => "scythe",
351 "confirm" => "scythe",
352 "token" => invite.token
353 }
354
355 {:error, msg} = TwitterAPI.register_user(data)
356
357 assert msg == "Expired token"
358 refute User.get_cached_by_nickname("GrimReaper")
359 end
360
361 test "returns error on overdue date" do
362 {:ok, invite} =
363 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
364
365 data = %{
366 "nickname" => "GrimReaper",
367 "email" => "death@reapers.afterlife",
368 "fullname" => "Reaper Grim",
369 "bio" => "Your time has come",
370 "password" => "scythe",
371 "confirm" => "scythe",
372 "token" => invite.token
373 }
374
375 {:error, msg} = TwitterAPI.register_user(data)
376
377 assert msg == "Expired token"
378 refute User.get_cached_by_nickname("GrimReaper")
379 end
380
381 test "returns error on with overdue date and after max" do
382 {:ok, invite} =
383 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
384
385 UserInviteToken.update_invite!(invite, uses: 100)
386
387 data = %{
388 "nickname" => "GrimReaper",
389 "email" => "death@reapers.afterlife",
390 "fullname" => "Reaper Grim",
391 "bio" => "Your time has come",
392 "password" => "scythe",
393 "confirm" => "scythe",
394 "token" => invite.token
395 }
396
397 {:error, msg} = TwitterAPI.register_user(data)
398
399 assert msg == "Expired token"
400 refute User.get_cached_by_nickname("GrimReaper")
401 end
402 end
403
404 test "it returns the error on registration problems" do
405 data = %{
406 "nickname" => "lain",
407 "email" => "lain@wired.jp",
408 "fullname" => "lain iwakura",
409 "bio" => "close the world.",
410 "password" => "bear"
411 }
412
413 {:error, error_object} = TwitterAPI.register_user(data)
414
415 assert is_binary(error_object[:error])
416 refute User.get_cached_by_nickname("lain")
417 end
418
419 setup do
420 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
421 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
422 :ok
423 end
424 end