1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPITest do
8 alias Pleroma.Conversation.Participation
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.ActivityPub.Visibility
14 alias Pleroma.Web.AdminAPI.AccountView
15 alias Pleroma.Web.CommonAPI
17 import Pleroma.Factory
20 require Pleroma.Constants
22 setup do: clear_config([:instance, :safe_dm_mentions])
23 setup do: clear_config([:instance, :limit])
24 setup do: clear_config([:instance, :max_pinned_statuses])
26 describe "deletion" do
27 test "it allows users to delete their posts" do
30 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
32 with_mock Pleroma.Web.Federator,
33 publish: fn _ -> nil end do
34 assert {:ok, delete} = CommonAPI.delete(post.id, user)
36 assert called(Pleroma.Web.Federator.publish(delete))
39 refute Activity.get_by_id(post.id)
42 test "it does not allow a user to delete their posts" do
44 other_user = insert(:user)
46 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
48 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
49 assert Activity.get_by_id(post.id)
52 test "it allows moderators to delete other user's posts" do
54 moderator = insert(:user, is_moderator: true)
56 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
58 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
61 refute Activity.get_by_id(post.id)
64 test "it allows admins to delete other user's posts" do
66 moderator = insert(:user, is_admin: true)
68 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
70 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
73 refute Activity.get_by_id(post.id)
76 test "superusers deleting non-local posts won't federate the delete" do
77 # This is the user of the ingested activity
81 ap_id: "http://mastodon.example.org/users/admin",
82 last_refreshed_at: NaiveDateTime.utc_now()
85 moderator = insert(:user, is_admin: true)
88 File.read!("test/fixtures/mastodon-post-activity.json")
91 {:ok, post} = Transmogrifier.handle_incoming(data)
93 with_mock Pleroma.Web.Federator,
94 publish: fn _ -> nil end do
95 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
97 refute called(Pleroma.Web.Federator.publish(:_))
100 refute Activity.get_by_id(post.id)
104 test "favoriting race condition" do
106 users_serial = insert_list(10, :user)
107 users = insert_list(10, :user)
109 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
112 |> Enum.map(fn user ->
113 CommonAPI.favorite(user, activity.id)
116 object = Object.get_by_ap_id(activity.data["object"])
117 assert object.data["like_count"] == 10
120 |> Enum.map(fn user ->
122 CommonAPI.favorite(user, activity.id)
125 |> Enum.map(&Task.await/1)
127 object = Object.get_by_ap_id(activity.data["object"])
128 assert object.data["like_count"] == 20
131 test "repeating race condition" do
133 users_serial = insert_list(10, :user)
134 users = insert_list(10, :user)
136 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
139 |> Enum.map(fn user ->
140 CommonAPI.repeat(activity.id, user)
143 object = Object.get_by_ap_id(activity.data["object"])
144 assert object.data["announcement_count"] == 10
147 |> Enum.map(fn user ->
149 CommonAPI.repeat(activity.id, user)
152 |> Enum.map(&Task.await/1)
154 object = Object.get_by_ap_id(activity.data["object"])
155 assert object.data["announcement_count"] == 20
158 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
160 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
162 [participation] = Participation.for_user(user)
165 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
167 assert Visibility.is_direct?(convo_reply)
169 assert activity.data["context"] == convo_reply.data["context"]
172 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
174 jafnhar = insert(:user)
175 tridi = insert(:user)
178 CommonAPI.post(har, %{
179 "status" => "@#{jafnhar.nickname} hey",
180 "visibility" => "direct"
183 assert har.ap_id in activity.recipients
184 assert jafnhar.ap_id in activity.recipients
186 [participation] = Participation.for_user(har)
189 CommonAPI.post(har, %{
190 "status" => "I don't really like @#{tridi.nickname}",
191 "visibility" => "direct",
192 "in_reply_to_status_id" => activity.id,
193 "in_reply_to_conversation_id" => participation.id
196 assert har.ap_id in activity.recipients
197 assert jafnhar.ap_id in activity.recipients
198 refute tridi.ap_id in activity.recipients
201 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
203 jafnhar = insert(:user)
204 tridi = insert(:user)
206 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
209 CommonAPI.post(har, %{
210 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
211 "visibility" => "direct"
214 refute tridi.ap_id in activity.recipients
215 assert jafnhar.ap_id in activity.recipients
218 test "it de-duplicates tags" do
220 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
222 object = Object.normalize(activity)
224 assert object.data["tag"] == ["2hu"]
227 test "it adds emoji in the object" do
229 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
231 assert Object.normalize(activity).data["emoji"]["firefox"]
234 describe "posting" do
235 test "it supports explicit addressing" do
237 user_two = insert(:user)
238 user_three = insert(:user)
239 user_four = insert(:user)
242 CommonAPI.post(user, %{
244 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
245 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
248 assert user.ap_id in activity.recipients
249 assert user_two.ap_id in activity.recipients
250 assert user_four.ap_id in activity.recipients
251 refute user_three.ap_id in activity.recipients
254 test "it filters out obviously bad tags when accepting a post as HTML" do
257 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
260 CommonAPI.post(user, %{
262 "content_type" => "text/html"
265 object = Object.normalize(activity)
267 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
270 test "it filters out obviously bad tags when accepting a post as Markdown" do
273 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
276 CommonAPI.post(user, %{
278 "content_type" => "text/markdown"
281 object = Object.normalize(activity)
283 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
286 test "it does not allow replies to direct messages that are not direct messages themselves" do
289 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
292 CommonAPI.post(user, %{
293 "status" => "suya..",
294 "visibility" => "direct",
295 "in_reply_to_status_id" => activity.id
298 Enum.each(["public", "private", "unlisted"], fn visibility ->
299 assert {:error, "The message visibility must be direct"} =
300 CommonAPI.post(user, %{
301 "status" => "suya..",
302 "visibility" => visibility,
303 "in_reply_to_status_id" => activity.id
308 test "it allows to address a list" do
310 {:ok, list} = Pleroma.List.create("foo", user)
313 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
315 assert activity.data["bcc"] == [list.ap_id]
316 assert activity.recipients == [list.ap_id, user.ap_id]
317 assert activity.data["listMessage"] == list.ap_id
320 test "it returns error when status is empty and no attachments" do
323 assert {:error, "Cannot post an empty status without attachments"} =
324 CommonAPI.post(user, %{"status" => ""})
327 test "it validates character limits are correctly enforced" do
328 Pleroma.Config.put([:instance, :limit], 5)
332 assert {:error, "The status is over the character limit"} =
333 CommonAPI.post(user, %{"status" => "foobar"})
335 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
338 test "it can handle activities that expire" do
342 NaiveDateTime.utc_now()
343 |> NaiveDateTime.truncate(:second)
344 |> NaiveDateTime.add(1_000_000, :second)
346 assert {:ok, activity} =
347 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
349 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
350 assert expiration.scheduled_at == expires_at
354 describe "reactions" do
355 test "reacting to a status with an emoji" do
357 other_user = insert(:user)
359 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
361 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
363 assert reaction.data["actor"] == user.ap_id
364 assert reaction.data["content"] == "👍"
366 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
368 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
371 test "unreacting to a status with an emoji" do
373 other_user = insert(:user)
375 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
376 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
378 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
380 assert unreaction.data["type"] == "Undo"
381 assert unreaction.data["object"] == reaction.data["id"]
384 test "repeating a status" do
386 other_user = insert(:user)
388 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
390 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
393 test "can't repeat a repeat" do
395 other_user = insert(:user)
396 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
398 {:ok, %Activity{} = announce, _} = CommonAPI.repeat(activity.id, other_user)
400 refute match?({:ok, %Activity{}, _}, CommonAPI.repeat(announce.id, user))
403 test "repeating a status privately" do
405 other_user = insert(:user)
407 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
409 {:ok, %Activity{} = announce_activity, _} =
410 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
412 assert Visibility.is_private?(announce_activity)
415 test "favoriting a status" do
417 other_user = insert(:user)
419 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
421 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
422 assert data["type"] == "Like"
423 assert data["actor"] == user.ap_id
424 assert data["object"] == post_activity.data["object"]
427 test "retweeting a status twice returns the status" do
429 other_user = insert(:user)
431 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
432 {:ok, %Activity{} = announce, object} = CommonAPI.repeat(activity.id, user)
433 {:ok, ^announce, ^object} = CommonAPI.repeat(activity.id, user)
436 test "favoriting a status twice returns ok, but without the like activity" do
438 other_user = insert(:user)
440 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
441 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
442 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
446 describe "pinned statuses" do
448 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
451 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
453 [user: user, activity: activity]
456 test "pin status", %{user: user, activity: activity} do
457 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
460 user = refresh_record(user)
462 assert %User{pinned_activities: [^id]} = user
465 test "pin poll", %{user: user} do
467 CommonAPI.post(user, %{
468 "status" => "How is fediverse today?",
469 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
472 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
475 user = refresh_record(user)
477 assert %User{pinned_activities: [^id]} = user
480 test "unlisted statuses can be pinned", %{user: user} do
481 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
482 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
485 test "only self-authored can be pinned", %{activity: activity} do
488 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
491 test "max pinned statuses", %{user: user, activity: activity_one} do
492 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
494 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
496 user = refresh_record(user)
498 assert {:error, "You have already pinned the maximum number of statuses"} =
499 CommonAPI.pin(activity_two.id, user)
502 test "unpin status", %{user: user, activity: activity} do
503 {:ok, activity} = CommonAPI.pin(activity.id, user)
505 user = refresh_record(user)
509 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
511 user = refresh_record(user)
513 assert %User{pinned_activities: []} = user
516 test "should unpin when deleting a status", %{user: user, activity: activity} do
517 {:ok, activity} = CommonAPI.pin(activity.id, user)
519 user = refresh_record(user)
521 assert {:ok, _} = CommonAPI.delete(activity.id, user)
523 user = refresh_record(user)
525 assert %User{pinned_activities: []} = user
529 describe "mute tests" do
533 activity = insert(:note_activity)
535 [user: user, activity: activity]
538 test "add mute", %{user: user, activity: activity} do
539 {:ok, _} = CommonAPI.add_mute(user, activity)
540 assert CommonAPI.thread_muted?(user, activity)
543 test "remove mute", %{user: user, activity: activity} do
544 CommonAPI.add_mute(user, activity)
545 {:ok, _} = CommonAPI.remove_mute(user, activity)
546 refute CommonAPI.thread_muted?(user, activity)
549 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
550 CommonAPI.add_mute(user, activity)
551 {:error, _} = CommonAPI.add_mute(user, activity)
555 describe "reports" do
556 test "creates a report" do
557 reporter = insert(:user)
558 target_user = insert(:user)
560 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
562 reporter_ap_id = reporter.ap_id
563 target_ap_id = target_user.ap_id
564 activity_ap_id = activity.data["id"]
568 account_id: target_user.id,
570 status_ids: [activity.id]
575 "id" => activity_ap_id,
576 "content" => "foobar",
577 "published" => activity.object.data["published"],
578 "actor" => AccountView.render("show.json", %{user: target_user})
581 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
584 actor: ^reporter_ap_id,
587 "content" => ^comment,
588 "object" => [^target_ap_id, ^note_obj],
594 test "updates report state" do
595 [reporter, target_user] = insert_pair(:user)
596 activity = insert(:note_activity, user: target_user)
598 {:ok, %Activity{id: report_id}} =
599 CommonAPI.report(reporter, %{
600 account_id: target_user.id,
601 comment: "I feel offended",
602 status_ids: [activity.id]
605 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
607 assert report.data["state"] == "resolved"
609 [reported_user, activity_id] = report.data["object"]
611 assert reported_user == target_user.ap_id
612 assert activity_id == activity.data["id"]
615 test "does not update report state when state is unsupported" do
616 [reporter, target_user] = insert_pair(:user)
617 activity = insert(:note_activity, user: target_user)
619 {:ok, %Activity{id: report_id}} =
620 CommonAPI.report(reporter, %{
621 account_id: target_user.id,
622 comment: "I feel offended",
623 status_ids: [activity.id]
626 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
629 test "updates state of multiple reports" do
630 [reporter, target_user] = insert_pair(:user)
631 activity = insert(:note_activity, user: target_user)
633 {:ok, %Activity{id: first_report_id}} =
634 CommonAPI.report(reporter, %{
635 account_id: target_user.id,
636 comment: "I feel offended",
637 status_ids: [activity.id]
640 {:ok, %Activity{id: second_report_id}} =
641 CommonAPI.report(reporter, %{
642 account_id: target_user.id,
643 comment: "I feel very offended!",
644 status_ids: [activity.id]
648 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
650 first_report = Activity.get_by_id(first_report_id)
651 second_report = Activity.get_by_id(second_report_id)
653 assert report_ids -- [first_report_id, second_report_id] == []
654 assert first_report.data["state"] == "resolved"
655 assert second_report.data["state"] == "resolved"
659 describe "reblog muting" do
661 muter = insert(:user)
663 muted = insert(:user)
665 [muter: muter, muted: muted]
668 test "add a reblog mute", %{muter: muter, muted: muted} do
669 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
671 assert User.showing_reblogs?(muter, muted) == false
674 test "remove a reblog mute", %{muter: muter, muted: muted} do
675 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
676 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
678 assert User.showing_reblogs?(muter, muted) == true
682 describe "unfollow/2" do
683 test "also unsubscribes a user" do
684 [follower, followed] = insert_pair(:user)
685 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
686 {:ok, _subscription} = User.subscribe(follower, followed)
688 assert User.subscribed_to?(follower, followed)
690 {:ok, follower} = CommonAPI.unfollow(follower, followed)
692 refute User.subscribed_to?(follower, followed)
695 test "cancels a pending follow for a local user" do
696 follower = insert(:user)
697 followed = insert(:user, locked: true)
699 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
700 CommonAPI.follow(follower, followed)
702 assert User.get_follow_state(follower, followed) == :follow_pending
703 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
704 assert User.get_follow_state(follower, followed) == nil
706 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
707 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
712 "object" => %{"type" => "Follow", "state" => "cancelled"}
714 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
717 test "cancels a pending follow for a remote user" do
718 follower = insert(:user)
719 followed = insert(:user, locked: true, local: false, ap_enabled: true)
721 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
722 CommonAPI.follow(follower, followed)
724 assert User.get_follow_state(follower, followed) == :follow_pending
725 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
726 assert User.get_follow_state(follower, followed) == nil
728 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
729 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
734 "object" => %{"type" => "Follow", "state" => "cancelled"}
736 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
740 describe "accept_follow_request/2" do
741 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
742 user = insert(:user, locked: true)
743 follower = insert(:user)
744 follower_two = insert(:user)
746 {:ok, follow_activity} = ActivityPub.follow(follower, user)
747 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
748 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
750 assert follow_activity.data["state"] == "pending"
751 assert follow_activity_two.data["state"] == "pending"
752 assert follow_activity_three.data["state"] == "pending"
754 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
756 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
757 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
758 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
761 test "after rejection, it sets all existing pending follow request states to 'reject'" do
762 user = insert(:user, locked: true)
763 follower = insert(:user)
764 follower_two = insert(:user)
766 {:ok, follow_activity} = ActivityPub.follow(follower, user)
767 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
768 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
770 assert follow_activity.data["state"] == "pending"
771 assert follow_activity_two.data["state"] == "pending"
772 assert follow_activity_three.data["state"] == "pending"
774 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
776 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
777 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
778 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
781 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
782 user = insert(:user, locked: true)
783 not_follower = insert(:user)
784 CommonAPI.accept_follow_request(not_follower, user)
786 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
791 test "does not allow to vote twice" do
793 other_user = insert(:user)
796 CommonAPI.post(user, %{
797 "status" => "Am I cute?",
798 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
801 object = Object.normalize(activity)
803 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
805 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
809 describe "listen/2" do
810 test "returns a valid activity" do
814 CommonAPI.listen(user, %{
815 "title" => "lain radio episode 1",
816 "album" => "lain radio",
821 object = Object.normalize(activity)
823 assert object.data["title"] == "lain radio episode 1"
825 assert Visibility.get_visibility(activity) == "public"
828 test "respects visibility=private" do
832 CommonAPI.listen(user, %{
833 "title" => "lain radio episode 1",
834 "album" => "lain radio",
837 "visibility" => "private"
840 object = Object.normalize(activity)
842 assert object.data["title"] == "lain radio episode 1"
844 assert Visibility.get_visibility(activity) == "private"