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 Object.normalize(post, false)
47 with_mock Pleroma.Web.Federator,
48 publish: fn _ -> nil end do
49 assert {:ok, delete} = CommonAPI.delete(post.id, user)
51 assert called(Pleroma.Web.Federator.publish(delete))
54 refute Activity.get_by_id(post.id)
57 test "it allows users to delete their posts" do
60 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
62 with_mock Pleroma.Web.Federator,
63 publish: fn _ -> nil end do
64 assert {:ok, delete} = CommonAPI.delete(post.id, user)
66 assert called(Pleroma.Web.Federator.publish(delete))
69 refute Activity.get_by_id(post.id)
72 test "it does not allow a user to delete their posts" do
74 other_user = insert(:user)
76 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
78 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
79 assert Activity.get_by_id(post.id)
82 test "it allows moderators to delete other user's posts" do
84 moderator = insert(:user, is_moderator: 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 "it allows admins to delete other user's posts" do
96 moderator = insert(:user, is_admin: true)
98 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
100 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
103 refute Activity.get_by_id(post.id)
106 test "superusers deleting non-local posts won't federate the delete" do
107 # This is the user of the ingested activity
111 ap_id: "http://mastodon.example.org/users/admin",
112 last_refreshed_at: NaiveDateTime.utc_now()
115 moderator = insert(:user, is_admin: true)
118 File.read!("test/fixtures/mastodon-post-activity.json")
121 {:ok, post} = Transmogrifier.handle_incoming(data)
123 with_mock Pleroma.Web.Federator,
124 publish: fn _ -> nil end do
125 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
127 refute called(Pleroma.Web.Federator.publish(:_))
130 refute Activity.get_by_id(post.id)
134 test "favoriting race condition" do
136 users_serial = insert_list(10, :user)
137 users = insert_list(10, :user)
139 {:ok, activity} = CommonAPI.post(user, %{status: "."})
142 |> Enum.map(fn user ->
143 CommonAPI.favorite(user, activity.id)
146 object = Object.get_by_ap_id(activity.data["object"])
147 assert object.data["like_count"] == 10
150 |> Enum.map(fn user ->
152 CommonAPI.favorite(user, activity.id)
155 |> Enum.map(&Task.await/1)
157 object = Object.get_by_ap_id(activity.data["object"])
158 assert object.data["like_count"] == 20
161 test "repeating race condition" do
163 users_serial = insert_list(10, :user)
164 users = insert_list(10, :user)
166 {:ok, activity} = CommonAPI.post(user, %{status: "."})
169 |> Enum.map(fn user ->
170 CommonAPI.repeat(activity.id, user)
173 object = Object.get_by_ap_id(activity.data["object"])
174 assert object.data["announcement_count"] == 10
177 |> Enum.map(fn user ->
179 CommonAPI.repeat(activity.id, user)
182 |> Enum.map(&Task.await/1)
184 object = Object.get_by_ap_id(activity.data["object"])
185 assert object.data["announcement_count"] == 20
188 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
190 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
192 [participation] = Participation.for_user(user)
195 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
197 assert Visibility.is_direct?(convo_reply)
199 assert activity.data["context"] == convo_reply.data["context"]
202 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
204 jafnhar = insert(:user)
205 tridi = insert(:user)
208 CommonAPI.post(har, %{
209 status: "@#{jafnhar.nickname} hey",
213 assert har.ap_id in activity.recipients
214 assert jafnhar.ap_id in activity.recipients
216 [participation] = Participation.for_user(har)
219 CommonAPI.post(har, %{
220 status: "I don't really like @#{tridi.nickname}",
221 visibility: "direct",
222 in_reply_to_status_id: activity.id,
223 in_reply_to_conversation_id: participation.id
226 assert har.ap_id in activity.recipients
227 assert jafnhar.ap_id in activity.recipients
228 refute tridi.ap_id in activity.recipients
231 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
233 jafnhar = insert(:user)
234 tridi = insert(:user)
236 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
239 CommonAPI.post(har, %{
240 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
244 refute tridi.ap_id in activity.recipients
245 assert jafnhar.ap_id in activity.recipients
248 test "it de-duplicates tags" do
250 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
252 object = Object.normalize(activity)
254 assert object.data["tag"] == ["2hu"]
257 test "it adds emoji in the object" do
259 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
261 assert Object.normalize(activity).data["emoji"]["firefox"]
264 describe "posting" do
265 test "it supports explicit addressing" do
267 user_two = insert(:user)
268 user_three = insert(:user)
269 user_four = insert(:user)
272 CommonAPI.post(user, %{
274 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
275 to: [user_two.nickname, user_four.nickname, "nonexistent"]
278 assert user.ap_id in activity.recipients
279 assert user_two.ap_id in activity.recipients
280 assert user_four.ap_id in activity.recipients
281 refute user_three.ap_id in activity.recipients
284 test "it filters out obviously bad tags when accepting a post as HTML" do
287 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
290 CommonAPI.post(user, %{
292 content_type: "text/html"
295 object = Object.normalize(activity)
297 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
300 test "it filters out obviously bad tags when accepting a post as Markdown" do
303 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
306 CommonAPI.post(user, %{
308 content_type: "text/markdown"
311 object = Object.normalize(activity)
313 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
316 test "it does not allow replies to direct messages that are not direct messages themselves" do
319 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
322 CommonAPI.post(user, %{
324 visibility: "direct",
325 in_reply_to_status_id: activity.id
328 Enum.each(["public", "private", "unlisted"], fn visibility ->
329 assert {:error, "The message visibility must be direct"} =
330 CommonAPI.post(user, %{
332 visibility: visibility,
333 in_reply_to_status_id: activity.id
338 test "it allows to address a list" do
340 {:ok, list} = Pleroma.List.create("foo", user)
342 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
344 assert activity.data["bcc"] == [list.ap_id]
345 assert activity.recipients == [list.ap_id, user.ap_id]
346 assert activity.data["listMessage"] == list.ap_id
349 test "it returns error when status is empty and no attachments" do
352 assert {:error, "Cannot post an empty status without attachments"} =
353 CommonAPI.post(user, %{status: ""})
356 test "it validates character limits are correctly enforced" do
357 Pleroma.Config.put([:instance, :limit], 5)
361 assert {:error, "The status is over the character limit"} =
362 CommonAPI.post(user, %{status: "foobar"})
364 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
367 test "it can handle activities that expire" do
371 NaiveDateTime.utc_now()
372 |> NaiveDateTime.truncate(:second)
373 |> NaiveDateTime.add(1_000_000, :second)
375 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
377 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
378 assert expiration.scheduled_at == expires_at
382 describe "reactions" do
383 test "reacting to a status with an emoji" do
385 other_user = insert(:user)
387 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
389 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
391 assert reaction.data["actor"] == user.ap_id
392 assert reaction.data["content"] == "👍"
394 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
396 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
399 test "unreacting to a status with an emoji" do
401 other_user = insert(:user)
403 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
404 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
406 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
408 assert unreaction.data["type"] == "Undo"
409 assert unreaction.data["object"] == reaction.data["id"]
410 assert unreaction.local
413 test "repeating a status" do
415 other_user = insert(:user)
417 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
419 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
420 assert Visibility.is_public?(announce_activity)
423 test "can't repeat a repeat" do
425 other_user = insert(:user)
426 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
428 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
430 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
433 test "repeating a status privately" do
435 other_user = insert(:user)
437 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
439 {:ok, %Activity{} = announce_activity} =
440 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
442 assert Visibility.is_private?(announce_activity)
445 test "favoriting a status" do
447 other_user = insert(:user)
449 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
451 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
452 assert data["type"] == "Like"
453 assert data["actor"] == user.ap_id
454 assert data["object"] == post_activity.data["object"]
457 test "retweeting a status twice returns the status" do
459 other_user = insert(:user)
461 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
462 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
463 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
466 test "favoriting a status twice returns ok, but without the like activity" do
468 other_user = insert(:user)
470 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
471 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
472 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
476 describe "pinned statuses" do
478 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
481 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
483 [user: user, activity: activity]
486 test "pin status", %{user: user, activity: activity} do
487 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
490 user = refresh_record(user)
492 assert %User{pinned_activities: [^id]} = user
495 test "pin poll", %{user: user} do
497 CommonAPI.post(user, %{
498 status: "How is fediverse today?",
499 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
502 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
505 user = refresh_record(user)
507 assert %User{pinned_activities: [^id]} = user
510 test "unlisted statuses can be pinned", %{user: user} do
511 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
512 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
515 test "only self-authored can be pinned", %{activity: activity} do
518 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
521 test "max pinned statuses", %{user: user, activity: activity_one} do
522 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
524 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
526 user = refresh_record(user)
528 assert {:error, "You have already pinned the maximum number of statuses"} =
529 CommonAPI.pin(activity_two.id, user)
532 test "unpin status", %{user: user, activity: activity} do
533 {:ok, activity} = CommonAPI.pin(activity.id, user)
535 user = refresh_record(user)
539 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
541 user = refresh_record(user)
543 assert %User{pinned_activities: []} = user
546 test "should unpin when deleting a status", %{user: user, activity: activity} do
547 {:ok, activity} = CommonAPI.pin(activity.id, user)
549 user = refresh_record(user)
551 assert {:ok, _} = CommonAPI.delete(activity.id, user)
553 user = refresh_record(user)
555 assert %User{pinned_activities: []} = user
559 describe "mute tests" do
563 activity = insert(:note_activity)
565 [user: user, activity: activity]
568 test "add mute", %{user: user, activity: activity} do
569 {:ok, _} = CommonAPI.add_mute(user, activity)
570 assert CommonAPI.thread_muted?(user, activity)
573 test "remove mute", %{user: user, activity: activity} do
574 CommonAPI.add_mute(user, activity)
575 {:ok, _} = CommonAPI.remove_mute(user, activity)
576 refute CommonAPI.thread_muted?(user, activity)
579 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
580 CommonAPI.add_mute(user, activity)
581 {:error, _} = CommonAPI.add_mute(user, activity)
585 describe "reports" do
586 test "creates a report" do
587 reporter = insert(:user)
588 target_user = insert(:user)
590 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
592 reporter_ap_id = reporter.ap_id
593 target_ap_id = target_user.ap_id
594 activity_ap_id = activity.data["id"]
598 account_id: target_user.id,
600 status_ids: [activity.id]
605 "id" => activity_ap_id,
606 "content" => "foobar",
607 "published" => activity.object.data["published"],
608 "actor" => AccountView.render("show.json", %{user: target_user})
611 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
614 actor: ^reporter_ap_id,
617 "content" => ^comment,
618 "object" => [^target_ap_id, ^note_obj],
624 test "updates report state" do
625 [reporter, target_user] = insert_pair(:user)
626 activity = insert(:note_activity, user: target_user)
628 {:ok, %Activity{id: report_id}} =
629 CommonAPI.report(reporter, %{
630 account_id: target_user.id,
631 comment: "I feel offended",
632 status_ids: [activity.id]
635 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
637 assert report.data["state"] == "resolved"
639 [reported_user, activity_id] = report.data["object"]
641 assert reported_user == target_user.ap_id
642 assert activity_id == activity.data["id"]
645 test "does not update report state when state is unsupported" do
646 [reporter, target_user] = insert_pair(:user)
647 activity = insert(:note_activity, user: target_user)
649 {:ok, %Activity{id: report_id}} =
650 CommonAPI.report(reporter, %{
651 account_id: target_user.id,
652 comment: "I feel offended",
653 status_ids: [activity.id]
656 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
659 test "updates state of multiple reports" do
660 [reporter, target_user] = insert_pair(:user)
661 activity = insert(:note_activity, user: target_user)
663 {:ok, %Activity{id: first_report_id}} =
664 CommonAPI.report(reporter, %{
665 account_id: target_user.id,
666 comment: "I feel offended",
667 status_ids: [activity.id]
670 {:ok, %Activity{id: second_report_id}} =
671 CommonAPI.report(reporter, %{
672 account_id: target_user.id,
673 comment: "I feel very offended!",
674 status_ids: [activity.id]
678 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
680 first_report = Activity.get_by_id(first_report_id)
681 second_report = Activity.get_by_id(second_report_id)
683 assert report_ids -- [first_report_id, second_report_id] == []
684 assert first_report.data["state"] == "resolved"
685 assert second_report.data["state"] == "resolved"
689 describe "reblog muting" do
691 muter = insert(:user)
693 muted = insert(:user)
695 [muter: muter, muted: muted]
698 test "add a reblog mute", %{muter: muter, muted: muted} do
699 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
701 assert User.showing_reblogs?(muter, muted) == false
704 test "remove a reblog mute", %{muter: muter, muted: muted} do
705 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
706 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
708 assert User.showing_reblogs?(muter, muted) == true
712 describe "unfollow/2" do
713 test "also unsubscribes a user" do
714 [follower, followed] = insert_pair(:user)
715 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
716 {:ok, _subscription} = User.subscribe(follower, followed)
718 assert User.subscribed_to?(follower, followed)
720 {:ok, follower} = CommonAPI.unfollow(follower, followed)
722 refute User.subscribed_to?(follower, followed)
725 test "cancels a pending follow for a local user" do
726 follower = insert(:user)
727 followed = insert(:user, locked: true)
729 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
730 CommonAPI.follow(follower, followed)
732 assert User.get_follow_state(follower, followed) == :follow_pending
733 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
734 assert User.get_follow_state(follower, followed) == nil
736 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
737 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
742 "object" => %{"type" => "Follow", "state" => "cancelled"}
744 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
747 test "cancels a pending follow for a remote user" do
748 follower = insert(:user)
749 followed = insert(:user, locked: true, local: false, ap_enabled: true)
751 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
752 CommonAPI.follow(follower, followed)
754 assert User.get_follow_state(follower, followed) == :follow_pending
755 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
756 assert User.get_follow_state(follower, followed) == nil
758 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
759 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
764 "object" => %{"type" => "Follow", "state" => "cancelled"}
766 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
770 describe "accept_follow_request/2" do
771 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
772 user = insert(:user, locked: true)
773 follower = insert(:user)
774 follower_two = insert(:user)
776 {:ok, follow_activity} = ActivityPub.follow(follower, user)
777 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
778 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
780 assert follow_activity.data["state"] == "pending"
781 assert follow_activity_two.data["state"] == "pending"
782 assert follow_activity_three.data["state"] == "pending"
784 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
786 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
787 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
788 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
791 test "after rejection, it sets all existing pending follow request states to 'reject'" do
792 user = insert(:user, locked: true)
793 follower = insert(:user)
794 follower_two = insert(:user)
796 {:ok, follow_activity} = ActivityPub.follow(follower, user)
797 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
798 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
800 assert follow_activity.data["state"] == "pending"
801 assert follow_activity_two.data["state"] == "pending"
802 assert follow_activity_three.data["state"] == "pending"
804 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
806 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
807 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
808 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
811 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
812 user = insert(:user, locked: true)
813 not_follower = insert(:user)
814 CommonAPI.accept_follow_request(not_follower, user)
816 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
821 test "does not allow to vote twice" do
823 other_user = insert(:user)
826 CommonAPI.post(user, %{
827 status: "Am I cute?",
828 poll: %{options: ["Yes", "No"], expires_in: 20}
831 object = Object.normalize(activity)
833 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
835 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
839 describe "listen/2" do
840 test "returns a valid activity" do
844 CommonAPI.listen(user, %{
845 title: "lain radio episode 1",
851 object = Object.normalize(activity)
853 assert object.data["title"] == "lain radio episode 1"
855 assert Visibility.get_visibility(activity) == "public"
858 test "respects visibility=private" do
862 CommonAPI.listen(user, %{
863 title: "lain radio episode 1",
867 visibility: "private"
870 object = Object.normalize(activity)
872 assert object.data["title"] == "lain radio episode 1"
874 assert Visibility.get_visibility(activity) == "private"