Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / twitter_api / twitter_api_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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("account.json", %{user: user}) ==
33 AccountView.render("account.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("account.json", %{user: user}) ==
51 AccountView.render("account.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.info.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 setup do
121 setting = Pleroma.Config.get([:instance, :registrations_open])
122
123 if setting do
124 Pleroma.Config.put([:instance, :registrations_open], false)
125 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
126 end
127
128 :ok
129 end
130
131 test "returns user on success" do
132 {:ok, invite} = UserInviteToken.create_invite()
133
134 data = %{
135 "nickname" => "vinny",
136 "email" => "pasta@pizza.vs",
137 "fullname" => "Vinny Vinesauce",
138 "bio" => "streamer",
139 "password" => "hiptofbees",
140 "confirm" => "hiptofbees",
141 "token" => invite.token
142 }
143
144 {:ok, user} = TwitterAPI.register_user(data)
145
146 fetched_user = User.get_cached_by_nickname("vinny")
147 invite = Repo.get_by(UserInviteToken, token: invite.token)
148
149 assert invite.used == true
150
151 assert AccountView.render("account.json", %{user: user}) ==
152 AccountView.render("account.json", %{user: fetched_user})
153 end
154
155 test "returns error on invalid token" do
156 data = %{
157 "nickname" => "GrimReaper",
158 "email" => "death@reapers.afterlife",
159 "fullname" => "Reaper Grim",
160 "bio" => "Your time has come",
161 "password" => "scythe",
162 "confirm" => "scythe",
163 "token" => "DudeLetMeInImAFairy"
164 }
165
166 {:error, msg} = TwitterAPI.register_user(data)
167
168 assert msg == "Invalid token"
169 refute User.get_cached_by_nickname("GrimReaper")
170 end
171
172 test "returns error on expired token" do
173 {:ok, invite} = UserInviteToken.create_invite()
174 UserInviteToken.update_invite!(invite, used: true)
175
176 data = %{
177 "nickname" => "GrimReaper",
178 "email" => "death@reapers.afterlife",
179 "fullname" => "Reaper Grim",
180 "bio" => "Your time has come",
181 "password" => "scythe",
182 "confirm" => "scythe",
183 "token" => invite.token
184 }
185
186 {:error, msg} = TwitterAPI.register_user(data)
187
188 assert msg == "Expired token"
189 refute User.get_cached_by_nickname("GrimReaper")
190 end
191 end
192
193 describe "registers with date limited token" do
194 setup do
195 setting = Pleroma.Config.get([:instance, :registrations_open])
196
197 if setting do
198 Pleroma.Config.put([:instance, :registrations_open], false)
199 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
200 end
201
202 data = %{
203 "nickname" => "vinny",
204 "email" => "pasta@pizza.vs",
205 "fullname" => "Vinny Vinesauce",
206 "bio" => "streamer",
207 "password" => "hiptofbees",
208 "confirm" => "hiptofbees"
209 }
210
211 check_fn = fn invite ->
212 data = Map.put(data, "token", invite.token)
213 {:ok, user} = TwitterAPI.register_user(data)
214 fetched_user = User.get_cached_by_nickname("vinny")
215
216 assert AccountView.render("account.json", %{user: user}) ==
217 AccountView.render("account.json", %{user: fetched_user})
218 end
219
220 {:ok, data: data, check_fn: check_fn}
221 end
222
223 test "returns user on success", %{check_fn: check_fn} do
224 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
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 user on token which expired tomorrow", %{check_fn: check_fn} do
234 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
235
236 check_fn.(invite)
237
238 invite = Repo.get_by(UserInviteToken, token: invite.token)
239
240 refute invite.used
241 end
242
243 test "returns an error on overdue date", %{data: data} do
244 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
245
246 data = Map.put(data, "token", invite.token)
247
248 {:error, msg} = TwitterAPI.register_user(data)
249
250 assert msg == "Expired token"
251 refute User.get_cached_by_nickname("vinny")
252 invite = Repo.get_by(UserInviteToken, token: invite.token)
253
254 refute invite.used
255 end
256 end
257
258 describe "registers with reusable token" do
259 setup do
260 setting = Pleroma.Config.get([:instance, :registrations_open])
261
262 if setting do
263 Pleroma.Config.put([:instance, :registrations_open], false)
264 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
265 end
266
267 :ok
268 end
269
270 test "returns user on success, after him registration fails" do
271 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
272
273 UserInviteToken.update_invite!(invite, uses: 99)
274
275 data = %{
276 "nickname" => "vinny",
277 "email" => "pasta@pizza.vs",
278 "fullname" => "Vinny Vinesauce",
279 "bio" => "streamer",
280 "password" => "hiptofbees",
281 "confirm" => "hiptofbees",
282 "token" => invite.token
283 }
284
285 {:ok, user} = TwitterAPI.register_user(data)
286 fetched_user = User.get_cached_by_nickname("vinny")
287 invite = Repo.get_by(UserInviteToken, token: invite.token)
288
289 assert invite.used == true
290
291 assert AccountView.render("account.json", %{user: user}) ==
292 AccountView.render("account.json", %{user: fetched_user})
293
294 data = %{
295 "nickname" => "GrimReaper",
296 "email" => "death@reapers.afterlife",
297 "fullname" => "Reaper Grim",
298 "bio" => "Your time has come",
299 "password" => "scythe",
300 "confirm" => "scythe",
301 "token" => invite.token
302 }
303
304 {:error, msg} = TwitterAPI.register_user(data)
305
306 assert msg == "Expired token"
307 refute User.get_cached_by_nickname("GrimReaper")
308 end
309 end
310
311 describe "registers with reusable date limited token" do
312 setup do
313 setting = Pleroma.Config.get([:instance, :registrations_open])
314
315 if setting do
316 Pleroma.Config.put([:instance, :registrations_open], false)
317 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
318 end
319
320 :ok
321 end
322
323 test "returns user on success" do
324 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
325
326 data = %{
327 "nickname" => "vinny",
328 "email" => "pasta@pizza.vs",
329 "fullname" => "Vinny Vinesauce",
330 "bio" => "streamer",
331 "password" => "hiptofbees",
332 "confirm" => "hiptofbees",
333 "token" => invite.token
334 }
335
336 {:ok, user} = TwitterAPI.register_user(data)
337 fetched_user = User.get_cached_by_nickname("vinny")
338 invite = Repo.get_by(UserInviteToken, token: invite.token)
339
340 refute invite.used
341
342 assert AccountView.render("account.json", %{user: user}) ==
343 AccountView.render("account.json", %{user: fetched_user})
344 end
345
346 test "error after max uses" do
347 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
348
349 UserInviteToken.update_invite!(invite, uses: 99)
350
351 data = %{
352 "nickname" => "vinny",
353 "email" => "pasta@pizza.vs",
354 "fullname" => "Vinny Vinesauce",
355 "bio" => "streamer",
356 "password" => "hiptofbees",
357 "confirm" => "hiptofbees",
358 "token" => invite.token
359 }
360
361 {:ok, user} = TwitterAPI.register_user(data)
362 fetched_user = User.get_cached_by_nickname("vinny")
363 invite = Repo.get_by(UserInviteToken, token: invite.token)
364 assert invite.used == true
365
366 assert AccountView.render("account.json", %{user: user}) ==
367 AccountView.render("account.json", %{user: fetched_user})
368
369 data = %{
370 "nickname" => "GrimReaper",
371 "email" => "death@reapers.afterlife",
372 "fullname" => "Reaper Grim",
373 "bio" => "Your time has come",
374 "password" => "scythe",
375 "confirm" => "scythe",
376 "token" => invite.token
377 }
378
379 {:error, msg} = TwitterAPI.register_user(data)
380
381 assert msg == "Expired token"
382 refute User.get_cached_by_nickname("GrimReaper")
383 end
384
385 test "returns error on overdue date" do
386 {:ok, invite} =
387 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
388
389 data = %{
390 "nickname" => "GrimReaper",
391 "email" => "death@reapers.afterlife",
392 "fullname" => "Reaper Grim",
393 "bio" => "Your time has come",
394 "password" => "scythe",
395 "confirm" => "scythe",
396 "token" => invite.token
397 }
398
399 {:error, msg} = TwitterAPI.register_user(data)
400
401 assert msg == "Expired token"
402 refute User.get_cached_by_nickname("GrimReaper")
403 end
404
405 test "returns error on with overdue date and after max" do
406 {:ok, invite} =
407 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
408
409 UserInviteToken.update_invite!(invite, uses: 100)
410
411 data = %{
412 "nickname" => "GrimReaper",
413 "email" => "death@reapers.afterlife",
414 "fullname" => "Reaper Grim",
415 "bio" => "Your time has come",
416 "password" => "scythe",
417 "confirm" => "scythe",
418 "token" => invite.token
419 }
420
421 {:error, msg} = TwitterAPI.register_user(data)
422
423 assert msg == "Expired token"
424 refute User.get_cached_by_nickname("GrimReaper")
425 end
426 end
427
428 test "it returns the error on registration problems" do
429 data = %{
430 "nickname" => "lain",
431 "email" => "lain@wired.jp",
432 "fullname" => "lain iwakura",
433 "bio" => "close the world.",
434 "password" => "bear"
435 }
436
437 {:error, error_object} = TwitterAPI.register_user(data)
438
439 assert is_binary(error_object[:error])
440 refute User.get_cached_by_nickname("lain")
441 end
442
443 setup do
444 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
445 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
446 :ok
447 end
448 end