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)
102 :email => "lain@wired.jp",
103 :fullname => "lain iwakura",
104 :bio => "@john test",
109 {:ok, user2} = TwitterAPI.register_user(data2)
112 ~s(<span class="h-card"><a class="u-url mention" data-user="#{user1.id}" 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 setup do: clear_config([:instance, :registrations_open], false)
122 test "returns user on success" do
123 {:ok, invite} = UserInviteToken.create_invite()
126 :nickname => "vinny",
127 :email => "pasta@pizza.vs",
128 :fullname => "Vinny Vinesauce",
130 :password => "hiptofbees",
131 :confirm => "hiptofbees",
132 :token => invite.token
135 {:ok, user} = TwitterAPI.register_user(data)
137 fetched_user = User.get_cached_by_nickname("vinny")
138 invite = Repo.get_by(UserInviteToken, token: invite.token)
140 assert invite.used == true
142 assert AccountView.render("show.json", %{user: user}) ==
143 AccountView.render("show.json", %{user: fetched_user})
146 test "returns error on invalid token" do
148 :nickname => "GrimReaper",
149 :email => "death@reapers.afterlife",
150 :fullname => "Reaper Grim",
151 :bio => "Your time has come",
152 :password => "scythe",
153 :confirm => "scythe",
154 :token => "DudeLetMeInImAFairy"
157 {:error, msg} = TwitterAPI.register_user(data)
159 assert msg == "Invalid token"
160 refute User.get_cached_by_nickname("GrimReaper")
163 test "returns error on expired token" do
164 {:ok, invite} = UserInviteToken.create_invite()
165 UserInviteToken.update_invite!(invite, used: true)
168 :nickname => "GrimReaper",
169 :email => "death@reapers.afterlife",
170 :fullname => "Reaper Grim",
171 :bio => "Your time has come",
172 :password => "scythe",
173 :confirm => "scythe",
174 :token => invite.token
177 {:error, msg} = TwitterAPI.register_user(data)
179 assert msg == "Expired token"
180 refute User.get_cached_by_nickname("GrimReaper")
184 describe "registers with date limited token" do
185 setup do: clear_config([:instance, :registrations_open], false)
189 :nickname => "vinny",
190 :email => "pasta@pizza.vs",
191 :fullname => "Vinny Vinesauce",
193 :password => "hiptofbees",
194 :confirm => "hiptofbees"
197 check_fn = fn invite ->
198 data = Map.put(data, :token, invite.token)
199 {:ok, user} = TwitterAPI.register_user(data)
200 fetched_user = User.get_cached_by_nickname("vinny")
202 assert AccountView.render("show.json", %{user: user}) ==
203 AccountView.render("show.json", %{user: fetched_user})
206 {:ok, data: data, check_fn: check_fn}
209 test "returns user on success", %{check_fn: check_fn} do
210 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
214 invite = Repo.get_by(UserInviteToken, token: invite.token)
219 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
220 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
224 invite = Repo.get_by(UserInviteToken, token: invite.token)
229 test "returns an error on overdue date", %{data: data} do
230 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
232 data = Map.put(data, "token", invite.token)
234 {:error, msg} = TwitterAPI.register_user(data)
236 assert msg == "Expired token"
237 refute User.get_cached_by_nickname("vinny")
238 invite = Repo.get_by(UserInviteToken, token: invite.token)
244 describe "registers with reusable token" do
245 setup do: clear_config([:instance, :registrations_open], false)
247 test "returns user on success, after him registration fails" do
248 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
250 UserInviteToken.update_invite!(invite, uses: 99)
253 :nickname => "vinny",
254 :email => "pasta@pizza.vs",
255 :fullname => "Vinny Vinesauce",
257 :password => "hiptofbees",
258 :confirm => "hiptofbees",
259 :token => invite.token
262 {:ok, user} = TwitterAPI.register_user(data)
263 fetched_user = User.get_cached_by_nickname("vinny")
264 invite = Repo.get_by(UserInviteToken, token: invite.token)
266 assert invite.used == true
268 assert AccountView.render("show.json", %{user: user}) ==
269 AccountView.render("show.json", %{user: fetched_user})
272 :nickname => "GrimReaper",
273 :email => "death@reapers.afterlife",
274 :fullname => "Reaper Grim",
275 :bio => "Your time has come",
276 :password => "scythe",
277 :confirm => "scythe",
278 :token => invite.token
281 {:error, msg} = TwitterAPI.register_user(data)
283 assert msg == "Expired token"
284 refute User.get_cached_by_nickname("GrimReaper")
288 describe "registers with reusable date limited token" do
289 setup do: clear_config([:instance, :registrations_open], false)
291 test "returns user on success" do
292 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
295 :nickname => "vinny",
296 :email => "pasta@pizza.vs",
297 :fullname => "Vinny Vinesauce",
299 :password => "hiptofbees",
300 :confirm => "hiptofbees",
301 :token => invite.token
304 {:ok, user} = TwitterAPI.register_user(data)
305 fetched_user = User.get_cached_by_nickname("vinny")
306 invite = Repo.get_by(UserInviteToken, token: invite.token)
310 assert AccountView.render("show.json", %{user: user}) ==
311 AccountView.render("show.json", %{user: fetched_user})
314 test "error after max uses" do
315 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
317 UserInviteToken.update_invite!(invite, uses: 99)
320 :nickname => "vinny",
321 :email => "pasta@pizza.vs",
322 :fullname => "Vinny Vinesauce",
324 :password => "hiptofbees",
325 :confirm => "hiptofbees",
326 :token => invite.token
329 {:ok, user} = TwitterAPI.register_user(data)
330 fetched_user = User.get_cached_by_nickname("vinny")
331 invite = Repo.get_by(UserInviteToken, token: invite.token)
332 assert invite.used == true
334 assert AccountView.render("show.json", %{user: user}) ==
335 AccountView.render("show.json", %{user: fetched_user})
338 :nickname => "GrimReaper",
339 :email => "death@reapers.afterlife",
340 :fullname => "Reaper Grim",
341 :bio => "Your time has come",
342 :password => "scythe",
343 :confirm => "scythe",
344 :token => invite.token
347 {:error, msg} = TwitterAPI.register_user(data)
349 assert msg == "Expired token"
350 refute User.get_cached_by_nickname("GrimReaper")
353 test "returns error on overdue date" do
355 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
358 :nickname => "GrimReaper",
359 :email => "death@reapers.afterlife",
360 :fullname => "Reaper Grim",
361 :bio => "Your time has come",
362 :password => "scythe",
363 :confirm => "scythe",
364 :token => invite.token
367 {:error, msg} = TwitterAPI.register_user(data)
369 assert msg == "Expired token"
370 refute User.get_cached_by_nickname("GrimReaper")
373 test "returns error on with overdue date and after max" do
375 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
377 UserInviteToken.update_invite!(invite, uses: 100)
380 :nickname => "GrimReaper",
381 :email => "death@reapers.afterlife",
382 :fullname => "Reaper Grim",
383 :bio => "Your time has come",
384 :password => "scythe",
385 :confirm => "scythe",
386 :token => invite.token
389 {:error, msg} = TwitterAPI.register_user(data)
391 assert msg == "Expired token"
392 refute User.get_cached_by_nickname("GrimReaper")
396 test "it returns the error on registration problems" do
399 :email => "lain@wired.jp",
400 :fullname => "lain iwakura",
401 :bio => "close the world.",
405 {:error, error_object} = TwitterAPI.register_user(data)
407 assert is_binary(error_object[:error])
408 refute User.get_cached_by_nickname("lain")
412 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
413 Supervisor.restart_child(Pleroma.Supervisor, Cachex)