1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 setting = Pleroma.Config.get([:instance, :account_activation_required])
52 Pleroma.Config.put([:instance, :account_activation_required], true)
53 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
58 :email => "lain@wired.jp",
59 :fullname => "lain iwakura",
65 {:ok, user} = TwitterAPI.register_user(data)
66 ObanHelpers.perform_all()
68 assert user.confirmation_pending
70 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
72 notify_email = Pleroma.Config.get([:instance, :notify_email])
73 instance_name = Pleroma.Config.get([:instance, :name])
75 Swoosh.TestAssertions.assert_email_sent(
76 from: {instance_name, notify_email},
77 to: {user.name, user.email},
78 html_body: email.html_body
82 test "it registers a new user and parses mentions in the bio" do
85 :email => "john@gmail.com",
86 :fullname => "John Doe",
92 {:ok, user1} = TwitterAPI.register_user(data1)
96 :email => "lain@wired.jp",
97 :fullname => "lain iwakura",
103 {:ok, user2} = TwitterAPI.register_user(data2)
106 ~s(<span class="h-card"><a class="u-url mention" data-user="#{user1.id}" href="#{
108 }" rel="ugc">@<span>john</span></a></span> test)
110 assert user2.bio == expected_text
113 describe "register with one time token" do
114 setup do: clear_config([:instance, :registrations_open], false)
116 test "returns user on success" do
117 {:ok, invite} = UserInviteToken.create_invite()
120 :username => "vinny",
121 :email => "pasta@pizza.vs",
122 :fullname => "Vinny Vinesauce",
124 :password => "hiptofbees",
125 :confirm => "hiptofbees",
126 :token => invite.token
129 {:ok, user} = TwitterAPI.register_user(data)
131 assert user == User.get_cached_by_nickname("vinny")
133 invite = Repo.get_by(UserInviteToken, token: invite.token)
134 assert invite.used == true
137 test "returns error on invalid token" do
139 :username => "GrimReaper",
140 :email => "death@reapers.afterlife",
141 :fullname => "Reaper Grim",
142 :bio => "Your time has come",
143 :password => "scythe",
144 :confirm => "scythe",
145 :token => "DudeLetMeInImAFairy"
148 {:error, msg} = TwitterAPI.register_user(data)
150 assert msg == "Invalid token"
151 refute User.get_cached_by_nickname("GrimReaper")
154 test "returns error on expired token" do
155 {:ok, invite} = UserInviteToken.create_invite()
156 UserInviteToken.update_invite!(invite, used: true)
159 :username => "GrimReaper",
160 :email => "death@reapers.afterlife",
161 :fullname => "Reaper Grim",
162 :bio => "Your time has come",
163 :password => "scythe",
164 :confirm => "scythe",
165 :token => invite.token
168 {:error, msg} = TwitterAPI.register_user(data)
170 assert msg == "Expired token"
171 refute User.get_cached_by_nickname("GrimReaper")
175 describe "registers with date limited token" do
176 setup do: clear_config([:instance, :registrations_open], false)
180 :username => "vinny",
181 :email => "pasta@pizza.vs",
182 :fullname => "Vinny Vinesauce",
184 :password => "hiptofbees",
185 :confirm => "hiptofbees"
188 check_fn = fn invite ->
189 data = Map.put(data, :token, invite.token)
190 {:ok, user} = TwitterAPI.register_user(data)
192 assert user == User.get_cached_by_nickname("vinny")
195 {:ok, data: data, check_fn: check_fn}
198 test "returns user on success", %{check_fn: check_fn} do
199 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
203 invite = Repo.get_by(UserInviteToken, token: invite.token)
208 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
209 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
213 invite = Repo.get_by(UserInviteToken, token: invite.token)
218 test "returns an error on overdue date", %{data: data} do
219 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
221 data = Map.put(data, "token", invite.token)
223 {:error, msg} = TwitterAPI.register_user(data)
225 assert msg == "Expired token"
226 refute User.get_cached_by_nickname("vinny")
227 invite = Repo.get_by(UserInviteToken, token: invite.token)
233 describe "registers with reusable token" do
234 setup do: clear_config([:instance, :registrations_open], false)
236 test "returns user on success, after him registration fails" do
237 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
239 UserInviteToken.update_invite!(invite, uses: 99)
242 :username => "vinny",
243 :email => "pasta@pizza.vs",
244 :fullname => "Vinny Vinesauce",
246 :password => "hiptofbees",
247 :confirm => "hiptofbees",
248 :token => invite.token
251 {:ok, user} = TwitterAPI.register_user(data)
252 assert user == User.get_cached_by_nickname("vinny")
254 invite = Repo.get_by(UserInviteToken, token: invite.token)
255 assert invite.used == true
258 :username => "GrimReaper",
259 :email => "death@reapers.afterlife",
260 :fullname => "Reaper Grim",
261 :bio => "Your time has come",
262 :password => "scythe",
263 :confirm => "scythe",
264 :token => invite.token
267 {:error, msg} = TwitterAPI.register_user(data)
269 assert msg == "Expired token"
270 refute User.get_cached_by_nickname("GrimReaper")
274 describe "registers with reusable date limited token" do
275 setup do: clear_config([:instance, :registrations_open], false)
277 test "returns user on success" do
278 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
281 :username => "vinny",
282 :email => "pasta@pizza.vs",
283 :fullname => "Vinny Vinesauce",
285 :password => "hiptofbees",
286 :confirm => "hiptofbees",
287 :token => invite.token
290 {:ok, user} = TwitterAPI.register_user(data)
291 assert user == User.get_cached_by_nickname("vinny")
293 invite = Repo.get_by(UserInviteToken, token: invite.token)
297 test "error after max uses" do
298 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
300 UserInviteToken.update_invite!(invite, uses: 99)
303 :username => "vinny",
304 :email => "pasta@pizza.vs",
305 :fullname => "Vinny Vinesauce",
307 :password => "hiptofbees",
308 :confirm => "hiptofbees",
309 :token => invite.token
312 {:ok, user} = TwitterAPI.register_user(data)
313 assert user == User.get_cached_by_nickname("vinny")
315 invite = Repo.get_by(UserInviteToken, token: invite.token)
316 assert invite.used == true
319 :username => "GrimReaper",
320 :email => "death@reapers.afterlife",
321 :fullname => "Reaper Grim",
322 :bio => "Your time has come",
323 :password => "scythe",
324 :confirm => "scythe",
325 :token => invite.token
328 {:error, msg} = TwitterAPI.register_user(data)
330 assert msg == "Expired token"
331 refute User.get_cached_by_nickname("GrimReaper")
334 test "returns error on overdue date" do
336 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
339 :username => "GrimReaper",
340 :email => "death@reapers.afterlife",
341 :fullname => "Reaper Grim",
342 :bio => "Your time has come",
343 :password => "scythe",
344 :confirm => "scythe",
345 :token => invite.token
348 {:error, msg} = TwitterAPI.register_user(data)
350 assert msg == "Expired token"
351 refute User.get_cached_by_nickname("GrimReaper")
354 test "returns error on with overdue date and after max" do
356 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
358 UserInviteToken.update_invite!(invite, uses: 100)
361 :username => "GrimReaper",
362 :email => "death@reapers.afterlife",
363 :fullname => "Reaper Grim",
364 :bio => "Your time has come",
365 :password => "scythe",
366 :confirm => "scythe",
367 :token => invite.token
370 {:error, msg} = TwitterAPI.register_user(data)
372 assert msg == "Expired token"
373 refute User.get_cached_by_nickname("GrimReaper")
377 test "it returns the error on registration problems" do
380 :email => "lain@wired.jp",
381 :fullname => "lain iwakura",
382 :bio => "close the world."
385 {:error, error} = TwitterAPI.register_user(data)
387 assert is_binary(error)
388 refute User.get_cached_by_nickname("lain")
392 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
393 Supervisor.restart_child(Pleroma.Supervisor, Cachex)