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
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Transmogrifier
15 alias Pleroma.Web.ActivityPub.Visibility
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.CommonAPI
19 import Pleroma.Factory
22 require Pleroma.Constants
24 setup do: clear_config([:instance, :safe_dm_mentions])
25 setup do: clear_config([:instance, :limit])
26 setup do: clear_config([:instance, :max_pinned_statuses])
28 describe "blocking" do
30 blocker = insert(:user)
31 blocked = insert(:user)
32 User.follow(blocker, blocked)
33 User.follow(blocked, blocker)
34 %{blocker: blocker, blocked: blocked}
37 test "it blocks and federates", %{blocker: blocker, blocked: blocked} do
38 clear_config([:instance, :federating], true)
40 with_mock Pleroma.Web.Federator,
41 publish: fn _ -> nil end do
42 assert {:ok, block} = CommonAPI.block(blocker, blocked)
45 assert User.blocks?(blocker, blocked)
46 refute User.following?(blocker, blocked)
47 refute User.following?(blocked, blocker)
49 assert called(Pleroma.Web.Federator.publish(block))
53 test "it blocks and does not federate if outgoing blocks are disabled", %{
57 clear_config([:instance, :federating], true)
58 clear_config([:activitypub, :outgoing_blocks], false)
60 with_mock Pleroma.Web.Federator,
61 publish: fn _ -> nil end do
62 assert {:ok, block} = CommonAPI.block(blocker, blocked)
65 assert User.blocks?(blocker, blocked)
66 refute User.following?(blocker, blocked)
67 refute User.following?(blocked, blocker)
69 refute called(Pleroma.Web.Federator.publish(block))
74 describe "posting chat messages" do
75 setup do: clear_config([:instance, :chat_limit])
77 test "it posts a chat message without content but with an attachment" do
78 author = insert(:user)
79 recipient = insert(:user)
82 content_type: "image/jpg",
83 path: Path.absname("test/fixtures/image.jpg"),
84 filename: "an_image.jpg"
87 {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id)
103 send: fn _ -> nil end
108 CommonAPI.post_chat_message(
116 Notification.for_user_and_activity(recipient, activity)
117 |> Repo.preload(:activity)
119 assert called(Pleroma.Web.Push.send(notification))
120 assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
121 assert called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
127 test "it adds html newlines" do
128 author = insert(:user)
129 recipient = insert(:user)
131 other_user = insert(:user)
134 CommonAPI.post_chat_message(
140 assert other_user.ap_id not in activity.recipients
142 object = Object.normalize(activity, false)
144 assert object.data["content"] == "uguu<br/>uguuu"
147 test "it linkifies" do
148 author = insert(:user)
149 recipient = insert(:user)
151 other_user = insert(:user)
154 CommonAPI.post_chat_message(
157 "https://example.org is the site of @#{other_user.nickname} #2hu"
160 assert other_user.ap_id not in activity.recipients
162 object = Object.normalize(activity, false)
164 assert object.data["content"] ==
165 "<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=\"#{
167 }\" 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>"
170 test "it posts a chat message" do
171 author = insert(:user)
172 recipient = insert(:user)
175 CommonAPI.post_chat_message(
178 "a test message <script>alert('uuu')</script> :firefox:"
181 assert activity.data["type"] == "Create"
182 assert activity.local
183 object = Object.normalize(activity)
185 assert object.data["type"] == "ChatMessage"
186 assert object.data["to"] == [recipient.ap_id]
188 assert object.data["content"] ==
189 "a test message <script>alert('uuu')</script> :firefox:"
191 assert object.data["emoji"] == %{
192 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
195 assert Chat.get(author.id, recipient.ap_id)
196 assert Chat.get(recipient.id, author.ap_id)
198 assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
201 test "it reject messages over the local limit" do
202 Pleroma.Config.put([:instance, :chat_limit], 2)
204 author = insert(:user)
205 recipient = insert(:user)
208 CommonAPI.post_chat_message(
214 assert message == :content_too_long
218 describe "unblocking" do
219 test "it works even without an existing block activity" do
220 blocked = insert(:user)
221 blocker = insert(:user)
222 User.block(blocker, blocked)
224 assert User.blocks?(blocker, blocked)
225 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
226 refute User.blocks?(blocker, blocked)
230 describe "deletion" do
231 test "it works with pruned objects" do
234 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
236 clear_config([:instance, :federating], true)
238 Object.normalize(post, false)
241 with_mock Pleroma.Web.Federator,
242 publish: fn _ -> nil end do
243 assert {:ok, delete} = CommonAPI.delete(post.id, user)
245 assert called(Pleroma.Web.Federator.publish(delete))
248 refute Activity.get_by_id(post.id)
251 test "it allows users to delete their posts" do
254 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
256 clear_config([:instance, :federating], true)
258 with_mock Pleroma.Web.Federator,
259 publish: fn _ -> nil end do
260 assert {:ok, delete} = CommonAPI.delete(post.id, user)
262 assert called(Pleroma.Web.Federator.publish(delete))
265 refute Activity.get_by_id(post.id)
268 test "it does not allow a user to delete their posts" do
270 other_user = insert(:user)
272 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
274 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
275 assert Activity.get_by_id(post.id)
278 test "it allows moderators to delete other user's posts" do
280 moderator = insert(:user, is_moderator: true)
282 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
284 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
287 refute Activity.get_by_id(post.id)
290 test "it allows admins to delete other user's posts" do
292 moderator = insert(:user, is_admin: true)
294 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
296 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
299 refute Activity.get_by_id(post.id)
302 test "superusers deleting non-local posts won't federate the delete" do
303 # This is the user of the ingested activity
307 ap_id: "http://mastodon.example.org/users/admin",
308 last_refreshed_at: NaiveDateTime.utc_now()
311 moderator = insert(:user, is_admin: true)
314 File.read!("test/fixtures/mastodon-post-activity.json")
317 {:ok, post} = Transmogrifier.handle_incoming(data)
319 with_mock Pleroma.Web.Federator,
320 publish: fn _ -> nil end do
321 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
323 refute called(Pleroma.Web.Federator.publish(:_))
326 refute Activity.get_by_id(post.id)
330 test "favoriting race condition" do
332 users_serial = insert_list(10, :user)
333 users = insert_list(10, :user)
335 {:ok, activity} = CommonAPI.post(user, %{status: "."})
338 |> Enum.map(fn user ->
339 CommonAPI.favorite(user, activity.id)
342 object = Object.get_by_ap_id(activity.data["object"])
343 assert object.data["like_count"] == 10
346 |> Enum.map(fn user ->
348 CommonAPI.favorite(user, activity.id)
351 |> Enum.map(&Task.await/1)
353 object = Object.get_by_ap_id(activity.data["object"])
354 assert object.data["like_count"] == 20
357 test "repeating race condition" do
359 users_serial = insert_list(10, :user)
360 users = insert_list(10, :user)
362 {:ok, activity} = CommonAPI.post(user, %{status: "."})
365 |> Enum.map(fn user ->
366 CommonAPI.repeat(activity.id, user)
369 object = Object.get_by_ap_id(activity.data["object"])
370 assert object.data["announcement_count"] == 10
373 |> Enum.map(fn user ->
375 CommonAPI.repeat(activity.id, user)
378 |> Enum.map(&Task.await/1)
380 object = Object.get_by_ap_id(activity.data["object"])
381 assert object.data["announcement_count"] == 20
384 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
386 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
388 [participation] = Participation.for_user(user)
391 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
393 assert Visibility.is_direct?(convo_reply)
395 assert activity.data["context"] == convo_reply.data["context"]
398 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
400 jafnhar = insert(:user)
401 tridi = insert(:user)
404 CommonAPI.post(har, %{
405 status: "@#{jafnhar.nickname} hey",
409 assert har.ap_id in activity.recipients
410 assert jafnhar.ap_id in activity.recipients
412 [participation] = Participation.for_user(har)
415 CommonAPI.post(har, %{
416 status: "I don't really like @#{tridi.nickname}",
417 visibility: "direct",
418 in_reply_to_status_id: activity.id,
419 in_reply_to_conversation_id: participation.id
422 assert har.ap_id in activity.recipients
423 assert jafnhar.ap_id in activity.recipients
424 refute tridi.ap_id in activity.recipients
427 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
429 jafnhar = insert(:user)
430 tridi = insert(:user)
432 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
435 CommonAPI.post(har, %{
436 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
440 refute tridi.ap_id in activity.recipients
441 assert jafnhar.ap_id in activity.recipients
444 test "it de-duplicates tags" do
446 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
448 object = Object.normalize(activity)
450 assert object.data["tag"] == ["2hu"]
453 test "it adds emoji in the object" do
455 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
457 assert Object.normalize(activity).data["emoji"]["firefox"]
460 describe "posting" do
461 test "it supports explicit addressing" do
463 user_two = insert(:user)
464 user_three = insert(:user)
465 user_four = insert(:user)
468 CommonAPI.post(user, %{
470 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
471 to: [user_two.nickname, user_four.nickname, "nonexistent"]
474 assert user.ap_id in activity.recipients
475 assert user_two.ap_id in activity.recipients
476 assert user_four.ap_id in activity.recipients
477 refute user_three.ap_id in activity.recipients
480 test "it filters out obviously bad tags when accepting a post as HTML" do
483 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
486 CommonAPI.post(user, %{
488 content_type: "text/html"
491 object = Object.normalize(activity)
493 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
494 assert object.data["source"] == post
497 test "it filters out obviously bad tags when accepting a post as Markdown" do
500 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
503 CommonAPI.post(user, %{
505 content_type: "text/markdown"
508 object = Object.normalize(activity)
510 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
511 assert object.data["source"] == post
514 test "it does not allow replies to direct messages that are not direct messages themselves" do
517 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
520 CommonAPI.post(user, %{
522 visibility: "direct",
523 in_reply_to_status_id: activity.id
526 Enum.each(["public", "private", "unlisted"], fn visibility ->
527 assert {:error, "The message visibility must be direct"} =
528 CommonAPI.post(user, %{
530 visibility: visibility,
531 in_reply_to_status_id: activity.id
536 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
538 other_user = insert(:user)
539 third_user = insert(:user)
541 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
544 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
546 # The OP is implicitly added
547 assert user.ap_id in open_answer.recipients
549 {:ok, secret_answer} =
550 CommonAPI.post(other_user, %{
551 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
552 in_reply_to_status_id: post.id,
556 assert third_user.ap_id in secret_answer.recipients
558 # The OP is not added
559 refute user.ap_id in secret_answer.recipients
562 test "it allows to address a list" do
564 {:ok, list} = Pleroma.List.create("foo", user)
566 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
568 assert activity.data["bcc"] == [list.ap_id]
569 assert activity.recipients == [list.ap_id, user.ap_id]
570 assert activity.data["listMessage"] == list.ap_id
573 test "it returns error when status is empty and no attachments" do
576 assert {:error, "Cannot post an empty status without attachments"} =
577 CommonAPI.post(user, %{status: ""})
580 test "it validates character limits are correctly enforced" do
581 Pleroma.Config.put([:instance, :limit], 5)
585 assert {:error, "The status is over the character limit"} =
586 CommonAPI.post(user, %{status: "foobar"})
588 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
591 test "it can handle activities that expire" do
595 NaiveDateTime.utc_now()
596 |> NaiveDateTime.truncate(:second)
597 |> NaiveDateTime.add(1_000_000, :second)
599 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
601 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
602 assert expiration.scheduled_at == expires_at
606 describe "reactions" do
607 test "reacting to a status with an emoji" do
609 other_user = insert(:user)
611 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
613 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
615 assert reaction.data["actor"] == user.ap_id
616 assert reaction.data["content"] == "👍"
618 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
620 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
623 test "unreacting to a status with an emoji" do
625 other_user = insert(:user)
627 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
628 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
630 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
632 assert unreaction.data["type"] == "Undo"
633 assert unreaction.data["object"] == reaction.data["id"]
634 assert unreaction.local
637 test "repeating a status" do
639 other_user = insert(:user)
641 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
643 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
644 assert Visibility.is_public?(announce_activity)
647 test "can't repeat a repeat" do
649 other_user = insert(:user)
650 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
652 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
654 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
657 test "repeating a status privately" do
659 other_user = insert(:user)
661 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
663 {:ok, %Activity{} = announce_activity} =
664 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
666 assert Visibility.is_private?(announce_activity)
667 refute Visibility.visible_for_user?(announce_activity, nil)
670 test "favoriting a status" do
672 other_user = insert(:user)
674 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
676 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
677 assert data["type"] == "Like"
678 assert data["actor"] == user.ap_id
679 assert data["object"] == post_activity.data["object"]
682 test "retweeting a status twice returns the status" do
684 other_user = insert(:user)
686 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
687 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
688 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
691 test "favoriting a status twice returns ok, but without the like activity" do
693 other_user = insert(:user)
695 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
696 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
697 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
701 describe "pinned statuses" do
703 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
706 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
708 [user: user, activity: activity]
711 test "pin status", %{user: user, activity: activity} do
712 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
715 user = refresh_record(user)
717 assert %User{pinned_activities: [^id]} = user
720 test "pin poll", %{user: user} do
722 CommonAPI.post(user, %{
723 status: "How is fediverse today?",
724 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
727 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
730 user = refresh_record(user)
732 assert %User{pinned_activities: [^id]} = user
735 test "unlisted statuses can be pinned", %{user: user} do
736 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
737 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
740 test "only self-authored can be pinned", %{activity: activity} do
743 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
746 test "max pinned statuses", %{user: user, activity: activity_one} do
747 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
749 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
751 user = refresh_record(user)
753 assert {:error, "You have already pinned the maximum number of statuses"} =
754 CommonAPI.pin(activity_two.id, user)
757 test "unpin status", %{user: user, activity: activity} do
758 {:ok, activity} = CommonAPI.pin(activity.id, user)
760 user = refresh_record(user)
764 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
766 user = refresh_record(user)
768 assert %User{pinned_activities: []} = user
771 test "should unpin when deleting a status", %{user: user, activity: activity} do
772 {:ok, activity} = CommonAPI.pin(activity.id, user)
774 user = refresh_record(user)
776 assert {:ok, _} = CommonAPI.delete(activity.id, user)
778 user = refresh_record(user)
780 assert %User{pinned_activities: []} = user
784 describe "mute tests" do
788 activity = insert(:note_activity)
790 [user: user, activity: activity]
793 test "add mute", %{user: user, activity: activity} do
794 {:ok, _} = CommonAPI.add_mute(user, activity)
795 assert CommonAPI.thread_muted?(user, activity)
798 test "remove mute", %{user: user, activity: activity} do
799 CommonAPI.add_mute(user, activity)
800 {:ok, _} = CommonAPI.remove_mute(user, activity)
801 refute CommonAPI.thread_muted?(user, activity)
804 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
805 CommonAPI.add_mute(user, activity)
806 {:error, _} = CommonAPI.add_mute(user, activity)
810 describe "reports" do
811 test "creates a report" do
812 reporter = insert(:user)
813 target_user = insert(:user)
815 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
817 reporter_ap_id = reporter.ap_id
818 target_ap_id = target_user.ap_id
819 activity_ap_id = activity.data["id"]
823 account_id: target_user.id,
825 status_ids: [activity.id]
830 "id" => activity_ap_id,
831 "content" => "foobar",
832 "published" => activity.object.data["published"],
833 "actor" => AccountView.render("show.json", %{user: target_user})
836 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
839 actor: ^reporter_ap_id,
842 "content" => ^comment,
843 "object" => [^target_ap_id, ^note_obj],
849 test "updates report state" do
850 [reporter, target_user] = insert_pair(:user)
851 activity = insert(:note_activity, user: target_user)
853 {:ok, %Activity{id: report_id}} =
854 CommonAPI.report(reporter, %{
855 account_id: target_user.id,
856 comment: "I feel offended",
857 status_ids: [activity.id]
860 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
862 assert report.data["state"] == "resolved"
864 [reported_user, activity_id] = report.data["object"]
866 assert reported_user == target_user.ap_id
867 assert activity_id == activity.data["id"]
870 test "does not update report state when state is unsupported" do
871 [reporter, target_user] = insert_pair(:user)
872 activity = insert(:note_activity, user: target_user)
874 {:ok, %Activity{id: report_id}} =
875 CommonAPI.report(reporter, %{
876 account_id: target_user.id,
877 comment: "I feel offended",
878 status_ids: [activity.id]
881 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
884 test "updates state of multiple reports" do
885 [reporter, target_user] = insert_pair(:user)
886 activity = insert(:note_activity, user: target_user)
888 {:ok, %Activity{id: first_report_id}} =
889 CommonAPI.report(reporter, %{
890 account_id: target_user.id,
891 comment: "I feel offended",
892 status_ids: [activity.id]
895 {:ok, %Activity{id: second_report_id}} =
896 CommonAPI.report(reporter, %{
897 account_id: target_user.id,
898 comment: "I feel very offended!",
899 status_ids: [activity.id]
903 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
905 first_report = Activity.get_by_id(first_report_id)
906 second_report = Activity.get_by_id(second_report_id)
908 assert report_ids -- [first_report_id, second_report_id] == []
909 assert first_report.data["state"] == "resolved"
910 assert second_report.data["state"] == "resolved"
914 describe "reblog muting" do
916 muter = insert(:user)
918 muted = insert(:user)
920 [muter: muter, muted: muted]
923 test "add a reblog mute", %{muter: muter, muted: muted} do
924 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
926 assert User.showing_reblogs?(muter, muted) == false
929 test "remove a reblog mute", %{muter: muter, muted: muted} do
930 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
931 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
933 assert User.showing_reblogs?(muter, muted) == true
937 describe "follow/2" do
938 test "directly follows a non-locked local user" do
939 [follower, followed] = insert_pair(:user)
940 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
942 assert User.following?(follower, followed)
946 describe "unfollow/2" do
947 test "also unsubscribes a user" do
948 [follower, followed] = insert_pair(:user)
949 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
950 {:ok, _subscription} = User.subscribe(follower, followed)
952 assert User.subscribed_to?(follower, followed)
954 {:ok, follower} = CommonAPI.unfollow(follower, followed)
956 refute User.subscribed_to?(follower, followed)
959 test "cancels a pending follow for a local user" do
960 follower = insert(:user)
961 followed = insert(:user, locked: true)
963 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
964 CommonAPI.follow(follower, followed)
966 assert User.get_follow_state(follower, followed) == :follow_pending
967 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
968 assert User.get_follow_state(follower, followed) == nil
970 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
971 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
976 "object" => %{"type" => "Follow", "state" => "cancelled"}
978 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
981 test "cancels a pending follow for a remote user" do
982 follower = insert(:user)
983 followed = insert(:user, locked: true, local: false, ap_enabled: true)
985 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
986 CommonAPI.follow(follower, followed)
988 assert User.get_follow_state(follower, followed) == :follow_pending
989 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
990 assert User.get_follow_state(follower, followed) == nil
992 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
993 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
998 "object" => %{"type" => "Follow", "state" => "cancelled"}
1000 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1004 describe "accept_follow_request/2" do
1005 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
1006 user = insert(:user, locked: true)
1007 follower = insert(:user)
1008 follower_two = insert(:user)
1010 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1011 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1012 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1014 assert follow_activity.data["state"] == "pending"
1015 assert follow_activity_two.data["state"] == "pending"
1016 assert follow_activity_three.data["state"] == "pending"
1018 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
1020 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
1021 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
1022 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1025 test "after rejection, it sets all existing pending follow request states to 'reject'" do
1026 user = insert(:user, locked: true)
1027 follower = insert(:user)
1028 follower_two = insert(:user)
1030 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1031 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1032 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1034 assert follow_activity.data["state"] == "pending"
1035 assert follow_activity_two.data["state"] == "pending"
1036 assert follow_activity_three.data["state"] == "pending"
1038 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
1040 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
1041 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
1042 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1045 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
1046 user = insert(:user, locked: true)
1047 not_follower = insert(:user)
1048 CommonAPI.accept_follow_request(not_follower, user)
1050 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
1054 describe "vote/3" do
1055 test "does not allow to vote twice" do
1056 user = insert(:user)
1057 other_user = insert(:user)
1060 CommonAPI.post(user, %{
1061 status: "Am I cute?",
1062 poll: %{options: ["Yes", "No"], expires_in: 20}
1065 object = Object.normalize(activity)
1067 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1069 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1073 describe "listen/2" do
1074 test "returns a valid activity" do
1075 user = insert(:user)
1078 CommonAPI.listen(user, %{
1079 title: "lain radio episode 1",
1080 album: "lain radio",
1085 object = Object.normalize(activity)
1087 assert object.data["title"] == "lain radio episode 1"
1089 assert Visibility.get_visibility(activity) == "public"
1092 test "respects visibility=private" do
1093 user = insert(:user)
1096 CommonAPI.listen(user, %{
1097 title: "lain radio episode 1",
1098 album: "lain radio",
1101 visibility: "private"
1104 object = Object.normalize(activity)
1106 assert object.data["title"] == "lain radio episode 1"
1108 assert Visibility.get_visibility(activity) == "private"