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)
51 "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>"
53 assert get_in(activity.data, ["object", "content"]) == expected_text
54 assert get_in(activity.data, ["object", "type"]) == "Note"
55 assert get_in(activity.data, ["object", "actor"]) == user.ap_id
56 assert get_in(activity.data, ["actor"]) == user.ap_id
57 assert Enum.member?(get_in(activity.data, ["cc"]), User.ap_followers(user))
60 get_in(activity.data, ["to"]),
61 "https://www.w3.org/ns/activitystreams#Public"
64 assert Enum.member?(get_in(activity.data, ["to"]), "shp")
65 assert activity.local == true
67 assert %{"moominmamma" => "http://localhost:4001/finmoji/128px/moominmamma-128.png"} =
68 activity.data["object"]["emoji"]
71 assert activity.data["object"]["tag"] == ["2hu", "epic", "phantasmagoric"]
74 assert is_binary(get_in(activity.data, ["context"]))
75 assert is_binary(get_in(activity.data, ["object", "context"]))
77 assert is_list(activity.data["object"]["attachment"])
79 assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
81 user = User.get_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)
96 "status" => "Here's your (you).",
97 "in_reply_to_status_id" => activity.id
100 {:ok, reply = %Activity{}} = TwitterAPI.create_status(user, input)
102 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
104 assert get_in(reply.data, ["object", "context"]) ==
105 get_in(activity.data, ["object", "context"])
107 assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
108 assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
111 test "Follow another user using user_id" do
113 followed = insert(:user)
115 {:ok, user, followed, _activity} = TwitterAPI.follow(user, %{"user_id" => followed.id})
116 assert User.ap_followers(followed) in user.following
118 {:error, msg} = TwitterAPI.follow(user, %{"user_id" => followed.id})
119 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
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_by_ap_id(followed.ap_id)
132 assert followed.info.follower_count == 1
134 {:error, msg} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
135 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
138 test "Unfollow another user using user_id" do
139 unfollowed = insert(:user)
140 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
141 ActivityPub.follow(user, unfollowed)
143 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
144 assert user.following == []
146 {:error, msg} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
147 assert msg == "Not subscribed!"
150 test "Unfollow another user using screen_name" do
151 unfollowed = insert(:user)
152 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
154 ActivityPub.follow(user, unfollowed)
156 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
157 assert user.following == []
159 {:error, msg} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
160 assert msg == "Not subscribed!"
163 test "Block another user using user_id" do
165 blocked = insert(:user)
167 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
168 assert User.blocks?(user, blocked)
171 test "Block another user using screen_name" do
173 blocked = insert(:user)
175 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
176 assert User.blocks?(user, blocked)
179 test "Unblock another user using user_id" do
180 unblocked = insert(:user)
182 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id})
184 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
185 assert user.info.blocks == []
188 test "Unblock another user using screen_name" do
189 unblocked = insert(:user)
191 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname})
193 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
194 assert user.info.blocks == []
197 test "upload a file" do
201 content_type: "image/jpg",
202 path: Path.absname("test/fixtures/image.jpg"),
203 filename: "an_image.jpg"
206 response = TwitterAPI.upload(file, user)
208 assert is_binary(response)
211 test "it favorites a status, returns the updated activity" do
213 other_user = insert(:user)
214 note_activity = insert(:note_activity)
216 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
217 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
218 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 1
220 object = Object.normalize(note_activity.data["object"])
222 assert object.data["like_count"] == 1
224 assert status == updated_activity
226 {:ok, _status} = TwitterAPI.fav(other_user, note_activity.id)
228 object = Object.normalize(note_activity.data["object"])
230 assert object.data["like_count"] == 2
232 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
233 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 2
236 test "it unfavorites a status, returns the updated activity" do
238 note_activity = insert(:note_activity)
239 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
241 {:ok, _like_activity, _object} = ActivityPub.like(user, object)
242 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
244 assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1
246 {:ok, activity} = TwitterAPI.unfav(user, note_activity.id)
248 assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 0
251 test "it retweets a status and returns the retweet" do
253 note_activity = insert(:note_activity)
255 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
256 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
258 assert status == updated_activity
261 test "it unretweets an already retweeted status" do
263 note_activity = insert(:note_activity)
265 {:ok, _status} = TwitterAPI.repeat(user, note_activity.id)
266 {:ok, status} = TwitterAPI.unrepeat(user, note_activity.id)
267 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
269 assert status == updated_activity
272 test "it registers a new user and returns the user." do
274 "nickname" => "lain",
275 "email" => "lain@wired.jp",
276 "fullname" => "lain iwakura",
277 "password" => "bear",
281 {:ok, user} = TwitterAPI.register_user(data)
283 fetched_user = User.get_by_nickname("lain")
285 assert UserView.render("show.json", %{user: user}) ==
286 UserView.render("show.json", %{user: fetched_user})
289 test "it registers a new user with empty string in bio and returns the user." do
291 "nickname" => "lain",
292 "email" => "lain@wired.jp",
293 "fullname" => "lain iwakura",
295 "password" => "bear",
299 {:ok, user} = TwitterAPI.register_user(data)
301 fetched_user = User.get_by_nickname("lain")
303 assert UserView.render("show.json", %{user: user}) ==
304 UserView.render("show.json", %{user: fetched_user})
307 test "it sends confirmation email if :account_activation_required is specified in instance config" do
308 setting = Pleroma.Config.get([:instance, :account_activation_required])
311 Pleroma.Config.put([:instance, :account_activation_required], true)
312 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
316 "nickname" => "lain",
317 "email" => "lain@wired.jp",
318 "fullname" => "lain iwakura",
320 "password" => "bear",
324 {:ok, user} = TwitterAPI.register_user(data)
326 assert user.info.confirmation_pending
328 Swoosh.TestAssertions.assert_email_sent(
329 Pleroma.Emails.UserEmail.account_confirmation_email(user)
333 test "it registers a new user and parses mentions in the bio" do
335 "nickname" => "john",
336 "email" => "john@gmail.com",
337 "fullname" => "John Doe",
339 "password" => "bear",
343 {:ok, user1} = TwitterAPI.register_user(data1)
346 "nickname" => "lain",
347 "email" => "lain@wired.jp",
348 "fullname" => "lain iwakura",
349 "bio" => "@john test",
350 "password" => "bear",
354 {:ok, user2} = TwitterAPI.register_user(data2)
357 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
359 assert user2.bio == expected_text
362 describe "register with one time token" do
364 setting = Pleroma.Config.get([:instance, :registrations_open])
367 Pleroma.Config.put([:instance, :registrations_open], false)
368 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
374 test "returns user on success" do
375 {:ok, invite} = UserInviteToken.create_invite()
378 "nickname" => "vinny",
379 "email" => "pasta@pizza.vs",
380 "fullname" => "Vinny Vinesauce",
382 "password" => "hiptofbees",
383 "confirm" => "hiptofbees",
384 "token" => invite.token
387 {:ok, user} = TwitterAPI.register_user(data)
389 fetched_user = User.get_by_nickname("vinny")
390 invite = Repo.get_by(UserInviteToken, token: invite.token)
392 assert invite.used == true
394 assert UserView.render("show.json", %{user: user}) ==
395 UserView.render("show.json", %{user: fetched_user})
398 test "returns error on invalid token" do
400 "nickname" => "GrimReaper",
401 "email" => "death@reapers.afterlife",
402 "fullname" => "Reaper Grim",
403 "bio" => "Your time has come",
404 "password" => "scythe",
405 "confirm" => "scythe",
406 "token" => "DudeLetMeInImAFairy"
409 {:error, msg} = TwitterAPI.register_user(data)
411 assert msg == "Invalid token"
412 refute User.get_by_nickname("GrimReaper")
415 test "returns error on expired token" do
416 {:ok, invite} = UserInviteToken.create_invite()
417 UserInviteToken.update_invite!(invite, used: true)
420 "nickname" => "GrimReaper",
421 "email" => "death@reapers.afterlife",
422 "fullname" => "Reaper Grim",
423 "bio" => "Your time has come",
424 "password" => "scythe",
425 "confirm" => "scythe",
426 "token" => invite.token
429 {:error, msg} = TwitterAPI.register_user(data)
431 assert msg == "Expired token"
432 refute User.get_by_nickname("GrimReaper")
436 describe "registers with date limited token" do
438 setting = Pleroma.Config.get([:instance, :registrations_open])
441 Pleroma.Config.put([:instance, :registrations_open], false)
442 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
446 "nickname" => "vinny",
447 "email" => "pasta@pizza.vs",
448 "fullname" => "Vinny Vinesauce",
450 "password" => "hiptofbees",
451 "confirm" => "hiptofbees"
454 check_fn = fn invite ->
455 data = Map.put(data, "token", invite.token)
456 {:ok, user} = TwitterAPI.register_user(data)
457 fetched_user = User.get_by_nickname("vinny")
459 assert UserView.render("show.json", %{user: user}) ==
460 UserView.render("show.json", %{user: fetched_user})
463 {:ok, data: data, check_fn: check_fn}
466 test "returns user on success", %{check_fn: check_fn} do
467 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
471 invite = Repo.get_by(UserInviteToken, token: invite.token)
476 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
477 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
481 invite = Repo.get_by(UserInviteToken, token: invite.token)
486 test "returns an error on overdue date", %{data: data} do
487 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
489 data = Map.put(data, "token", invite.token)
491 {:error, msg} = TwitterAPI.register_user(data)
493 assert msg == "Expired token"
494 refute User.get_by_nickname("vinny")
495 invite = Repo.get_by(UserInviteToken, token: invite.token)
501 describe "registers with reusable token" do
503 setting = Pleroma.Config.get([:instance, :registrations_open])
506 Pleroma.Config.put([:instance, :registrations_open], false)
507 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
513 test "returns user on success, after him registration fails" do
514 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
516 UserInviteToken.update_invite!(invite, uses: 99)
519 "nickname" => "vinny",
520 "email" => "pasta@pizza.vs",
521 "fullname" => "Vinny Vinesauce",
523 "password" => "hiptofbees",
524 "confirm" => "hiptofbees",
525 "token" => invite.token
528 {:ok, user} = TwitterAPI.register_user(data)
529 fetched_user = User.get_by_nickname("vinny")
530 invite = Repo.get_by(UserInviteToken, token: invite.token)
532 assert invite.used == true
534 assert UserView.render("show.json", %{user: user}) ==
535 UserView.render("show.json", %{user: fetched_user})
538 "nickname" => "GrimReaper",
539 "email" => "death@reapers.afterlife",
540 "fullname" => "Reaper Grim",
541 "bio" => "Your time has come",
542 "password" => "scythe",
543 "confirm" => "scythe",
544 "token" => invite.token
547 {:error, msg} = TwitterAPI.register_user(data)
549 assert msg == "Expired token"
550 refute User.get_by_nickname("GrimReaper")
554 describe "registers with reusable date limited token" do
556 setting = Pleroma.Config.get([:instance, :registrations_open])
559 Pleroma.Config.put([:instance, :registrations_open], false)
560 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
566 test "returns user on success" do
567 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
570 "nickname" => "vinny",
571 "email" => "pasta@pizza.vs",
572 "fullname" => "Vinny Vinesauce",
574 "password" => "hiptofbees",
575 "confirm" => "hiptofbees",
576 "token" => invite.token
579 {:ok, user} = TwitterAPI.register_user(data)
580 fetched_user = User.get_by_nickname("vinny")
581 invite = Repo.get_by(UserInviteToken, token: invite.token)
585 assert UserView.render("show.json", %{user: user}) ==
586 UserView.render("show.json", %{user: fetched_user})
589 test "error after max uses" do
590 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
592 UserInviteToken.update_invite!(invite, uses: 99)
595 "nickname" => "vinny",
596 "email" => "pasta@pizza.vs",
597 "fullname" => "Vinny Vinesauce",
599 "password" => "hiptofbees",
600 "confirm" => "hiptofbees",
601 "token" => invite.token
604 {:ok, user} = TwitterAPI.register_user(data)
605 fetched_user = User.get_by_nickname("vinny")
606 invite = Repo.get_by(UserInviteToken, token: invite.token)
607 assert invite.used == true
609 assert UserView.render("show.json", %{user: user}) ==
610 UserView.render("show.json", %{user: fetched_user})
613 "nickname" => "GrimReaper",
614 "email" => "death@reapers.afterlife",
615 "fullname" => "Reaper Grim",
616 "bio" => "Your time has come",
617 "password" => "scythe",
618 "confirm" => "scythe",
619 "token" => invite.token
622 {:error, msg} = TwitterAPI.register_user(data)
624 assert msg == "Expired token"
625 refute User.get_by_nickname("GrimReaper")
628 test "returns error on overdue date" do
630 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
633 "nickname" => "GrimReaper",
634 "email" => "death@reapers.afterlife",
635 "fullname" => "Reaper Grim",
636 "bio" => "Your time has come",
637 "password" => "scythe",
638 "confirm" => "scythe",
639 "token" => invite.token
642 {:error, msg} = TwitterAPI.register_user(data)
644 assert msg == "Expired token"
645 refute User.get_by_nickname("GrimReaper")
648 test "returns error on with overdue date and after max" do
650 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
652 UserInviteToken.update_invite!(invite, uses: 100)
655 "nickname" => "GrimReaper",
656 "email" => "death@reapers.afterlife",
657 "fullname" => "Reaper Grim",
658 "bio" => "Your time has come",
659 "password" => "scythe",
660 "confirm" => "scythe",
661 "token" => invite.token
664 {:error, msg} = TwitterAPI.register_user(data)
666 assert msg == "Expired token"
667 refute User.get_by_nickname("GrimReaper")
671 test "it returns the error on registration problems" do
673 "nickname" => "lain",
674 "email" => "lain@wired.jp",
675 "fullname" => "lain iwakura",
676 "bio" => "close the world.",
680 {:error, error_object} = TwitterAPI.register_user(data)
682 assert is_binary(error_object[:error])
683 refute User.get_by_nickname("lain")
686 test "it assigns an integer conversation_id" do
687 note_activity = insert(:note_activity)
688 status = ActivityView.render("activity.json", activity: note_activity)
690 assert is_number(status["statusnet_conversation_id"])
694 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
695 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
699 describe "fetching a user by uri" do
700 test "fetches a user by uri" do
701 id = "https://mastodon.social/users/lambadalambda"
703 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
704 remote = User.get_by_ap_id(id)
706 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
708 # Also fetches the feed.
709 # assert Activity.get_create_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
710 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength