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
14 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 test "it registers a new user and returns the user." do
21 "email" => "lain@wired.jp",
22 "fullname" => "lain iwakura",
27 {:ok, user} = TwitterAPI.register_user(data)
29 fetched_user = User.get_cached_by_nickname("lain")
31 assert AccountView.render("account.json", %{user: user}) ==
32 AccountView.render("account.json", %{user: fetched_user})
35 test "it registers a new user with empty string in bio and returns the user." do
38 "email" => "lain@wired.jp",
39 "fullname" => "lain iwakura",
45 {:ok, user} = TwitterAPI.register_user(data)
47 fetched_user = User.get_cached_by_nickname("lain")
49 assert AccountView.render("account.json", %{user: user}) ==
50 AccountView.render("account.json", %{user: fetched_user})
53 test "it sends confirmation email if :account_activation_required is specified in instance config" do
54 setting = Pleroma.Config.get([:instance, :account_activation_required])
57 Pleroma.Config.put([:instance, :account_activation_required], true)
58 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
63 "email" => "lain@wired.jp",
64 "fullname" => "lain iwakura",
70 {:ok, user} = TwitterAPI.register_user(data)
72 assert user.info.confirmation_pending
74 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
76 notify_email = Pleroma.Config.get([:instance, :notify_email])
77 instance_name = Pleroma.Config.get([:instance, :name])
79 Swoosh.TestAssertions.assert_email_sent(
80 from: {instance_name, notify_email},
81 to: {user.name, user.email},
82 html_body: email.html_body
86 test "it registers a new user and parses mentions in the bio" do
89 "email" => "john@gmail.com",
90 "fullname" => "John Doe",
96 {:ok, user1} = TwitterAPI.register_user(data1)
100 "email" => "lain@wired.jp",
101 "fullname" => "lain iwakura",
102 "bio" => "@john test",
103 "password" => "bear",
107 {:ok, user2} = TwitterAPI.register_user(data2)
110 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
112 assert user2.bio == expected_text
115 describe "register with one time token" do
117 setting = Pleroma.Config.get([:instance, :registrations_open])
120 Pleroma.Config.put([:instance, :registrations_open], false)
121 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
127 test "returns user on success" do
128 {:ok, invite} = UserInviteToken.create_invite()
131 "nickname" => "vinny",
132 "email" => "pasta@pizza.vs",
133 "fullname" => "Vinny Vinesauce",
135 "password" => "hiptofbees",
136 "confirm" => "hiptofbees",
137 "token" => invite.token
140 {:ok, user} = TwitterAPI.register_user(data)
142 fetched_user = User.get_cached_by_nickname("vinny")
143 invite = Repo.get_by(UserInviteToken, token: invite.token)
145 assert invite.used == true
147 assert AccountView.render("account.json", %{user: user}) ==
148 AccountView.render("account.json", %{user: fetched_user})
151 test "returns error on invalid token" do
153 "nickname" => "GrimReaper",
154 "email" => "death@reapers.afterlife",
155 "fullname" => "Reaper Grim",
156 "bio" => "Your time has come",
157 "password" => "scythe",
158 "confirm" => "scythe",
159 "token" => "DudeLetMeInImAFairy"
162 {:error, msg} = TwitterAPI.register_user(data)
164 assert msg == "Invalid token"
165 refute User.get_cached_by_nickname("GrimReaper")
168 test "returns error on expired token" do
169 {:ok, invite} = UserInviteToken.create_invite()
170 UserInviteToken.update_invite!(invite, used: true)
173 "nickname" => "GrimReaper",
174 "email" => "death@reapers.afterlife",
175 "fullname" => "Reaper Grim",
176 "bio" => "Your time has come",
177 "password" => "scythe",
178 "confirm" => "scythe",
179 "token" => invite.token
182 {:error, msg} = TwitterAPI.register_user(data)
184 assert msg == "Expired token"
185 refute User.get_cached_by_nickname("GrimReaper")
189 describe "registers with date limited token" do
191 setting = Pleroma.Config.get([:instance, :registrations_open])
194 Pleroma.Config.put([:instance, :registrations_open], false)
195 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
199 "nickname" => "vinny",
200 "email" => "pasta@pizza.vs",
201 "fullname" => "Vinny Vinesauce",
203 "password" => "hiptofbees",
204 "confirm" => "hiptofbees"
207 check_fn = fn invite ->
208 data = Map.put(data, "token", invite.token)
209 {:ok, user} = TwitterAPI.register_user(data)
210 fetched_user = User.get_cached_by_nickname("vinny")
212 assert AccountView.render("account.json", %{user: user}) ==
213 AccountView.render("account.json", %{user: fetched_user})
216 {:ok, data: data, check_fn: check_fn}
219 test "returns user on success", %{check_fn: check_fn} do
220 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
224 invite = Repo.get_by(UserInviteToken, token: invite.token)
229 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
230 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
234 invite = Repo.get_by(UserInviteToken, token: invite.token)
239 test "returns an error on overdue date", %{data: data} do
240 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
242 data = Map.put(data, "token", invite.token)
244 {:error, msg} = TwitterAPI.register_user(data)
246 assert msg == "Expired token"
247 refute User.get_cached_by_nickname("vinny")
248 invite = Repo.get_by(UserInviteToken, token: invite.token)
254 describe "registers with reusable token" do
256 setting = Pleroma.Config.get([:instance, :registrations_open])
259 Pleroma.Config.put([:instance, :registrations_open], false)
260 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
266 test "returns user on success, after him registration fails" do
267 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
269 UserInviteToken.update_invite!(invite, uses: 99)
272 "nickname" => "vinny",
273 "email" => "pasta@pizza.vs",
274 "fullname" => "Vinny Vinesauce",
276 "password" => "hiptofbees",
277 "confirm" => "hiptofbees",
278 "token" => invite.token
281 {:ok, user} = TwitterAPI.register_user(data)
282 fetched_user = User.get_cached_by_nickname("vinny")
283 invite = Repo.get_by(UserInviteToken, token: invite.token)
285 assert invite.used == true
287 assert AccountView.render("account.json", %{user: user}) ==
288 AccountView.render("account.json", %{user: fetched_user})
291 "nickname" => "GrimReaper",
292 "email" => "death@reapers.afterlife",
293 "fullname" => "Reaper Grim",
294 "bio" => "Your time has come",
295 "password" => "scythe",
296 "confirm" => "scythe",
297 "token" => invite.token
300 {:error, msg} = TwitterAPI.register_user(data)
302 assert msg == "Expired token"
303 refute User.get_cached_by_nickname("GrimReaper")
307 describe "registers with reusable date limited token" do
309 setting = Pleroma.Config.get([:instance, :registrations_open])
312 Pleroma.Config.put([:instance, :registrations_open], false)
313 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
319 test "returns user on success" do
320 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
323 "nickname" => "vinny",
324 "email" => "pasta@pizza.vs",
325 "fullname" => "Vinny Vinesauce",
327 "password" => "hiptofbees",
328 "confirm" => "hiptofbees",
329 "token" => invite.token
332 {:ok, user} = TwitterAPI.register_user(data)
333 fetched_user = User.get_cached_by_nickname("vinny")
334 invite = Repo.get_by(UserInviteToken, token: invite.token)
338 assert AccountView.render("account.json", %{user: user}) ==
339 AccountView.render("account.json", %{user: fetched_user})
342 test "error after max uses" do
343 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
345 UserInviteToken.update_invite!(invite, uses: 99)
348 "nickname" => "vinny",
349 "email" => "pasta@pizza.vs",
350 "fullname" => "Vinny Vinesauce",
352 "password" => "hiptofbees",
353 "confirm" => "hiptofbees",
354 "token" => invite.token
357 {:ok, user} = TwitterAPI.register_user(data)
358 fetched_user = User.get_cached_by_nickname("vinny")
359 invite = Repo.get_by(UserInviteToken, token: invite.token)
360 assert invite.used == true
362 assert AccountView.render("account.json", %{user: user}) ==
363 AccountView.render("account.json", %{user: fetched_user})
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 overdue date" do
383 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
386 "nickname" => "GrimReaper",
387 "email" => "death@reapers.afterlife",
388 "fullname" => "Reaper Grim",
389 "bio" => "Your time has come",
390 "password" => "scythe",
391 "confirm" => "scythe",
392 "token" => invite.token
395 {:error, msg} = TwitterAPI.register_user(data)
397 assert msg == "Expired token"
398 refute User.get_cached_by_nickname("GrimReaper")
401 test "returns error on with overdue date and after max" do
403 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
405 UserInviteToken.update_invite!(invite, uses: 100)
408 "nickname" => "GrimReaper",
409 "email" => "death@reapers.afterlife",
410 "fullname" => "Reaper Grim",
411 "bio" => "Your time has come",
412 "password" => "scythe",
413 "confirm" => "scythe",
414 "token" => invite.token
417 {:error, msg} = TwitterAPI.register_user(data)
419 assert msg == "Expired token"
420 refute User.get_cached_by_nickname("GrimReaper")
424 test "it returns the error on registration problems" do
426 "nickname" => "lain",
427 "email" => "lain@wired.jp",
428 "fullname" => "lain iwakura",
429 "bio" => "close the world.",
433 {:error, error_object} = TwitterAPI.register_user(data)
435 assert is_binary(error_object[:error])
436 refute User.get_cached_by_nickname("lain")
440 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
441 Supervisor.restart_child(Pleroma.Supervisor, Cachex)