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",
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",
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, %{
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, %{
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)
330 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
332 assert activity.data["bcc"] == [list.ap_id]
333 assert activity.recipients == [list.ap_id, user.ap_id]
334 assert activity.data["listMessage"] == list.ap_id
337 test "it returns error when status is empty and no attachments" do
340 assert {:error, "Cannot post an empty status without attachments"} =
341 CommonAPI.post(user, %{status: ""})
344 test "it validates character limits are correctly enforced" do
345 Pleroma.Config.put([:instance, :limit], 5)
349 assert {:error, "The status is over the character limit"} =
350 CommonAPI.post(user, %{status: "foobar"})
352 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
355 test "it can handle activities that expire" do
359 NaiveDateTime.utc_now()
360 |> NaiveDateTime.truncate(:second)
361 |> NaiveDateTime.add(1_000_000, :second)
363 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
365 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
366 assert expiration.scheduled_at == expires_at
370 describe "reactions" do
371 test "reacting to a status with an emoji" do
373 other_user = insert(:user)
375 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
377 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
379 assert reaction.data["actor"] == user.ap_id
380 assert reaction.data["content"] == "👍"
382 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
384 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
387 test "unreacting to a status with an emoji" do
389 other_user = insert(:user)
391 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
392 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
394 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
396 assert unreaction.data["type"] == "Undo"
397 assert unreaction.data["object"] == reaction.data["id"]
398 assert unreaction.local
401 test "repeating a status" do
403 other_user = insert(:user)
405 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
407 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
410 test "can't repeat a repeat" do
412 other_user = insert(:user)
413 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
415 {:ok, %Activity{} = announce, _} = CommonAPI.repeat(activity.id, other_user)
417 refute match?({:ok, %Activity{}, _}, CommonAPI.repeat(announce.id, user))
420 test "repeating a status privately" do
422 other_user = insert(:user)
424 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
426 {:ok, %Activity{} = announce_activity, _} =
427 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
429 assert Visibility.is_private?(announce_activity)
432 test "favoriting a status" do
434 other_user = insert(:user)
436 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
438 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
439 assert data["type"] == "Like"
440 assert data["actor"] == user.ap_id
441 assert data["object"] == post_activity.data["object"]
444 test "retweeting a status twice returns the status" do
446 other_user = insert(:user)
448 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
449 {:ok, %Activity{} = announce, object} = CommonAPI.repeat(activity.id, user)
450 {:ok, ^announce, ^object} = CommonAPI.repeat(activity.id, user)
453 test "favoriting a status twice returns ok, but without the like activity" do
455 other_user = insert(:user)
457 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
458 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
459 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
463 describe "pinned statuses" do
465 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
468 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
470 [user: user, activity: activity]
473 test "pin status", %{user: user, activity: activity} do
474 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
477 user = refresh_record(user)
479 assert %User{pinned_activities: [^id]} = user
482 test "pin poll", %{user: user} do
484 CommonAPI.post(user, %{
485 status: "How is fediverse today?",
486 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
489 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
492 user = refresh_record(user)
494 assert %User{pinned_activities: [^id]} = user
497 test "unlisted statuses can be pinned", %{user: user} do
498 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
499 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
502 test "only self-authored can be pinned", %{activity: activity} do
505 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
508 test "max pinned statuses", %{user: user, activity: activity_one} do
509 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
511 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
513 user = refresh_record(user)
515 assert {:error, "You have already pinned the maximum number of statuses"} =
516 CommonAPI.pin(activity_two.id, user)
519 test "unpin status", %{user: user, activity: activity} do
520 {:ok, activity} = CommonAPI.pin(activity.id, user)
522 user = refresh_record(user)
526 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
528 user = refresh_record(user)
530 assert %User{pinned_activities: []} = user
533 test "should unpin when deleting a status", %{user: user, activity: activity} do
534 {:ok, activity} = CommonAPI.pin(activity.id, user)
536 user = refresh_record(user)
538 assert {:ok, _} = CommonAPI.delete(activity.id, user)
540 user = refresh_record(user)
542 assert %User{pinned_activities: []} = user
546 describe "mute tests" do
550 activity = insert(:note_activity)
552 [user: user, activity: activity]
555 test "add mute", %{user: user, activity: activity} do
556 {:ok, _} = CommonAPI.add_mute(user, activity)
557 assert CommonAPI.thread_muted?(user, activity)
560 test "remove mute", %{user: user, activity: activity} do
561 CommonAPI.add_mute(user, activity)
562 {:ok, _} = CommonAPI.remove_mute(user, activity)
563 refute CommonAPI.thread_muted?(user, activity)
566 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
567 CommonAPI.add_mute(user, activity)
568 {:error, _} = CommonAPI.add_mute(user, activity)
572 describe "reports" do
573 test "creates a report" do
574 reporter = insert(:user)
575 target_user = insert(:user)
577 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
579 reporter_ap_id = reporter.ap_id
580 target_ap_id = target_user.ap_id
581 activity_ap_id = activity.data["id"]
585 account_id: target_user.id,
587 status_ids: [activity.id]
592 "id" => activity_ap_id,
593 "content" => "foobar",
594 "published" => activity.object.data["published"],
595 "actor" => AccountView.render("show.json", %{user: target_user})
598 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
601 actor: ^reporter_ap_id,
604 "content" => ^comment,
605 "object" => [^target_ap_id, ^note_obj],
611 test "updates report state" do
612 [reporter, target_user] = insert_pair(:user)
613 activity = insert(:note_activity, user: target_user)
615 {:ok, %Activity{id: report_id}} =
616 CommonAPI.report(reporter, %{
617 account_id: target_user.id,
618 comment: "I feel offended",
619 status_ids: [activity.id]
622 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
624 assert report.data["state"] == "resolved"
626 [reported_user, activity_id] = report.data["object"]
628 assert reported_user == target_user.ap_id
629 assert activity_id == activity.data["id"]
632 test "does not update report state when state is unsupported" do
633 [reporter, target_user] = insert_pair(:user)
634 activity = insert(:note_activity, user: target_user)
636 {:ok, %Activity{id: report_id}} =
637 CommonAPI.report(reporter, %{
638 account_id: target_user.id,
639 comment: "I feel offended",
640 status_ids: [activity.id]
643 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
646 test "updates state of multiple reports" do
647 [reporter, target_user] = insert_pair(:user)
648 activity = insert(:note_activity, user: target_user)
650 {:ok, %Activity{id: first_report_id}} =
651 CommonAPI.report(reporter, %{
652 account_id: target_user.id,
653 comment: "I feel offended",
654 status_ids: [activity.id]
657 {:ok, %Activity{id: second_report_id}} =
658 CommonAPI.report(reporter, %{
659 account_id: target_user.id,
660 comment: "I feel very offended!",
661 status_ids: [activity.id]
665 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
667 first_report = Activity.get_by_id(first_report_id)
668 second_report = Activity.get_by_id(second_report_id)
670 assert report_ids -- [first_report_id, second_report_id] == []
671 assert first_report.data["state"] == "resolved"
672 assert second_report.data["state"] == "resolved"
676 describe "reblog muting" do
678 muter = insert(:user)
680 muted = insert(:user)
682 [muter: muter, muted: muted]
685 test "add a reblog mute", %{muter: muter, muted: muted} do
686 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
688 assert User.showing_reblogs?(muter, muted) == false
691 test "remove a reblog mute", %{muter: muter, muted: muted} do
692 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
693 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
695 assert User.showing_reblogs?(muter, muted) == true
699 describe "unfollow/2" do
700 test "also unsubscribes a user" do
701 [follower, followed] = insert_pair(:user)
702 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
703 {:ok, _subscription} = User.subscribe(follower, followed)
705 assert User.subscribed_to?(follower, followed)
707 {:ok, follower} = CommonAPI.unfollow(follower, followed)
709 refute User.subscribed_to?(follower, followed)
712 test "cancels a pending follow for a local user" do
713 follower = insert(:user)
714 followed = insert(:user, locked: true)
716 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
717 CommonAPI.follow(follower, followed)
719 assert User.get_follow_state(follower, followed) == :follow_pending
720 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
721 assert User.get_follow_state(follower, followed) == nil
723 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
724 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
729 "object" => %{"type" => "Follow", "state" => "cancelled"}
731 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
734 test "cancels a pending follow for a remote user" do
735 follower = insert(:user)
736 followed = insert(:user, locked: true, local: false, ap_enabled: true)
738 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
739 CommonAPI.follow(follower, followed)
741 assert User.get_follow_state(follower, followed) == :follow_pending
742 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
743 assert User.get_follow_state(follower, followed) == nil
745 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
746 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
751 "object" => %{"type" => "Follow", "state" => "cancelled"}
753 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
757 describe "accept_follow_request/2" do
758 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
759 user = insert(:user, locked: true)
760 follower = insert(:user)
761 follower_two = insert(:user)
763 {:ok, follow_activity} = ActivityPub.follow(follower, user)
764 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
765 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
767 assert follow_activity.data["state"] == "pending"
768 assert follow_activity_two.data["state"] == "pending"
769 assert follow_activity_three.data["state"] == "pending"
771 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
773 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
774 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
775 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
778 test "after rejection, it sets all existing pending follow request states to 'reject'" do
779 user = insert(:user, locked: true)
780 follower = insert(:user)
781 follower_two = insert(:user)
783 {:ok, follow_activity} = ActivityPub.follow(follower, user)
784 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
785 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
787 assert follow_activity.data["state"] == "pending"
788 assert follow_activity_two.data["state"] == "pending"
789 assert follow_activity_three.data["state"] == "pending"
791 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
793 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
794 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
795 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
798 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
799 user = insert(:user, locked: true)
800 not_follower = insert(:user)
801 CommonAPI.accept_follow_request(not_follower, user)
803 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
808 test "does not allow to vote twice" do
810 other_user = insert(:user)
813 CommonAPI.post(user, %{
814 status: "Am I cute?",
815 poll: %{options: ["Yes", "No"], expires_in: 20}
818 object = Object.normalize(activity)
820 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
822 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
826 describe "listen/2" do
827 test "returns a valid activity" do
831 CommonAPI.listen(user, %{
832 "title" => "lain radio episode 1",
833 "album" => "lain radio",
838 object = Object.normalize(activity)
840 assert object.data["title"] == "lain radio episode 1"
842 assert Visibility.get_visibility(activity) == "public"
845 test "respects visibility=private" do
849 CommonAPI.listen(user, %{
850 "title" => "lain radio episode 1",
851 "album" => "lain radio",
854 "visibility" => "private"
857 object = Object.normalize(activity)
859 assert object.data["title"] == "lain radio episode 1"
861 assert Visibility.get_visibility(activity) == "private"