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 "unblocking" do
27 test "it works even without an existing block activity" do
28 blocked = insert(:user)
29 blocker = insert(:user)
30 User.block(blocker, blocked)
32 assert User.blocks?(blocker, blocked)
33 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
34 refute User.blocks?(blocker, blocked)
38 describe "deletion" do
39 test "it works with pruned objects" do
42 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
44 clear_config([:instance, :federating], true)
46 Object.normalize(post, false)
49 with_mock Pleroma.Web.Federator,
50 publish: fn _ -> nil end do
51 assert {:ok, delete} = CommonAPI.delete(post.id, user)
53 assert called(Pleroma.Web.Federator.publish(delete))
56 refute Activity.get_by_id(post.id)
59 test "it allows users to delete their posts" do
62 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
64 clear_config([:instance, :federating], true)
66 with_mock Pleroma.Web.Federator,
67 publish: fn _ -> nil end do
68 assert {:ok, delete} = CommonAPI.delete(post.id, user)
70 assert called(Pleroma.Web.Federator.publish(delete))
73 refute Activity.get_by_id(post.id)
76 test "it does not allow a user to delete their posts" do
78 other_user = insert(:user)
80 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
82 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
83 assert Activity.get_by_id(post.id)
86 test "it allows moderators to delete other user's posts" do
88 moderator = insert(:user, is_moderator: true)
90 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
92 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
95 refute Activity.get_by_id(post.id)
98 test "it allows admins to delete other user's posts" do
100 moderator = insert(:user, is_admin: true)
102 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
104 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
107 refute Activity.get_by_id(post.id)
110 test "superusers deleting non-local posts won't federate the delete" do
111 # This is the user of the ingested activity
115 ap_id: "http://mastodon.example.org/users/admin",
116 last_refreshed_at: NaiveDateTime.utc_now()
119 moderator = insert(:user, is_admin: true)
122 File.read!("test/fixtures/mastodon-post-activity.json")
125 {:ok, post} = Transmogrifier.handle_incoming(data)
127 with_mock Pleroma.Web.Federator,
128 publish: fn _ -> nil end do
129 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
131 refute called(Pleroma.Web.Federator.publish(:_))
134 refute Activity.get_by_id(post.id)
138 test "favoriting race condition" do
140 users_serial = insert_list(10, :user)
141 users = insert_list(10, :user)
143 {:ok, activity} = CommonAPI.post(user, %{status: "."})
146 |> Enum.map(fn user ->
147 CommonAPI.favorite(user, activity.id)
150 object = Object.get_by_ap_id(activity.data["object"])
151 assert object.data["like_count"] == 10
154 |> Enum.map(fn user ->
156 CommonAPI.favorite(user, activity.id)
159 |> Enum.map(&Task.await/1)
161 object = Object.get_by_ap_id(activity.data["object"])
162 assert object.data["like_count"] == 20
165 test "repeating race condition" do
167 users_serial = insert_list(10, :user)
168 users = insert_list(10, :user)
170 {:ok, activity} = CommonAPI.post(user, %{status: "."})
173 |> Enum.map(fn user ->
174 CommonAPI.repeat(activity.id, user)
177 object = Object.get_by_ap_id(activity.data["object"])
178 assert object.data["announcement_count"] == 10
181 |> Enum.map(fn user ->
183 CommonAPI.repeat(activity.id, user)
186 |> Enum.map(&Task.await/1)
188 object = Object.get_by_ap_id(activity.data["object"])
189 assert object.data["announcement_count"] == 20
192 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
194 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
196 [participation] = Participation.for_user(user)
199 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
201 assert Visibility.is_direct?(convo_reply)
203 assert activity.data["context"] == convo_reply.data["context"]
206 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
208 jafnhar = insert(:user)
209 tridi = insert(:user)
212 CommonAPI.post(har, %{
213 status: "@#{jafnhar.nickname} hey",
217 assert har.ap_id in activity.recipients
218 assert jafnhar.ap_id in activity.recipients
220 [participation] = Participation.for_user(har)
223 CommonAPI.post(har, %{
224 status: "I don't really like @#{tridi.nickname}",
225 visibility: "direct",
226 in_reply_to_status_id: activity.id,
227 in_reply_to_conversation_id: participation.id
230 assert har.ap_id in activity.recipients
231 assert jafnhar.ap_id in activity.recipients
232 refute tridi.ap_id in activity.recipients
235 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
237 jafnhar = insert(:user)
238 tridi = insert(:user)
240 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
243 CommonAPI.post(har, %{
244 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
248 refute tridi.ap_id in activity.recipients
249 assert jafnhar.ap_id in activity.recipients
252 test "it de-duplicates tags" do
254 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
256 object = Object.normalize(activity)
258 assert object.data["tag"] == ["2hu"]
261 test "it adds emoji in the object" do
263 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
265 assert Object.normalize(activity).data["emoji"]["firefox"]
268 describe "posting" do
269 test "it supports explicit addressing" do
271 user_two = insert(:user)
272 user_three = insert(:user)
273 user_four = insert(:user)
276 CommonAPI.post(user, %{
278 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
279 to: [user_two.nickname, user_four.nickname, "nonexistent"]
282 assert user.ap_id in activity.recipients
283 assert user_two.ap_id in activity.recipients
284 assert user_four.ap_id in activity.recipients
285 refute user_three.ap_id in activity.recipients
288 test "it filters out obviously bad tags when accepting a post as HTML" do
291 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
294 CommonAPI.post(user, %{
296 content_type: "text/html"
299 object = Object.normalize(activity)
301 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
304 test "it filters out obviously bad tags when accepting a post as Markdown" do
307 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
310 CommonAPI.post(user, %{
312 content_type: "text/markdown"
315 object = Object.normalize(activity)
317 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
320 test "it does not allow replies to direct messages that are not direct messages themselves" do
323 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
326 CommonAPI.post(user, %{
328 visibility: "direct",
329 in_reply_to_status_id: activity.id
332 Enum.each(["public", "private", "unlisted"], fn visibility ->
333 assert {:error, "The message visibility must be direct"} =
334 CommonAPI.post(user, %{
336 visibility: visibility,
337 in_reply_to_status_id: activity.id
342 test "it allows to address a list" do
344 {:ok, list} = Pleroma.List.create("foo", user)
346 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
348 assert activity.data["bcc"] == [list.ap_id]
349 assert activity.recipients == [list.ap_id, user.ap_id]
350 assert activity.data["listMessage"] == list.ap_id
353 test "it returns error when status is empty and no attachments" do
356 assert {:error, "Cannot post an empty status without attachments"} =
357 CommonAPI.post(user, %{status: ""})
360 test "it validates character limits are correctly enforced" do
361 Pleroma.Config.put([:instance, :limit], 5)
365 assert {:error, "The status is over the character limit"} =
366 CommonAPI.post(user, %{status: "foobar"})
368 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
371 test "it can handle activities that expire" do
375 NaiveDateTime.utc_now()
376 |> NaiveDateTime.truncate(:second)
377 |> NaiveDateTime.add(1_000_000, :second)
379 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
381 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
382 assert expiration.scheduled_at == expires_at
386 describe "reactions" do
387 test "reacting to a status with an emoji" do
389 other_user = insert(:user)
391 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
393 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
395 assert reaction.data["actor"] == user.ap_id
396 assert reaction.data["content"] == "👍"
398 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
400 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
403 test "unreacting to a status with an emoji" do
405 other_user = insert(:user)
407 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
408 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
410 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
412 assert unreaction.data["type"] == "Undo"
413 assert unreaction.data["object"] == reaction.data["id"]
414 assert unreaction.local
417 test "repeating a status" do
419 other_user = insert(:user)
421 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
423 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
424 assert Visibility.is_public?(announce_activity)
427 test "can't repeat a repeat" do
429 other_user = insert(:user)
430 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
432 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
434 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
437 test "repeating a status privately" do
439 other_user = insert(:user)
441 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
443 {:ok, %Activity{} = announce_activity} =
444 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
446 assert Visibility.is_private?(announce_activity)
447 refute Visibility.visible_for_user?(announce_activity, nil)
450 test "favoriting a status" do
452 other_user = insert(:user)
454 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
456 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
457 assert data["type"] == "Like"
458 assert data["actor"] == user.ap_id
459 assert data["object"] == post_activity.data["object"]
462 test "retweeting a status twice returns the status" do
464 other_user = insert(:user)
466 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
467 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
468 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
471 test "favoriting a status twice returns ok, but without the like activity" do
473 other_user = insert(:user)
475 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
476 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
477 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
481 describe "pinned statuses" do
483 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
486 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
488 [user: user, activity: activity]
491 test "pin status", %{user: user, activity: activity} do
492 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
495 user = refresh_record(user)
497 assert %User{pinned_activities: [^id]} = user
500 test "pin poll", %{user: user} do
502 CommonAPI.post(user, %{
503 status: "How is fediverse today?",
504 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
507 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
510 user = refresh_record(user)
512 assert %User{pinned_activities: [^id]} = user
515 test "unlisted statuses can be pinned", %{user: user} do
516 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
517 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
520 test "only self-authored can be pinned", %{activity: activity} do
523 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
526 test "max pinned statuses", %{user: user, activity: activity_one} do
527 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
529 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
531 user = refresh_record(user)
533 assert {:error, "You have already pinned the maximum number of statuses"} =
534 CommonAPI.pin(activity_two.id, user)
537 test "unpin status", %{user: user, activity: activity} do
538 {:ok, activity} = CommonAPI.pin(activity.id, user)
540 user = refresh_record(user)
544 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
546 user = refresh_record(user)
548 assert %User{pinned_activities: []} = user
551 test "should unpin when deleting a status", %{user: user, activity: activity} do
552 {:ok, activity} = CommonAPI.pin(activity.id, user)
554 user = refresh_record(user)
556 assert {:ok, _} = CommonAPI.delete(activity.id, user)
558 user = refresh_record(user)
560 assert %User{pinned_activities: []} = user
564 describe "mute tests" do
568 activity = insert(:note_activity)
570 [user: user, activity: activity]
573 test "add mute", %{user: user, activity: activity} do
574 {:ok, _} = CommonAPI.add_mute(user, activity)
575 assert CommonAPI.thread_muted?(user, activity)
578 test "remove mute", %{user: user, activity: activity} do
579 CommonAPI.add_mute(user, activity)
580 {:ok, _} = CommonAPI.remove_mute(user, activity)
581 refute CommonAPI.thread_muted?(user, activity)
584 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
585 CommonAPI.add_mute(user, activity)
586 {:error, _} = CommonAPI.add_mute(user, activity)
590 describe "reports" do
591 test "creates a report" do
592 reporter = insert(:user)
593 target_user = insert(:user)
595 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
597 reporter_ap_id = reporter.ap_id
598 target_ap_id = target_user.ap_id
599 activity_ap_id = activity.data["id"]
603 account_id: target_user.id,
605 status_ids: [activity.id]
610 "id" => activity_ap_id,
611 "content" => "foobar",
612 "published" => activity.object.data["published"],
613 "actor" => AccountView.render("show.json", %{user: target_user})
616 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
619 actor: ^reporter_ap_id,
622 "content" => ^comment,
623 "object" => [^target_ap_id, ^note_obj],
629 test "updates report state" do
630 [reporter, target_user] = insert_pair(:user)
631 activity = insert(:note_activity, user: target_user)
633 {:ok, %Activity{id: report_id}} =
634 CommonAPI.report(reporter, %{
635 account_id: target_user.id,
636 comment: "I feel offended",
637 status_ids: [activity.id]
640 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
642 assert report.data["state"] == "resolved"
644 [reported_user, activity_id] = report.data["object"]
646 assert reported_user == target_user.ap_id
647 assert activity_id == activity.data["id"]
650 test "does not update report state when state is unsupported" do
651 [reporter, target_user] = insert_pair(:user)
652 activity = insert(:note_activity, user: target_user)
654 {:ok, %Activity{id: report_id}} =
655 CommonAPI.report(reporter, %{
656 account_id: target_user.id,
657 comment: "I feel offended",
658 status_ids: [activity.id]
661 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
664 test "updates state of multiple reports" do
665 [reporter, target_user] = insert_pair(:user)
666 activity = insert(:note_activity, user: target_user)
668 {:ok, %Activity{id: first_report_id}} =
669 CommonAPI.report(reporter, %{
670 account_id: target_user.id,
671 comment: "I feel offended",
672 status_ids: [activity.id]
675 {:ok, %Activity{id: second_report_id}} =
676 CommonAPI.report(reporter, %{
677 account_id: target_user.id,
678 comment: "I feel very offended!",
679 status_ids: [activity.id]
683 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
685 first_report = Activity.get_by_id(first_report_id)
686 second_report = Activity.get_by_id(second_report_id)
688 assert report_ids -- [first_report_id, second_report_id] == []
689 assert first_report.data["state"] == "resolved"
690 assert second_report.data["state"] == "resolved"
694 describe "reblog muting" do
696 muter = insert(:user)
698 muted = insert(:user)
700 [muter: muter, muted: muted]
703 test "add a reblog mute", %{muter: muter, muted: muted} do
704 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
706 assert User.showing_reblogs?(muter, muted) == false
709 test "remove a reblog mute", %{muter: muter, muted: muted} do
710 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
711 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
713 assert User.showing_reblogs?(muter, muted) == true
717 describe "unfollow/2" do
718 test "also unsubscribes a user" do
719 [follower, followed] = insert_pair(:user)
720 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
721 {:ok, _subscription} = User.subscribe(follower, followed)
723 assert User.subscribed_to?(follower, followed)
725 {:ok, follower} = CommonAPI.unfollow(follower, followed)
727 refute User.subscribed_to?(follower, followed)
730 test "cancels a pending follow for a local user" do
731 follower = insert(:user)
732 followed = insert(:user, locked: true)
734 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
735 CommonAPI.follow(follower, followed)
737 assert User.get_follow_state(follower, followed) == :follow_pending
738 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
739 assert User.get_follow_state(follower, followed) == nil
741 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
742 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
747 "object" => %{"type" => "Follow", "state" => "cancelled"}
749 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
752 test "cancels a pending follow for a remote user" do
753 follower = insert(:user)
754 followed = insert(:user, locked: true, local: false, ap_enabled: true)
756 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
757 CommonAPI.follow(follower, followed)
759 assert User.get_follow_state(follower, followed) == :follow_pending
760 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
761 assert User.get_follow_state(follower, followed) == nil
763 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
764 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
769 "object" => %{"type" => "Follow", "state" => "cancelled"}
771 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
775 describe "accept_follow_request/2" do
776 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
777 user = insert(:user, locked: true)
778 follower = insert(:user)
779 follower_two = insert(:user)
781 {:ok, follow_activity} = ActivityPub.follow(follower, user)
782 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
783 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
785 assert follow_activity.data["state"] == "pending"
786 assert follow_activity_two.data["state"] == "pending"
787 assert follow_activity_three.data["state"] == "pending"
789 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
791 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
792 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
793 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
796 test "after rejection, it sets all existing pending follow request states to 'reject'" do
797 user = insert(:user, locked: true)
798 follower = insert(:user)
799 follower_two = insert(:user)
801 {:ok, follow_activity} = ActivityPub.follow(follower, user)
802 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
803 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
805 assert follow_activity.data["state"] == "pending"
806 assert follow_activity_two.data["state"] == "pending"
807 assert follow_activity_three.data["state"] == "pending"
809 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
811 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
812 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
813 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
816 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
817 user = insert(:user, locked: true)
818 not_follower = insert(:user)
819 CommonAPI.accept_follow_request(not_follower, user)
821 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
826 test "does not allow to vote twice" do
828 other_user = insert(:user)
831 CommonAPI.post(user, %{
832 status: "Am I cute?",
833 poll: %{options: ["Yes", "No"], expires_in: 20}
836 object = Object.normalize(activity)
838 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
840 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
844 describe "listen/2" do
845 test "returns a valid activity" do
849 CommonAPI.listen(user, %{
850 title: "lain radio episode 1",
856 object = Object.normalize(activity)
858 assert object.data["title"] == "lain radio episode 1"
860 assert Visibility.get_visibility(activity) == "public"
863 test "respects visibility=private" do
867 CommonAPI.listen(user, %{
868 title: "lain radio episode 1",
872 visibility: "private"
875 object = Object.normalize(activity)
877 assert object.data["title"] == "lain radio episode 1"
879 assert Visibility.get_visibility(activity) == "private"