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