1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
9 alias Pleroma.UserInviteToken
10 alias Pleroma.Web.MastodonAPI.AccountView
11 alias Pleroma.Web.TwitterAPI.TwitterAPI
13 import Pleroma.Factory
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 test "it registers a new user and returns the user." do
23 "email" => "lain@wired.jp",
24 "fullname" => "lain iwakura",
29 {:ok, user} = TwitterAPI.register_user(data)
31 fetched_user = User.get_cached_by_nickname("lain")
33 assert AccountView.render("account.json", %{user: user}) ==
34 AccountView.render("account.json", %{user: fetched_user})
37 test "it registers a new user with empty string in bio and returns the user." do
40 "email" => "lain@wired.jp",
41 "fullname" => "lain iwakura",
47 {:ok, user} = TwitterAPI.register_user(data)
49 fetched_user = User.get_cached_by_nickname("lain")
51 assert AccountView.render("account.json", %{user: user}) ==
52 AccountView.render("account.json", %{user: fetched_user})
55 test "it sends confirmation email if :account_activation_required is specified in instance config" do
56 setting = Pleroma.Config.get([:instance, :account_activation_required])
59 Pleroma.Config.put([:instance, :account_activation_required], true)
60 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
65 "email" => "lain@wired.jp",
66 "fullname" => "lain iwakura",
72 {:ok, user} = TwitterAPI.register_user(data)
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 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
114 assert user2.bio == expected_text
117 describe "register with one time token" do
119 setting = Pleroma.Config.get([:instance, :registrations_open])
122 Pleroma.Config.put([:instance, :registrations_open], false)
123 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
129 test "returns user on success" do
130 {:ok, invite} = UserInviteToken.create_invite()
133 "nickname" => "vinny",
134 "email" => "pasta@pizza.vs",
135 "fullname" => "Vinny Vinesauce",
137 "password" => "hiptofbees",
138 "confirm" => "hiptofbees",
139 "token" => invite.token
142 {:ok, user} = TwitterAPI.register_user(data)
144 fetched_user = User.get_cached_by_nickname("vinny")
145 invite = Repo.get_by(UserInviteToken, token: invite.token)
147 assert invite.used == true
149 assert AccountView.render("account.json", %{user: user}) ==
150 AccountView.render("account.json", %{user: fetched_user})
153 test "returns error on invalid token" do
155 "nickname" => "GrimReaper",
156 "email" => "death@reapers.afterlife",
157 "fullname" => "Reaper Grim",
158 "bio" => "Your time has come",
159 "password" => "scythe",
160 "confirm" => "scythe",
161 "token" => "DudeLetMeInImAFairy"
164 {:error, msg} = TwitterAPI.register_user(data)
166 assert msg == "Invalid token"
167 refute User.get_cached_by_nickname("GrimReaper")
170 test "returns error on expired token" do
171 {:ok, invite} = UserInviteToken.create_invite()
172 UserInviteToken.update_invite!(invite, used: true)
175 "nickname" => "GrimReaper",
176 "email" => "death@reapers.afterlife",
177 "fullname" => "Reaper Grim",
178 "bio" => "Your time has come",
179 "password" => "scythe",
180 "confirm" => "scythe",
181 "token" => invite.token
184 {:error, msg} = TwitterAPI.register_user(data)
186 assert msg == "Expired token"
187 refute User.get_cached_by_nickname("GrimReaper")
191 describe "registers with date limited token" do
193 setting = Pleroma.Config.get([:instance, :registrations_open])
196 Pleroma.Config.put([:instance, :registrations_open], false)
197 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
201 "nickname" => "vinny",
202 "email" => "pasta@pizza.vs",
203 "fullname" => "Vinny Vinesauce",
205 "password" => "hiptofbees",
206 "confirm" => "hiptofbees"
209 check_fn = fn invite ->
210 data = Map.put(data, "token", invite.token)
211 {:ok, user} = TwitterAPI.register_user(data)
212 fetched_user = User.get_cached_by_nickname("vinny")
214 assert AccountView.render("account.json", %{user: user}) ==
215 AccountView.render("account.json", %{user: fetched_user})
218 {:ok, data: data, check_fn: check_fn}
221 test "returns user on success", %{check_fn: check_fn} do
222 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
226 invite = Repo.get_by(UserInviteToken, token: invite.token)
231 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
232 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
236 invite = Repo.get_by(UserInviteToken, token: invite.token)
241 test "returns an error on overdue date", %{data: data} do
242 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
244 data = Map.put(data, "token", invite.token)
246 {:error, msg} = TwitterAPI.register_user(data)
248 assert msg == "Expired token"
249 refute User.get_cached_by_nickname("vinny")
250 invite = Repo.get_by(UserInviteToken, token: invite.token)
256 describe "registers with reusable token" do
258 setting = Pleroma.Config.get([:instance, :registrations_open])
261 Pleroma.Config.put([:instance, :registrations_open], false)
262 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
268 test "returns user on success, after him registration fails" do
269 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
271 UserInviteToken.update_invite!(invite, uses: 99)
274 "nickname" => "vinny",
275 "email" => "pasta@pizza.vs",
276 "fullname" => "Vinny Vinesauce",
278 "password" => "hiptofbees",
279 "confirm" => "hiptofbees",
280 "token" => invite.token
283 {:ok, user} = TwitterAPI.register_user(data)
284 fetched_user = User.get_cached_by_nickname("vinny")
285 invite = Repo.get_by(UserInviteToken, token: invite.token)
287 assert invite.used == true
289 assert AccountView.render("account.json", %{user: user}) ==
290 AccountView.render("account.json", %{user: fetched_user})
293 "nickname" => "GrimReaper",
294 "email" => "death@reapers.afterlife",
295 "fullname" => "Reaper Grim",
296 "bio" => "Your time has come",
297 "password" => "scythe",
298 "confirm" => "scythe",
299 "token" => invite.token
302 {:error, msg} = TwitterAPI.register_user(data)
304 assert msg == "Expired token"
305 refute User.get_cached_by_nickname("GrimReaper")
309 describe "registers with reusable date limited token" do
311 setting = Pleroma.Config.get([:instance, :registrations_open])
314 Pleroma.Config.put([:instance, :registrations_open], false)
315 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
321 test "returns user on success" do
322 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
325 "nickname" => "vinny",
326 "email" => "pasta@pizza.vs",
327 "fullname" => "Vinny Vinesauce",
329 "password" => "hiptofbees",
330 "confirm" => "hiptofbees",
331 "token" => invite.token
334 {:ok, user} = TwitterAPI.register_user(data)
335 fetched_user = User.get_cached_by_nickname("vinny")
336 invite = Repo.get_by(UserInviteToken, token: invite.token)
340 assert AccountView.render("account.json", %{user: user}) ==
341 AccountView.render("account.json", %{user: fetched_user})
344 test "error after max uses" do
345 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
347 UserInviteToken.update_invite!(invite, uses: 99)
350 "nickname" => "vinny",
351 "email" => "pasta@pizza.vs",
352 "fullname" => "Vinny Vinesauce",
354 "password" => "hiptofbees",
355 "confirm" => "hiptofbees",
356 "token" => invite.token
359 {:ok, user} = TwitterAPI.register_user(data)
360 fetched_user = User.get_cached_by_nickname("vinny")
361 invite = Repo.get_by(UserInviteToken, token: invite.token)
362 assert invite.used == true
364 assert AccountView.render("account.json", %{user: user}) ==
365 AccountView.render("account.json", %{user: fetched_user})
368 "nickname" => "GrimReaper",
369 "email" => "death@reapers.afterlife",
370 "fullname" => "Reaper Grim",
371 "bio" => "Your time has come",
372 "password" => "scythe",
373 "confirm" => "scythe",
374 "token" => invite.token
377 {:error, msg} = TwitterAPI.register_user(data)
379 assert msg == "Expired token"
380 refute User.get_cached_by_nickname("GrimReaper")
383 test "returns error on overdue date" do
385 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 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")
403 test "returns error on with overdue date and after max" do
405 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
407 UserInviteToken.update_invite!(invite, uses: 100)
410 "nickname" => "GrimReaper",
411 "email" => "death@reapers.afterlife",
412 "fullname" => "Reaper Grim",
413 "bio" => "Your time has come",
414 "password" => "scythe",
415 "confirm" => "scythe",
416 "token" => invite.token
419 {:error, msg} = TwitterAPI.register_user(data)
421 assert msg == "Expired token"
422 refute User.get_cached_by_nickname("GrimReaper")
426 test "it returns the error on registration problems" do
428 "nickname" => "lain",
429 "email" => "lain@wired.jp",
430 "fullname" => "lain iwakura",
431 "bio" => "close the world.",
435 {:error, error_object} = TwitterAPI.register_user(data)
437 assert is_binary(error_object[:error])
438 refute User.get_cached_by_nickname("lain")
442 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
443 Supervisor.restart_child(Pleroma.Supervisor, Cachex)