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 :moominmamma: line. #2hu #epic #phantasmagoric",
45 "media_ids" => [object.id]
48 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
49 object = Object.normalize(activity.data["object"])
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 :moominmamma: 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 %{"moominmamma" => "http://localhost:4001/finmoji/128px/moominmamma-128.png"} =
72 assert object.data["tag"] == ["2hu", "epic", "phantasmagoric"]
75 assert is_binary(get_in(activity.data, ["context"]))
76 assert is_binary(get_in(object.data, ["context"]))
78 assert is_list(object.data["attachment"])
80 assert activity.data["object"] == object.data["id"]
82 user = User.get_by_ap_id(user.ap_id)
84 assert user.info.note_count == 1
87 test "create a status that is a reply" do
91 "status" => "Hello again."
94 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
95 object = Object.normalize(activity.data["object"])
98 "status" => "Here's your (you).",
99 "in_reply_to_status_id" => activity.id
102 {:ok, reply = %Activity{}} = TwitterAPI.create_status(user, input)
103 reply_object = Object.normalize(reply.data["object"])
105 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
107 assert get_in(reply_object.data, ["context"]) == get_in(object.data, ["context"])
109 assert get_in(reply_object.data, ["inReplyTo"]) == get_in(activity.data, ["object"])
110 assert Activity.get_in_reply_to_activity(reply).id == activity.id
113 test "Follow another user using user_id" do
115 followed = insert(:user)
117 {:ok, user, followed, _activity} = TwitterAPI.follow(user, %{"user_id" => followed.id})
118 assert User.ap_followers(followed) in user.following
120 {:error, msg} = TwitterAPI.follow(user, %{"user_id" => followed.id})
121 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
124 test "Follow another user using screen_name" do
126 followed = insert(:user)
128 {:ok, user, followed, _activity} =
129 TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
131 assert User.ap_followers(followed) in user.following
133 followed = User.get_by_ap_id(followed.ap_id)
134 assert followed.info.follower_count == 1
136 {:error, msg} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
137 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
140 test "Unfollow another user using user_id" do
141 unfollowed = insert(:user)
142 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
143 ActivityPub.follow(user, unfollowed)
145 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
146 assert user.following == []
148 {:error, msg} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
149 assert msg == "Not subscribed!"
152 test "Unfollow another user using screen_name" do
153 unfollowed = insert(:user)
154 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
156 ActivityPub.follow(user, unfollowed)
158 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
159 assert user.following == []
161 {:error, msg} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
162 assert msg == "Not subscribed!"
165 test "Block another user using user_id" do
167 blocked = insert(:user)
169 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
170 assert User.blocks?(user, blocked)
173 test "Block another user using screen_name" do
175 blocked = insert(:user)
177 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
178 assert User.blocks?(user, blocked)
181 test "Unblock another user using user_id" do
182 unblocked = insert(:user)
184 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id})
186 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
187 assert user.info.blocks == []
190 test "Unblock another user using screen_name" do
191 unblocked = insert(:user)
193 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname})
195 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
196 assert user.info.blocks == []
199 test "upload a file" do
203 content_type: "image/jpg",
204 path: Path.absname("test/fixtures/image.jpg"),
205 filename: "an_image.jpg"
208 response = TwitterAPI.upload(file, user)
210 assert is_binary(response)
213 test "it favorites a status, returns the updated activity" do
215 other_user = insert(:user)
216 note_activity = insert(:note_activity)
218 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
219 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
220 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 1
222 object = Object.normalize(note_activity.data["object"])
224 assert object.data["like_count"] == 1
226 assert status == updated_activity
228 {:ok, _status} = TwitterAPI.fav(other_user, note_activity.id)
230 object = Object.normalize(note_activity.data["object"])
232 assert object.data["like_count"] == 2
234 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
235 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 2
238 test "it unfavorites a status, returns the updated activity" do
240 note_activity = insert(:note_activity)
241 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
243 {:ok, _like_activity, _object} = ActivityPub.like(user, object)
244 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
246 assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1
248 {:ok, activity} = TwitterAPI.unfav(user, note_activity.id)
250 assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 0
253 test "it retweets a status and returns the retweet" do
255 note_activity = insert(:note_activity)
257 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
258 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
260 assert status == updated_activity
263 test "it unretweets an already retweeted status" do
265 note_activity = insert(:note_activity)
267 {:ok, _status} = TwitterAPI.repeat(user, note_activity.id)
268 {:ok, status} = TwitterAPI.unrepeat(user, note_activity.id)
269 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
271 assert status == updated_activity
274 test "it registers a new user and returns the user." do
276 "nickname" => "lain",
277 "email" => "lain@wired.jp",
278 "fullname" => "lain iwakura",
279 "password" => "bear",
283 {:ok, user} = TwitterAPI.register_user(data)
285 fetched_user = User.get_by_nickname("lain")
287 assert UserView.render("show.json", %{user: user}) ==
288 UserView.render("show.json", %{user: fetched_user})
291 test "it registers a new user with empty string in bio and returns the user." do
293 "nickname" => "lain",
294 "email" => "lain@wired.jp",
295 "fullname" => "lain iwakura",
297 "password" => "bear",
301 {:ok, user} = TwitterAPI.register_user(data)
303 fetched_user = User.get_by_nickname("lain")
305 assert UserView.render("show.json", %{user: user}) ==
306 UserView.render("show.json", %{user: fetched_user})
309 test "it sends confirmation email if :account_activation_required is specified in instance config" do
310 setting = Pleroma.Config.get([:instance, :account_activation_required])
313 Pleroma.Config.put([:instance, :account_activation_required], true)
314 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
318 "nickname" => "lain",
319 "email" => "lain@wired.jp",
320 "fullname" => "lain iwakura",
322 "password" => "bear",
326 {:ok, user} = TwitterAPI.register_user(data)
328 assert user.info.confirmation_pending
330 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
332 notify_email = Pleroma.Config.get([:instance, :notify_email])
333 instance_name = Pleroma.Config.get([:instance, :name])
335 Swoosh.TestAssertions.assert_email_sent(
336 from: {instance_name, notify_email},
337 to: {user.name, user.email},
338 html_body: email.html_body
342 test "it registers a new user and parses mentions in the bio" do
344 "nickname" => "john",
345 "email" => "john@gmail.com",
346 "fullname" => "John Doe",
348 "password" => "bear",
352 {:ok, user1} = TwitterAPI.register_user(data1)
355 "nickname" => "lain",
356 "email" => "lain@wired.jp",
357 "fullname" => "lain iwakura",
358 "bio" => "@john test",
359 "password" => "bear",
363 {:ok, user2} = TwitterAPI.register_user(data2)
366 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
368 assert user2.bio == expected_text
371 describe "register with one time token" do
373 setting = Pleroma.Config.get([:instance, :registrations_open])
376 Pleroma.Config.put([:instance, :registrations_open], false)
377 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
383 test "returns user on success" do
384 {:ok, invite} = UserInviteToken.create_invite()
387 "nickname" => "vinny",
388 "email" => "pasta@pizza.vs",
389 "fullname" => "Vinny Vinesauce",
391 "password" => "hiptofbees",
392 "confirm" => "hiptofbees",
393 "token" => invite.token
396 {:ok, user} = TwitterAPI.register_user(data)
398 fetched_user = User.get_by_nickname("vinny")
399 invite = Repo.get_by(UserInviteToken, token: invite.token)
401 assert invite.used == true
403 assert UserView.render("show.json", %{user: user}) ==
404 UserView.render("show.json", %{user: fetched_user})
407 test "returns error on invalid token" do
409 "nickname" => "GrimReaper",
410 "email" => "death@reapers.afterlife",
411 "fullname" => "Reaper Grim",
412 "bio" => "Your time has come",
413 "password" => "scythe",
414 "confirm" => "scythe",
415 "token" => "DudeLetMeInImAFairy"
418 {:error, msg} = TwitterAPI.register_user(data)
420 assert msg == "Invalid token"
421 refute User.get_by_nickname("GrimReaper")
424 test "returns error on expired token" do
425 {:ok, invite} = UserInviteToken.create_invite()
426 UserInviteToken.update_invite!(invite, used: true)
429 "nickname" => "GrimReaper",
430 "email" => "death@reapers.afterlife",
431 "fullname" => "Reaper Grim",
432 "bio" => "Your time has come",
433 "password" => "scythe",
434 "confirm" => "scythe",
435 "token" => invite.token
438 {:error, msg} = TwitterAPI.register_user(data)
440 assert msg == "Expired token"
441 refute User.get_by_nickname("GrimReaper")
445 describe "registers with date limited token" do
447 setting = Pleroma.Config.get([:instance, :registrations_open])
450 Pleroma.Config.put([:instance, :registrations_open], false)
451 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
455 "nickname" => "vinny",
456 "email" => "pasta@pizza.vs",
457 "fullname" => "Vinny Vinesauce",
459 "password" => "hiptofbees",
460 "confirm" => "hiptofbees"
463 check_fn = fn invite ->
464 data = Map.put(data, "token", invite.token)
465 {:ok, user} = TwitterAPI.register_user(data)
466 fetched_user = User.get_by_nickname("vinny")
468 assert UserView.render("show.json", %{user: user}) ==
469 UserView.render("show.json", %{user: fetched_user})
472 {:ok, data: data, check_fn: check_fn}
475 test "returns user on success", %{check_fn: check_fn} do
476 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
480 invite = Repo.get_by(UserInviteToken, token: invite.token)
485 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
486 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
490 invite = Repo.get_by(UserInviteToken, token: invite.token)
495 test "returns an error on overdue date", %{data: data} do
496 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
498 data = Map.put(data, "token", invite.token)
500 {:error, msg} = TwitterAPI.register_user(data)
502 assert msg == "Expired token"
503 refute User.get_by_nickname("vinny")
504 invite = Repo.get_by(UserInviteToken, token: invite.token)
510 describe "registers with reusable token" do
512 setting = Pleroma.Config.get([:instance, :registrations_open])
515 Pleroma.Config.put([:instance, :registrations_open], false)
516 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
522 test "returns user on success, after him registration fails" do
523 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
525 UserInviteToken.update_invite!(invite, uses: 99)
528 "nickname" => "vinny",
529 "email" => "pasta@pizza.vs",
530 "fullname" => "Vinny Vinesauce",
532 "password" => "hiptofbees",
533 "confirm" => "hiptofbees",
534 "token" => invite.token
537 {:ok, user} = TwitterAPI.register_user(data)
538 fetched_user = User.get_by_nickname("vinny")
539 invite = Repo.get_by(UserInviteToken, token: invite.token)
541 assert invite.used == true
543 assert UserView.render("show.json", %{user: user}) ==
544 UserView.render("show.json", %{user: fetched_user})
547 "nickname" => "GrimReaper",
548 "email" => "death@reapers.afterlife",
549 "fullname" => "Reaper Grim",
550 "bio" => "Your time has come",
551 "password" => "scythe",
552 "confirm" => "scythe",
553 "token" => invite.token
556 {:error, msg} = TwitterAPI.register_user(data)
558 assert msg == "Expired token"
559 refute User.get_by_nickname("GrimReaper")
563 describe "registers with reusable date limited token" do
565 setting = Pleroma.Config.get([:instance, :registrations_open])
568 Pleroma.Config.put([:instance, :registrations_open], false)
569 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
575 test "returns user on success" do
576 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
579 "nickname" => "vinny",
580 "email" => "pasta@pizza.vs",
581 "fullname" => "Vinny Vinesauce",
583 "password" => "hiptofbees",
584 "confirm" => "hiptofbees",
585 "token" => invite.token
588 {:ok, user} = TwitterAPI.register_user(data)
589 fetched_user = User.get_by_nickname("vinny")
590 invite = Repo.get_by(UserInviteToken, token: invite.token)
594 assert UserView.render("show.json", %{user: user}) ==
595 UserView.render("show.json", %{user: fetched_user})
598 test "error after max uses" do
599 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
601 UserInviteToken.update_invite!(invite, uses: 99)
604 "nickname" => "vinny",
605 "email" => "pasta@pizza.vs",
606 "fullname" => "Vinny Vinesauce",
608 "password" => "hiptofbees",
609 "confirm" => "hiptofbees",
610 "token" => invite.token
613 {:ok, user} = TwitterAPI.register_user(data)
614 fetched_user = User.get_by_nickname("vinny")
615 invite = Repo.get_by(UserInviteToken, token: invite.token)
616 assert invite.used == true
618 assert UserView.render("show.json", %{user: user}) ==
619 UserView.render("show.json", %{user: fetched_user})
622 "nickname" => "GrimReaper",
623 "email" => "death@reapers.afterlife",
624 "fullname" => "Reaper Grim",
625 "bio" => "Your time has come",
626 "password" => "scythe",
627 "confirm" => "scythe",
628 "token" => invite.token
631 {:error, msg} = TwitterAPI.register_user(data)
633 assert msg == "Expired token"
634 refute User.get_by_nickname("GrimReaper")
637 test "returns error on overdue date" do
639 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
642 "nickname" => "GrimReaper",
643 "email" => "death@reapers.afterlife",
644 "fullname" => "Reaper Grim",
645 "bio" => "Your time has come",
646 "password" => "scythe",
647 "confirm" => "scythe",
648 "token" => invite.token
651 {:error, msg} = TwitterAPI.register_user(data)
653 assert msg == "Expired token"
654 refute User.get_by_nickname("GrimReaper")
657 test "returns error on with overdue date and after max" do
659 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
661 UserInviteToken.update_invite!(invite, uses: 100)
664 "nickname" => "GrimReaper",
665 "email" => "death@reapers.afterlife",
666 "fullname" => "Reaper Grim",
667 "bio" => "Your time has come",
668 "password" => "scythe",
669 "confirm" => "scythe",
670 "token" => invite.token
673 {:error, msg} = TwitterAPI.register_user(data)
675 assert msg == "Expired token"
676 refute User.get_by_nickname("GrimReaper")
680 test "it returns the error on registration problems" do
682 "nickname" => "lain",
683 "email" => "lain@wired.jp",
684 "fullname" => "lain iwakura",
685 "bio" => "close the world.",
689 {:error, error_object} = TwitterAPI.register_user(data)
691 assert is_binary(error_object[:error])
692 refute User.get_by_nickname("lain")
695 test "it assigns an integer conversation_id" do
696 note_activity = insert(:note_activity)
697 status = ActivityView.render("activity.json", activity: note_activity)
699 assert is_number(status["statusnet_conversation_id"])
703 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
704 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
708 describe "fetching a user by uri" do
709 test "fetches a user by uri" do
710 id = "https://mastodon.social/users/lambadalambda"
712 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
713 remote = User.get_by_ap_id(id)
715 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
717 # Also fetches the feed.
718 # assert Activity.get_create_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
719 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength