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
6 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
221 test "it reject messages via MRF" do
222 clear_config([:mrf_keyword, :reject], ["GNO"])
223 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
225 author = insert(:user)
226 recipient = insert(:user)
228 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
229 CommonAPI.post_chat_message(author, recipient, "GNO/Linux")
233 describe "unblocking" do
234 test "it works even without an existing block activity" do
235 blocked = insert(:user)
236 blocker = insert(:user)
237 User.block(blocker, blocked)
239 assert User.blocks?(blocker, blocked)
240 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
241 refute User.blocks?(blocker, blocked)
245 describe "deletion" do
246 test "it works with pruned objects" do
249 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
251 clear_config([:instance, :federating], true)
253 Object.normalize(post, false)
256 with_mock Pleroma.Web.Federator,
257 publish: fn _ -> nil end do
258 assert {:ok, delete} = CommonAPI.delete(post.id, user)
260 assert called(Pleroma.Web.Federator.publish(delete))
263 refute Activity.get_by_id(post.id)
266 test "it allows users to delete their posts" do
269 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
271 clear_config([:instance, :federating], true)
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 does not allow a user to delete their posts" do
285 other_user = insert(:user)
287 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
289 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
290 assert Activity.get_by_id(post.id)
293 test "it allows moderators to delete other user's posts" do
295 moderator = insert(:user, is_moderator: true)
297 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
299 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
302 refute Activity.get_by_id(post.id)
305 test "it allows admins to delete other user's posts" do
307 moderator = insert(:user, is_admin: true)
309 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
311 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
314 refute Activity.get_by_id(post.id)
317 test "superusers deleting non-local posts won't federate the delete" do
318 # This is the user of the ingested activity
322 ap_id: "http://mastodon.example.org/users/admin",
323 last_refreshed_at: NaiveDateTime.utc_now()
326 moderator = insert(:user, is_admin: true)
329 File.read!("test/fixtures/mastodon-post-activity.json")
332 {:ok, post} = Transmogrifier.handle_incoming(data)
334 with_mock Pleroma.Web.Federator,
335 publish: fn _ -> nil end do
336 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
338 refute called(Pleroma.Web.Federator.publish(:_))
341 refute Activity.get_by_id(post.id)
345 test "favoriting race condition" do
347 users_serial = insert_list(10, :user)
348 users = insert_list(10, :user)
350 {:ok, activity} = CommonAPI.post(user, %{status: "."})
353 |> Enum.map(fn user ->
354 CommonAPI.favorite(user, activity.id)
357 object = Object.get_by_ap_id(activity.data["object"])
358 assert object.data["like_count"] == 10
361 |> Enum.map(fn user ->
363 CommonAPI.favorite(user, activity.id)
366 |> Enum.map(&Task.await/1)
368 object = Object.get_by_ap_id(activity.data["object"])
369 assert object.data["like_count"] == 20
372 test "repeating race condition" do
374 users_serial = insert_list(10, :user)
375 users = insert_list(10, :user)
377 {:ok, activity} = CommonAPI.post(user, %{status: "."})
380 |> Enum.map(fn user ->
381 CommonAPI.repeat(activity.id, user)
384 object = Object.get_by_ap_id(activity.data["object"])
385 assert object.data["announcement_count"] == 10
388 |> Enum.map(fn user ->
390 CommonAPI.repeat(activity.id, user)
393 |> Enum.map(&Task.await/1)
395 object = Object.get_by_ap_id(activity.data["object"])
396 assert object.data["announcement_count"] == 20
399 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
401 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
403 [participation] = Participation.for_user(user)
406 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
408 assert Visibility.is_direct?(convo_reply)
410 assert activity.data["context"] == convo_reply.data["context"]
413 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
415 jafnhar = insert(:user)
416 tridi = insert(:user)
419 CommonAPI.post(har, %{
420 status: "@#{jafnhar.nickname} hey",
424 assert har.ap_id in activity.recipients
425 assert jafnhar.ap_id in activity.recipients
427 [participation] = Participation.for_user(har)
430 CommonAPI.post(har, %{
431 status: "I don't really like @#{tridi.nickname}",
432 visibility: "direct",
433 in_reply_to_status_id: activity.id,
434 in_reply_to_conversation_id: participation.id
437 assert har.ap_id in activity.recipients
438 assert jafnhar.ap_id in activity.recipients
439 refute tridi.ap_id in activity.recipients
442 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
444 jafnhar = insert(:user)
445 tridi = insert(:user)
447 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
450 CommonAPI.post(har, %{
451 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
455 refute tridi.ap_id in activity.recipients
456 assert jafnhar.ap_id in activity.recipients
459 test "it de-duplicates tags" do
461 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
463 object = Object.normalize(activity)
465 assert object.data["tag"] == ["2hu"]
468 test "it adds emoji in the object" do
470 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
472 assert Object.normalize(activity).data["emoji"]["firefox"]
475 describe "posting" do
476 test "deactivated users can't post" do
477 user = insert(:user, deactivated: true)
478 assert {:error, _} = CommonAPI.post(user, %{status: "ye"})
481 test "it supports explicit addressing" do
483 user_two = insert(:user)
484 user_three = insert(:user)
485 user_four = insert(:user)
488 CommonAPI.post(user, %{
490 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
491 to: [user_two.nickname, user_four.nickname, "nonexistent"]
494 assert user.ap_id in activity.recipients
495 assert user_two.ap_id in activity.recipients
496 assert user_four.ap_id in activity.recipients
497 refute user_three.ap_id in activity.recipients
500 test "it filters out obviously bad tags when accepting a post as HTML" do
503 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
506 CommonAPI.post(user, %{
508 content_type: "text/html"
511 object = Object.normalize(activity)
513 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
514 assert object.data["source"] == post
517 test "it filters out obviously bad tags when accepting a post as Markdown" do
520 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
523 CommonAPI.post(user, %{
525 content_type: "text/markdown"
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 does not allow replies to direct messages that are not direct messages themselves" do
537 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
540 CommonAPI.post(user, %{
542 visibility: "direct",
543 in_reply_to_status_id: activity.id
546 Enum.each(["public", "private", "unlisted"], fn visibility ->
547 assert {:error, "The message visibility must be direct"} =
548 CommonAPI.post(user, %{
550 visibility: visibility,
551 in_reply_to_status_id: activity.id
556 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
558 other_user = insert(:user)
559 third_user = insert(:user)
561 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
564 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
566 # The OP is implicitly added
567 assert user.ap_id in open_answer.recipients
569 {:ok, secret_answer} =
570 CommonAPI.post(other_user, %{
571 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
572 in_reply_to_status_id: post.id,
576 assert third_user.ap_id in secret_answer.recipients
578 # The OP is not added
579 refute user.ap_id in secret_answer.recipients
582 test "it allows to address a list" do
584 {:ok, list} = Pleroma.List.create("foo", user)
586 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
588 assert activity.data["bcc"] == [list.ap_id]
589 assert activity.recipients == [list.ap_id, user.ap_id]
590 assert activity.data["listMessage"] == list.ap_id
593 test "it returns error when status is empty and no attachments" do
596 assert {:error, "Cannot post an empty status without attachments"} =
597 CommonAPI.post(user, %{status: ""})
600 test "it validates character limits are correctly enforced" do
601 Pleroma.Config.put([:instance, :limit], 5)
605 assert {:error, "The status is over the character limit"} =
606 CommonAPI.post(user, %{status: "foobar"})
608 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
611 test "it can handle activities that expire" do
614 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
616 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
619 worker: Pleroma.Workers.PurgeExpiredActivity,
620 args: %{activity_id: activity.id},
621 scheduled_at: expires_at
626 describe "reactions" do
627 test "reacting to a status with an emoji" do
629 other_user = insert(:user)
631 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
633 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
635 assert reaction.data["actor"] == user.ap_id
636 assert reaction.data["content"] == "👍"
638 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
640 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
643 test "unreacting to a status with an emoji" do
645 other_user = insert(:user)
647 clear_config([:instance, :federating], true)
649 with_mock Pleroma.Web.Federator,
650 publish: fn _ -> nil end do
651 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
652 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
654 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
656 assert unreaction.data["type"] == "Undo"
657 assert unreaction.data["object"] == reaction.data["id"]
658 assert unreaction.local
660 # On federation, it contains the undone (and deleted) object
661 unreaction_with_object = %{
663 | data: Map.put(unreaction.data, "object", reaction.data)
666 assert called(Pleroma.Web.Federator.publish(unreaction_with_object))
670 test "repeating a status" do
672 other_user = insert(:user)
674 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
676 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
677 assert Visibility.is_public?(announce_activity)
680 test "can't repeat a repeat" do
682 other_user = insert(:user)
683 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
685 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
687 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
690 test "repeating a status privately" do
692 other_user = insert(:user)
694 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
696 {:ok, %Activity{} = announce_activity} =
697 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
699 assert Visibility.is_private?(announce_activity)
700 refute Visibility.visible_for_user?(announce_activity, nil)
703 test "favoriting a status" do
705 other_user = insert(:user)
707 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
709 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
710 assert data["type"] == "Like"
711 assert data["actor"] == user.ap_id
712 assert data["object"] == post_activity.data["object"]
715 test "retweeting a status twice returns the status" do
717 other_user = insert(:user)
719 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
720 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
721 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
724 test "favoriting a status twice returns ok, but without the like activity" do
726 other_user = insert(:user)
728 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
729 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
730 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
734 describe "pinned statuses" do
736 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
739 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
741 [user: user, activity: activity]
744 test "pin status", %{user: user, activity: activity} do
745 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
748 user = refresh_record(user)
750 assert %User{pinned_activities: [^id]} = user
753 test "pin poll", %{user: user} do
755 CommonAPI.post(user, %{
756 status: "How is fediverse today?",
757 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
760 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
763 user = refresh_record(user)
765 assert %User{pinned_activities: [^id]} = user
768 test "unlisted statuses can be pinned", %{user: user} do
769 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
770 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
773 test "only self-authored can be pinned", %{activity: activity} do
776 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
779 test "max pinned statuses", %{user: user, activity: activity_one} do
780 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
782 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
784 user = refresh_record(user)
786 assert {:error, "You have already pinned the maximum number of statuses"} =
787 CommonAPI.pin(activity_two.id, user)
790 test "unpin status", %{user: user, activity: activity} do
791 {:ok, activity} = CommonAPI.pin(activity.id, user)
793 user = refresh_record(user)
797 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
799 user = refresh_record(user)
801 assert %User{pinned_activities: []} = user
804 test "should unpin when deleting a status", %{user: user, activity: activity} do
805 {:ok, activity} = CommonAPI.pin(activity.id, user)
807 user = refresh_record(user)
809 assert {:ok, _} = CommonAPI.delete(activity.id, user)
811 user = refresh_record(user)
813 assert %User{pinned_activities: []} = user
817 describe "mute tests" do
821 activity = insert(:note_activity)
823 [user: user, activity: activity]
826 test "marks notifications as read after mute" do
827 author = insert(:user)
828 activity = insert(:note_activity, user: author)
830 friend1 = insert(:user)
831 friend2 = insert(:user)
833 {:ok, reply_activity} =
837 status: "@#{author.nickname} @#{friend1.nickname} test reply",
838 in_reply_to_status_id: activity.id
842 {:ok, favorite_activity} = CommonAPI.favorite(friend2, activity.id)
843 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, friend1)
845 assert Repo.aggregate(
846 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
850 unread_notifications =
851 Repo.all(from(n in Notification, where: n.seen == false, where: n.user_id == ^author.id))
853 assert Enum.any?(unread_notifications, fn n ->
854 n.type == "favourite" && n.activity_id == favorite_activity.id
857 assert Enum.any?(unread_notifications, fn n ->
858 n.type == "reblog" && n.activity_id == repeat_activity.id
861 assert Enum.any?(unread_notifications, fn n ->
862 n.type == "mention" && n.activity_id == reply_activity.id
865 {:ok, _} = CommonAPI.add_mute(author, activity)
866 assert CommonAPI.thread_muted?(author, activity)
868 assert Repo.aggregate(
869 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
874 Repo.all(from(n in Notification, where: n.seen == true, where: n.user_id == ^author.id))
876 assert Enum.any?(read_notifications, fn n ->
877 n.type == "favourite" && n.activity_id == favorite_activity.id
880 assert Enum.any?(read_notifications, fn n ->
881 n.type == "reblog" && n.activity_id == repeat_activity.id
884 assert Enum.any?(read_notifications, fn n ->
885 n.type == "mention" && n.activity_id == reply_activity.id
889 test "add mute", %{user: user, activity: activity} do
890 {:ok, _} = CommonAPI.add_mute(user, activity)
891 assert CommonAPI.thread_muted?(user, activity)
894 test "add expiring mute", %{user: user, activity: activity} do
895 {:ok, _} = CommonAPI.add_mute(user, activity, %{expires_in: 60})
896 assert CommonAPI.thread_muted?(user, activity)
898 worker = Pleroma.Workers.MuteExpireWorker
899 args = %{"op" => "unmute_conversation", "user_id" => user.id, "activity_id" => activity.id}
906 assert :ok = perform_job(worker, args)
907 refute CommonAPI.thread_muted?(user, activity)
910 test "remove mute", %{user: user, activity: activity} do
911 CommonAPI.add_mute(user, activity)
912 {:ok, _} = CommonAPI.remove_mute(user, activity)
913 refute CommonAPI.thread_muted?(user, activity)
916 test "remove mute by ids", %{user: user, activity: activity} do
917 CommonAPI.add_mute(user, activity)
918 {:ok, _} = CommonAPI.remove_mute(user.id, activity.id)
919 refute CommonAPI.thread_muted?(user, activity)
922 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
923 CommonAPI.add_mute(user, activity)
924 {:error, _} = CommonAPI.add_mute(user, activity)
928 describe "reports" do
929 test "creates a report" do
930 reporter = insert(:user)
931 target_user = insert(:user)
933 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
935 reporter_ap_id = reporter.ap_id
936 target_ap_id = target_user.ap_id
937 activity_ap_id = activity.data["id"]
941 account_id: target_user.id,
943 status_ids: [activity.id]
948 "id" => activity_ap_id,
949 "content" => "foobar",
950 "published" => activity.object.data["published"],
951 "actor" => AccountView.render("show.json", %{user: target_user})
954 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
957 actor: ^reporter_ap_id,
960 "content" => ^comment,
961 "object" => [^target_ap_id, ^note_obj],
967 test "updates report state" do
968 [reporter, target_user] = insert_pair(:user)
969 activity = insert(:note_activity, user: target_user)
971 {:ok, %Activity{id: report_id}} =
972 CommonAPI.report(reporter, %{
973 account_id: target_user.id,
974 comment: "I feel offended",
975 status_ids: [activity.id]
978 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
980 assert report.data["state"] == "resolved"
982 [reported_user, activity_id] = report.data["object"]
984 assert reported_user == target_user.ap_id
985 assert activity_id == activity.data["id"]
988 test "does not update report state when state is unsupported" do
989 [reporter, target_user] = insert_pair(:user)
990 activity = insert(:note_activity, user: target_user)
992 {:ok, %Activity{id: report_id}} =
993 CommonAPI.report(reporter, %{
994 account_id: target_user.id,
995 comment: "I feel offended",
996 status_ids: [activity.id]
999 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
1002 test "updates state of multiple reports" do
1003 [reporter, target_user] = insert_pair(:user)
1004 activity = insert(:note_activity, user: target_user)
1006 {:ok, %Activity{id: first_report_id}} =
1007 CommonAPI.report(reporter, %{
1008 account_id: target_user.id,
1009 comment: "I feel offended",
1010 status_ids: [activity.id]
1013 {:ok, %Activity{id: second_report_id}} =
1014 CommonAPI.report(reporter, %{
1015 account_id: target_user.id,
1016 comment: "I feel very offended!",
1017 status_ids: [activity.id]
1021 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
1023 first_report = Activity.get_by_id(first_report_id)
1024 second_report = Activity.get_by_id(second_report_id)
1026 assert report_ids -- [first_report_id, second_report_id] == []
1027 assert first_report.data["state"] == "resolved"
1028 assert second_report.data["state"] == "resolved"
1032 describe "reblog muting" do
1034 muter = insert(:user)
1036 muted = insert(:user)
1038 [muter: muter, muted: muted]
1041 test "add a reblog mute", %{muter: muter, muted: muted} do
1042 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1044 assert User.showing_reblogs?(muter, muted) == false
1047 test "remove a reblog mute", %{muter: muter, muted: muted} do
1048 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1049 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
1051 assert User.showing_reblogs?(muter, muted) == true
1055 describe "follow/2" do
1056 test "directly follows a non-locked local user" do
1057 [follower, followed] = insert_pair(:user)
1058 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1060 assert User.following?(follower, followed)
1064 describe "unfollow/2" do
1065 test "also unsubscribes a user" do
1066 [follower, followed] = insert_pair(:user)
1067 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1068 {:ok, _subscription} = User.subscribe(follower, followed)
1070 assert User.subscribed_to?(follower, followed)
1072 {:ok, follower} = CommonAPI.unfollow(follower, followed)
1074 refute User.subscribed_to?(follower, followed)
1077 test "cancels a pending follow for a local user" do
1078 follower = insert(:user)
1079 followed = insert(:user, locked: true)
1081 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1082 CommonAPI.follow(follower, followed)
1084 assert User.get_follow_state(follower, followed) == :follow_pending
1085 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1086 assert User.get_follow_state(follower, followed) == nil
1088 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1089 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1094 "object" => %{"type" => "Follow", "state" => "cancelled"}
1096 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1099 test "cancels a pending follow for a remote user" do
1100 follower = insert(:user)
1101 followed = insert(:user, locked: true, local: false, ap_enabled: true)
1103 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1104 CommonAPI.follow(follower, followed)
1106 assert User.get_follow_state(follower, followed) == :follow_pending
1107 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1108 assert User.get_follow_state(follower, followed) == nil
1110 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1111 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1116 "object" => %{"type" => "Follow", "state" => "cancelled"}
1118 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1122 describe "accept_follow_request/2" do
1123 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
1124 user = insert(:user, locked: true)
1125 follower = insert(:user)
1126 follower_two = insert(:user)
1128 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1129 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1130 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1132 assert follow_activity.data["state"] == "pending"
1133 assert follow_activity_two.data["state"] == "pending"
1134 assert follow_activity_three.data["state"] == "pending"
1136 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
1138 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
1139 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
1140 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1143 test "after rejection, it sets all existing pending follow request states to 'reject'" do
1144 user = insert(:user, locked: true)
1145 follower = insert(:user)
1146 follower_two = insert(:user)
1148 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1149 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1150 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1152 assert follow_activity.data["state"] == "pending"
1153 assert follow_activity_two.data["state"] == "pending"
1154 assert follow_activity_three.data["state"] == "pending"
1156 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
1158 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
1159 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
1160 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1163 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
1164 user = insert(:user, locked: true)
1165 not_follower = insert(:user)
1166 CommonAPI.accept_follow_request(not_follower, user)
1168 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
1172 describe "vote/3" do
1173 test "does not allow to vote twice" do
1174 user = insert(:user)
1175 other_user = insert(:user)
1178 CommonAPI.post(user, %{
1179 status: "Am I cute?",
1180 poll: %{options: ["Yes", "No"], expires_in: 20}
1183 object = Object.normalize(activity)
1185 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1187 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1191 describe "listen/2" do
1192 test "returns a valid activity" do
1193 user = insert(:user)
1196 CommonAPI.listen(user, %{
1197 title: "lain radio episode 1",
1198 album: "lain radio",
1203 object = Object.normalize(activity)
1205 assert object.data["title"] == "lain radio episode 1"
1207 assert Visibility.get_visibility(activity) == "public"
1210 test "respects visibility=private" do
1211 user = insert(:user)
1214 CommonAPI.listen(user, %{
1215 title: "lain radio episode 1",
1216 album: "lain radio",
1219 visibility: "private"
1222 object = Object.normalize(activity)
1224 assert object.data["title"] == "lain radio episode 1"
1226 assert Visibility.get_visibility(activity) == "private"
1230 describe "get_user/1" do
1231 test "gets user by ap_id" do
1232 user = insert(:user)
1233 assert CommonAPI.get_user(user.ap_id) == user
1236 test "gets user by guessed nickname" do
1237 user = insert(:user, ap_id: "", nickname: "mario@mushroom.kingdom")
1238 assert CommonAPI.get_user("https://mushroom.kingdom/users/mario") == user
1245 nickname: "erroruser@example.com"
1246 } = CommonAPI.get_user("")