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
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.ActivityPub.Visibility
15 alias Pleroma.Web.AdminAPI.AccountView
16 alias Pleroma.Web.CommonAPI
18 import Pleroma.Factory
21 require Pleroma.Constants
23 setup do: clear_config([:instance, :safe_dm_mentions])
24 setup do: clear_config([:instance, :limit])
25 setup do: clear_config([:instance, :max_pinned_statuses])
27 describe "posting chat messages" do
28 setup do: clear_config([:instance, :chat_limit])
30 test "it posts a chat message without content but with an attachment" do
31 author = insert(:user)
32 recipient = insert(:user)
35 content_type: "image/jpg",
36 path: Path.absname("test/fixtures/image.jpg"),
37 filename: "an_image.jpg"
40 {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id)
43 CommonAPI.post_chat_message(
53 test "it adds html newlines" do
54 author = insert(:user)
55 recipient = insert(:user)
57 other_user = insert(:user)
60 CommonAPI.post_chat_message(
66 assert other_user.ap_id not in activity.recipients
68 object = Object.normalize(activity, false)
70 assert object.data["content"] == "uguu<br/>uguuu"
73 test "it linkifies" do
74 author = insert(:user)
75 recipient = insert(:user)
77 other_user = insert(:user)
80 CommonAPI.post_chat_message(
83 "https://example.org is the site of @#{other_user.nickname} #2hu"
86 assert other_user.ap_id not in activity.recipients
88 object = Object.normalize(activity, false)
90 assert object.data["content"] ==
91 "<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=\"#{
93 }\" 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>"
96 test "it posts a chat message" do
97 author = insert(:user)
98 recipient = insert(:user)
101 CommonAPI.post_chat_message(
104 "a test message <script>alert('uuu')</script> :firefox:"
107 assert activity.data["type"] == "Create"
108 assert activity.local
109 object = Object.normalize(activity)
111 assert object.data["type"] == "ChatMessage"
112 assert object.data["to"] == [recipient.ap_id]
114 assert object.data["content"] ==
115 "a test message <script>alert('uuu')</script> :firefox:"
117 assert object.data["emoji"] == %{
118 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
121 assert Chat.get(author.id, recipient.ap_id)
122 assert Chat.get(recipient.id, author.ap_id)
124 assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
127 test "it reject messages over the local limit" do
128 Pleroma.Config.put([:instance, :chat_limit], 2)
130 author = insert(:user)
131 recipient = insert(:user)
134 CommonAPI.post_chat_message(
140 assert message == :content_too_long
144 describe "unblocking" do
145 test "it works even without an existing block activity" do
146 blocked = insert(:user)
147 blocker = insert(:user)
148 User.block(blocker, blocked)
150 assert User.blocks?(blocker, blocked)
151 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
152 refute User.blocks?(blocker, blocked)
156 describe "deletion" do
157 test "it works with pruned objects" do
160 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
162 clear_config([:instance, :federating], true)
164 Object.normalize(post, false)
167 with_mock Pleroma.Web.Federator,
168 publish: fn _ -> nil end do
169 assert {:ok, delete} = CommonAPI.delete(post.id, user)
171 assert called(Pleroma.Web.Federator.publish(delete))
174 refute Activity.get_by_id(post.id)
177 test "it allows users to delete their posts" do
180 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
182 clear_config([:instance, :federating], true)
184 with_mock Pleroma.Web.Federator,
185 publish: fn _ -> nil end do
186 assert {:ok, delete} = CommonAPI.delete(post.id, user)
188 assert called(Pleroma.Web.Federator.publish(delete))
191 refute Activity.get_by_id(post.id)
194 test "it does not allow a user to delete their posts" do
196 other_user = insert(:user)
198 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
200 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
201 assert Activity.get_by_id(post.id)
204 test "it allows moderators to delete other user's posts" do
206 moderator = insert(:user, is_moderator: true)
208 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
210 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
213 refute Activity.get_by_id(post.id)
216 test "it allows admins to delete other user's posts" do
218 moderator = insert(:user, is_admin: true)
220 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
222 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
225 refute Activity.get_by_id(post.id)
228 test "superusers deleting non-local posts won't federate the delete" do
229 # This is the user of the ingested activity
233 ap_id: "http://mastodon.example.org/users/admin",
234 last_refreshed_at: NaiveDateTime.utc_now()
237 moderator = insert(:user, is_admin: true)
240 File.read!("test/fixtures/mastodon-post-activity.json")
243 {:ok, post} = Transmogrifier.handle_incoming(data)
245 with_mock Pleroma.Web.Federator,
246 publish: fn _ -> nil end do
247 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
249 refute called(Pleroma.Web.Federator.publish(:_))
252 refute Activity.get_by_id(post.id)
256 test "favoriting race condition" do
258 users_serial = insert_list(10, :user)
259 users = insert_list(10, :user)
261 {:ok, activity} = CommonAPI.post(user, %{status: "."})
264 |> Enum.map(fn user ->
265 CommonAPI.favorite(user, activity.id)
268 object = Object.get_by_ap_id(activity.data["object"])
269 assert object.data["like_count"] == 10
272 |> Enum.map(fn user ->
274 CommonAPI.favorite(user, activity.id)
277 |> Enum.map(&Task.await/1)
279 object = Object.get_by_ap_id(activity.data["object"])
280 assert object.data["like_count"] == 20
283 test "repeating race condition" do
285 users_serial = insert_list(10, :user)
286 users = insert_list(10, :user)
288 {:ok, activity} = CommonAPI.post(user, %{status: "."})
291 |> Enum.map(fn user ->
292 CommonAPI.repeat(activity.id, user)
295 object = Object.get_by_ap_id(activity.data["object"])
296 assert object.data["announcement_count"] == 10
299 |> Enum.map(fn user ->
301 CommonAPI.repeat(activity.id, user)
304 |> Enum.map(&Task.await/1)
306 object = Object.get_by_ap_id(activity.data["object"])
307 assert object.data["announcement_count"] == 20
310 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
312 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
314 [participation] = Participation.for_user(user)
317 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
319 assert Visibility.is_direct?(convo_reply)
321 assert activity.data["context"] == convo_reply.data["context"]
324 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
326 jafnhar = insert(:user)
327 tridi = insert(:user)
330 CommonAPI.post(har, %{
331 status: "@#{jafnhar.nickname} hey",
335 assert har.ap_id in activity.recipients
336 assert jafnhar.ap_id in activity.recipients
338 [participation] = Participation.for_user(har)
341 CommonAPI.post(har, %{
342 status: "I don't really like @#{tridi.nickname}",
343 visibility: "direct",
344 in_reply_to_status_id: activity.id,
345 in_reply_to_conversation_id: participation.id
348 assert har.ap_id in activity.recipients
349 assert jafnhar.ap_id in activity.recipients
350 refute tridi.ap_id in activity.recipients
353 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
355 jafnhar = insert(:user)
356 tridi = insert(:user)
358 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
361 CommonAPI.post(har, %{
362 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
366 refute tridi.ap_id in activity.recipients
367 assert jafnhar.ap_id in activity.recipients
370 test "it de-duplicates tags" do
372 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
374 object = Object.normalize(activity)
376 assert object.data["tag"] == ["2hu"]
379 test "it adds emoji in the object" do
381 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
383 assert Object.normalize(activity).data["emoji"]["firefox"]
386 describe "posting" do
387 test "it supports explicit addressing" do
389 user_two = insert(:user)
390 user_three = insert(:user)
391 user_four = insert(:user)
394 CommonAPI.post(user, %{
396 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
397 to: [user_two.nickname, user_four.nickname, "nonexistent"]
400 assert user.ap_id in activity.recipients
401 assert user_two.ap_id in activity.recipients
402 assert user_four.ap_id in activity.recipients
403 refute user_three.ap_id in activity.recipients
406 test "it filters out obviously bad tags when accepting a post as HTML" do
409 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
412 CommonAPI.post(user, %{
414 content_type: "text/html"
417 object = Object.normalize(activity)
419 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
422 test "it filters out obviously bad tags when accepting a post as Markdown" do
425 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
428 CommonAPI.post(user, %{
430 content_type: "text/markdown"
433 object = Object.normalize(activity)
435 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
438 test "it does not allow replies to direct messages that are not direct messages themselves" do
441 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
444 CommonAPI.post(user, %{
446 visibility: "direct",
447 in_reply_to_status_id: activity.id
450 Enum.each(["public", "private", "unlisted"], fn visibility ->
451 assert {:error, "The message visibility must be direct"} =
452 CommonAPI.post(user, %{
454 visibility: visibility,
455 in_reply_to_status_id: activity.id
460 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
462 other_user = insert(:user)
463 third_user = insert(:user)
465 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
468 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
470 # The OP is implicitly added
471 assert user.ap_id in open_answer.recipients
473 {:ok, secret_answer} =
474 CommonAPI.post(other_user, %{
475 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
476 in_reply_to_status_id: post.id,
480 assert third_user.ap_id in secret_answer.recipients
482 # The OP is not added
483 refute user.ap_id in secret_answer.recipients
486 test "it allows to address a list" do
488 {:ok, list} = Pleroma.List.create("foo", user)
490 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
492 assert activity.data["bcc"] == [list.ap_id]
493 assert activity.recipients == [list.ap_id, user.ap_id]
494 assert activity.data["listMessage"] == list.ap_id
497 test "it returns error when status is empty and no attachments" do
500 assert {:error, "Cannot post an empty status without attachments"} =
501 CommonAPI.post(user, %{status: ""})
504 test "it validates character limits are correctly enforced" do
505 Pleroma.Config.put([:instance, :limit], 5)
509 assert {:error, "The status is over the character limit"} =
510 CommonAPI.post(user, %{status: "foobar"})
512 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
515 test "it can handle activities that expire" do
519 NaiveDateTime.utc_now()
520 |> NaiveDateTime.truncate(:second)
521 |> NaiveDateTime.add(1_000_000, :second)
523 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
525 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
526 assert expiration.scheduled_at == expires_at
530 describe "reactions" do
531 test "reacting to a status with an emoji" do
533 other_user = insert(:user)
535 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
537 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
539 assert reaction.data["actor"] == user.ap_id
540 assert reaction.data["content"] == "👍"
542 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
544 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
547 test "unreacting to a status with an emoji" do
549 other_user = insert(:user)
551 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
552 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
554 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
556 assert unreaction.data["type"] == "Undo"
557 assert unreaction.data["object"] == reaction.data["id"]
558 assert unreaction.local
561 test "repeating a status" do
563 other_user = insert(:user)
565 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
567 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
568 assert Visibility.is_public?(announce_activity)
571 test "can't repeat a repeat" do
573 other_user = insert(:user)
574 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
576 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
578 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
581 test "repeating a status privately" do
583 other_user = insert(:user)
585 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
587 {:ok, %Activity{} = announce_activity} =
588 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
590 assert Visibility.is_private?(announce_activity)
591 refute Visibility.visible_for_user?(announce_activity, nil)
594 test "favoriting a status" do
596 other_user = insert(:user)
598 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
600 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
601 assert data["type"] == "Like"
602 assert data["actor"] == user.ap_id
603 assert data["object"] == post_activity.data["object"]
606 test "retweeting a status twice returns the status" do
608 other_user = insert(:user)
610 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
611 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
612 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
615 test "favoriting a status twice returns ok, but without the like activity" do
617 other_user = insert(:user)
619 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
620 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
621 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
625 describe "pinned statuses" do
627 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
630 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
632 [user: user, activity: activity]
635 test "pin status", %{user: user, activity: activity} do
636 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
639 user = refresh_record(user)
641 assert %User{pinned_activities: [^id]} = user
644 test "pin poll", %{user: user} do
646 CommonAPI.post(user, %{
647 status: "How is fediverse today?",
648 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
651 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
654 user = refresh_record(user)
656 assert %User{pinned_activities: [^id]} = user
659 test "unlisted statuses can be pinned", %{user: user} do
660 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
661 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
664 test "only self-authored can be pinned", %{activity: activity} do
667 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
670 test "max pinned statuses", %{user: user, activity: activity_one} do
671 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
673 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
675 user = refresh_record(user)
677 assert {:error, "You have already pinned the maximum number of statuses"} =
678 CommonAPI.pin(activity_two.id, user)
681 test "unpin status", %{user: user, activity: activity} do
682 {:ok, activity} = CommonAPI.pin(activity.id, user)
684 user = refresh_record(user)
688 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
690 user = refresh_record(user)
692 assert %User{pinned_activities: []} = user
695 test "should unpin when deleting a status", %{user: user, activity: activity} do
696 {:ok, activity} = CommonAPI.pin(activity.id, user)
698 user = refresh_record(user)
700 assert {:ok, _} = CommonAPI.delete(activity.id, user)
702 user = refresh_record(user)
704 assert %User{pinned_activities: []} = user
708 describe "mute tests" do
712 activity = insert(:note_activity)
714 [user: user, activity: activity]
717 test "add mute", %{user: user, activity: activity} do
718 {:ok, _} = CommonAPI.add_mute(user, activity)
719 assert CommonAPI.thread_muted?(user, activity)
722 test "remove mute", %{user: user, activity: activity} do
723 CommonAPI.add_mute(user, activity)
724 {:ok, _} = CommonAPI.remove_mute(user, activity)
725 refute CommonAPI.thread_muted?(user, activity)
728 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
729 CommonAPI.add_mute(user, activity)
730 {:error, _} = CommonAPI.add_mute(user, activity)
734 describe "reports" do
735 test "creates a report" do
736 reporter = insert(:user)
737 target_user = insert(:user)
739 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
741 reporter_ap_id = reporter.ap_id
742 target_ap_id = target_user.ap_id
743 activity_ap_id = activity.data["id"]
747 account_id: target_user.id,
749 status_ids: [activity.id]
754 "id" => activity_ap_id,
755 "content" => "foobar",
756 "published" => activity.object.data["published"],
757 "actor" => AccountView.render("show.json", %{user: target_user})
760 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
763 actor: ^reporter_ap_id,
766 "content" => ^comment,
767 "object" => [^target_ap_id, ^note_obj],
773 test "updates report state" do
774 [reporter, target_user] = insert_pair(:user)
775 activity = insert(:note_activity, user: target_user)
777 {:ok, %Activity{id: report_id}} =
778 CommonAPI.report(reporter, %{
779 account_id: target_user.id,
780 comment: "I feel offended",
781 status_ids: [activity.id]
784 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
786 assert report.data["state"] == "resolved"
788 [reported_user, activity_id] = report.data["object"]
790 assert reported_user == target_user.ap_id
791 assert activity_id == activity.data["id"]
794 test "does not update report state when state is unsupported" do
795 [reporter, target_user] = insert_pair(:user)
796 activity = insert(:note_activity, user: target_user)
798 {:ok, %Activity{id: report_id}} =
799 CommonAPI.report(reporter, %{
800 account_id: target_user.id,
801 comment: "I feel offended",
802 status_ids: [activity.id]
805 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
808 test "updates state of multiple reports" do
809 [reporter, target_user] = insert_pair(:user)
810 activity = insert(:note_activity, user: target_user)
812 {:ok, %Activity{id: first_report_id}} =
813 CommonAPI.report(reporter, %{
814 account_id: target_user.id,
815 comment: "I feel offended",
816 status_ids: [activity.id]
819 {:ok, %Activity{id: second_report_id}} =
820 CommonAPI.report(reporter, %{
821 account_id: target_user.id,
822 comment: "I feel very offended!",
823 status_ids: [activity.id]
827 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
829 first_report = Activity.get_by_id(first_report_id)
830 second_report = Activity.get_by_id(second_report_id)
832 assert report_ids -- [first_report_id, second_report_id] == []
833 assert first_report.data["state"] == "resolved"
834 assert second_report.data["state"] == "resolved"
838 describe "reblog muting" do
840 muter = insert(:user)
842 muted = insert(:user)
844 [muter: muter, muted: muted]
847 test "add a reblog mute", %{muter: muter, muted: muted} do
848 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
850 assert User.showing_reblogs?(muter, muted) == false
853 test "remove a reblog mute", %{muter: muter, muted: muted} do
854 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
855 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
857 assert User.showing_reblogs?(muter, muted) == true
861 describe "unfollow/2" do
862 test "also unsubscribes a user" do
863 [follower, followed] = insert_pair(:user)
864 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
865 {:ok, _subscription} = User.subscribe(follower, followed)
867 assert User.subscribed_to?(follower, followed)
869 {:ok, follower} = CommonAPI.unfollow(follower, followed)
871 refute User.subscribed_to?(follower, followed)
874 test "cancels a pending follow for a local user" do
875 follower = insert(:user)
876 followed = insert(:user, locked: true)
878 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
879 CommonAPI.follow(follower, followed)
881 assert User.get_follow_state(follower, followed) == :follow_pending
882 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
883 assert User.get_follow_state(follower, followed) == nil
885 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
886 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
891 "object" => %{"type" => "Follow", "state" => "cancelled"}
893 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
896 test "cancels a pending follow for a remote user" do
897 follower = insert(:user)
898 followed = insert(:user, locked: true, local: false, ap_enabled: true)
900 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
901 CommonAPI.follow(follower, followed)
903 assert User.get_follow_state(follower, followed) == :follow_pending
904 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
905 assert User.get_follow_state(follower, followed) == nil
907 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
908 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
913 "object" => %{"type" => "Follow", "state" => "cancelled"}
915 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
919 describe "accept_follow_request/2" do
920 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
921 user = insert(:user, locked: true)
922 follower = insert(:user)
923 follower_two = insert(:user)
925 {:ok, follow_activity} = ActivityPub.follow(follower, user)
926 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
927 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
929 assert follow_activity.data["state"] == "pending"
930 assert follow_activity_two.data["state"] == "pending"
931 assert follow_activity_three.data["state"] == "pending"
933 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
935 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
936 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
937 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
940 test "after rejection, it sets all existing pending follow request states to 'reject'" do
941 user = insert(:user, locked: true)
942 follower = insert(:user)
943 follower_two = insert(:user)
945 {:ok, follow_activity} = ActivityPub.follow(follower, user)
946 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
947 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
949 assert follow_activity.data["state"] == "pending"
950 assert follow_activity_two.data["state"] == "pending"
951 assert follow_activity_three.data["state"] == "pending"
953 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
955 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
956 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
957 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
960 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
961 user = insert(:user, locked: true)
962 not_follower = insert(:user)
963 CommonAPI.accept_follow_request(not_follower, user)
965 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
970 test "does not allow to vote twice" do
972 other_user = insert(:user)
975 CommonAPI.post(user, %{
976 status: "Am I cute?",
977 poll: %{options: ["Yes", "No"], expires_in: 20}
980 object = Object.normalize(activity)
982 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
984 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
988 describe "listen/2" do
989 test "returns a valid activity" do
993 CommonAPI.listen(user, %{
994 title: "lain radio episode 1",
1000 object = Object.normalize(activity)
1002 assert object.data["title"] == "lain radio episode 1"
1004 assert Visibility.get_visibility(activity) == "public"
1007 test "respects visibility=private" do
1008 user = insert(:user)
1011 CommonAPI.listen(user, %{
1012 title: "lain radio episode 1",
1013 album: "lain radio",
1016 visibility: "private"
1019 object = Object.normalize(activity)
1021 assert object.data["title"] == "lain radio episode 1"
1023 assert Visibility.get_visibility(activity) == "private"