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="#{
144 }" rel="ugc">@<span>john</span></a></span> test)
146 assert user2.bio == expected_text
149 describe "register with one time token" do
150 setup do: clear_config([:instance, :registrations_open], false)
152 test "returns user on success" do
153 {:ok, invite} = UserInviteToken.create_invite()
156 :username => "vinny",
157 :email => "pasta@pizza.vs",
158 :fullname => "Vinny Vinesauce",
160 :password => "hiptofbees",
161 :confirm => "hiptofbees",
162 :token => invite.token
165 {:ok, user} = TwitterAPI.register_user(data)
167 assert user == User.get_cached_by_nickname("vinny")
169 invite = Repo.get_by(UserInviteToken, token: invite.token)
170 assert invite.used == true
173 test "returns error on invalid token" do
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"
184 {:error, msg} = TwitterAPI.register_user(data)
186 assert msg == "Invalid token"
187 refute User.get_cached_by_nickname("GrimReaper")
190 test "returns error on expired token" do
191 {:ok, invite} = UserInviteToken.create_invite()
192 UserInviteToken.update_invite!(invite, used: true)
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
204 {:error, msg} = TwitterAPI.register_user(data)
206 assert msg == "Expired token"
207 refute User.get_cached_by_nickname("GrimReaper")
211 describe "registers with date limited token" do
212 setup do: clear_config([:instance, :registrations_open], false)
216 :username => "vinny",
217 :email => "pasta@pizza.vs",
218 :fullname => "Vinny Vinesauce",
220 :password => "hiptofbees",
221 :confirm => "hiptofbees"
224 check_fn = fn invite ->
225 data = Map.put(data, :token, invite.token)
226 {:ok, user} = TwitterAPI.register_user(data)
228 assert user == User.get_cached_by_nickname("vinny")
231 {:ok, data: data, check_fn: check_fn}
234 test "returns user on success", %{check_fn: check_fn} do
235 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
239 invite = Repo.get_by(UserInviteToken, token: invite.token)
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)})
249 invite = Repo.get_by(UserInviteToken, token: invite.token)
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)})
257 data = Map.put(data, "token", invite.token)
259 {:error, msg} = TwitterAPI.register_user(data)
261 assert msg == "Expired token"
262 refute User.get_cached_by_nickname("vinny")
263 invite = Repo.get_by(UserInviteToken, token: invite.token)
269 describe "registers with reusable token" do
270 setup do: clear_config([:instance, :registrations_open], false)
272 test "returns user on success, after him registration fails" do
273 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
275 UserInviteToken.update_invite!(invite, uses: 99)
278 :username => "vinny",
279 :email => "pasta@pizza.vs",
280 :fullname => "Vinny Vinesauce",
282 :password => "hiptofbees",
283 :confirm => "hiptofbees",
284 :token => invite.token
287 {:ok, user} = TwitterAPI.register_user(data)
288 assert user == User.get_cached_by_nickname("vinny")
290 invite = Repo.get_by(UserInviteToken, token: invite.token)
291 assert invite.used == true
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
303 {:error, msg} = TwitterAPI.register_user(data)
305 assert msg == "Expired token"
306 refute User.get_cached_by_nickname("GrimReaper")
310 describe "registers with reusable date limited token" do
311 setup do: clear_config([:instance, :registrations_open], false)
313 test "returns user on success" do
314 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
317 :username => "vinny",
318 :email => "pasta@pizza.vs",
319 :fullname => "Vinny Vinesauce",
321 :password => "hiptofbees",
322 :confirm => "hiptofbees",
323 :token => invite.token
326 {:ok, user} = TwitterAPI.register_user(data)
327 assert user == User.get_cached_by_nickname("vinny")
329 invite = Repo.get_by(UserInviteToken, token: invite.token)
333 test "error after max uses" do
334 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
336 UserInviteToken.update_invite!(invite, uses: 99)
339 :username => "vinny",
340 :email => "pasta@pizza.vs",
341 :fullname => "Vinny Vinesauce",
343 :password => "hiptofbees",
344 :confirm => "hiptofbees",
345 :token => invite.token
348 {:ok, user} = TwitterAPI.register_user(data)
349 assert user == User.get_cached_by_nickname("vinny")
351 invite = Repo.get_by(UserInviteToken, token: invite.token)
352 assert invite.used == true
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
364 {:error, msg} = TwitterAPI.register_user(data)
366 assert msg == "Expired token"
367 refute User.get_cached_by_nickname("GrimReaper")
370 test "returns error on overdue date" do
372 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
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
384 {:error, msg} = TwitterAPI.register_user(data)
386 assert msg == "Expired token"
387 refute User.get_cached_by_nickname("GrimReaper")
390 test "returns error on with overdue date and after max" do
392 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
394 UserInviteToken.update_invite!(invite, uses: 100)
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
406 {:error, msg} = TwitterAPI.register_user(data)
408 assert msg == "Expired token"
409 refute User.get_cached_by_nickname("GrimReaper")
413 test "it returns the error on registration problems" do
416 :email => "lain@wired.jp",
417 :fullname => "lain iwakura",
418 :bio => "close the world."
421 {:error, error} = TwitterAPI.register_user(data)
423 assert is_binary(error)
424 refute User.get_cached_by_nickname("lain")