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
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("show.json", %{user: user}) ==
33 AccountView.render("show.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("show.json", %{user: user}) ==
51 AccountView.render("show.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.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
120 clear_config([:instance, :registrations_open]) do
121 Pleroma.Config.put([:instance, :registrations_open], false)
124 test "returns user on success" do
125 {:ok, invite} = UserInviteToken.create_invite()
128 "nickname" => "vinny",
129 "email" => "pasta@pizza.vs",
130 "fullname" => "Vinny Vinesauce",
132 "password" => "hiptofbees",
133 "confirm" => "hiptofbees",
134 "token" => invite.token
137 {:ok, user} = TwitterAPI.register_user(data)
139 fetched_user = User.get_cached_by_nickname("vinny")
140 invite = Repo.get_by(UserInviteToken, token: invite.token)
142 assert invite.used == true
144 assert AccountView.render("show.json", %{user: user}) ==
145 AccountView.render("show.json", %{user: fetched_user})
148 test "returns error on invalid token" do
150 "nickname" => "GrimReaper",
151 "email" => "death@reapers.afterlife",
152 "fullname" => "Reaper Grim",
153 "bio" => "Your time has come",
154 "password" => "scythe",
155 "confirm" => "scythe",
156 "token" => "DudeLetMeInImAFairy"
159 {:error, msg} = TwitterAPI.register_user(data)
161 assert msg == "Invalid token"
162 refute User.get_cached_by_nickname("GrimReaper")
165 test "returns error on expired token" do
166 {:ok, invite} = UserInviteToken.create_invite()
167 UserInviteToken.update_invite!(invite, used: true)
170 "nickname" => "GrimReaper",
171 "email" => "death@reapers.afterlife",
172 "fullname" => "Reaper Grim",
173 "bio" => "Your time has come",
174 "password" => "scythe",
175 "confirm" => "scythe",
176 "token" => invite.token
179 {:error, msg} = TwitterAPI.register_user(data)
181 assert msg == "Expired token"
182 refute User.get_cached_by_nickname("GrimReaper")
186 describe "registers with date limited token" do
187 clear_config([:instance, :registrations_open]) do
188 Pleroma.Config.put([:instance, :registrations_open], false)
193 "nickname" => "vinny",
194 "email" => "pasta@pizza.vs",
195 "fullname" => "Vinny Vinesauce",
197 "password" => "hiptofbees",
198 "confirm" => "hiptofbees"
201 check_fn = fn invite ->
202 data = Map.put(data, "token", invite.token)
203 {:ok, user} = TwitterAPI.register_user(data)
204 fetched_user = User.get_cached_by_nickname("vinny")
206 assert AccountView.render("show.json", %{user: user}) ==
207 AccountView.render("show.json", %{user: fetched_user})
210 {:ok, data: data, check_fn: check_fn}
213 test "returns user on success", %{check_fn: check_fn} do
214 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
218 invite = Repo.get_by(UserInviteToken, token: invite.token)
223 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
224 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
228 invite = Repo.get_by(UserInviteToken, token: invite.token)
233 test "returns an error on overdue date", %{data: data} do
234 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
236 data = Map.put(data, "token", invite.token)
238 {:error, msg} = TwitterAPI.register_user(data)
240 assert msg == "Expired token"
241 refute User.get_cached_by_nickname("vinny")
242 invite = Repo.get_by(UserInviteToken, token: invite.token)
248 describe "registers with reusable token" do
249 clear_config([:instance, :registrations_open]) do
250 Pleroma.Config.put([:instance, :registrations_open], false)
253 test "returns user on success, after him registration fails" do
254 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
256 UserInviteToken.update_invite!(invite, uses: 99)
259 "nickname" => "vinny",
260 "email" => "pasta@pizza.vs",
261 "fullname" => "Vinny Vinesauce",
263 "password" => "hiptofbees",
264 "confirm" => "hiptofbees",
265 "token" => invite.token
268 {:ok, user} = TwitterAPI.register_user(data)
269 fetched_user = User.get_cached_by_nickname("vinny")
270 invite = Repo.get_by(UserInviteToken, token: invite.token)
272 assert invite.used == true
274 assert AccountView.render("show.json", %{user: user}) ==
275 AccountView.render("show.json", %{user: fetched_user})
278 "nickname" => "GrimReaper",
279 "email" => "death@reapers.afterlife",
280 "fullname" => "Reaper Grim",
281 "bio" => "Your time has come",
282 "password" => "scythe",
283 "confirm" => "scythe",
284 "token" => invite.token
287 {:error, msg} = TwitterAPI.register_user(data)
289 assert msg == "Expired token"
290 refute User.get_cached_by_nickname("GrimReaper")
294 describe "registers with reusable date limited token" do
295 clear_config([:instance, :registrations_open]) do
296 Pleroma.Config.put([:instance, :registrations_open], false)
299 test "returns user on success" do
300 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
303 "nickname" => "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 fetched_user = User.get_cached_by_nickname("vinny")
314 invite = Repo.get_by(UserInviteToken, token: invite.token)
318 assert AccountView.render("show.json", %{user: user}) ==
319 AccountView.render("show.json", %{user: fetched_user})
322 test "error after max uses" do
323 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
325 UserInviteToken.update_invite!(invite, uses: 99)
328 "nickname" => "vinny",
329 "email" => "pasta@pizza.vs",
330 "fullname" => "Vinny Vinesauce",
332 "password" => "hiptofbees",
333 "confirm" => "hiptofbees",
334 "token" => invite.token
337 {:ok, user} = TwitterAPI.register_user(data)
338 fetched_user = User.get_cached_by_nickname("vinny")
339 invite = Repo.get_by(UserInviteToken, token: invite.token)
340 assert invite.used == true
342 assert AccountView.render("show.json", %{user: user}) ==
343 AccountView.render("show.json", %{user: fetched_user})
346 "nickname" => "GrimReaper",
347 "email" => "death@reapers.afterlife",
348 "fullname" => "Reaper Grim",
349 "bio" => "Your time has come",
350 "password" => "scythe",
351 "confirm" => "scythe",
352 "token" => invite.token
355 {:error, msg} = TwitterAPI.register_user(data)
357 assert msg == "Expired token"
358 refute User.get_cached_by_nickname("GrimReaper")
361 test "returns error on overdue date" do
363 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
366 "nickname" => "GrimReaper",
367 "email" => "death@reapers.afterlife",
368 "fullname" => "Reaper Grim",
369 "bio" => "Your time has come",
370 "password" => "scythe",
371 "confirm" => "scythe",
372 "token" => invite.token
375 {:error, msg} = TwitterAPI.register_user(data)
377 assert msg == "Expired token"
378 refute User.get_cached_by_nickname("GrimReaper")
381 test "returns error on with overdue date and after max" do
383 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
385 UserInviteToken.update_invite!(invite, uses: 100)
388 "nickname" => "GrimReaper",
389 "email" => "death@reapers.afterlife",
390 "fullname" => "Reaper Grim",
391 "bio" => "Your time has come",
392 "password" => "scythe",
393 "confirm" => "scythe",
394 "token" => invite.token
397 {:error, msg} = TwitterAPI.register_user(data)
399 assert msg == "Expired token"
400 refute User.get_cached_by_nickname("GrimReaper")
404 test "it returns the error on registration problems" do
406 "nickname" => "lain",
407 "email" => "lain@wired.jp",
408 "fullname" => "lain iwakura",
409 "bio" => "close the world.",
413 {:error, error_object} = TwitterAPI.register_user(data)
415 assert is_binary(error_object[:error])
416 refute User.get_cached_by_nickname("lain")
420 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
421 Supervisor.restart_child(Pleroma.Supervisor, Cachex)