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
7 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Conversation.Participation
12 alias Pleroma.Notification
16 alias Pleroma.Web.ActivityPub.ActivityPub
17 alias Pleroma.Web.ActivityPub.Transmogrifier
18 alias Pleroma.Web.ActivityPub.Visibility
19 alias Pleroma.Web.AdminAPI.AccountView
20 alias Pleroma.Web.CommonAPI
22 import Pleroma.Factory
24 import Ecto.Query, only: [from: 2]
26 require Pleroma.Constants
28 setup do: clear_config([:instance, :safe_dm_mentions])
29 setup do: clear_config([:instance, :limit])
30 setup do: clear_config([:instance, :max_pinned_statuses])
32 describe "blocking" do
34 blocker = insert(:user)
35 blocked = insert(:user)
36 User.follow(blocker, blocked)
37 User.follow(blocked, blocker)
38 %{blocker: blocker, blocked: blocked}
41 test "it blocks and federates", %{blocker: blocker, blocked: blocked} do
42 clear_config([:instance, :federating], true)
44 with_mock Pleroma.Web.Federator,
45 publish: fn _ -> nil end do
46 assert {:ok, block} = CommonAPI.block(blocker, blocked)
49 assert User.blocks?(blocker, blocked)
50 refute User.following?(blocker, blocked)
51 refute User.following?(blocked, blocker)
53 assert called(Pleroma.Web.Federator.publish(block))
57 test "it blocks and does not federate if outgoing blocks are disabled", %{
61 clear_config([:instance, :federating], true)
62 clear_config([:activitypub, :outgoing_blocks], false)
64 with_mock Pleroma.Web.Federator,
65 publish: fn _ -> nil end do
66 assert {:ok, block} = CommonAPI.block(blocker, blocked)
69 assert User.blocks?(blocker, blocked)
70 refute User.following?(blocker, blocked)
71 refute User.following?(blocked, blocker)
73 refute called(Pleroma.Web.Federator.publish(block))
78 describe "posting chat messages" do
79 setup do: clear_config([:instance, :chat_limit])
81 test "it posts a chat message without content but with an attachment" do
82 author = insert(:user)
83 recipient = insert(:user)
86 content_type: "image/jpg",
87 path: Path.absname("test/fixtures/image.jpg"),
88 filename: "an_image.jpg"
91 {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id)
107 send: fn _ -> nil end
112 CommonAPI.post_chat_message(
120 Notification.for_user_and_activity(recipient, activity)
121 |> Repo.preload(:activity)
123 assert called(Pleroma.Web.Push.send(notification))
124 assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
125 assert called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
131 test "it adds html newlines" do
132 author = insert(:user)
133 recipient = insert(:user)
135 other_user = insert(:user)
138 CommonAPI.post_chat_message(
144 assert other_user.ap_id not in activity.recipients
146 object = Object.normalize(activity, false)
148 assert object.data["content"] == "uguu<br/>uguuu"
151 test "it linkifies" do
152 author = insert(:user)
153 recipient = insert(:user)
155 other_user = insert(:user)
158 CommonAPI.post_chat_message(
161 "https://example.org is the site of @#{other_user.nickname} #2hu"
164 assert other_user.ap_id not in activity.recipients
166 object = Object.normalize(activity, false)
168 assert object.data["content"] ==
169 "<a href=\"https://example.org\" rel=\"ugc\">https://example.org</a> is the site of <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{
171 }\" href=\"#{other_user.ap_id}\" rel=\"ugc\">@<span>#{other_user.nickname}</span></a></span> <a class=\"hashtag\" data-tag=\"2hu\" href=\"http://localhost:4001/tag/2hu\">#2hu</a>"
174 test "it posts a chat message" do
175 author = insert(:user)
176 recipient = insert(:user)
179 CommonAPI.post_chat_message(
182 "a test message <script>alert('uuu')</script> :firefox:"
185 assert activity.data["type"] == "Create"
186 assert activity.local
187 object = Object.normalize(activity)
189 assert object.data["type"] == "ChatMessage"
190 assert object.data["to"] == [recipient.ap_id]
192 assert object.data["content"] ==
193 "a test message <script>alert('uuu')</script> :firefox:"
195 assert object.data["emoji"] == %{
196 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
199 assert Chat.get(author.id, recipient.ap_id)
200 assert Chat.get(recipient.id, author.ap_id)
202 assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
205 test "it reject messages over the local limit" do
206 Pleroma.Config.put([:instance, :chat_limit], 2)
208 author = insert(:user)
209 recipient = insert(:user)
212 CommonAPI.post_chat_message(
218 assert message == :content_too_long
222 describe "unblocking" do
223 test "it works even without an existing block activity" do
224 blocked = insert(:user)
225 blocker = insert(:user)
226 User.block(blocker, blocked)
228 assert User.blocks?(blocker, blocked)
229 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
230 refute User.blocks?(blocker, blocked)
234 describe "deletion" do
235 test "it works with pruned objects" do
238 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
240 clear_config([:instance, :federating], true)
242 Object.normalize(post, false)
245 with_mock Pleroma.Web.Federator,
246 publish: fn _ -> nil end do
247 assert {:ok, delete} = CommonAPI.delete(post.id, user)
249 assert called(Pleroma.Web.Federator.publish(delete))
252 refute Activity.get_by_id(post.id)
255 test "it allows users to delete their posts" do
258 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
260 clear_config([:instance, :federating], true)
262 with_mock Pleroma.Web.Federator,
263 publish: fn _ -> nil end do
264 assert {:ok, delete} = CommonAPI.delete(post.id, user)
266 assert called(Pleroma.Web.Federator.publish(delete))
269 refute Activity.get_by_id(post.id)
272 test "it does not allow a user to delete their posts" do
274 other_user = insert(:user)
276 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
278 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
279 assert Activity.get_by_id(post.id)
282 test "it allows moderators to delete other user's posts" do
284 moderator = insert(:user, is_moderator: true)
286 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
288 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
291 refute Activity.get_by_id(post.id)
294 test "it allows admins to delete other user's posts" do
296 moderator = insert(:user, is_admin: true)
298 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
300 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
303 refute Activity.get_by_id(post.id)
306 test "superusers deleting non-local posts won't federate the delete" do
307 # This is the user of the ingested activity
311 ap_id: "http://mastodon.example.org/users/admin",
312 last_refreshed_at: NaiveDateTime.utc_now()
315 moderator = insert(:user, is_admin: true)
318 File.read!("test/fixtures/mastodon-post-activity.json")
321 {:ok, post} = Transmogrifier.handle_incoming(data)
323 with_mock Pleroma.Web.Federator,
324 publish: fn _ -> nil end do
325 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
327 refute called(Pleroma.Web.Federator.publish(:_))
330 refute Activity.get_by_id(post.id)
334 test "favoriting race condition" do
336 users_serial = insert_list(10, :user)
337 users = insert_list(10, :user)
339 {:ok, activity} = CommonAPI.post(user, %{status: "."})
342 |> Enum.map(fn user ->
343 CommonAPI.favorite(user, activity.id)
346 object = Object.get_by_ap_id(activity.data["object"])
347 assert object.data["like_count"] == 10
350 |> Enum.map(fn user ->
352 CommonAPI.favorite(user, activity.id)
355 |> Enum.map(&Task.await/1)
357 object = Object.get_by_ap_id(activity.data["object"])
358 assert object.data["like_count"] == 20
361 test "repeating race condition" do
363 users_serial = insert_list(10, :user)
364 users = insert_list(10, :user)
366 {:ok, activity} = CommonAPI.post(user, %{status: "."})
369 |> Enum.map(fn user ->
370 CommonAPI.repeat(activity.id, user)
373 object = Object.get_by_ap_id(activity.data["object"])
374 assert object.data["announcement_count"] == 10
377 |> Enum.map(fn user ->
379 CommonAPI.repeat(activity.id, user)
382 |> Enum.map(&Task.await/1)
384 object = Object.get_by_ap_id(activity.data["object"])
385 assert object.data["announcement_count"] == 20
388 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
390 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
392 [participation] = Participation.for_user(user)
395 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
397 assert Visibility.is_direct?(convo_reply)
399 assert activity.data["context"] == convo_reply.data["context"]
402 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
404 jafnhar = insert(:user)
405 tridi = insert(:user)
408 CommonAPI.post(har, %{
409 status: "@#{jafnhar.nickname} hey",
413 assert har.ap_id in activity.recipients
414 assert jafnhar.ap_id in activity.recipients
416 [participation] = Participation.for_user(har)
419 CommonAPI.post(har, %{
420 status: "I don't really like @#{tridi.nickname}",
421 visibility: "direct",
422 in_reply_to_status_id: activity.id,
423 in_reply_to_conversation_id: participation.id
426 assert har.ap_id in activity.recipients
427 assert jafnhar.ap_id in activity.recipients
428 refute tridi.ap_id in activity.recipients
431 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
433 jafnhar = insert(:user)
434 tridi = insert(:user)
436 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
439 CommonAPI.post(har, %{
440 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
444 refute tridi.ap_id in activity.recipients
445 assert jafnhar.ap_id in activity.recipients
448 test "it de-duplicates tags" do
450 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
452 object = Object.normalize(activity)
454 assert object.data["tag"] == ["2hu"]
457 test "it adds emoji in the object" do
459 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
461 assert Object.normalize(activity).data["emoji"]["firefox"]
464 describe "posting" do
465 test "deactivated users can't post" do
466 user = insert(:user, deactivated: true)
467 assert {:error, _} = CommonAPI.post(user, %{status: "ye"})
470 test "it supports explicit addressing" do
472 user_two = insert(:user)
473 user_three = insert(:user)
474 user_four = insert(:user)
477 CommonAPI.post(user, %{
479 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
480 to: [user_two.nickname, user_four.nickname, "nonexistent"]
483 assert user.ap_id in activity.recipients
484 assert user_two.ap_id in activity.recipients
485 assert user_four.ap_id in activity.recipients
486 refute user_three.ap_id in activity.recipients
489 test "it filters out obviously bad tags when accepting a post as HTML" do
492 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
495 CommonAPI.post(user, %{
497 content_type: "text/html"
500 object = Object.normalize(activity)
502 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
503 assert object.data["source"] == post
506 test "it filters out obviously bad tags when accepting a post as Markdown" do
509 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
512 CommonAPI.post(user, %{
514 content_type: "text/markdown"
517 object = Object.normalize(activity)
519 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
520 assert object.data["source"] == post
523 test "it does not allow replies to direct messages that are not direct messages themselves" do
526 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
529 CommonAPI.post(user, %{
531 visibility: "direct",
532 in_reply_to_status_id: activity.id
535 Enum.each(["public", "private", "unlisted"], fn visibility ->
536 assert {:error, "The message visibility must be direct"} =
537 CommonAPI.post(user, %{
539 visibility: visibility,
540 in_reply_to_status_id: activity.id
545 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
547 other_user = insert(:user)
548 third_user = insert(:user)
550 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
553 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
555 # The OP is implicitly added
556 assert user.ap_id in open_answer.recipients
558 {:ok, secret_answer} =
559 CommonAPI.post(other_user, %{
560 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
561 in_reply_to_status_id: post.id,
565 assert third_user.ap_id in secret_answer.recipients
567 # The OP is not added
568 refute user.ap_id in secret_answer.recipients
571 test "it allows to address a list" do
573 {:ok, list} = Pleroma.List.create("foo", user)
575 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
577 assert activity.data["bcc"] == [list.ap_id]
578 assert activity.recipients == [list.ap_id, user.ap_id]
579 assert activity.data["listMessage"] == list.ap_id
582 test "it returns error when status is empty and no attachments" do
585 assert {:error, "Cannot post an empty status without attachments"} =
586 CommonAPI.post(user, %{status: ""})
589 test "it validates character limits are correctly enforced" do
590 Pleroma.Config.put([:instance, :limit], 5)
594 assert {:error, "The status is over the character limit"} =
595 CommonAPI.post(user, %{status: "foobar"})
597 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
600 test "it can handle activities that expire" do
603 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
605 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
608 worker: Pleroma.Workers.PurgeExpiredActivity,
609 args: %{activity_id: activity.id},
610 scheduled_at: expires_at
615 describe "reactions" do
616 test "reacting to a status with an emoji" do
618 other_user = insert(:user)
620 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
622 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
624 assert reaction.data["actor"] == user.ap_id
625 assert reaction.data["content"] == "👍"
627 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
629 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
632 test "unreacting to a status with an emoji" do
634 other_user = insert(:user)
636 clear_config([:instance, :federating], true)
638 with_mock Pleroma.Web.Federator,
639 publish: fn _ -> nil end do
640 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
641 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
643 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
645 assert unreaction.data["type"] == "Undo"
646 assert unreaction.data["object"] == reaction.data["id"]
647 assert unreaction.local
649 # On federation, it contains the undone (and deleted) object
650 unreaction_with_object = %{
652 | data: Map.put(unreaction.data, "object", reaction.data)
655 assert called(Pleroma.Web.Federator.publish(unreaction_with_object))
659 test "repeating a status" do
661 other_user = insert(:user)
663 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
665 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
666 assert Visibility.is_public?(announce_activity)
669 test "can't repeat a repeat" do
671 other_user = insert(:user)
672 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
674 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
676 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
679 test "repeating a status privately" do
681 other_user = insert(:user)
683 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
685 {:ok, %Activity{} = announce_activity} =
686 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
688 assert Visibility.is_private?(announce_activity)
689 refute Visibility.visible_for_user?(announce_activity, nil)
692 test "favoriting a status" do
694 other_user = insert(:user)
696 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
698 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
699 assert data["type"] == "Like"
700 assert data["actor"] == user.ap_id
701 assert data["object"] == post_activity.data["object"]
704 test "retweeting a status twice returns the status" do
706 other_user = insert(:user)
708 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
709 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
710 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
713 test "favoriting a status twice returns ok, but without the like activity" do
715 other_user = insert(:user)
717 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
718 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
719 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
723 describe "pinned statuses" do
725 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
728 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
730 [user: user, activity: activity]
733 test "pin status", %{user: user, activity: activity} do
734 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
737 user = refresh_record(user)
739 assert %User{pinned_activities: [^id]} = user
742 test "pin poll", %{user: user} do
744 CommonAPI.post(user, %{
745 status: "How is fediverse today?",
746 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
749 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
752 user = refresh_record(user)
754 assert %User{pinned_activities: [^id]} = user
757 test "unlisted statuses can be pinned", %{user: user} do
758 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
759 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
762 test "only self-authored can be pinned", %{activity: activity} do
765 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
768 test "max pinned statuses", %{user: user, activity: activity_one} do
769 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
771 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
773 user = refresh_record(user)
775 assert {:error, "You have already pinned the maximum number of statuses"} =
776 CommonAPI.pin(activity_two.id, user)
779 test "unpin status", %{user: user, activity: activity} do
780 {:ok, activity} = CommonAPI.pin(activity.id, user)
782 user = refresh_record(user)
786 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
788 user = refresh_record(user)
790 assert %User{pinned_activities: []} = user
793 test "should unpin when deleting a status", %{user: user, activity: activity} do
794 {:ok, activity} = CommonAPI.pin(activity.id, user)
796 user = refresh_record(user)
798 assert {:ok, _} = CommonAPI.delete(activity.id, user)
800 user = refresh_record(user)
802 assert %User{pinned_activities: []} = user
806 describe "mute tests" do
810 activity = insert(:note_activity)
812 [user: user, activity: activity]
815 test "marks notifications as read after mute" do
816 author = insert(:user)
817 activity = insert(:note_activity, user: author)
819 friend1 = insert(:user)
820 friend2 = insert(:user)
822 {:ok, reply_activity} =
826 status: "@#{author.nickname} @#{friend1.nickname} test reply",
827 in_reply_to_status_id: activity.id
831 {:ok, favorite_activity} = CommonAPI.favorite(friend2, activity.id)
832 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, friend1)
834 assert Repo.aggregate(
835 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
839 unread_notifications =
840 Repo.all(from(n in Notification, where: n.seen == false, where: n.user_id == ^author.id))
842 assert Enum.any?(unread_notifications, fn n ->
843 n.type == "favourite" && n.activity_id == favorite_activity.id
846 assert Enum.any?(unread_notifications, fn n ->
847 n.type == "reblog" && n.activity_id == repeat_activity.id
850 assert Enum.any?(unread_notifications, fn n ->
851 n.type == "mention" && n.activity_id == reply_activity.id
854 {:ok, _} = CommonAPI.add_mute(author, activity)
855 assert CommonAPI.thread_muted?(author, activity)
857 assert Repo.aggregate(
858 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
863 Repo.all(from(n in Notification, where: n.seen == true, where: n.user_id == ^author.id))
865 assert Enum.any?(read_notifications, fn n ->
866 n.type == "favourite" && n.activity_id == favorite_activity.id
869 assert Enum.any?(read_notifications, fn n ->
870 n.type == "reblog" && n.activity_id == repeat_activity.id
873 assert Enum.any?(read_notifications, fn n ->
874 n.type == "mention" && n.activity_id == reply_activity.id
878 test "add mute", %{user: user, activity: activity} do
879 {:ok, _} = CommonAPI.add_mute(user, activity)
880 assert CommonAPI.thread_muted?(user, activity)
883 test "remove mute", %{user: user, activity: activity} do
884 CommonAPI.add_mute(user, activity)
885 {:ok, _} = CommonAPI.remove_mute(user, activity)
886 refute CommonAPI.thread_muted?(user, activity)
889 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
890 CommonAPI.add_mute(user, activity)
891 {:error, _} = CommonAPI.add_mute(user, activity)
895 describe "reports" do
896 test "creates a report" do
897 reporter = insert(:user)
898 target_user = insert(:user)
900 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
902 reporter_ap_id = reporter.ap_id
903 target_ap_id = target_user.ap_id
904 activity_ap_id = activity.data["id"]
908 account_id: target_user.id,
910 status_ids: [activity.id]
915 "id" => activity_ap_id,
916 "content" => "foobar",
917 "published" => activity.object.data["published"],
918 "actor" => AccountView.render("show.json", %{user: target_user})
921 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
924 actor: ^reporter_ap_id,
927 "content" => ^comment,
928 "object" => [^target_ap_id, ^note_obj],
934 test "updates report state" do
935 [reporter, target_user] = insert_pair(:user)
936 activity = insert(:note_activity, user: target_user)
938 {:ok, %Activity{id: report_id}} =
939 CommonAPI.report(reporter, %{
940 account_id: target_user.id,
941 comment: "I feel offended",
942 status_ids: [activity.id]
945 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
947 assert report.data["state"] == "resolved"
949 [reported_user, activity_id] = report.data["object"]
951 assert reported_user == target_user.ap_id
952 assert activity_id == activity.data["id"]
955 test "does not update report state when state is unsupported" do
956 [reporter, target_user] = insert_pair(:user)
957 activity = insert(:note_activity, user: target_user)
959 {:ok, %Activity{id: report_id}} =
960 CommonAPI.report(reporter, %{
961 account_id: target_user.id,
962 comment: "I feel offended",
963 status_ids: [activity.id]
966 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
969 test "updates state of multiple reports" do
970 [reporter, target_user] = insert_pair(:user)
971 activity = insert(:note_activity, user: target_user)
973 {:ok, %Activity{id: first_report_id}} =
974 CommonAPI.report(reporter, %{
975 account_id: target_user.id,
976 comment: "I feel offended",
977 status_ids: [activity.id]
980 {:ok, %Activity{id: second_report_id}} =
981 CommonAPI.report(reporter, %{
982 account_id: target_user.id,
983 comment: "I feel very offended!",
984 status_ids: [activity.id]
988 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
990 first_report = Activity.get_by_id(first_report_id)
991 second_report = Activity.get_by_id(second_report_id)
993 assert report_ids -- [first_report_id, second_report_id] == []
994 assert first_report.data["state"] == "resolved"
995 assert second_report.data["state"] == "resolved"
999 describe "reblog muting" do
1001 muter = insert(:user)
1003 muted = insert(:user)
1005 [muter: muter, muted: muted]
1008 test "add a reblog mute", %{muter: muter, muted: muted} do
1009 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1011 assert User.showing_reblogs?(muter, muted) == false
1014 test "remove a reblog mute", %{muter: muter, muted: muted} do
1015 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1016 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
1018 assert User.showing_reblogs?(muter, muted) == true
1022 describe "follow/2" do
1023 test "directly follows a non-locked local user" do
1024 [follower, followed] = insert_pair(:user)
1025 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1027 assert User.following?(follower, followed)
1031 describe "unfollow/2" do
1032 test "also unsubscribes a user" do
1033 [follower, followed] = insert_pair(:user)
1034 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1035 {:ok, _subscription} = User.subscribe(follower, followed)
1037 assert User.subscribed_to?(follower, followed)
1039 {:ok, follower} = CommonAPI.unfollow(follower, followed)
1041 refute User.subscribed_to?(follower, followed)
1044 test "cancels a pending follow for a local user" do
1045 follower = insert(:user)
1046 followed = insert(:user, locked: true)
1048 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1049 CommonAPI.follow(follower, followed)
1051 assert User.get_follow_state(follower, followed) == :follow_pending
1052 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1053 assert User.get_follow_state(follower, followed) == nil
1055 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1056 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1061 "object" => %{"type" => "Follow", "state" => "cancelled"}
1063 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1066 test "cancels a pending follow for a remote user" do
1067 follower = insert(:user)
1068 followed = insert(:user, locked: true, local: false, ap_enabled: true)
1070 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1071 CommonAPI.follow(follower, followed)
1073 assert User.get_follow_state(follower, followed) == :follow_pending
1074 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1075 assert User.get_follow_state(follower, followed) == nil
1077 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1078 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1083 "object" => %{"type" => "Follow", "state" => "cancelled"}
1085 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1089 describe "accept_follow_request/2" do
1090 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
1091 user = insert(:user, locked: true)
1092 follower = insert(:user)
1093 follower_two = insert(:user)
1095 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1096 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1097 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1099 assert follow_activity.data["state"] == "pending"
1100 assert follow_activity_two.data["state"] == "pending"
1101 assert follow_activity_three.data["state"] == "pending"
1103 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
1105 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
1106 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
1107 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1110 test "after rejection, it sets all existing pending follow request states to 'reject'" do
1111 user = insert(:user, locked: true)
1112 follower = insert(:user)
1113 follower_two = insert(:user)
1115 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1116 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1117 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1119 assert follow_activity.data["state"] == "pending"
1120 assert follow_activity_two.data["state"] == "pending"
1121 assert follow_activity_three.data["state"] == "pending"
1123 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
1125 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
1126 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
1127 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1130 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
1131 user = insert(:user, locked: true)
1132 not_follower = insert(:user)
1133 CommonAPI.accept_follow_request(not_follower, user)
1135 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
1139 describe "vote/3" do
1140 test "does not allow to vote twice" do
1141 user = insert(:user)
1142 other_user = insert(:user)
1145 CommonAPI.post(user, %{
1146 status: "Am I cute?",
1147 poll: %{options: ["Yes", "No"], expires_in: 20}
1150 object = Object.normalize(activity)
1152 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1154 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1158 describe "listen/2" do
1159 test "returns a valid activity" do
1160 user = insert(:user)
1163 CommonAPI.listen(user, %{
1164 title: "lain radio episode 1",
1165 album: "lain radio",
1170 object = Object.normalize(activity)
1172 assert object.data["title"] == "lain radio episode 1"
1174 assert Visibility.get_visibility(activity) == "public"
1177 test "respects visibility=private" do
1178 user = insert(:user)
1181 CommonAPI.listen(user, %{
1182 title: "lain radio episode 1",
1183 album: "lain radio",
1186 visibility: "private"
1189 object = Object.normalize(activity)
1191 assert object.data["title"] == "lain radio episode 1"
1193 assert Visibility.get_visibility(activity) == "private"
1197 describe "get_user/1" do
1198 test "gets user by ap_id" do
1199 user = insert(:user)
1200 assert CommonAPI.get_user(user.ap_id) == user
1203 test "gets user by guessed nickname" do
1204 user = insert(:user, ap_id: "", nickname: "mario@mushroom.kingdom")
1205 assert CommonAPI.get_user("https://mushroom.kingdom/users/mario") == user
1212 nickname: "erroruser@example.com"
1213 } = CommonAPI.get_user("")