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