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
11 alias Pleroma.UserInviteToken
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.TwitterAPI.ActivityView
14 alias Pleroma.Web.TwitterAPI.TwitterAPI
15 alias Pleroma.Web.TwitterAPI.UserView
17 import Pleroma.Factory
20 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 test "create a status" do
26 mentioned_user = insert(:user, %{nickname: "shp", ap_id: "shp"})
33 "mediaType" => "image/jpg",
34 "href" => "http://example.org/image.jpg"
40 object = Repo.insert!(%Object{data: object_data})
44 "Hello again, @shp.<script></script>\nThis is on another :firefox: line. #2hu #epic #phantasmagoric",
45 "media_ids" => [object.id]
48 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
49 object = Object.normalize(activity)
52 "Hello again, <span class='h-card'><a data-user='#{mentioned_user.id}' class='u-url mention' href='shp'>@<span>shp</span></a></span>.<script></script><br>This is on another :firefox: line. <a class='hashtag' data-tag='2hu' href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a> <a class='hashtag' data-tag='epic' href='http://localhost:4001/tag/epic' rel='tag'>#epic</a> <a class='hashtag' data-tag='phantasmagoric' href='http://localhost:4001/tag/phantasmagoric' rel='tag'>#phantasmagoric</a><br><a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>"
54 assert get_in(object.data, ["content"]) == expected_text
55 assert get_in(object.data, ["type"]) == "Note"
56 assert get_in(object.data, ["actor"]) == user.ap_id
57 assert get_in(activity.data, ["actor"]) == user.ap_id
58 assert Enum.member?(get_in(activity.data, ["cc"]), User.ap_followers(user))
61 get_in(activity.data, ["to"]),
62 "https://www.w3.org/ns/activitystreams#Public"
65 assert Enum.member?(get_in(activity.data, ["to"]), "shp")
66 assert activity.local == true
68 assert %{"firefox" => "http://localhost:4001/emoji/Firefox.gif"} = object.data["emoji"]
71 assert object.data["tag"] == ["2hu", "epic", "phantasmagoric"]
74 assert is_binary(get_in(activity.data, ["context"]))
75 assert is_binary(get_in(object.data, ["context"]))
77 assert is_list(object.data["attachment"])
79 assert activity.data["object"] == object.data["id"]
81 user = User.get_cached_by_ap_id(user.ap_id)
83 assert user.info.note_count == 1
86 test "create a status that is a reply" do
90 "status" => "Hello again."
93 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
94 object = Object.normalize(activity)
97 "status" => "Here's your (you).",
98 "in_reply_to_status_id" => activity.id
101 {:ok, reply = %Activity{}} = TwitterAPI.create_status(user, input)
102 reply_object = Object.normalize(reply)
104 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
106 assert get_in(reply_object.data, ["context"]) == get_in(object.data, ["context"])
108 assert get_in(reply_object.data, ["inReplyTo"]) == get_in(activity.data, ["object"])
109 assert Activity.get_in_reply_to_activity(reply).id == activity.id
112 test "Follow another user using user_id" do
114 followed = insert(:user)
116 {:ok, user, followed, _activity} = TwitterAPI.follow(user, %{"user_id" => followed.id})
117 assert User.ap_followers(followed) in user.following
119 {:ok, _, _, _} = TwitterAPI.follow(user, %{"user_id" => followed.id})
122 test "Follow another user using screen_name" do
124 followed = insert(:user)
126 {:ok, user, followed, _activity} =
127 TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
129 assert User.ap_followers(followed) in user.following
131 followed = User.get_cached_by_ap_id(followed.ap_id)
132 assert followed.info.follower_count == 1
134 {:ok, _, _, _} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
137 test "Unfollow another user using user_id" do
138 unfollowed = insert(:user)
139 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
140 ActivityPub.follow(user, unfollowed)
142 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
143 assert user.following == []
145 {:error, msg} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
146 assert msg == "Not subscribed!"
149 test "Unfollow another user using screen_name" do
150 unfollowed = insert(:user)
151 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
153 ActivityPub.follow(user, unfollowed)
155 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
156 assert user.following == []
158 {:error, msg} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
159 assert msg == "Not subscribed!"
162 test "Block another user using user_id" do
164 blocked = insert(:user)
166 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
167 assert User.blocks?(user, blocked)
170 test "Block another user using screen_name" do
172 blocked = insert(:user)
174 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
175 assert User.blocks?(user, blocked)
178 test "Unblock another user using user_id" do
179 unblocked = insert(:user)
181 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id})
183 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
184 assert user.info.blocks == []
187 test "Unblock another user using screen_name" do
188 unblocked = insert(:user)
190 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname})
192 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
193 assert user.info.blocks == []
196 test "upload a file" do
200 content_type: "image/jpg",
201 path: Path.absname("test/fixtures/image.jpg"),
202 filename: "an_image.jpg"
205 response = TwitterAPI.upload(file, user)
207 assert is_binary(response)
210 test "it favorites a status, returns the updated activity" do
212 other_user = insert(:user)
213 note_activity = insert(:note_activity)
215 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
216 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
217 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 1
219 object = Object.normalize(note_activity)
221 assert object.data["like_count"] == 1
223 assert status == updated_activity
225 {:ok, _status} = TwitterAPI.fav(other_user, note_activity.id)
227 object = Object.normalize(note_activity)
229 assert object.data["like_count"] == 2
231 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
232 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 2
235 test "it unfavorites a status, returns the updated activity" do
237 note_activity = insert(:note_activity)
238 object = Object.normalize(note_activity)
240 {:ok, _like_activity, _object} = ActivityPub.like(user, object)
241 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
243 assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1
245 {:ok, activity} = TwitterAPI.unfav(user, note_activity.id)
247 assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 0
250 test "it retweets a status and returns the retweet" do
252 note_activity = insert(:note_activity)
254 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
255 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
257 assert status == updated_activity
260 test "it unretweets an already retweeted status" do
262 note_activity = insert(:note_activity)
264 {:ok, _status} = TwitterAPI.repeat(user, note_activity.id)
265 {:ok, status} = TwitterAPI.unrepeat(user, note_activity.id)
266 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
268 assert status == updated_activity
271 test "it registers a new user and returns the user." do
273 "nickname" => "lain",
274 "email" => "lain@wired.jp",
275 "fullname" => "lain iwakura",
276 "password" => "bear",
280 {:ok, user} = TwitterAPI.register_user(data)
282 fetched_user = User.get_cached_by_nickname("lain")
284 assert UserView.render("show.json", %{user: user}) ==
285 UserView.render("show.json", %{user: fetched_user})
288 test "it registers a new user with empty string in bio and returns the user." do
290 "nickname" => "lain",
291 "email" => "lain@wired.jp",
292 "fullname" => "lain iwakura",
294 "password" => "bear",
298 {:ok, user} = TwitterAPI.register_user(data)
300 fetched_user = User.get_cached_by_nickname("lain")
302 assert UserView.render("show.json", %{user: user}) ==
303 UserView.render("show.json", %{user: fetched_user})
306 test "it sends confirmation email if :account_activation_required is specified in instance config" do
307 setting = Pleroma.Config.get([:instance, :account_activation_required])
310 Pleroma.Config.put([:instance, :account_activation_required], true)
311 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
315 "nickname" => "lain",
316 "email" => "lain@wired.jp",
317 "fullname" => "lain iwakura",
319 "password" => "bear",
323 {:ok, user} = TwitterAPI.register_user(data)
325 assert user.info.confirmation_pending
327 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
329 notify_email = Pleroma.Config.get([:instance, :notify_email])
330 instance_name = Pleroma.Config.get([:instance, :name])
332 Swoosh.TestAssertions.assert_email_sent(
333 from: {instance_name, notify_email},
334 to: {user.name, user.email},
335 html_body: email.html_body
339 test "it registers a new user and parses mentions in the bio" do
341 "nickname" => "john",
342 "email" => "john@gmail.com",
343 "fullname" => "John Doe",
345 "password" => "bear",
349 {:ok, user1} = TwitterAPI.register_user(data1)
352 "nickname" => "lain",
353 "email" => "lain@wired.jp",
354 "fullname" => "lain iwakura",
355 "bio" => "@john test",
356 "password" => "bear",
360 {:ok, user2} = TwitterAPI.register_user(data2)
363 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
365 assert user2.bio == expected_text
368 describe "register with one time token" do
370 setting = Pleroma.Config.get([:instance, :registrations_open])
373 Pleroma.Config.put([:instance, :registrations_open], false)
374 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
380 test "returns user on success" do
381 {:ok, invite} = UserInviteToken.create_invite()
384 "nickname" => "vinny",
385 "email" => "pasta@pizza.vs",
386 "fullname" => "Vinny Vinesauce",
388 "password" => "hiptofbees",
389 "confirm" => "hiptofbees",
390 "token" => invite.token
393 {:ok, user} = TwitterAPI.register_user(data)
395 fetched_user = User.get_cached_by_nickname("vinny")
396 invite = Repo.get_by(UserInviteToken, token: invite.token)
398 assert invite.used == true
400 assert UserView.render("show.json", %{user: user}) ==
401 UserView.render("show.json", %{user: fetched_user})
404 test "returns error on invalid token" do
406 "nickname" => "GrimReaper",
407 "email" => "death@reapers.afterlife",
408 "fullname" => "Reaper Grim",
409 "bio" => "Your time has come",
410 "password" => "scythe",
411 "confirm" => "scythe",
412 "token" => "DudeLetMeInImAFairy"
415 {:error, msg} = TwitterAPI.register_user(data)
417 assert msg == "Invalid token"
418 refute User.get_cached_by_nickname("GrimReaper")
421 test "returns error on expired token" do
422 {:ok, invite} = UserInviteToken.create_invite()
423 UserInviteToken.update_invite!(invite, used: true)
426 "nickname" => "GrimReaper",
427 "email" => "death@reapers.afterlife",
428 "fullname" => "Reaper Grim",
429 "bio" => "Your time has come",
430 "password" => "scythe",
431 "confirm" => "scythe",
432 "token" => invite.token
435 {:error, msg} = TwitterAPI.register_user(data)
437 assert msg == "Expired token"
438 refute User.get_cached_by_nickname("GrimReaper")
442 describe "registers with date limited token" do
444 setting = Pleroma.Config.get([:instance, :registrations_open])
447 Pleroma.Config.put([:instance, :registrations_open], false)
448 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
452 "nickname" => "vinny",
453 "email" => "pasta@pizza.vs",
454 "fullname" => "Vinny Vinesauce",
456 "password" => "hiptofbees",
457 "confirm" => "hiptofbees"
460 check_fn = fn invite ->
461 data = Map.put(data, "token", invite.token)
462 {:ok, user} = TwitterAPI.register_user(data)
463 fetched_user = User.get_cached_by_nickname("vinny")
465 assert UserView.render("show.json", %{user: user}) ==
466 UserView.render("show.json", %{user: fetched_user})
469 {:ok, data: data, check_fn: check_fn}
472 test "returns user on success", %{check_fn: check_fn} do
473 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
477 invite = Repo.get_by(UserInviteToken, token: invite.token)
482 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
483 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
487 invite = Repo.get_by(UserInviteToken, token: invite.token)
492 test "returns an error on overdue date", %{data: data} do
493 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
495 data = Map.put(data, "token", invite.token)
497 {:error, msg} = TwitterAPI.register_user(data)
499 assert msg == "Expired token"
500 refute User.get_cached_by_nickname("vinny")
501 invite = Repo.get_by(UserInviteToken, token: invite.token)
507 describe "registers with reusable token" do
509 setting = Pleroma.Config.get([:instance, :registrations_open])
512 Pleroma.Config.put([:instance, :registrations_open], false)
513 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
519 test "returns user on success, after him registration fails" do
520 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
522 UserInviteToken.update_invite!(invite, uses: 99)
525 "nickname" => "vinny",
526 "email" => "pasta@pizza.vs",
527 "fullname" => "Vinny Vinesauce",
529 "password" => "hiptofbees",
530 "confirm" => "hiptofbees",
531 "token" => invite.token
534 {:ok, user} = TwitterAPI.register_user(data)
535 fetched_user = User.get_cached_by_nickname("vinny")
536 invite = Repo.get_by(UserInviteToken, token: invite.token)
538 assert invite.used == true
540 assert UserView.render("show.json", %{user: user}) ==
541 UserView.render("show.json", %{user: fetched_user})
544 "nickname" => "GrimReaper",
545 "email" => "death@reapers.afterlife",
546 "fullname" => "Reaper Grim",
547 "bio" => "Your time has come",
548 "password" => "scythe",
549 "confirm" => "scythe",
550 "token" => invite.token
553 {:error, msg} = TwitterAPI.register_user(data)
555 assert msg == "Expired token"
556 refute User.get_cached_by_nickname("GrimReaper")
560 describe "registers with reusable date limited token" do
562 setting = Pleroma.Config.get([:instance, :registrations_open])
565 Pleroma.Config.put([:instance, :registrations_open], false)
566 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
572 test "returns user on success" do
573 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
576 "nickname" => "vinny",
577 "email" => "pasta@pizza.vs",
578 "fullname" => "Vinny Vinesauce",
580 "password" => "hiptofbees",
581 "confirm" => "hiptofbees",
582 "token" => invite.token
585 {:ok, user} = TwitterAPI.register_user(data)
586 fetched_user = User.get_cached_by_nickname("vinny")
587 invite = Repo.get_by(UserInviteToken, token: invite.token)
591 assert UserView.render("show.json", %{user: user}) ==
592 UserView.render("show.json", %{user: fetched_user})
595 test "error after max uses" do
596 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
598 UserInviteToken.update_invite!(invite, uses: 99)
601 "nickname" => "vinny",
602 "email" => "pasta@pizza.vs",
603 "fullname" => "Vinny Vinesauce",
605 "password" => "hiptofbees",
606 "confirm" => "hiptofbees",
607 "token" => invite.token
610 {:ok, user} = TwitterAPI.register_user(data)
611 fetched_user = User.get_cached_by_nickname("vinny")
612 invite = Repo.get_by(UserInviteToken, token: invite.token)
613 assert invite.used == true
615 assert UserView.render("show.json", %{user: user}) ==
616 UserView.render("show.json", %{user: fetched_user})
619 "nickname" => "GrimReaper",
620 "email" => "death@reapers.afterlife",
621 "fullname" => "Reaper Grim",
622 "bio" => "Your time has come",
623 "password" => "scythe",
624 "confirm" => "scythe",
625 "token" => invite.token
628 {:error, msg} = TwitterAPI.register_user(data)
630 assert msg == "Expired token"
631 refute User.get_cached_by_nickname("GrimReaper")
634 test "returns error on overdue date" do
636 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
639 "nickname" => "GrimReaper",
640 "email" => "death@reapers.afterlife",
641 "fullname" => "Reaper Grim",
642 "bio" => "Your time has come",
643 "password" => "scythe",
644 "confirm" => "scythe",
645 "token" => invite.token
648 {:error, msg} = TwitterAPI.register_user(data)
650 assert msg == "Expired token"
651 refute User.get_cached_by_nickname("GrimReaper")
654 test "returns error on with overdue date and after max" do
656 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
658 UserInviteToken.update_invite!(invite, uses: 100)
661 "nickname" => "GrimReaper",
662 "email" => "death@reapers.afterlife",
663 "fullname" => "Reaper Grim",
664 "bio" => "Your time has come",
665 "password" => "scythe",
666 "confirm" => "scythe",
667 "token" => invite.token
670 {:error, msg} = TwitterAPI.register_user(data)
672 assert msg == "Expired token"
673 refute User.get_cached_by_nickname("GrimReaper")
677 test "it returns the error on registration problems" do
679 "nickname" => "lain",
680 "email" => "lain@wired.jp",
681 "fullname" => "lain iwakura",
682 "bio" => "close the world.",
686 {:error, error_object} = TwitterAPI.register_user(data)
688 assert is_binary(error_object[:error])
689 refute User.get_cached_by_nickname("lain")
692 test "it assigns an integer conversation_id" do
693 note_activity = insert(:note_activity)
694 status = ActivityView.render("activity.json", activity: note_activity)
696 assert is_number(status["statusnet_conversation_id"])
700 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
701 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
705 describe "fetching a user by uri" do
706 test "fetches a user by uri" do
707 id = "https://mastodon.social/users/lambadalambda"
709 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
710 remote = User.get_cached_by_ap_id(id)
712 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
714 # Also fetches the feed.
715 # assert Activity.get_create_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
716 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength