1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
8 alias Pleroma.Tests.ObanHelpers
10 alias Pleroma.UserInviteToken
11 alias Pleroma.Web.MastodonAPI.AccountView
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 fetched_user = User.get_cached_by_nickname("lain")
32 assert AccountView.render("account.json", %{user: user}) ==
33 AccountView.render("account.json", %{user: fetched_user})
36 test "it registers a new user with empty string in bio and returns the user." do
39 "email" => "lain@wired.jp",
40 "fullname" => "lain iwakura",
46 {:ok, user} = TwitterAPI.register_user(data)
48 fetched_user = User.get_cached_by_nickname("lain")
50 assert AccountView.render("account.json", %{user: user}) ==
51 AccountView.render("account.json", %{user: fetched_user})
54 test "it sends confirmation email if :account_activation_required is specified in instance config" do
55 setting = Pleroma.Config.get([:instance, :account_activation_required])
58 Pleroma.Config.put([:instance, :account_activation_required], true)
59 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
64 "email" => "lain@wired.jp",
65 "fullname" => "lain iwakura",
71 {:ok, user} = TwitterAPI.register_user(data)
72 ObanHelpers.perform_all()
74 assert user.info.confirmation_pending
76 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
78 notify_email = Pleroma.Config.get([:instance, :notify_email])
79 instance_name = Pleroma.Config.get([:instance, :name])
81 Swoosh.TestAssertions.assert_email_sent(
82 from: {instance_name, notify_email},
83 to: {user.name, user.email},
84 html_body: email.html_body
88 test "it registers a new user and parses mentions in the bio" do
91 "email" => "john@gmail.com",
92 "fullname" => "John Doe",
98 {:ok, user1} = TwitterAPI.register_user(data1)
101 "nickname" => "lain",
102 "email" => "lain@wired.jp",
103 "fullname" => "lain iwakura",
104 "bio" => "@john test",
105 "password" => "bear",
109 {:ok, user2} = TwitterAPI.register_user(data2)
112 ~s(<span class="h-card"><a data-user="#{user1.id}" class="u-url mention" href="#{
114 }" rel="ugc">@<span>john</span></a></span> test)
116 assert user2.bio == expected_text
119 describe "register with one time token" do
121 setting = Pleroma.Config.get([:instance, :registrations_open])
124 Pleroma.Config.put([:instance, :registrations_open], false)
125 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
131 test "returns user on success" do
132 {:ok, invite} = UserInviteToken.create_invite()
135 "nickname" => "vinny",
136 "email" => "pasta@pizza.vs",
137 "fullname" => "Vinny Vinesauce",
139 "password" => "hiptofbees",
140 "confirm" => "hiptofbees",
141 "token" => invite.token
144 {:ok, user} = TwitterAPI.register_user(data)
146 fetched_user = User.get_cached_by_nickname("vinny")
147 invite = Repo.get_by(UserInviteToken, token: invite.token)
149 assert invite.used == true
151 assert AccountView.render("account.json", %{user: user}) ==
152 AccountView.render("account.json", %{user: fetched_user})
155 test "returns error on invalid token" do
157 "nickname" => "GrimReaper",
158 "email" => "death@reapers.afterlife",
159 "fullname" => "Reaper Grim",
160 "bio" => "Your time has come",
161 "password" => "scythe",
162 "confirm" => "scythe",
163 "token" => "DudeLetMeInImAFairy"
166 {:error, msg} = TwitterAPI.register_user(data)
168 assert msg == "Invalid token"
169 refute User.get_cached_by_nickname("GrimReaper")
172 test "returns error on expired token" do
173 {:ok, invite} = UserInviteToken.create_invite()
174 UserInviteToken.update_invite!(invite, used: true)
177 "nickname" => "GrimReaper",
178 "email" => "death@reapers.afterlife",
179 "fullname" => "Reaper Grim",
180 "bio" => "Your time has come",
181 "password" => "scythe",
182 "confirm" => "scythe",
183 "token" => invite.token
186 {:error, msg} = TwitterAPI.register_user(data)
188 assert msg == "Expired token"
189 refute User.get_cached_by_nickname("GrimReaper")
193 describe "registers with date limited token" do
195 setting = Pleroma.Config.get([:instance, :registrations_open])
198 Pleroma.Config.put([:instance, :registrations_open], false)
199 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
203 "nickname" => "vinny",
204 "email" => "pasta@pizza.vs",
205 "fullname" => "Vinny Vinesauce",
207 "password" => "hiptofbees",
208 "confirm" => "hiptofbees"
211 check_fn = fn invite ->
212 data = Map.put(data, "token", invite.token)
213 {:ok, user} = TwitterAPI.register_user(data)
214 fetched_user = User.get_cached_by_nickname("vinny")
216 assert AccountView.render("account.json", %{user: user}) ==
217 AccountView.render("account.json", %{user: fetched_user})
220 {:ok, data: data, check_fn: check_fn}
223 test "returns user on success", %{check_fn: check_fn} do
224 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
228 invite = Repo.get_by(UserInviteToken, token: invite.token)
233 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
234 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
238 invite = Repo.get_by(UserInviteToken, token: invite.token)
243 test "returns an error on overdue date", %{data: data} do
244 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
246 data = Map.put(data, "token", invite.token)
248 {:error, msg} = TwitterAPI.register_user(data)
250 assert msg == "Expired token"
251 refute User.get_cached_by_nickname("vinny")
252 invite = Repo.get_by(UserInviteToken, token: invite.token)
258 describe "registers with reusable token" do
260 setting = Pleroma.Config.get([:instance, :registrations_open])
263 Pleroma.Config.put([:instance, :registrations_open], false)
264 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
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 "nickname" => "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 fetched_user = User.get_cached_by_nickname("vinny")
287 invite = Repo.get_by(UserInviteToken, token: invite.token)
289 assert invite.used == true
291 assert AccountView.render("account.json", %{user: user}) ==
292 AccountView.render("account.json", %{user: fetched_user})
295 "nickname" => "GrimReaper",
296 "email" => "death@reapers.afterlife",
297 "fullname" => "Reaper Grim",
298 "bio" => "Your time has come",
299 "password" => "scythe",
300 "confirm" => "scythe",
301 "token" => invite.token
304 {:error, msg} = TwitterAPI.register_user(data)
306 assert msg == "Expired token"
307 refute User.get_cached_by_nickname("GrimReaper")
311 describe "registers with reusable date limited token" do
313 setting = Pleroma.Config.get([:instance, :registrations_open])
316 Pleroma.Config.put([:instance, :registrations_open], false)
317 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
323 test "returns user on success" do
324 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
327 "nickname" => "vinny",
328 "email" => "pasta@pizza.vs",
329 "fullname" => "Vinny Vinesauce",
331 "password" => "hiptofbees",
332 "confirm" => "hiptofbees",
333 "token" => invite.token
336 {:ok, user} = TwitterAPI.register_user(data)
337 fetched_user = User.get_cached_by_nickname("vinny")
338 invite = Repo.get_by(UserInviteToken, token: invite.token)
342 assert AccountView.render("account.json", %{user: user}) ==
343 AccountView.render("account.json", %{user: fetched_user})
346 test "error after max uses" do
347 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
349 UserInviteToken.update_invite!(invite, uses: 99)
352 "nickname" => "vinny",
353 "email" => "pasta@pizza.vs",
354 "fullname" => "Vinny Vinesauce",
356 "password" => "hiptofbees",
357 "confirm" => "hiptofbees",
358 "token" => invite.token
361 {:ok, user} = TwitterAPI.register_user(data)
362 fetched_user = User.get_cached_by_nickname("vinny")
363 invite = Repo.get_by(UserInviteToken, token: invite.token)
364 assert invite.used == true
366 assert AccountView.render("account.json", %{user: user}) ==
367 AccountView.render("account.json", %{user: fetched_user})
370 "nickname" => "GrimReaper",
371 "email" => "death@reapers.afterlife",
372 "fullname" => "Reaper Grim",
373 "bio" => "Your time has come",
374 "password" => "scythe",
375 "confirm" => "scythe",
376 "token" => invite.token
379 {:error, msg} = TwitterAPI.register_user(data)
381 assert msg == "Expired token"
382 refute User.get_cached_by_nickname("GrimReaper")
385 test "returns error on overdue date" do
387 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
390 "nickname" => "GrimReaper",
391 "email" => "death@reapers.afterlife",
392 "fullname" => "Reaper Grim",
393 "bio" => "Your time has come",
394 "password" => "scythe",
395 "confirm" => "scythe",
396 "token" => invite.token
399 {:error, msg} = TwitterAPI.register_user(data)
401 assert msg == "Expired token"
402 refute User.get_cached_by_nickname("GrimReaper")
405 test "returns error on with overdue date and after max" do
407 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
409 UserInviteToken.update_invite!(invite, uses: 100)
412 "nickname" => "GrimReaper",
413 "email" => "death@reapers.afterlife",
414 "fullname" => "Reaper Grim",
415 "bio" => "Your time has come",
416 "password" => "scythe",
417 "confirm" => "scythe",
418 "token" => invite.token
421 {:error, msg} = TwitterAPI.register_user(data)
423 assert msg == "Expired token"
424 refute User.get_cached_by_nickname("GrimReaper")
428 test "it returns the error on registration problems" do
430 "nickname" => "lain",
431 "email" => "lain@wired.jp",
432 "fullname" => "lain iwakura",
433 "bio" => "close the world.",
437 {:error, error_object} = TwitterAPI.register_user(data)
439 assert is_binary(error_object[:error])
440 refute User.get_cached_by_nickname("lain")
444 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
445 Supervisor.restart_child(Pleroma.Supervisor, Cachex)