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 "posting polls" do
33 test "it posts a poll" do
37 CommonAPI.post(user, %{
38 status: "who is the best",
39 poll: %{expires_in: 600, options: ["reimu", "marisa"]}
42 object = Object.normalize(activity)
44 assert object.data["type"] == "Question"
45 assert object.data["oneOf"] |> length() == 2
49 describe "blocking" do
51 blocker = insert(:user)
52 blocked = insert(:user)
53 User.follow(blocker, blocked)
54 User.follow(blocked, blocker)
55 %{blocker: blocker, blocked: blocked}
58 test "it blocks and federates", %{blocker: blocker, blocked: blocked} do
59 clear_config([:instance, :federating], true)
61 with_mock Pleroma.Web.Federator,
62 publish: fn _ -> nil end do
63 assert {:ok, block} = CommonAPI.block(blocker, blocked)
66 assert User.blocks?(blocker, blocked)
67 refute User.following?(blocker, blocked)
68 refute User.following?(blocked, blocker)
70 assert called(Pleroma.Web.Federator.publish(block))
74 test "it blocks and does not federate if outgoing blocks are disabled", %{
78 clear_config([:instance, :federating], true)
79 clear_config([:activitypub, :outgoing_blocks], false)
81 with_mock Pleroma.Web.Federator,
82 publish: fn _ -> nil end do
83 assert {:ok, block} = CommonAPI.block(blocker, blocked)
86 assert User.blocks?(blocker, blocked)
87 refute User.following?(blocker, blocked)
88 refute User.following?(blocked, blocker)
90 refute called(Pleroma.Web.Federator.publish(block))
95 describe "posting chat messages" do
96 setup do: clear_config([:instance, :chat_limit])
98 test "it posts a chat message without content but with an attachment" do
99 author = insert(:user)
100 recipient = insert(:user)
103 content_type: "image/jpg",
104 path: Path.absname("test/fixtures/image.jpg"),
105 filename: "an_image.jpg"
108 {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id)
112 Pleroma.Web.Streamer,
124 send: fn _ -> nil end
129 CommonAPI.post_chat_message(
137 Notification.for_user_and_activity(recipient, activity)
138 |> Repo.preload(:activity)
140 assert called(Pleroma.Web.Push.send(notification))
141 assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
142 assert called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
148 test "it adds html newlines" do
149 author = insert(:user)
150 recipient = insert(:user)
152 other_user = insert(:user)
155 CommonAPI.post_chat_message(
161 assert other_user.ap_id not in activity.recipients
163 object = Object.normalize(activity, false)
165 assert object.data["content"] == "uguu<br/>uguuu"
168 test "it linkifies" do
169 author = insert(:user)
170 recipient = insert(:user)
172 other_user = insert(:user)
175 CommonAPI.post_chat_message(
178 "https://example.org is the site of @#{other_user.nickname} #2hu"
181 assert other_user.ap_id not in activity.recipients
183 object = Object.normalize(activity, false)
185 assert object.data["content"] ==
186 "<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=\"#{
188 }\" 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>"
191 test "it posts a chat message" do
192 author = insert(:user)
193 recipient = insert(:user)
196 CommonAPI.post_chat_message(
199 "a test message <script>alert('uuu')</script> :firefox:"
202 assert activity.data["type"] == "Create"
203 assert activity.local
204 object = Object.normalize(activity)
206 assert object.data["type"] == "ChatMessage"
207 assert object.data["to"] == [recipient.ap_id]
209 assert object.data["content"] ==
210 "a test message <script>alert('uuu')</script> :firefox:"
212 assert object.data["emoji"] == %{
213 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
216 assert Chat.get(author.id, recipient.ap_id)
217 assert Chat.get(recipient.id, author.ap_id)
219 assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
222 test "it reject messages over the local limit" do
223 Pleroma.Config.put([:instance, :chat_limit], 2)
225 author = insert(:user)
226 recipient = insert(:user)
229 CommonAPI.post_chat_message(
235 assert message == :content_too_long
238 test "it reject messages via MRF" do
239 clear_config([:mrf_keyword, :reject], ["GNO"])
240 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
242 author = insert(:user)
243 recipient = insert(:user)
245 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
246 CommonAPI.post_chat_message(author, recipient, "GNO/Linux")
250 describe "unblocking" do
251 test "it works even without an existing block activity" do
252 blocked = insert(:user)
253 blocker = insert(:user)
254 User.block(blocker, blocked)
256 assert User.blocks?(blocker, blocked)
257 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
258 refute User.blocks?(blocker, blocked)
262 describe "deletion" do
263 test "it works with pruned objects" do
266 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
268 clear_config([:instance, :federating], true)
270 Object.normalize(post, false)
273 with_mock Pleroma.Web.Federator,
274 publish: fn _ -> nil end do
275 assert {:ok, delete} = CommonAPI.delete(post.id, user)
277 assert called(Pleroma.Web.Federator.publish(delete))
280 refute Activity.get_by_id(post.id)
283 test "it allows users to delete their posts" do
286 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
288 clear_config([:instance, :federating], true)
290 with_mock Pleroma.Web.Federator,
291 publish: fn _ -> nil end do
292 assert {:ok, delete} = CommonAPI.delete(post.id, user)
294 assert called(Pleroma.Web.Federator.publish(delete))
297 refute Activity.get_by_id(post.id)
300 test "it does not allow a user to delete their posts" do
302 other_user = insert(:user)
304 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
306 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
307 assert Activity.get_by_id(post.id)
310 test "it allows moderators to delete other user's posts" do
312 moderator = insert(:user, is_moderator: true)
314 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
316 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
319 refute Activity.get_by_id(post.id)
322 test "it allows admins to delete other user's posts" do
324 moderator = insert(:user, is_admin: true)
326 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
328 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
331 refute Activity.get_by_id(post.id)
334 test "superusers deleting non-local posts won't federate the delete" do
335 # This is the user of the ingested activity
339 ap_id: "http://mastodon.example.org/users/admin",
340 last_refreshed_at: NaiveDateTime.utc_now()
343 moderator = insert(:user, is_admin: true)
346 File.read!("test/fixtures/mastodon-post-activity.json")
349 {:ok, post} = Transmogrifier.handle_incoming(data)
351 with_mock Pleroma.Web.Federator,
352 publish: fn _ -> nil end do
353 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
355 refute called(Pleroma.Web.Federator.publish(:_))
358 refute Activity.get_by_id(post.id)
362 test "favoriting race condition" do
364 users_serial = insert_list(10, :user)
365 users = insert_list(10, :user)
367 {:ok, activity} = CommonAPI.post(user, %{status: "."})
370 |> Enum.map(fn user ->
371 CommonAPI.favorite(user, activity.id)
374 object = Object.get_by_ap_id(activity.data["object"])
375 assert object.data["like_count"] == 10
378 |> Enum.map(fn user ->
380 CommonAPI.favorite(user, activity.id)
383 |> Enum.map(&Task.await/1)
385 object = Object.get_by_ap_id(activity.data["object"])
386 assert object.data["like_count"] == 20
389 test "repeating race condition" do
391 users_serial = insert_list(10, :user)
392 users = insert_list(10, :user)
394 {:ok, activity} = CommonAPI.post(user, %{status: "."})
397 |> Enum.map(fn user ->
398 CommonAPI.repeat(activity.id, user)
401 object = Object.get_by_ap_id(activity.data["object"])
402 assert object.data["announcement_count"] == 10
405 |> Enum.map(fn user ->
407 CommonAPI.repeat(activity.id, user)
410 |> Enum.map(&Task.await/1)
412 object = Object.get_by_ap_id(activity.data["object"])
413 assert object.data["announcement_count"] == 20
416 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
418 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
420 [participation] = Participation.for_user(user)
423 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
425 assert Visibility.is_direct?(convo_reply)
427 assert activity.data["context"] == convo_reply.data["context"]
430 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
432 jafnhar = insert(:user)
433 tridi = insert(:user)
436 CommonAPI.post(har, %{
437 status: "@#{jafnhar.nickname} hey",
441 assert har.ap_id in activity.recipients
442 assert jafnhar.ap_id in activity.recipients
444 [participation] = Participation.for_user(har)
447 CommonAPI.post(har, %{
448 status: "I don't really like @#{tridi.nickname}",
449 visibility: "direct",
450 in_reply_to_status_id: activity.id,
451 in_reply_to_conversation_id: participation.id
454 assert har.ap_id in activity.recipients
455 assert jafnhar.ap_id in activity.recipients
456 refute tridi.ap_id in activity.recipients
459 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
461 jafnhar = insert(:user)
462 tridi = insert(:user)
464 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
467 CommonAPI.post(har, %{
468 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
472 refute tridi.ap_id in activity.recipients
473 assert jafnhar.ap_id in activity.recipients
476 test "it de-duplicates tags" do
478 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
480 object = Object.normalize(activity)
482 assert object.data["tag"] == ["2hu"]
485 test "it adds emoji in the object" do
487 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
489 assert Object.normalize(activity).data["emoji"]["firefox"]
492 describe "posting" do
493 test "deactivated users can't post" do
494 user = insert(:user, deactivated: true)
495 assert {:error, _} = CommonAPI.post(user, %{status: "ye"})
498 test "it supports explicit addressing" do
500 user_two = insert(:user)
501 user_three = insert(:user)
502 user_four = insert(:user)
505 CommonAPI.post(user, %{
507 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
508 to: [user_two.nickname, user_four.nickname, "nonexistent"]
511 assert user.ap_id in activity.recipients
512 assert user_two.ap_id in activity.recipients
513 assert user_four.ap_id in activity.recipients
514 refute user_three.ap_id in activity.recipients
517 test "it filters out obviously bad tags when accepting a post as HTML" do
520 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
523 CommonAPI.post(user, %{
525 content_type: "text/html"
528 object = Object.normalize(activity)
530 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
531 assert object.data["source"] == post
534 test "it filters out obviously bad tags when accepting a post as Markdown" do
537 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
540 CommonAPI.post(user, %{
542 content_type: "text/markdown"
545 object = Object.normalize(activity)
547 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
548 assert object.data["source"] == post
551 test "it does not allow replies to direct messages that are not direct messages themselves" do
554 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
557 CommonAPI.post(user, %{
559 visibility: "direct",
560 in_reply_to_status_id: activity.id
563 Enum.each(["public", "private", "unlisted"], fn visibility ->
564 assert {:error, "The message visibility must be direct"} =
565 CommonAPI.post(user, %{
567 visibility: visibility,
568 in_reply_to_status_id: activity.id
573 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
575 other_user = insert(:user)
576 third_user = insert(:user)
578 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
581 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
583 # The OP is implicitly added
584 assert user.ap_id in open_answer.recipients
586 {:ok, secret_answer} =
587 CommonAPI.post(other_user, %{
588 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
589 in_reply_to_status_id: post.id,
593 assert third_user.ap_id in secret_answer.recipients
595 # The OP is not added
596 refute user.ap_id in secret_answer.recipients
599 test "it allows to address a list" do
601 {:ok, list} = Pleroma.List.create("foo", user)
603 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
605 assert activity.data["bcc"] == [list.ap_id]
606 assert activity.recipients == [list.ap_id, user.ap_id]
607 assert activity.data["listMessage"] == list.ap_id
610 test "it returns error when status is empty and no attachments" do
613 assert {:error, "Cannot post an empty status without attachments"} =
614 CommonAPI.post(user, %{status: ""})
617 test "it validates character limits are correctly enforced" do
618 Pleroma.Config.put([:instance, :limit], 5)
622 assert {:error, "The status is over the character limit"} =
623 CommonAPI.post(user, %{status: "foobar"})
625 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
628 test "it can handle activities that expire" do
631 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
633 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
636 worker: Pleroma.Workers.PurgeExpiredActivity,
637 args: %{activity_id: activity.id},
638 scheduled_at: expires_at
643 describe "reactions" do
644 test "reacting to a status with an emoji" do
646 other_user = insert(:user)
648 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
650 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
652 assert reaction.data["actor"] == user.ap_id
653 assert reaction.data["content"] == "👍"
655 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
657 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
660 test "unreacting to a status with an emoji" do
662 other_user = insert(:user)
664 clear_config([:instance, :federating], true)
666 with_mock Pleroma.Web.Federator,
667 publish: fn _ -> nil end do
668 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
669 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
671 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
673 assert unreaction.data["type"] == "Undo"
674 assert unreaction.data["object"] == reaction.data["id"]
675 assert unreaction.local
677 # On federation, it contains the undone (and deleted) object
678 unreaction_with_object = %{
680 | data: Map.put(unreaction.data, "object", reaction.data)
683 assert called(Pleroma.Web.Federator.publish(unreaction_with_object))
687 test "repeating a status" do
689 other_user = insert(:user)
691 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
693 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
694 assert Visibility.is_public?(announce_activity)
697 test "can't repeat a repeat" do
699 other_user = insert(:user)
700 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
702 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
704 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
707 test "repeating a status privately" do
709 other_user = insert(:user)
711 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
713 {:ok, %Activity{} = announce_activity} =
714 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
716 assert Visibility.is_private?(announce_activity)
717 refute Visibility.visible_for_user?(announce_activity, nil)
720 test "favoriting a status" do
722 other_user = insert(:user)
724 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
726 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
727 assert data["type"] == "Like"
728 assert data["actor"] == user.ap_id
729 assert data["object"] == post_activity.data["object"]
732 test "retweeting a status twice returns the status" do
734 other_user = insert(:user)
736 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
737 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
738 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
741 test "favoriting a status twice returns ok, but without the like activity" do
743 other_user = insert(:user)
745 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
746 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
747 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
751 describe "pinned statuses" do
753 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
756 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
758 [user: user, activity: activity]
761 test "pin status", %{user: user, activity: activity} do
762 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
765 user = refresh_record(user)
767 assert %User{pinned_activities: [^id]} = user
770 test "pin poll", %{user: user} do
772 CommonAPI.post(user, %{
773 status: "How is fediverse today?",
774 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
777 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
780 user = refresh_record(user)
782 assert %User{pinned_activities: [^id]} = user
785 test "unlisted statuses can be pinned", %{user: user} do
786 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
787 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
790 test "only self-authored can be pinned", %{activity: activity} do
793 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
796 test "max pinned statuses", %{user: user, activity: activity_one} do
797 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
799 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
801 user = refresh_record(user)
803 assert {:error, "You have already pinned the maximum number of statuses"} =
804 CommonAPI.pin(activity_two.id, user)
807 test "unpin status", %{user: user, activity: activity} do
808 {:ok, activity} = CommonAPI.pin(activity.id, user)
810 user = refresh_record(user)
814 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
816 user = refresh_record(user)
818 assert %User{pinned_activities: []} = user
821 test "should unpin when deleting a status", %{user: user, activity: activity} do
822 {:ok, activity} = CommonAPI.pin(activity.id, user)
824 user = refresh_record(user)
826 assert {:ok, _} = CommonAPI.delete(activity.id, user)
828 user = refresh_record(user)
830 assert %User{pinned_activities: []} = user
834 describe "mute tests" do
838 activity = insert(:note_activity)
840 [user: user, activity: activity]
843 test "marks notifications as read after mute" do
844 author = insert(:user)
845 activity = insert(:note_activity, user: author)
847 friend1 = insert(:user)
848 friend2 = insert(:user)
850 {:ok, reply_activity} =
854 status: "@#{author.nickname} @#{friend1.nickname} test reply",
855 in_reply_to_status_id: activity.id
859 {:ok, favorite_activity} = CommonAPI.favorite(friend2, activity.id)
860 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, friend1)
862 assert Repo.aggregate(
863 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
867 unread_notifications =
868 Repo.all(from(n in Notification, where: n.seen == false, where: n.user_id == ^author.id))
870 assert Enum.any?(unread_notifications, fn n ->
871 n.type == "favourite" && n.activity_id == favorite_activity.id
874 assert Enum.any?(unread_notifications, fn n ->
875 n.type == "reblog" && n.activity_id == repeat_activity.id
878 assert Enum.any?(unread_notifications, fn n ->
879 n.type == "mention" && n.activity_id == reply_activity.id
882 {:ok, _} = CommonAPI.add_mute(author, activity)
883 assert CommonAPI.thread_muted?(author, activity)
885 assert Repo.aggregate(
886 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
891 Repo.all(from(n in Notification, where: n.seen == true, where: n.user_id == ^author.id))
893 assert Enum.any?(read_notifications, fn n ->
894 n.type == "favourite" && n.activity_id == favorite_activity.id
897 assert Enum.any?(read_notifications, fn n ->
898 n.type == "reblog" && n.activity_id == repeat_activity.id
901 assert Enum.any?(read_notifications, fn n ->
902 n.type == "mention" && n.activity_id == reply_activity.id
906 test "add mute", %{user: user, activity: activity} do
907 {:ok, _} = CommonAPI.add_mute(user, activity)
908 assert CommonAPI.thread_muted?(user, activity)
911 test "remove mute", %{user: user, activity: activity} do
912 CommonAPI.add_mute(user, activity)
913 {:ok, _} = CommonAPI.remove_mute(user, activity)
914 refute CommonAPI.thread_muted?(user, activity)
917 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
918 CommonAPI.add_mute(user, activity)
919 {:error, _} = CommonAPI.add_mute(user, activity)
923 describe "reports" do
924 test "creates a report" do
925 reporter = insert(:user)
926 target_user = insert(:user)
928 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
930 reporter_ap_id = reporter.ap_id
931 target_ap_id = target_user.ap_id
932 activity_ap_id = activity.data["id"]
936 account_id: target_user.id,
938 status_ids: [activity.id]
943 "id" => activity_ap_id,
944 "content" => "foobar",
945 "published" => activity.object.data["published"],
946 "actor" => AccountView.render("show.json", %{user: target_user})
949 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
952 actor: ^reporter_ap_id,
955 "content" => ^comment,
956 "object" => [^target_ap_id, ^note_obj],
962 test "updates report state" do
963 [reporter, target_user] = insert_pair(:user)
964 activity = insert(:note_activity, user: target_user)
966 {:ok, %Activity{id: report_id}} =
967 CommonAPI.report(reporter, %{
968 account_id: target_user.id,
969 comment: "I feel offended",
970 status_ids: [activity.id]
973 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
975 assert report.data["state"] == "resolved"
977 [reported_user, activity_id] = report.data["object"]
979 assert reported_user == target_user.ap_id
980 assert activity_id == activity.data["id"]
983 test "does not update report state when state is unsupported" do
984 [reporter, target_user] = insert_pair(:user)
985 activity = insert(:note_activity, user: target_user)
987 {:ok, %Activity{id: report_id}} =
988 CommonAPI.report(reporter, %{
989 account_id: target_user.id,
990 comment: "I feel offended",
991 status_ids: [activity.id]
994 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
997 test "updates state of multiple reports" do
998 [reporter, target_user] = insert_pair(:user)
999 activity = insert(:note_activity, user: target_user)
1001 {:ok, %Activity{id: first_report_id}} =
1002 CommonAPI.report(reporter, %{
1003 account_id: target_user.id,
1004 comment: "I feel offended",
1005 status_ids: [activity.id]
1008 {:ok, %Activity{id: second_report_id}} =
1009 CommonAPI.report(reporter, %{
1010 account_id: target_user.id,
1011 comment: "I feel very offended!",
1012 status_ids: [activity.id]
1016 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
1018 first_report = Activity.get_by_id(first_report_id)
1019 second_report = Activity.get_by_id(second_report_id)
1021 assert report_ids -- [first_report_id, second_report_id] == []
1022 assert first_report.data["state"] == "resolved"
1023 assert second_report.data["state"] == "resolved"
1027 describe "reblog muting" do
1029 muter = insert(:user)
1031 muted = insert(:user)
1033 [muter: muter, muted: muted]
1036 test "add a reblog mute", %{muter: muter, muted: muted} do
1037 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1039 assert User.showing_reblogs?(muter, muted) == false
1042 test "remove a reblog mute", %{muter: muter, muted: muted} do
1043 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1044 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
1046 assert User.showing_reblogs?(muter, muted) == true
1050 describe "follow/2" do
1051 test "directly follows a non-locked local user" do
1052 [follower, followed] = insert_pair(:user)
1053 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1055 assert User.following?(follower, followed)
1059 describe "unfollow/2" do
1060 test "also unsubscribes a user" do
1061 [follower, followed] = insert_pair(:user)
1062 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1063 {:ok, _subscription} = User.subscribe(follower, followed)
1065 assert User.subscribed_to?(follower, followed)
1067 {:ok, follower} = CommonAPI.unfollow(follower, followed)
1069 refute User.subscribed_to?(follower, followed)
1072 test "cancels a pending follow for a local user" do
1073 follower = insert(:user)
1074 followed = insert(:user, locked: true)
1076 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1077 CommonAPI.follow(follower, followed)
1079 assert User.get_follow_state(follower, followed) == :follow_pending
1080 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1081 assert User.get_follow_state(follower, followed) == nil
1083 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1084 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1089 "object" => %{"type" => "Follow", "state" => "cancelled"}
1091 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1094 test "cancels a pending follow for a remote user" do
1095 follower = insert(:user)
1096 followed = insert(:user, locked: true, local: false, ap_enabled: true)
1098 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1099 CommonAPI.follow(follower, followed)
1101 assert User.get_follow_state(follower, followed) == :follow_pending
1102 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1103 assert User.get_follow_state(follower, followed) == nil
1105 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1106 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1111 "object" => %{"type" => "Follow", "state" => "cancelled"}
1113 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1117 describe "accept_follow_request/2" do
1118 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
1119 user = insert(:user, locked: true)
1120 follower = insert(:user)
1121 follower_two = insert(:user)
1123 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1124 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1125 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1127 assert follow_activity.data["state"] == "pending"
1128 assert follow_activity_two.data["state"] == "pending"
1129 assert follow_activity_three.data["state"] == "pending"
1131 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
1133 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
1134 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
1135 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1138 test "after rejection, it sets all existing pending follow request states to 'reject'" do
1139 user = insert(:user, locked: true)
1140 follower = insert(:user)
1141 follower_two = insert(:user)
1143 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1144 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1145 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1147 assert follow_activity.data["state"] == "pending"
1148 assert follow_activity_two.data["state"] == "pending"
1149 assert follow_activity_three.data["state"] == "pending"
1151 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
1153 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
1154 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
1155 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1158 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
1159 user = insert(:user, locked: true)
1160 not_follower = insert(:user)
1161 CommonAPI.accept_follow_request(not_follower, user)
1163 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
1167 describe "vote/3" do
1168 test "does not allow to vote twice" do
1169 user = insert(:user)
1170 other_user = insert(:user)
1173 CommonAPI.post(user, %{
1174 status: "Am I cute?",
1175 poll: %{options: ["Yes", "No"], expires_in: 20}
1178 object = Object.normalize(activity)
1180 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1182 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1186 describe "listen/2" do
1187 test "returns a valid activity" do
1188 user = insert(:user)
1191 CommonAPI.listen(user, %{
1192 title: "lain radio episode 1",
1193 album: "lain radio",
1198 object = Object.normalize(activity)
1200 assert object.data["title"] == "lain radio episode 1"
1202 assert Visibility.get_visibility(activity) == "public"
1205 test "respects visibility=private" do
1206 user = insert(:user)
1209 CommonAPI.listen(user, %{
1210 title: "lain radio episode 1",
1211 album: "lain radio",
1214 visibility: "private"
1217 object = Object.normalize(activity)
1219 assert object.data["title"] == "lain radio episode 1"
1221 assert Visibility.get_visibility(activity) == "private"
1225 describe "get_user/1" do
1226 test "gets user by ap_id" do
1227 user = insert(:user)
1228 assert CommonAPI.get_user(user.ap_id) == user
1231 test "gets user by guessed nickname" do
1232 user = insert(:user, ap_id: "", nickname: "mario@mushroom.kingdom")
1233 assert CommonAPI.get_user("https://mushroom.kingdom/users/mario") == user
1240 nickname: "erroruser@example.com"
1241 } = CommonAPI.get_user("")