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