1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
9 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.UserInviteToken
12 alias Pleroma.Web.TwitterAPI.TwitterAPI
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 test "it registers a new user and returns the user." do
22 :email => "lain@wired.jp",
23 :fullname => "lain iwakura",
28 {:ok, user} = TwitterAPI.register_user(data)
30 assert user == User.get_cached_by_nickname("lain")
33 test "it registers a new user with empty string in bio and returns the user" do
36 :email => "lain@wired.jp",
37 :fullname => "lain iwakura",
43 {:ok, user} = TwitterAPI.register_user(data)
45 assert user == User.get_cached_by_nickname("lain")
48 test "it sends confirmation email if :account_activation_required is specified in instance config" do
49 clear_config([:instance, :account_activation_required], true)
53 :email => "lain@wired.jp",
54 :fullname => "lain iwakura",
60 {:ok, user} = TwitterAPI.register_user(data)
61 ObanHelpers.perform_all()
63 refute user.is_confirmed
65 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
67 notify_email = Pleroma.Config.get([:instance, :notify_email])
68 instance_name = Pleroma.Config.get([:instance, :name])
70 Swoosh.TestAssertions.assert_email_sent(
71 from: {instance_name, notify_email},
72 to: {user.name, user.email},
73 html_body: email.html_body
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)
80 admin = insert(:user, is_admin: true)
84 :email => "lain@wired.jp",
85 :fullname => "lain iwakura",
89 :reason => "I love anime"
92 {:ok, user} = TwitterAPI.register_user(data)
93 ObanHelpers.perform_all()
95 refute user.is_approved
97 user_email = Pleroma.Emails.UserEmail.approval_pending_email(user)
98 admin_email = Pleroma.Emails.AdminEmail.new_unapproved_registration(admin, user)
100 notify_email = Pleroma.Config.get([:instance, :notify_email])
101 instance_name = Pleroma.Config.get([:instance, :name])
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
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
118 test "it registers a new user and parses mentions in the bio" do
121 :email => "john@gmail.com",
122 :fullname => "John Doe",
128 {:ok, user1} = TwitterAPI.register_user(data1)
132 :email => "lain@wired.jp",
133 :fullname => "lain iwakura",
134 :bio => "@john test",
139 {:ok, user2} = TwitterAPI.register_user(data2)
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)
144 assert user2.bio == expected_text
147 describe "register with one time token" do
148 setup do: clear_config([:instance, :registrations_open], false)
150 test "returns user on success" do
151 {:ok, invite} = UserInviteToken.create_invite()
154 :username => "vinny",
155 :email => "pasta@pizza.vs",
156 :fullname => "Vinny Vinesauce",
158 :password => "hiptofbees",
159 :confirm => "hiptofbees",
160 :token => invite.token
163 {:ok, user} = TwitterAPI.register_user(data)
165 assert user == User.get_cached_by_nickname("vinny")
167 invite = Repo.get_by(UserInviteToken, token: invite.token)
168 assert invite.used == true
171 test "returns error on invalid token" do
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"
182 {:error, msg} = TwitterAPI.register_user(data)
184 assert msg == "Invalid token"
185 refute User.get_cached_by_nickname("GrimReaper")
188 test "returns error on expired token" do
189 {:ok, invite} = UserInviteToken.create_invite()
190 UserInviteToken.update_invite!(invite, used: true)
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
202 {:error, msg} = TwitterAPI.register_user(data)
204 assert msg == "Expired token"
205 refute User.get_cached_by_nickname("GrimReaper")
209 describe "registers with date limited token" do
210 setup do: clear_config([:instance, :registrations_open], false)
214 :username => "vinny",
215 :email => "pasta@pizza.vs",
216 :fullname => "Vinny Vinesauce",
218 :password => "hiptofbees",
219 :confirm => "hiptofbees"
222 check_fn = fn invite ->
223 data = Map.put(data, :token, invite.token)
224 {:ok, user} = TwitterAPI.register_user(data)
226 assert user == User.get_cached_by_nickname("vinny")
229 {:ok, data: data, check_fn: check_fn}
232 test "returns user on success", %{check_fn: check_fn} do
233 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
237 invite = Repo.get_by(UserInviteToken, token: invite.token)
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)})
247 invite = Repo.get_by(UserInviteToken, token: invite.token)
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)})
255 data = Map.put(data, "token", invite.token)
257 {:error, msg} = TwitterAPI.register_user(data)
259 assert msg == "Expired token"
260 refute User.get_cached_by_nickname("vinny")
261 invite = Repo.get_by(UserInviteToken, token: invite.token)
267 describe "registers with reusable token" do
268 setup do: clear_config([:instance, :registrations_open], false)
270 test "returns user on success, after him registration fails" do
271 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
273 UserInviteToken.update_invite!(invite, uses: 99)
276 :username => "vinny",
277 :email => "pasta@pizza.vs",
278 :fullname => "Vinny Vinesauce",
280 :password => "hiptofbees",
281 :confirm => "hiptofbees",
282 :token => invite.token
285 {:ok, user} = TwitterAPI.register_user(data)
286 assert user == User.get_cached_by_nickname("vinny")
288 invite = Repo.get_by(UserInviteToken, token: invite.token)
289 assert invite.used == true
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
301 {:error, msg} = TwitterAPI.register_user(data)
303 assert msg == "Expired token"
304 refute User.get_cached_by_nickname("GrimReaper")
308 describe "registers with reusable date limited token" do
309 setup do: clear_config([:instance, :registrations_open], false)
311 test "returns user on success" do
312 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
315 :username => "vinny",
316 :email => "pasta@pizza.vs",
317 :fullname => "Vinny Vinesauce",
319 :password => "hiptofbees",
320 :confirm => "hiptofbees",
321 :token => invite.token
324 {:ok, user} = TwitterAPI.register_user(data)
325 assert user == User.get_cached_by_nickname("vinny")
327 invite = Repo.get_by(UserInviteToken, token: invite.token)
331 test "error after max uses" do
332 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
334 UserInviteToken.update_invite!(invite, uses: 99)
337 :username => "vinny",
338 :email => "pasta@pizza.vs",
339 :fullname => "Vinny Vinesauce",
341 :password => "hiptofbees",
342 :confirm => "hiptofbees",
343 :token => invite.token
346 {:ok, user} = TwitterAPI.register_user(data)
347 assert user == User.get_cached_by_nickname("vinny")
349 invite = Repo.get_by(UserInviteToken, token: invite.token)
350 assert invite.used == true
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
362 {:error, msg} = TwitterAPI.register_user(data)
364 assert msg == "Expired token"
365 refute User.get_cached_by_nickname("GrimReaper")
368 test "returns error on overdue date" do
370 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
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
382 {:error, msg} = TwitterAPI.register_user(data)
384 assert msg == "Expired token"
385 refute User.get_cached_by_nickname("GrimReaper")
388 test "returns error on with overdue date and after max" do
390 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
392 UserInviteToken.update_invite!(invite, uses: 100)
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
404 {:error, msg} = TwitterAPI.register_user(data)
406 assert msg == "Expired token"
407 refute User.get_cached_by_nickname("GrimReaper")
411 test "it returns the error on registration problems" do
414 :email => "lain@wired.jp",
415 :fullname => "lain iwakura",
416 :bio => "close the world."
419 {:error, error} = TwitterAPI.register_user(data)
421 assert is_binary(error)
422 refute User.get_cached_by_nickname("lain")