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
9 alias Pleroma.Conversation.Participation
10 alias Pleroma.Notification
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.ActivityPub.Visibility
17 alias Pleroma.Web.AdminAPI.AccountView
18 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
22 import Ecto.Query, only: [from: 2]
24 require Pleroma.Constants
26 setup do: clear_config([:instance, :safe_dm_mentions])
27 setup do: clear_config([:instance, :limit])
28 setup do: clear_config([:instance, :max_pinned_statuses])
30 describe "blocking" do
32 blocker = insert(:user)
33 blocked = insert(:user)
34 User.follow(blocker, blocked)
35 User.follow(blocked, blocker)
36 %{blocker: blocker, blocked: blocked}
39 test "it blocks and federates", %{blocker: blocker, blocked: blocked} do
40 clear_config([:instance, :federating], true)
42 with_mock Pleroma.Web.Federator,
43 publish: fn _ -> nil end do
44 assert {:ok, block} = CommonAPI.block(blocker, blocked)
47 assert User.blocks?(blocker, blocked)
48 refute User.following?(blocker, blocked)
49 refute User.following?(blocked, blocker)
51 assert called(Pleroma.Web.Federator.publish(block))
55 test "it blocks and does not federate if outgoing blocks are disabled", %{
59 clear_config([:instance, :federating], true)
60 clear_config([:activitypub, :outgoing_blocks], false)
62 with_mock Pleroma.Web.Federator,
63 publish: fn _ -> nil end do
64 assert {:ok, block} = CommonAPI.block(blocker, blocked)
67 assert User.blocks?(blocker, blocked)
68 refute User.following?(blocker, blocked)
69 refute User.following?(blocked, blocker)
71 refute called(Pleroma.Web.Federator.publish(block))
76 describe "posting chat messages" do
77 setup do: clear_config([:instance, :chat_limit])
79 test "it posts a chat message without content but with an attachment" do
80 author = insert(:user)
81 recipient = insert(:user)
84 content_type: "image/jpg",
85 path: Path.absname("test/fixtures/image.jpg"),
86 filename: "an_image.jpg"
89 {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id)
105 send: fn _ -> nil end
110 CommonAPI.post_chat_message(
118 Notification.for_user_and_activity(recipient, activity)
119 |> Repo.preload(:activity)
121 assert called(Pleroma.Web.Push.send(notification))
122 assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
123 assert called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
129 test "it adds html newlines" do
130 author = insert(:user)
131 recipient = insert(:user)
133 other_user = insert(:user)
136 CommonAPI.post_chat_message(
142 assert other_user.ap_id not in activity.recipients
144 object = Object.normalize(activity, false)
146 assert object.data["content"] == "uguu<br/>uguuu"
149 test "it linkifies" do
150 author = insert(:user)
151 recipient = insert(:user)
153 other_user = insert(:user)
156 CommonAPI.post_chat_message(
159 "https://example.org is the site of @#{other_user.nickname} #2hu"
162 assert other_user.ap_id not in activity.recipients
164 object = Object.normalize(activity, false)
166 assert object.data["content"] ==
167 "<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=\"#{
169 }\" 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>"
172 test "it posts a chat message" do
173 author = insert(:user)
174 recipient = insert(:user)
177 CommonAPI.post_chat_message(
180 "a test message <script>alert('uuu')</script> :firefox:"
183 assert activity.data["type"] == "Create"
184 assert activity.local
185 object = Object.normalize(activity)
187 assert object.data["type"] == "ChatMessage"
188 assert object.data["to"] == [recipient.ap_id]
190 assert object.data["content"] ==
191 "a test message <script>alert('uuu')</script> :firefox:"
193 assert object.data["emoji"] == %{
194 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
197 assert Chat.get(author.id, recipient.ap_id)
198 assert Chat.get(recipient.id, author.ap_id)
200 assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
203 test "it reject messages over the local limit" do
204 Pleroma.Config.put([:instance, :chat_limit], 2)
206 author = insert(:user)
207 recipient = insert(:user)
210 CommonAPI.post_chat_message(
216 assert message == :content_too_long
220 describe "unblocking" do
221 test "it works even without an existing block activity" do
222 blocked = insert(:user)
223 blocker = insert(:user)
224 User.block(blocker, blocked)
226 assert User.blocks?(blocker, blocked)
227 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
228 refute User.blocks?(blocker, blocked)
232 describe "deletion" do
233 test "it works with pruned objects" do
236 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
238 clear_config([:instance, :federating], true)
240 Object.normalize(post, false)
243 with_mock Pleroma.Web.Federator,
244 publish: fn _ -> nil end do
245 assert {:ok, delete} = CommonAPI.delete(post.id, user)
247 assert called(Pleroma.Web.Federator.publish(delete))
250 refute Activity.get_by_id(post.id)
253 test "it allows users to delete their posts" do
256 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
258 clear_config([:instance, :federating], true)
260 with_mock Pleroma.Web.Federator,
261 publish: fn _ -> nil end do
262 assert {:ok, delete} = CommonAPI.delete(post.id, user)
264 assert called(Pleroma.Web.Federator.publish(delete))
267 refute Activity.get_by_id(post.id)
270 test "it does not allow a user to delete their posts" do
272 other_user = insert(:user)
274 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
276 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
277 assert Activity.get_by_id(post.id)
280 test "it allows moderators to delete other user's posts" do
282 moderator = insert(:user, is_moderator: true)
284 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
286 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
289 refute Activity.get_by_id(post.id)
292 test "it allows admins to delete other user's posts" do
294 moderator = insert(:user, is_admin: true)
296 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
298 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
301 refute Activity.get_by_id(post.id)
304 test "superusers deleting non-local posts won't federate the delete" do
305 # This is the user of the ingested activity
309 ap_id: "http://mastodon.example.org/users/admin",
310 last_refreshed_at: NaiveDateTime.utc_now()
313 moderator = insert(:user, is_admin: true)
316 File.read!("test/fixtures/mastodon-post-activity.json")
319 {:ok, post} = Transmogrifier.handle_incoming(data)
321 with_mock Pleroma.Web.Federator,
322 publish: fn _ -> nil end do
323 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
325 refute called(Pleroma.Web.Federator.publish(:_))
328 refute Activity.get_by_id(post.id)
332 test "favoriting race condition" do
334 users_serial = insert_list(10, :user)
335 users = insert_list(10, :user)
337 {:ok, activity} = CommonAPI.post(user, %{status: "."})
340 |> Enum.map(fn user ->
341 CommonAPI.favorite(user, activity.id)
344 object = Object.get_by_ap_id(activity.data["object"])
345 assert object.data["like_count"] == 10
348 |> Enum.map(fn user ->
350 CommonAPI.favorite(user, activity.id)
353 |> Enum.map(&Task.await/1)
355 object = Object.get_by_ap_id(activity.data["object"])
356 assert object.data["like_count"] == 20
359 test "repeating race condition" do
361 users_serial = insert_list(10, :user)
362 users = insert_list(10, :user)
364 {:ok, activity} = CommonAPI.post(user, %{status: "."})
367 |> Enum.map(fn user ->
368 CommonAPI.repeat(activity.id, user)
371 object = Object.get_by_ap_id(activity.data["object"])
372 assert object.data["announcement_count"] == 10
375 |> Enum.map(fn user ->
377 CommonAPI.repeat(activity.id, user)
380 |> Enum.map(&Task.await/1)
382 object = Object.get_by_ap_id(activity.data["object"])
383 assert object.data["announcement_count"] == 20
386 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
388 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
390 [participation] = Participation.for_user(user)
393 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
395 assert Visibility.is_direct?(convo_reply)
397 assert activity.data["context"] == convo_reply.data["context"]
400 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
402 jafnhar = insert(:user)
403 tridi = insert(:user)
406 CommonAPI.post(har, %{
407 status: "@#{jafnhar.nickname} hey",
411 assert har.ap_id in activity.recipients
412 assert jafnhar.ap_id in activity.recipients
414 [participation] = Participation.for_user(har)
417 CommonAPI.post(har, %{
418 status: "I don't really like @#{tridi.nickname}",
419 visibility: "direct",
420 in_reply_to_status_id: activity.id,
421 in_reply_to_conversation_id: participation.id
424 assert har.ap_id in activity.recipients
425 assert jafnhar.ap_id in activity.recipients
426 refute tridi.ap_id in activity.recipients
429 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
431 jafnhar = insert(:user)
432 tridi = insert(:user)
434 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
437 CommonAPI.post(har, %{
438 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
442 refute tridi.ap_id in activity.recipients
443 assert jafnhar.ap_id in activity.recipients
446 test "it de-duplicates tags" do
448 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
450 object = Object.normalize(activity)
452 assert object.data["tag"] == ["2hu"]
455 test "it adds emoji in the object" do
457 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
459 assert Object.normalize(activity).data["emoji"]["firefox"]
462 describe "posting" do
463 test "deactivated users can't post" do
464 user = insert(:user, deactivated: true)
465 assert {:error, _} = CommonAPI.post(user, %{status: "ye"})
468 test "it supports explicit addressing" do
470 user_two = insert(:user)
471 user_three = insert(:user)
472 user_four = insert(:user)
475 CommonAPI.post(user, %{
477 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
478 to: [user_two.nickname, user_four.nickname, "nonexistent"]
481 assert user.ap_id in activity.recipients
482 assert user_two.ap_id in activity.recipients
483 assert user_four.ap_id in activity.recipients
484 refute user_three.ap_id in activity.recipients
487 test "it filters out obviously bad tags when accepting a post as HTML" do
490 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
493 CommonAPI.post(user, %{
495 content_type: "text/html"
498 object = Object.normalize(activity)
500 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
501 assert object.data["source"] == post
504 test "it filters out obviously bad tags when accepting a post as Markdown" do
507 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
510 CommonAPI.post(user, %{
512 content_type: "text/markdown"
515 object = Object.normalize(activity)
517 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
518 assert object.data["source"] == post
521 test "it does not allow replies to direct messages that are not direct messages themselves" do
524 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
527 CommonAPI.post(user, %{
529 visibility: "direct",
530 in_reply_to_status_id: activity.id
533 Enum.each(["public", "private", "unlisted"], fn visibility ->
534 assert {:error, "The message visibility must be direct"} =
535 CommonAPI.post(user, %{
537 visibility: visibility,
538 in_reply_to_status_id: activity.id
543 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
545 other_user = insert(:user)
546 third_user = insert(:user)
548 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
551 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
553 # The OP is implicitly added
554 assert user.ap_id in open_answer.recipients
556 {:ok, secret_answer} =
557 CommonAPI.post(other_user, %{
558 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
559 in_reply_to_status_id: post.id,
563 assert third_user.ap_id in secret_answer.recipients
565 # The OP is not added
566 refute user.ap_id in secret_answer.recipients
569 test "it allows to address a list" do
571 {:ok, list} = Pleroma.List.create("foo", user)
573 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
575 assert activity.data["bcc"] == [list.ap_id]
576 assert activity.recipients == [list.ap_id, user.ap_id]
577 assert activity.data["listMessage"] == list.ap_id
580 test "it returns error when status is empty and no attachments" do
583 assert {:error, "Cannot post an empty status without attachments"} =
584 CommonAPI.post(user, %{status: ""})
587 test "it validates character limits are correctly enforced" do
588 Pleroma.Config.put([:instance, :limit], 5)
592 assert {:error, "The status is over the character limit"} =
593 CommonAPI.post(user, %{status: "foobar"})
595 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
598 test "it can handle activities that expire" do
602 NaiveDateTime.utc_now()
603 |> NaiveDateTime.truncate(:second)
604 |> NaiveDateTime.add(1_000_000, :second)
606 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
608 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
609 assert expiration.scheduled_at == expires_at
613 describe "reactions" do
614 test "reacting to a status with an emoji" do
616 other_user = insert(:user)
618 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
620 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
622 assert reaction.data["actor"] == user.ap_id
623 assert reaction.data["content"] == "👍"
625 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
627 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
630 test "unreacting to a status with an emoji" do
632 other_user = insert(:user)
634 clear_config([:instance, :federating], true)
636 with_mock Pleroma.Web.Federator,
637 publish: fn _ -> nil end do
638 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
639 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
641 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
643 assert unreaction.data["type"] == "Undo"
644 assert unreaction.data["object"] == reaction.data["id"]
645 assert unreaction.local
647 # On federation, it contains the undone (and deleted) object
648 unreaction_with_object = %{
650 | data: Map.put(unreaction.data, "object", reaction.data)
653 assert called(Pleroma.Web.Federator.publish(unreaction_with_object))
657 test "repeating a status" do
659 other_user = insert(:user)
661 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
663 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
664 assert Visibility.is_public?(announce_activity)
667 test "can't repeat a repeat" do
669 other_user = insert(:user)
670 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
672 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
674 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
677 test "repeating a status privately" do
679 other_user = insert(:user)
681 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
683 {:ok, %Activity{} = announce_activity} =
684 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
686 assert Visibility.is_private?(announce_activity)
687 refute Visibility.visible_for_user?(announce_activity, nil)
690 test "favoriting a status" do
692 other_user = insert(:user)
694 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
696 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
697 assert data["type"] == "Like"
698 assert data["actor"] == user.ap_id
699 assert data["object"] == post_activity.data["object"]
702 test "retweeting a status twice returns the status" do
704 other_user = insert(:user)
706 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
707 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
708 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
711 test "favoriting a status twice returns ok, but without the like activity" do
713 other_user = insert(:user)
715 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
716 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
717 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
721 describe "pinned statuses" do
723 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
726 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
728 [user: user, activity: activity]
731 test "pin status", %{user: user, activity: activity} do
732 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
735 user = refresh_record(user)
737 assert %User{pinned_activities: [^id]} = user
740 test "pin poll", %{user: user} do
742 CommonAPI.post(user, %{
743 status: "How is fediverse today?",
744 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
747 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
750 user = refresh_record(user)
752 assert %User{pinned_activities: [^id]} = user
755 test "unlisted statuses can be pinned", %{user: user} do
756 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
757 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
760 test "only self-authored can be pinned", %{activity: activity} do
763 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
766 test "max pinned statuses", %{user: user, activity: activity_one} do
767 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
769 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
771 user = refresh_record(user)
773 assert {:error, "You have already pinned the maximum number of statuses"} =
774 CommonAPI.pin(activity_two.id, user)
777 test "unpin status", %{user: user, activity: activity} do
778 {:ok, activity} = CommonAPI.pin(activity.id, user)
780 user = refresh_record(user)
784 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
786 user = refresh_record(user)
788 assert %User{pinned_activities: []} = user
791 test "should unpin when deleting a status", %{user: user, activity: activity} do
792 {:ok, activity} = CommonAPI.pin(activity.id, user)
794 user = refresh_record(user)
796 assert {:ok, _} = CommonAPI.delete(activity.id, user)
798 user = refresh_record(user)
800 assert %User{pinned_activities: []} = user
804 describe "mute tests" do
808 activity = insert(:note_activity)
810 [user: user, activity: activity]
813 test "marks notifications as read after mute" do
814 author = insert(:user)
815 activity = insert(:note_activity, user: author)
817 friend1 = insert(:user)
818 friend2 = insert(:user)
820 {:ok, reply_activity} =
824 status: "@#{author.nickname} @#{friend1.nickname} test reply",
825 in_reply_to_status_id: activity.id
829 {:ok, favorite_activity} = CommonAPI.favorite(friend2, activity.id)
830 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, friend1)
832 assert Repo.aggregate(
833 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
837 unread_notifications =
838 Repo.all(from(n in Notification, where: n.seen == false, where: n.user_id == ^author.id))
840 assert Enum.any?(unread_notifications, fn n ->
841 n.type == "favourite" && n.activity_id == favorite_activity.id
844 assert Enum.any?(unread_notifications, fn n ->
845 n.type == "reblog" && n.activity_id == repeat_activity.id
848 assert Enum.any?(unread_notifications, fn n ->
849 n.type == "mention" && n.activity_id == reply_activity.id
852 {:ok, _} = CommonAPI.add_mute(author, activity)
853 assert CommonAPI.thread_muted?(author, activity)
855 assert Repo.aggregate(
856 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
861 Repo.all(from(n in Notification, where: n.seen == true, where: n.user_id == ^author.id))
863 assert Enum.any?(read_notifications, fn n ->
864 n.type == "favourite" && n.activity_id == favorite_activity.id
867 assert Enum.any?(read_notifications, fn n ->
868 n.type == "reblog" && n.activity_id == repeat_activity.id
871 assert Enum.any?(read_notifications, fn n ->
872 n.type == "mention" && n.activity_id == reply_activity.id
876 test "add mute", %{user: user, activity: activity} do
877 {:ok, _} = CommonAPI.add_mute(user, activity)
878 assert CommonAPI.thread_muted?(user, activity)
881 test "remove mute", %{user: user, activity: activity} do
882 CommonAPI.add_mute(user, activity)
883 {:ok, _} = CommonAPI.remove_mute(user, activity)
884 refute CommonAPI.thread_muted?(user, activity)
887 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
888 CommonAPI.add_mute(user, activity)
889 {:error, _} = CommonAPI.add_mute(user, activity)
893 describe "reports" do
894 test "creates a report" do
895 reporter = insert(:user)
896 target_user = insert(:user)
898 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
900 reporter_ap_id = reporter.ap_id
901 target_ap_id = target_user.ap_id
902 activity_ap_id = activity.data["id"]
906 account_id: target_user.id,
908 status_ids: [activity.id]
913 "id" => activity_ap_id,
914 "content" => "foobar",
915 "published" => activity.object.data["published"],
916 "actor" => AccountView.render("show.json", %{user: target_user})
919 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
922 actor: ^reporter_ap_id,
925 "content" => ^comment,
926 "object" => [^target_ap_id, ^note_obj],
932 test "updates report state" do
933 [reporter, target_user] = insert_pair(:user)
934 activity = insert(:note_activity, user: target_user)
936 {:ok, %Activity{id: report_id}} =
937 CommonAPI.report(reporter, %{
938 account_id: target_user.id,
939 comment: "I feel offended",
940 status_ids: [activity.id]
943 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
945 assert report.data["state"] == "resolved"
947 [reported_user, activity_id] = report.data["object"]
949 assert reported_user == target_user.ap_id
950 assert activity_id == activity.data["id"]
953 test "does not update report state when state is unsupported" do
954 [reporter, target_user] = insert_pair(:user)
955 activity = insert(:note_activity, user: target_user)
957 {:ok, %Activity{id: report_id}} =
958 CommonAPI.report(reporter, %{
959 account_id: target_user.id,
960 comment: "I feel offended",
961 status_ids: [activity.id]
964 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
967 test "updates state of multiple reports" do
968 [reporter, target_user] = insert_pair(:user)
969 activity = insert(:note_activity, user: target_user)
971 {:ok, %Activity{id: first_report_id}} =
972 CommonAPI.report(reporter, %{
973 account_id: target_user.id,
974 comment: "I feel offended",
975 status_ids: [activity.id]
978 {:ok, %Activity{id: second_report_id}} =
979 CommonAPI.report(reporter, %{
980 account_id: target_user.id,
981 comment: "I feel very offended!",
982 status_ids: [activity.id]
986 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
988 first_report = Activity.get_by_id(first_report_id)
989 second_report = Activity.get_by_id(second_report_id)
991 assert report_ids -- [first_report_id, second_report_id] == []
992 assert first_report.data["state"] == "resolved"
993 assert second_report.data["state"] == "resolved"
997 describe "reblog muting" do
999 muter = insert(:user)
1001 muted = insert(:user)
1003 [muter: muter, muted: muted]
1006 test "add a reblog mute", %{muter: muter, muted: muted} do
1007 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1009 assert User.showing_reblogs?(muter, muted) == false
1012 test "remove a reblog mute", %{muter: muter, muted: muted} do
1013 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1014 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
1016 assert User.showing_reblogs?(muter, muted) == true
1020 describe "follow/2" do
1021 test "directly follows a non-locked local user" do
1022 [follower, followed] = insert_pair(:user)
1023 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1025 assert User.following?(follower, followed)
1029 describe "unfollow/2" do
1030 test "also unsubscribes a user" do
1031 [follower, followed] = insert_pair(:user)
1032 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1033 {:ok, _subscription} = User.subscribe(follower, followed)
1035 assert User.subscribed_to?(follower, followed)
1037 {:ok, follower} = CommonAPI.unfollow(follower, followed)
1039 refute User.subscribed_to?(follower, followed)
1042 test "cancels a pending follow for a local user" do
1043 follower = insert(:user)
1044 followed = insert(:user, locked: true)
1046 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1047 CommonAPI.follow(follower, followed)
1049 assert User.get_follow_state(follower, followed) == :follow_pending
1050 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1051 assert User.get_follow_state(follower, followed) == nil
1053 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1054 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1059 "object" => %{"type" => "Follow", "state" => "cancelled"}
1061 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1064 test "cancels a pending follow for a remote user" do
1065 follower = insert(:user)
1066 followed = insert(:user, locked: true, local: false, ap_enabled: true)
1068 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1069 CommonAPI.follow(follower, followed)
1071 assert User.get_follow_state(follower, followed) == :follow_pending
1072 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1073 assert User.get_follow_state(follower, followed) == nil
1075 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1076 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1081 "object" => %{"type" => "Follow", "state" => "cancelled"}
1083 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1087 describe "accept_follow_request/2" do
1088 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
1089 user = insert(:user, locked: true)
1090 follower = insert(:user)
1091 follower_two = insert(:user)
1093 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1094 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1095 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1097 assert follow_activity.data["state"] == "pending"
1098 assert follow_activity_two.data["state"] == "pending"
1099 assert follow_activity_three.data["state"] == "pending"
1101 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
1103 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
1104 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
1105 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1108 test "after rejection, it sets all existing pending follow request states to 'reject'" do
1109 user = insert(:user, locked: true)
1110 follower = insert(:user)
1111 follower_two = insert(:user)
1113 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1114 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1115 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1117 assert follow_activity.data["state"] == "pending"
1118 assert follow_activity_two.data["state"] == "pending"
1119 assert follow_activity_three.data["state"] == "pending"
1121 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
1123 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
1124 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
1125 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1128 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
1129 user = insert(:user, locked: true)
1130 not_follower = insert(:user)
1131 CommonAPI.accept_follow_request(not_follower, user)
1133 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
1137 describe "vote/3" do
1138 test "does not allow to vote twice" do
1139 user = insert(:user)
1140 other_user = insert(:user)
1143 CommonAPI.post(user, %{
1144 status: "Am I cute?",
1145 poll: %{options: ["Yes", "No"], expires_in: 20}
1148 object = Object.normalize(activity)
1150 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1152 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1156 describe "listen/2" do
1157 test "returns a valid activity" do
1158 user = insert(:user)
1161 CommonAPI.listen(user, %{
1162 title: "lain radio episode 1",
1163 album: "lain radio",
1168 object = Object.normalize(activity)
1170 assert object.data["title"] == "lain radio episode 1"
1172 assert Visibility.get_visibility(activity) == "public"
1175 test "respects visibility=private" do
1176 user = insert(:user)
1179 CommonAPI.listen(user, %{
1180 title: "lain radio episode 1",
1181 album: "lain radio",
1184 visibility: "private"
1187 object = Object.normalize(activity)
1189 assert object.data["title"] == "lain radio episode 1"
1191 assert Visibility.get_visibility(activity) == "private"