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.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 :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_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.data["object"])
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.data["object"])
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 {:error, msg} = TwitterAPI.follow(user, %{"user_id" => followed.id})
120 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
123 test "Follow another user using screen_name" do
125 followed = insert(:user)
127 {:ok, user, followed, _activity} =
128 TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
130 assert User.ap_followers(followed) in user.following
132 followed = User.get_by_ap_id(followed.ap_id)
133 assert followed.info.follower_count == 1
135 {:error, msg} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
136 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
139 test "Unfollow another user using user_id" do
140 unfollowed = insert(:user)
141 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
142 ActivityPub.follow(user, unfollowed)
144 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
145 assert user.following == []
147 {:error, msg} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
148 assert msg == "Not subscribed!"
151 test "Unfollow another user using screen_name" do
152 unfollowed = insert(:user)
153 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
155 ActivityPub.follow(user, unfollowed)
157 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
158 assert user.following == []
160 {:error, msg} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
161 assert msg == "Not subscribed!"
164 test "Block another user using user_id" do
166 blocked = insert(:user)
168 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
169 assert User.blocks?(user, blocked)
172 test "Block another user using screen_name" do
174 blocked = insert(:user)
176 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
177 assert User.blocks?(user, blocked)
180 test "Unblock another user using user_id" do
181 unblocked = insert(:user)
183 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id})
185 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
186 assert user.info.blocks == []
189 test "Unblock another user using screen_name" do
190 unblocked = insert(:user)
192 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname})
194 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
195 assert user.info.blocks == []
198 test "upload a file" do
202 content_type: "image/jpg",
203 path: Path.absname("test/fixtures/image.jpg"),
204 filename: "an_image.jpg"
207 response = TwitterAPI.upload(file, user)
209 assert is_binary(response)
212 test "it favorites a status, returns the updated activity" do
214 other_user = insert(:user)
215 note_activity = insert(:note_activity)
217 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
218 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
219 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 1
221 object = Object.normalize(note_activity.data["object"])
223 assert object.data["like_count"] == 1
225 assert status == updated_activity
227 {:ok, _status} = TwitterAPI.fav(other_user, note_activity.id)
229 object = Object.normalize(note_activity.data["object"])
231 assert object.data["like_count"] == 2
233 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
234 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 2
237 test "it unfavorites a status, returns the updated activity" do
239 note_activity = insert(:note_activity)
240 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
242 {:ok, _like_activity, _object} = ActivityPub.like(user, object)
243 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
245 assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1
247 {:ok, activity} = TwitterAPI.unfav(user, note_activity.id)
249 assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 0
252 test "it retweets a status and returns the retweet" do
254 note_activity = insert(:note_activity)
256 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
257 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
259 assert status == updated_activity
262 test "it unretweets an already retweeted status" do
264 note_activity = insert(:note_activity)
266 {:ok, _status} = TwitterAPI.repeat(user, note_activity.id)
267 {:ok, status} = TwitterAPI.unrepeat(user, note_activity.id)
268 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
270 assert status == updated_activity
273 test "it registers a new user and returns the user." do
275 "nickname" => "lain",
276 "email" => "lain@wired.jp",
277 "fullname" => "lain iwakura",
278 "password" => "bear",
282 {:ok, user} = TwitterAPI.register_user(data)
284 fetched_user = User.get_by_nickname("lain")
286 assert UserView.render("show.json", %{user: user}) ==
287 UserView.render("show.json", %{user: fetched_user})
290 test "it registers a new user with empty string in bio and returns the user." do
292 "nickname" => "lain",
293 "email" => "lain@wired.jp",
294 "fullname" => "lain iwakura",
296 "password" => "bear",
300 {:ok, user} = TwitterAPI.register_user(data)
302 fetched_user = User.get_by_nickname("lain")
304 assert UserView.render("show.json", %{user: user}) ==
305 UserView.render("show.json", %{user: fetched_user})
308 test "it sends confirmation email if :account_activation_required is specified in instance config" do
309 setting = Pleroma.Config.get([:instance, :account_activation_required])
312 Pleroma.Config.put([:instance, :account_activation_required], true)
313 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
317 "nickname" => "lain",
318 "email" => "lain@wired.jp",
319 "fullname" => "lain iwakura",
321 "password" => "bear",
325 {:ok, user} = TwitterAPI.register_user(data)
327 assert user.info.confirmation_pending
329 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
331 notify_email = Pleroma.Config.get([:instance, :notify_email])
332 instance_name = Pleroma.Config.get([:instance, :name])
334 Swoosh.TestAssertions.assert_email_sent(
335 from: {instance_name, notify_email},
336 to: {user.name, user.email},
337 html_body: email.html_body
341 test "it registers a new user and parses mentions in the bio" do
343 "nickname" => "john",
344 "email" => "john@gmail.com",
345 "fullname" => "John Doe",
347 "password" => "bear",
351 {:ok, user1} = TwitterAPI.register_user(data1)
354 "nickname" => "lain",
355 "email" => "lain@wired.jp",
356 "fullname" => "lain iwakura",
357 "bio" => "@john test",
358 "password" => "bear",
362 {:ok, user2} = TwitterAPI.register_user(data2)
365 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
367 assert user2.bio == expected_text
370 describe "register with one time token" do
372 setting = Pleroma.Config.get([:instance, :registrations_open])
375 Pleroma.Config.put([:instance, :registrations_open], false)
376 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
382 test "returns user on success" do
383 {:ok, invite} = UserInviteToken.create_invite()
386 "nickname" => "vinny",
387 "email" => "pasta@pizza.vs",
388 "fullname" => "Vinny Vinesauce",
390 "password" => "hiptofbees",
391 "confirm" => "hiptofbees",
392 "token" => invite.token
395 {:ok, user} = TwitterAPI.register_user(data)
397 fetched_user = User.get_by_nickname("vinny")
398 invite = Repo.get_by(UserInviteToken, token: invite.token)
400 assert invite.used == true
402 assert UserView.render("show.json", %{user: user}) ==
403 UserView.render("show.json", %{user: fetched_user})
406 test "returns error on invalid token" do
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" => "DudeLetMeInImAFairy"
417 {:error, msg} = TwitterAPI.register_user(data)
419 assert msg == "Invalid token"
420 refute User.get_by_nickname("GrimReaper")
423 test "returns error on expired token" do
424 {:ok, invite} = UserInviteToken.create_invite()
425 UserInviteToken.update_invite!(invite, used: true)
428 "nickname" => "GrimReaper",
429 "email" => "death@reapers.afterlife",
430 "fullname" => "Reaper Grim",
431 "bio" => "Your time has come",
432 "password" => "scythe",
433 "confirm" => "scythe",
434 "token" => invite.token
437 {:error, msg} = TwitterAPI.register_user(data)
439 assert msg == "Expired token"
440 refute User.get_by_nickname("GrimReaper")
444 describe "registers with date limited token" do
446 setting = Pleroma.Config.get([:instance, :registrations_open])
449 Pleroma.Config.put([:instance, :registrations_open], false)
450 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
454 "nickname" => "vinny",
455 "email" => "pasta@pizza.vs",
456 "fullname" => "Vinny Vinesauce",
458 "password" => "hiptofbees",
459 "confirm" => "hiptofbees"
462 check_fn = fn invite ->
463 data = Map.put(data, "token", invite.token)
464 {:ok, user} = TwitterAPI.register_user(data)
465 fetched_user = User.get_by_nickname("vinny")
467 assert UserView.render("show.json", %{user: user}) ==
468 UserView.render("show.json", %{user: fetched_user})
471 {:ok, data: data, check_fn: check_fn}
474 test "returns user on success", %{check_fn: check_fn} do
475 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
479 invite = Repo.get_by(UserInviteToken, token: invite.token)
484 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
485 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
489 invite = Repo.get_by(UserInviteToken, token: invite.token)
494 test "returns an error on overdue date", %{data: data} do
495 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
497 data = Map.put(data, "token", invite.token)
499 {:error, msg} = TwitterAPI.register_user(data)
501 assert msg == "Expired token"
502 refute User.get_by_nickname("vinny")
503 invite = Repo.get_by(UserInviteToken, token: invite.token)
509 describe "registers with reusable token" do
511 setting = Pleroma.Config.get([:instance, :registrations_open])
514 Pleroma.Config.put([:instance, :registrations_open], false)
515 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
521 test "returns user on success, after him registration fails" do
522 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
524 UserInviteToken.update_invite!(invite, uses: 99)
527 "nickname" => "vinny",
528 "email" => "pasta@pizza.vs",
529 "fullname" => "Vinny Vinesauce",
531 "password" => "hiptofbees",
532 "confirm" => "hiptofbees",
533 "token" => invite.token
536 {:ok, user} = TwitterAPI.register_user(data)
537 fetched_user = User.get_by_nickname("vinny")
538 invite = Repo.get_by(UserInviteToken, token: invite.token)
540 assert invite.used == true
542 assert UserView.render("show.json", %{user: user}) ==
543 UserView.render("show.json", %{user: fetched_user})
546 "nickname" => "GrimReaper",
547 "email" => "death@reapers.afterlife",
548 "fullname" => "Reaper Grim",
549 "bio" => "Your time has come",
550 "password" => "scythe",
551 "confirm" => "scythe",
552 "token" => invite.token
555 {:error, msg} = TwitterAPI.register_user(data)
557 assert msg == "Expired token"
558 refute User.get_by_nickname("GrimReaper")
562 describe "registers with reusable date limited token" do
564 setting = Pleroma.Config.get([:instance, :registrations_open])
567 Pleroma.Config.put([:instance, :registrations_open], false)
568 on_exit(fn -> Pleroma.Config.put([:instance, :registrations_open], setting) end)
574 test "returns user on success" do
575 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
578 "nickname" => "vinny",
579 "email" => "pasta@pizza.vs",
580 "fullname" => "Vinny Vinesauce",
582 "password" => "hiptofbees",
583 "confirm" => "hiptofbees",
584 "token" => invite.token
587 {:ok, user} = TwitterAPI.register_user(data)
588 fetched_user = User.get_by_nickname("vinny")
589 invite = Repo.get_by(UserInviteToken, token: invite.token)
593 assert UserView.render("show.json", %{user: user}) ==
594 UserView.render("show.json", %{user: fetched_user})
597 test "error after max uses" do
598 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
600 UserInviteToken.update_invite!(invite, uses: 99)
603 "nickname" => "vinny",
604 "email" => "pasta@pizza.vs",
605 "fullname" => "Vinny Vinesauce",
607 "password" => "hiptofbees",
608 "confirm" => "hiptofbees",
609 "token" => invite.token
612 {:ok, user} = TwitterAPI.register_user(data)
613 fetched_user = User.get_by_nickname("vinny")
614 invite = Repo.get_by(UserInviteToken, token: invite.token)
615 assert invite.used == true
617 assert UserView.render("show.json", %{user: user}) ==
618 UserView.render("show.json", %{user: fetched_user})
621 "nickname" => "GrimReaper",
622 "email" => "death@reapers.afterlife",
623 "fullname" => "Reaper Grim",
624 "bio" => "Your time has come",
625 "password" => "scythe",
626 "confirm" => "scythe",
627 "token" => invite.token
630 {:error, msg} = TwitterAPI.register_user(data)
632 assert msg == "Expired token"
633 refute User.get_by_nickname("GrimReaper")
636 test "returns error on overdue date" do
638 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
641 "nickname" => "GrimReaper",
642 "email" => "death@reapers.afterlife",
643 "fullname" => "Reaper Grim",
644 "bio" => "Your time has come",
645 "password" => "scythe",
646 "confirm" => "scythe",
647 "token" => invite.token
650 {:error, msg} = TwitterAPI.register_user(data)
652 assert msg == "Expired token"
653 refute User.get_by_nickname("GrimReaper")
656 test "returns error on with overdue date and after max" do
658 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
660 UserInviteToken.update_invite!(invite, uses: 100)
663 "nickname" => "GrimReaper",
664 "email" => "death@reapers.afterlife",
665 "fullname" => "Reaper Grim",
666 "bio" => "Your time has come",
667 "password" => "scythe",
668 "confirm" => "scythe",
669 "token" => invite.token
672 {:error, msg} = TwitterAPI.register_user(data)
674 assert msg == "Expired token"
675 refute User.get_by_nickname("GrimReaper")
679 test "it returns the error on registration problems" do
681 "nickname" => "lain",
682 "email" => "lain@wired.jp",
683 "fullname" => "lain iwakura",
684 "bio" => "close the world.",
688 {:error, error_object} = TwitterAPI.register_user(data)
690 assert is_binary(error_object[:error])
691 refute User.get_by_nickname("lain")
694 test "it assigns an integer conversation_id" do
695 note_activity = insert(:note_activity)
696 status = ActivityView.render("activity.json", activity: note_activity)
698 assert is_number(status["statusnet_conversation_id"])
702 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
703 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
707 describe "fetching a user by uri" do
708 test "fetches a user by uri" do
709 id = "https://mastodon.social/users/lambadalambda"
711 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
712 remote = User.get_by_ap_id(id)
714 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
716 # Also fetches the feed.
717 # assert Activity.get_create_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
718 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength