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 works with pruned objects" do
30 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
32 Object.normalize(post, false)
35 with_mock Pleroma.Web.Federator,
36 publish: fn _ -> nil end do
37 assert {:ok, delete} = CommonAPI.delete(post.id, user)
39 assert called(Pleroma.Web.Federator.publish(delete))
42 refute Activity.get_by_id(post.id)
45 test "it allows users to delete their posts" do
48 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
50 with_mock Pleroma.Web.Federator,
51 publish: fn _ -> nil end do
52 assert {:ok, delete} = CommonAPI.delete(post.id, user)
54 assert called(Pleroma.Web.Federator.publish(delete))
57 refute Activity.get_by_id(post.id)
60 test "it does not allow a user to delete their posts" do
62 other_user = insert(:user)
64 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
66 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
67 assert Activity.get_by_id(post.id)
70 test "it allows moderators to delete other user's posts" do
72 moderator = insert(:user, is_moderator: true)
74 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
76 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
79 refute Activity.get_by_id(post.id)
82 test "it allows admins to delete other user's posts" do
84 moderator = insert(:user, is_admin: true)
86 {:ok, post} = CommonAPI.post(user, %{"status" => "namu amida butsu"})
88 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
91 refute Activity.get_by_id(post.id)
94 test "superusers deleting non-local posts won't federate the delete" do
95 # This is the user of the ingested activity
99 ap_id: "http://mastodon.example.org/users/admin",
100 last_refreshed_at: NaiveDateTime.utc_now()
103 moderator = insert(:user, is_admin: true)
106 File.read!("test/fixtures/mastodon-post-activity.json")
109 {:ok, post} = Transmogrifier.handle_incoming(data)
111 with_mock Pleroma.Web.Federator,
112 publish: fn _ -> nil end do
113 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
115 refute called(Pleroma.Web.Federator.publish(:_))
118 refute Activity.get_by_id(post.id)
122 test "favoriting race condition" do
124 users_serial = insert_list(10, :user)
125 users = insert_list(10, :user)
127 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
130 |> Enum.map(fn user ->
131 CommonAPI.favorite(user, activity.id)
134 object = Object.get_by_ap_id(activity.data["object"])
135 assert object.data["like_count"] == 10
138 |> Enum.map(fn user ->
140 CommonAPI.favorite(user, activity.id)
143 |> Enum.map(&Task.await/1)
145 object = Object.get_by_ap_id(activity.data["object"])
146 assert object.data["like_count"] == 20
149 test "repeating race condition" do
151 users_serial = insert_list(10, :user)
152 users = insert_list(10, :user)
154 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
157 |> Enum.map(fn user ->
158 CommonAPI.repeat(activity.id, user)
161 object = Object.get_by_ap_id(activity.data["object"])
162 assert object.data["announcement_count"] == 10
165 |> Enum.map(fn user ->
167 CommonAPI.repeat(activity.id, user)
170 |> Enum.map(&Task.await/1)
172 object = Object.get_by_ap_id(activity.data["object"])
173 assert object.data["announcement_count"] == 20
176 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
178 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
180 [participation] = Participation.for_user(user)
183 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
185 assert Visibility.is_direct?(convo_reply)
187 assert activity.data["context"] == convo_reply.data["context"]
190 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
192 jafnhar = insert(:user)
193 tridi = insert(:user)
196 CommonAPI.post(har, %{
197 "status" => "@#{jafnhar.nickname} hey",
198 "visibility" => "direct"
201 assert har.ap_id in activity.recipients
202 assert jafnhar.ap_id in activity.recipients
204 [participation] = Participation.for_user(har)
207 CommonAPI.post(har, %{
208 "status" => "I don't really like @#{tridi.nickname}",
209 "visibility" => "direct",
210 "in_reply_to_status_id" => activity.id,
211 "in_reply_to_conversation_id" => participation.id
214 assert har.ap_id in activity.recipients
215 assert jafnhar.ap_id in activity.recipients
216 refute tridi.ap_id in activity.recipients
219 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
221 jafnhar = insert(:user)
222 tridi = insert(:user)
224 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
227 CommonAPI.post(har, %{
228 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
229 "visibility" => "direct"
232 refute tridi.ap_id in activity.recipients
233 assert jafnhar.ap_id in activity.recipients
236 test "it de-duplicates tags" do
238 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
240 object = Object.normalize(activity)
242 assert object.data["tag"] == ["2hu"]
245 test "it adds emoji in the object" do
247 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
249 assert Object.normalize(activity).data["emoji"]["firefox"]
252 describe "posting" do
253 test "it supports explicit addressing" do
255 user_two = insert(:user)
256 user_three = insert(:user)
257 user_four = insert(:user)
260 CommonAPI.post(user, %{
262 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
263 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
266 assert user.ap_id in activity.recipients
267 assert user_two.ap_id in activity.recipients
268 assert user_four.ap_id in activity.recipients
269 refute user_three.ap_id in activity.recipients
272 test "it filters out obviously bad tags when accepting a post as HTML" do
275 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
278 CommonAPI.post(user, %{
280 "content_type" => "text/html"
283 object = Object.normalize(activity)
285 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
288 test "it filters out obviously bad tags when accepting a post as Markdown" do
291 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
294 CommonAPI.post(user, %{
296 "content_type" => "text/markdown"
299 object = Object.normalize(activity)
301 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
304 test "it does not allow replies to direct messages that are not direct messages themselves" do
307 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
310 CommonAPI.post(user, %{
311 "status" => "suya..",
312 "visibility" => "direct",
313 "in_reply_to_status_id" => activity.id
316 Enum.each(["public", "private", "unlisted"], fn visibility ->
317 assert {:error, "The message visibility must be direct"} =
318 CommonAPI.post(user, %{
319 "status" => "suya..",
320 "visibility" => visibility,
321 "in_reply_to_status_id" => activity.id
326 test "it allows to address a list" do
328 {:ok, list} = Pleroma.List.create("foo", user)
331 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
333 assert activity.data["bcc"] == [list.ap_id]
334 assert activity.recipients == [list.ap_id, user.ap_id]
335 assert activity.data["listMessage"] == list.ap_id
338 test "it returns error when status is empty and no attachments" do
341 assert {:error, "Cannot post an empty status without attachments"} =
342 CommonAPI.post(user, %{"status" => ""})
345 test "it validates character limits are correctly enforced" do
346 Pleroma.Config.put([:instance, :limit], 5)
350 assert {:error, "The status is over the character limit"} =
351 CommonAPI.post(user, %{"status" => "foobar"})
353 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
356 test "it can handle activities that expire" do
360 NaiveDateTime.utc_now()
361 |> NaiveDateTime.truncate(:second)
362 |> NaiveDateTime.add(1_000_000, :second)
364 assert {:ok, activity} =
365 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
367 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
368 assert expiration.scheduled_at == expires_at
372 describe "reactions" do
373 test "reacting to a status with an emoji" do
375 other_user = insert(:user)
377 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
379 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
381 assert reaction.data["actor"] == user.ap_id
382 assert reaction.data["content"] == "👍"
384 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
386 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
389 test "unreacting to a status with an emoji" do
391 other_user = insert(:user)
393 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
394 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
396 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
398 assert unreaction.data["type"] == "Undo"
399 assert unreaction.data["object"] == reaction.data["id"]
400 assert unreaction.local
403 test "repeating a status" do
405 other_user = insert(:user)
407 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
409 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
412 test "can't repeat a repeat" do
414 other_user = insert(:user)
415 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
417 {:ok, %Activity{} = announce, _} = CommonAPI.repeat(activity.id, other_user)
419 refute match?({:ok, %Activity{}, _}, CommonAPI.repeat(announce.id, user))
422 test "repeating a status privately" do
424 other_user = insert(:user)
426 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
428 {:ok, %Activity{} = announce_activity, _} =
429 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
431 assert Visibility.is_private?(announce_activity)
434 test "favoriting a status" do
436 other_user = insert(:user)
438 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
440 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
441 assert data["type"] == "Like"
442 assert data["actor"] == user.ap_id
443 assert data["object"] == post_activity.data["object"]
446 test "retweeting a status twice returns the status" do
448 other_user = insert(:user)
450 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
451 {:ok, %Activity{} = announce, object} = CommonAPI.repeat(activity.id, user)
452 {:ok, ^announce, ^object} = CommonAPI.repeat(activity.id, user)
455 test "favoriting a status twice returns ok, but without the like activity" do
457 other_user = insert(:user)
459 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
460 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
461 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
465 describe "pinned statuses" do
467 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
470 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
472 [user: user, activity: activity]
475 test "pin status", %{user: user, activity: activity} do
476 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
479 user = refresh_record(user)
481 assert %User{pinned_activities: [^id]} = user
484 test "pin poll", %{user: user} do
486 CommonAPI.post(user, %{
487 "status" => "How is fediverse today?",
488 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
491 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
494 user = refresh_record(user)
496 assert %User{pinned_activities: [^id]} = user
499 test "unlisted statuses can be pinned", %{user: user} do
500 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
501 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
504 test "only self-authored can be pinned", %{activity: activity} do
507 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
510 test "max pinned statuses", %{user: user, activity: activity_one} do
511 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
513 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
515 user = refresh_record(user)
517 assert {:error, "You have already pinned the maximum number of statuses"} =
518 CommonAPI.pin(activity_two.id, user)
521 test "unpin status", %{user: user, activity: activity} do
522 {:ok, activity} = CommonAPI.pin(activity.id, user)
524 user = refresh_record(user)
528 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
530 user = refresh_record(user)
532 assert %User{pinned_activities: []} = user
535 test "should unpin when deleting a status", %{user: user, activity: activity} do
536 {:ok, activity} = CommonAPI.pin(activity.id, user)
538 user = refresh_record(user)
540 assert {:ok, _} = CommonAPI.delete(activity.id, user)
542 user = refresh_record(user)
544 assert %User{pinned_activities: []} = user
548 describe "mute tests" do
552 activity = insert(:note_activity)
554 [user: user, activity: activity]
557 test "add mute", %{user: user, activity: activity} do
558 {:ok, _} = CommonAPI.add_mute(user, activity)
559 assert CommonAPI.thread_muted?(user, activity)
562 test "remove mute", %{user: user, activity: activity} do
563 CommonAPI.add_mute(user, activity)
564 {:ok, _} = CommonAPI.remove_mute(user, activity)
565 refute CommonAPI.thread_muted?(user, activity)
568 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
569 CommonAPI.add_mute(user, activity)
570 {:error, _} = CommonAPI.add_mute(user, activity)
574 describe "reports" do
575 test "creates a report" do
576 reporter = insert(:user)
577 target_user = insert(:user)
579 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
581 reporter_ap_id = reporter.ap_id
582 target_ap_id = target_user.ap_id
583 activity_ap_id = activity.data["id"]
587 account_id: target_user.id,
589 status_ids: [activity.id]
594 "id" => activity_ap_id,
595 "content" => "foobar",
596 "published" => activity.object.data["published"],
597 "actor" => AccountView.render("show.json", %{user: target_user})
600 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
603 actor: ^reporter_ap_id,
606 "content" => ^comment,
607 "object" => [^target_ap_id, ^note_obj],
613 test "updates report state" do
614 [reporter, target_user] = insert_pair(:user)
615 activity = insert(:note_activity, user: target_user)
617 {:ok, %Activity{id: report_id}} =
618 CommonAPI.report(reporter, %{
619 account_id: target_user.id,
620 comment: "I feel offended",
621 status_ids: [activity.id]
624 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
626 assert report.data["state"] == "resolved"
628 [reported_user, activity_id] = report.data["object"]
630 assert reported_user == target_user.ap_id
631 assert activity_id == activity.data["id"]
634 test "does not update report state when state is unsupported" do
635 [reporter, target_user] = insert_pair(:user)
636 activity = insert(:note_activity, user: target_user)
638 {:ok, %Activity{id: report_id}} =
639 CommonAPI.report(reporter, %{
640 account_id: target_user.id,
641 comment: "I feel offended",
642 status_ids: [activity.id]
645 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
648 test "updates state of multiple reports" do
649 [reporter, target_user] = insert_pair(:user)
650 activity = insert(:note_activity, user: target_user)
652 {:ok, %Activity{id: first_report_id}} =
653 CommonAPI.report(reporter, %{
654 account_id: target_user.id,
655 comment: "I feel offended",
656 status_ids: [activity.id]
659 {:ok, %Activity{id: second_report_id}} =
660 CommonAPI.report(reporter, %{
661 account_id: target_user.id,
662 comment: "I feel very offended!",
663 status_ids: [activity.id]
667 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
669 first_report = Activity.get_by_id(first_report_id)
670 second_report = Activity.get_by_id(second_report_id)
672 assert report_ids -- [first_report_id, second_report_id] == []
673 assert first_report.data["state"] == "resolved"
674 assert second_report.data["state"] == "resolved"
678 describe "reblog muting" do
680 muter = insert(:user)
682 muted = insert(:user)
684 [muter: muter, muted: muted]
687 test "add a reblog mute", %{muter: muter, muted: muted} do
688 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
690 assert User.showing_reblogs?(muter, muted) == false
693 test "remove a reblog mute", %{muter: muter, muted: muted} do
694 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
695 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
697 assert User.showing_reblogs?(muter, muted) == true
701 describe "unfollow/2" do
702 test "also unsubscribes a user" do
703 [follower, followed] = insert_pair(:user)
704 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
705 {:ok, _subscription} = User.subscribe(follower, followed)
707 assert User.subscribed_to?(follower, followed)
709 {:ok, follower} = CommonAPI.unfollow(follower, followed)
711 refute User.subscribed_to?(follower, followed)
714 test "cancels a pending follow for a local user" do
715 follower = insert(:user)
716 followed = insert(:user, locked: true)
718 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
719 CommonAPI.follow(follower, followed)
721 assert User.get_follow_state(follower, followed) == :follow_pending
722 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
723 assert User.get_follow_state(follower, followed) == nil
725 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
726 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
731 "object" => %{"type" => "Follow", "state" => "cancelled"}
733 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
736 test "cancels a pending follow for a remote user" do
737 follower = insert(:user)
738 followed = insert(:user, locked: true, local: false, ap_enabled: true)
740 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
741 CommonAPI.follow(follower, followed)
743 assert User.get_follow_state(follower, followed) == :follow_pending
744 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
745 assert User.get_follow_state(follower, followed) == nil
747 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
748 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
753 "object" => %{"type" => "Follow", "state" => "cancelled"}
755 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
759 describe "accept_follow_request/2" do
760 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
761 user = insert(:user, locked: true)
762 follower = insert(:user)
763 follower_two = insert(:user)
765 {:ok, follow_activity} = ActivityPub.follow(follower, user)
766 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
767 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
769 assert follow_activity.data["state"] == "pending"
770 assert follow_activity_two.data["state"] == "pending"
771 assert follow_activity_three.data["state"] == "pending"
773 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
775 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
776 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
777 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
780 test "after rejection, it sets all existing pending follow request states to 'reject'" do
781 user = insert(:user, locked: true)
782 follower = insert(:user)
783 follower_two = insert(:user)
785 {:ok, follow_activity} = ActivityPub.follow(follower, user)
786 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
787 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
789 assert follow_activity.data["state"] == "pending"
790 assert follow_activity_two.data["state"] == "pending"
791 assert follow_activity_three.data["state"] == "pending"
793 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
795 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
796 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
797 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
800 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
801 user = insert(:user, locked: true)
802 not_follower = insert(:user)
803 CommonAPI.accept_follow_request(not_follower, user)
805 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
810 test "does not allow to vote twice" do
812 other_user = insert(:user)
815 CommonAPI.post(user, %{
816 "status" => "Am I cute?",
817 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
820 object = Object.normalize(activity)
822 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
824 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
828 describe "listen/2" do
829 test "returns a valid activity" do
833 CommonAPI.listen(user, %{
834 "title" => "lain radio episode 1",
835 "album" => "lain radio",
840 object = Object.normalize(activity)
842 assert object.data["title"] == "lain radio episode 1"
844 assert Visibility.get_visibility(activity) == "public"
847 test "respects visibility=private" do
851 CommonAPI.listen(user, %{
852 "title" => "lain radio episode 1",
853 "album" => "lain radio",
856 "visibility" => "private"
859 object = Object.normalize(activity)
861 assert object.data["title"] == "lain radio episode 1"
863 assert Visibility.get_visibility(activity) == "private"