Merge remote-tracking branch 'pleroma/develop' into cycles-streaming
[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="#{
143 user1.ap_id
144 }" rel="ugc">@<span>john</span></a></span> test)
145
146 assert user2.bio == expected_text
147 end
148
149 describe "register with one time token" do
150 setup do: clear_config([:instance, :registrations_open], false)
151
152 test "returns user on success" do
153 {:ok, invite} = UserInviteToken.create_invite()
154
155 data = %{
156 :username => "vinny",
157 :email => "pasta@pizza.vs",
158 :fullname => "Vinny Vinesauce",
159 :bio => "streamer",
160 :password => "hiptofbees",
161 :confirm => "hiptofbees",
162 :token => invite.token
163 }
164
165 {:ok, user} = TwitterAPI.register_user(data)
166
167 assert user == User.get_cached_by_nickname("vinny")
168
169 invite = Repo.get_by(UserInviteToken, token: invite.token)
170 assert invite.used == true
171 end
172
173 test "returns error on invalid token" do
174 data = %{
175 :username => "GrimReaper",
176 :email => "death@reapers.afterlife",
177 :fullname => "Reaper Grim",
178 :bio => "Your time has come",
179 :password => "scythe",
180 :confirm => "scythe",
181 :token => "DudeLetMeInImAFairy"
182 }
183
184 {:error, msg} = TwitterAPI.register_user(data)
185
186 assert msg == "Invalid token"
187 refute User.get_cached_by_nickname("GrimReaper")
188 end
189
190 test "returns error on expired token" do
191 {:ok, invite} = UserInviteToken.create_invite()
192 UserInviteToken.update_invite!(invite, used: true)
193
194 data = %{
195 :username => "GrimReaper",
196 :email => "death@reapers.afterlife",
197 :fullname => "Reaper Grim",
198 :bio => "Your time has come",
199 :password => "scythe",
200 :confirm => "scythe",
201 :token => invite.token
202 }
203
204 {:error, msg} = TwitterAPI.register_user(data)
205
206 assert msg == "Expired token"
207 refute User.get_cached_by_nickname("GrimReaper")
208 end
209 end
210
211 describe "registers with date limited token" do
212 setup do: clear_config([:instance, :registrations_open], false)
213
214 setup do
215 data = %{
216 :username => "vinny",
217 :email => "pasta@pizza.vs",
218 :fullname => "Vinny Vinesauce",
219 :bio => "streamer",
220 :password => "hiptofbees",
221 :confirm => "hiptofbees"
222 }
223
224 check_fn = fn invite ->
225 data = Map.put(data, :token, invite.token)
226 {:ok, user} = TwitterAPI.register_user(data)
227
228 assert user == User.get_cached_by_nickname("vinny")
229 end
230
231 {:ok, data: data, check_fn: check_fn}
232 end
233
234 test "returns user on success", %{check_fn: check_fn} do
235 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
236
237 check_fn.(invite)
238
239 invite = Repo.get_by(UserInviteToken, token: invite.token)
240
241 refute invite.used
242 end
243
244 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
245 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
246
247 check_fn.(invite)
248
249 invite = Repo.get_by(UserInviteToken, token: invite.token)
250
251 refute invite.used
252 end
253
254 test "returns an error on overdue date", %{data: data} do
255 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
256
257 data = Map.put(data, "token", invite.token)
258
259 {:error, msg} = TwitterAPI.register_user(data)
260
261 assert msg == "Expired token"
262 refute User.get_cached_by_nickname("vinny")
263 invite = Repo.get_by(UserInviteToken, token: invite.token)
264
265 refute invite.used
266 end
267 end
268
269 describe "registers with reusable token" do
270 setup do: clear_config([:instance, :registrations_open], false)
271
272 test "returns user on success, after him registration fails" do
273 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
274
275 UserInviteToken.update_invite!(invite, uses: 99)
276
277 data = %{
278 :username => "vinny",
279 :email => "pasta@pizza.vs",
280 :fullname => "Vinny Vinesauce",
281 :bio => "streamer",
282 :password => "hiptofbees",
283 :confirm => "hiptofbees",
284 :token => invite.token
285 }
286
287 {:ok, user} = TwitterAPI.register_user(data)
288 assert user == User.get_cached_by_nickname("vinny")
289
290 invite = Repo.get_by(UserInviteToken, token: invite.token)
291 assert invite.used == true
292
293 data = %{
294 :username => "GrimReaper",
295 :email => "death@reapers.afterlife",
296 :fullname => "Reaper Grim",
297 :bio => "Your time has come",
298 :password => "scythe",
299 :confirm => "scythe",
300 :token => invite.token
301 }
302
303 {:error, msg} = TwitterAPI.register_user(data)
304
305 assert msg == "Expired token"
306 refute User.get_cached_by_nickname("GrimReaper")
307 end
308 end
309
310 describe "registers with reusable date limited token" do
311 setup do: clear_config([:instance, :registrations_open], false)
312
313 test "returns user on success" do
314 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
315
316 data = %{
317 :username => "vinny",
318 :email => "pasta@pizza.vs",
319 :fullname => "Vinny Vinesauce",
320 :bio => "streamer",
321 :password => "hiptofbees",
322 :confirm => "hiptofbees",
323 :token => invite.token
324 }
325
326 {:ok, user} = TwitterAPI.register_user(data)
327 assert user == User.get_cached_by_nickname("vinny")
328
329 invite = Repo.get_by(UserInviteToken, token: invite.token)
330 refute invite.used
331 end
332
333 test "error after max uses" do
334 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
335
336 UserInviteToken.update_invite!(invite, uses: 99)
337
338 data = %{
339 :username => "vinny",
340 :email => "pasta@pizza.vs",
341 :fullname => "Vinny Vinesauce",
342 :bio => "streamer",
343 :password => "hiptofbees",
344 :confirm => "hiptofbees",
345 :token => invite.token
346 }
347
348 {:ok, user} = TwitterAPI.register_user(data)
349 assert user == User.get_cached_by_nickname("vinny")
350
351 invite = Repo.get_by(UserInviteToken, token: invite.token)
352 assert invite.used == true
353
354 data = %{
355 :username => "GrimReaper",
356 :email => "death@reapers.afterlife",
357 :fullname => "Reaper Grim",
358 :bio => "Your time has come",
359 :password => "scythe",
360 :confirm => "scythe",
361 :token => invite.token
362 }
363
364 {:error, msg} = TwitterAPI.register_user(data)
365
366 assert msg == "Expired token"
367 refute User.get_cached_by_nickname("GrimReaper")
368 end
369
370 test "returns error on overdue date" do
371 {:ok, invite} =
372 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
373
374 data = %{
375 :username => "GrimReaper",
376 :email => "death@reapers.afterlife",
377 :fullname => "Reaper Grim",
378 :bio => "Your time has come",
379 :password => "scythe",
380 :confirm => "scythe",
381 :token => invite.token
382 }
383
384 {:error, msg} = TwitterAPI.register_user(data)
385
386 assert msg == "Expired token"
387 refute User.get_cached_by_nickname("GrimReaper")
388 end
389
390 test "returns error on with overdue date and after max" do
391 {:ok, invite} =
392 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
393
394 UserInviteToken.update_invite!(invite, uses: 100)
395
396 data = %{
397 :username => "GrimReaper",
398 :email => "death@reapers.afterlife",
399 :fullname => "Reaper Grim",
400 :bio => "Your time has come",
401 :password => "scythe",
402 :confirm => "scythe",
403 :token => invite.token
404 }
405
406 {:error, msg} = TwitterAPI.register_user(data)
407
408 assert msg == "Expired token"
409 refute User.get_cached_by_nickname("GrimReaper")
410 end
411 end
412
413 test "it returns the error on registration problems" do
414 data = %{
415 :username => "lain",
416 :email => "lain@wired.jp",
417 :fullname => "lain iwakura",
418 :bio => "close the world."
419 }
420
421 {:error, error} = TwitterAPI.register_user(data)
422
423 assert is_binary(error)
424 refute User.get_cached_by_nickname("lain")
425 end
426 end