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 "posting chat messages" do
29 setup do: clear_config([:instance, :chat_limit])
31 test "it posts a chat message without content but with an attachment" do
32 author = insert(:user)
33 recipient = insert(:user)
36 content_type: "image/jpg",
37 path: Path.absname("test/fixtures/image.jpg"),
38 filename: "an_image.jpg"
41 {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id)
62 CommonAPI.post_chat_message(
70 Notification.for_user_and_activity(recipient, activity)
71 |> Repo.preload(:activity)
73 assert called(Pleroma.Web.Push.send(notification))
74 assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
75 assert called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
81 test "it adds html newlines" do
82 author = insert(:user)
83 recipient = insert(:user)
85 other_user = insert(:user)
88 CommonAPI.post_chat_message(
94 assert other_user.ap_id not in activity.recipients
96 object = Object.normalize(activity, false)
98 assert object.data["content"] == "uguu<br/>uguuu"
101 test "it linkifies" do
102 author = insert(:user)
103 recipient = insert(:user)
105 other_user = insert(:user)
108 CommonAPI.post_chat_message(
111 "https://example.org is the site of @#{other_user.nickname} #2hu"
114 assert other_user.ap_id not in activity.recipients
116 object = Object.normalize(activity, false)
118 assert object.data["content"] ==
119 "<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=\"#{
121 }\" 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>"
124 test "it posts a chat message" do
125 author = insert(:user)
126 recipient = insert(:user)
129 CommonAPI.post_chat_message(
132 "a test message <script>alert('uuu')</script> :firefox:"
135 assert activity.data["type"] == "Create"
136 assert activity.local
137 object = Object.normalize(activity)
139 assert object.data["type"] == "ChatMessage"
140 assert object.data["to"] == [recipient.ap_id]
142 assert object.data["content"] ==
143 "a test message <script>alert('uuu')</script> :firefox:"
145 assert object.data["emoji"] == %{
146 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
149 assert Chat.get(author.id, recipient.ap_id)
150 assert Chat.get(recipient.id, author.ap_id)
152 assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
155 test "it reject messages over the local limit" do
156 Pleroma.Config.put([:instance, :chat_limit], 2)
158 author = insert(:user)
159 recipient = insert(:user)
162 CommonAPI.post_chat_message(
168 assert message == :content_too_long
172 describe "unblocking" do
173 test "it works even without an existing block activity" do
174 blocked = insert(:user)
175 blocker = insert(:user)
176 User.block(blocker, blocked)
178 assert User.blocks?(blocker, blocked)
179 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
180 refute User.blocks?(blocker, blocked)
184 describe "deletion" do
185 test "it works with pruned objects" do
188 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
190 clear_config([:instance, :federating], true)
192 Object.normalize(post, false)
195 with_mock Pleroma.Web.Federator,
196 publish: fn _ -> nil end do
197 assert {:ok, delete} = CommonAPI.delete(post.id, user)
199 assert called(Pleroma.Web.Federator.publish(delete))
202 refute Activity.get_by_id(post.id)
205 test "it allows users to delete their posts" do
208 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
210 clear_config([:instance, :federating], true)
212 with_mock Pleroma.Web.Federator,
213 publish: fn _ -> nil end do
214 assert {:ok, delete} = CommonAPI.delete(post.id, user)
216 assert called(Pleroma.Web.Federator.publish(delete))
219 refute Activity.get_by_id(post.id)
222 test "it does not allow a user to delete their posts" do
224 other_user = insert(:user)
226 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
228 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
229 assert Activity.get_by_id(post.id)
232 test "it allows moderators to delete other user's posts" do
234 moderator = insert(:user, is_moderator: true)
236 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
238 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
241 refute Activity.get_by_id(post.id)
244 test "it allows admins to delete other user's posts" do
246 moderator = insert(:user, is_admin: true)
248 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
250 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
253 refute Activity.get_by_id(post.id)
256 test "superusers deleting non-local posts won't federate the delete" do
257 # This is the user of the ingested activity
261 ap_id: "http://mastodon.example.org/users/admin",
262 last_refreshed_at: NaiveDateTime.utc_now()
265 moderator = insert(:user, is_admin: true)
268 File.read!("test/fixtures/mastodon-post-activity.json")
271 {:ok, post} = Transmogrifier.handle_incoming(data)
273 with_mock Pleroma.Web.Federator,
274 publish: fn _ -> nil end do
275 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
277 refute called(Pleroma.Web.Federator.publish(:_))
280 refute Activity.get_by_id(post.id)
284 test "favoriting race condition" do
286 users_serial = insert_list(10, :user)
287 users = insert_list(10, :user)
289 {:ok, activity} = CommonAPI.post(user, %{status: "."})
292 |> Enum.map(fn user ->
293 CommonAPI.favorite(user, activity.id)
296 object = Object.get_by_ap_id(activity.data["object"])
297 assert object.data["like_count"] == 10
300 |> Enum.map(fn user ->
302 CommonAPI.favorite(user, activity.id)
305 |> Enum.map(&Task.await/1)
307 object = Object.get_by_ap_id(activity.data["object"])
308 assert object.data["like_count"] == 20
311 test "repeating race condition" do
313 users_serial = insert_list(10, :user)
314 users = insert_list(10, :user)
316 {:ok, activity} = CommonAPI.post(user, %{status: "."})
319 |> Enum.map(fn user ->
320 CommonAPI.repeat(activity.id, user)
323 object = Object.get_by_ap_id(activity.data["object"])
324 assert object.data["announcement_count"] == 10
327 |> Enum.map(fn user ->
329 CommonAPI.repeat(activity.id, user)
332 |> Enum.map(&Task.await/1)
334 object = Object.get_by_ap_id(activity.data["object"])
335 assert object.data["announcement_count"] == 20
338 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
340 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
342 [participation] = Participation.for_user(user)
345 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
347 assert Visibility.is_direct?(convo_reply)
349 assert activity.data["context"] == convo_reply.data["context"]
352 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
354 jafnhar = insert(:user)
355 tridi = insert(:user)
358 CommonAPI.post(har, %{
359 status: "@#{jafnhar.nickname} hey",
363 assert har.ap_id in activity.recipients
364 assert jafnhar.ap_id in activity.recipients
366 [participation] = Participation.for_user(har)
369 CommonAPI.post(har, %{
370 status: "I don't really like @#{tridi.nickname}",
371 visibility: "direct",
372 in_reply_to_status_id: activity.id,
373 in_reply_to_conversation_id: participation.id
376 assert har.ap_id in activity.recipients
377 assert jafnhar.ap_id in activity.recipients
378 refute tridi.ap_id in activity.recipients
381 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
383 jafnhar = insert(:user)
384 tridi = insert(:user)
386 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
389 CommonAPI.post(har, %{
390 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
394 refute tridi.ap_id in activity.recipients
395 assert jafnhar.ap_id in activity.recipients
398 test "it de-duplicates tags" do
400 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
402 object = Object.normalize(activity)
404 assert object.data["tag"] == ["2hu"]
407 test "it adds emoji in the object" do
409 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
411 assert Object.normalize(activity).data["emoji"]["firefox"]
414 describe "posting" do
415 test "it supports explicit addressing" do
417 user_two = insert(:user)
418 user_three = insert(:user)
419 user_four = insert(:user)
422 CommonAPI.post(user, %{
424 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
425 to: [user_two.nickname, user_four.nickname, "nonexistent"]
428 assert user.ap_id in activity.recipients
429 assert user_two.ap_id in activity.recipients
430 assert user_four.ap_id in activity.recipients
431 refute user_three.ap_id in activity.recipients
434 test "it filters out obviously bad tags when accepting a post as HTML" do
437 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
440 CommonAPI.post(user, %{
442 content_type: "text/html"
445 object = Object.normalize(activity)
447 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
450 test "it filters out obviously bad tags when accepting a post as Markdown" do
453 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
456 CommonAPI.post(user, %{
458 content_type: "text/markdown"
461 object = Object.normalize(activity)
463 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
466 test "it does not allow replies to direct messages that are not direct messages themselves" do
469 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
472 CommonAPI.post(user, %{
474 visibility: "direct",
475 in_reply_to_status_id: activity.id
478 Enum.each(["public", "private", "unlisted"], fn visibility ->
479 assert {:error, "The message visibility must be direct"} =
480 CommonAPI.post(user, %{
482 visibility: visibility,
483 in_reply_to_status_id: activity.id
488 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
490 other_user = insert(:user)
491 third_user = insert(:user)
493 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
496 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
498 # The OP is implicitly added
499 assert user.ap_id in open_answer.recipients
501 {:ok, secret_answer} =
502 CommonAPI.post(other_user, %{
503 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
504 in_reply_to_status_id: post.id,
508 assert third_user.ap_id in secret_answer.recipients
510 # The OP is not added
511 refute user.ap_id in secret_answer.recipients
514 test "it allows to address a list" do
516 {:ok, list} = Pleroma.List.create("foo", user)
518 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
520 assert activity.data["bcc"] == [list.ap_id]
521 assert activity.recipients == [list.ap_id, user.ap_id]
522 assert activity.data["listMessage"] == list.ap_id
525 test "it returns error when status is empty and no attachments" do
528 assert {:error, "Cannot post an empty status without attachments"} =
529 CommonAPI.post(user, %{status: ""})
532 test "it validates character limits are correctly enforced" do
533 Pleroma.Config.put([:instance, :limit], 5)
537 assert {:error, "The status is over the character limit"} =
538 CommonAPI.post(user, %{status: "foobar"})
540 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
543 test "it can handle activities that expire" do
547 NaiveDateTime.utc_now()
548 |> NaiveDateTime.truncate(:second)
549 |> NaiveDateTime.add(1_000_000, :second)
551 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
553 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
554 assert expiration.scheduled_at == expires_at
558 describe "reactions" do
559 test "reacting to a status with an emoji" do
561 other_user = insert(:user)
563 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
565 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
567 assert reaction.data["actor"] == user.ap_id
568 assert reaction.data["content"] == "👍"
570 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
572 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
575 test "unreacting to a status with an emoji" do
577 other_user = insert(:user)
579 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
580 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
582 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
584 assert unreaction.data["type"] == "Undo"
585 assert unreaction.data["object"] == reaction.data["id"]
586 assert unreaction.local
589 test "repeating a status" do
591 other_user = insert(:user)
593 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
595 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
596 assert Visibility.is_public?(announce_activity)
599 test "can't repeat a repeat" do
601 other_user = insert(:user)
602 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
604 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
606 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
609 test "repeating a status privately" do
611 other_user = insert(:user)
613 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
615 {:ok, %Activity{} = announce_activity} =
616 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
618 assert Visibility.is_private?(announce_activity)
619 refute Visibility.visible_for_user?(announce_activity, nil)
622 test "favoriting a status" do
624 other_user = insert(:user)
626 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
628 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
629 assert data["type"] == "Like"
630 assert data["actor"] == user.ap_id
631 assert data["object"] == post_activity.data["object"]
634 test "retweeting a status twice returns the status" do
636 other_user = insert(:user)
638 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
639 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
640 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
643 test "favoriting a status twice returns ok, but without the like activity" do
645 other_user = insert(:user)
647 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
648 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
649 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
653 describe "pinned statuses" do
655 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
658 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
660 [user: user, activity: activity]
663 test "pin status", %{user: user, activity: activity} do
664 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
667 user = refresh_record(user)
669 assert %User{pinned_activities: [^id]} = user
672 test "pin poll", %{user: user} do
674 CommonAPI.post(user, %{
675 status: "How is fediverse today?",
676 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
679 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
682 user = refresh_record(user)
684 assert %User{pinned_activities: [^id]} = user
687 test "unlisted statuses can be pinned", %{user: user} do
688 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
689 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
692 test "only self-authored can be pinned", %{activity: activity} do
695 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
698 test "max pinned statuses", %{user: user, activity: activity_one} do
699 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
701 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
703 user = refresh_record(user)
705 assert {:error, "You have already pinned the maximum number of statuses"} =
706 CommonAPI.pin(activity_two.id, user)
709 test "unpin status", %{user: user, activity: activity} do
710 {:ok, activity} = CommonAPI.pin(activity.id, user)
712 user = refresh_record(user)
716 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
718 user = refresh_record(user)
720 assert %User{pinned_activities: []} = user
723 test "should unpin when deleting a status", %{user: user, activity: activity} do
724 {:ok, activity} = CommonAPI.pin(activity.id, user)
726 user = refresh_record(user)
728 assert {:ok, _} = CommonAPI.delete(activity.id, user)
730 user = refresh_record(user)
732 assert %User{pinned_activities: []} = user
736 describe "mute tests" do
740 activity = insert(:note_activity)
742 [user: user, activity: activity]
745 test "add mute", %{user: user, activity: activity} do
746 {:ok, _} = CommonAPI.add_mute(user, activity)
747 assert CommonAPI.thread_muted?(user, activity)
750 test "remove mute", %{user: user, activity: activity} do
751 CommonAPI.add_mute(user, activity)
752 {:ok, _} = CommonAPI.remove_mute(user, activity)
753 refute CommonAPI.thread_muted?(user, activity)
756 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
757 CommonAPI.add_mute(user, activity)
758 {:error, _} = CommonAPI.add_mute(user, activity)
762 describe "reports" do
763 test "creates a report" do
764 reporter = insert(:user)
765 target_user = insert(:user)
767 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
769 reporter_ap_id = reporter.ap_id
770 target_ap_id = target_user.ap_id
771 activity_ap_id = activity.data["id"]
775 account_id: target_user.id,
777 status_ids: [activity.id]
782 "id" => activity_ap_id,
783 "content" => "foobar",
784 "published" => activity.object.data["published"],
785 "actor" => AccountView.render("show.json", %{user: target_user})
788 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
791 actor: ^reporter_ap_id,
794 "content" => ^comment,
795 "object" => [^target_ap_id, ^note_obj],
801 test "updates report state" do
802 [reporter, target_user] = insert_pair(:user)
803 activity = insert(:note_activity, user: target_user)
805 {:ok, %Activity{id: report_id}} =
806 CommonAPI.report(reporter, %{
807 account_id: target_user.id,
808 comment: "I feel offended",
809 status_ids: [activity.id]
812 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
814 assert report.data["state"] == "resolved"
816 [reported_user, activity_id] = report.data["object"]
818 assert reported_user == target_user.ap_id
819 assert activity_id == activity.data["id"]
822 test "does not update report state when state is unsupported" do
823 [reporter, target_user] = insert_pair(:user)
824 activity = insert(:note_activity, user: target_user)
826 {:ok, %Activity{id: report_id}} =
827 CommonAPI.report(reporter, %{
828 account_id: target_user.id,
829 comment: "I feel offended",
830 status_ids: [activity.id]
833 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
836 test "updates state of multiple reports" do
837 [reporter, target_user] = insert_pair(:user)
838 activity = insert(:note_activity, user: target_user)
840 {:ok, %Activity{id: first_report_id}} =
841 CommonAPI.report(reporter, %{
842 account_id: target_user.id,
843 comment: "I feel offended",
844 status_ids: [activity.id]
847 {:ok, %Activity{id: second_report_id}} =
848 CommonAPI.report(reporter, %{
849 account_id: target_user.id,
850 comment: "I feel very offended!",
851 status_ids: [activity.id]
855 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
857 first_report = Activity.get_by_id(first_report_id)
858 second_report = Activity.get_by_id(second_report_id)
860 assert report_ids -- [first_report_id, second_report_id] == []
861 assert first_report.data["state"] == "resolved"
862 assert second_report.data["state"] == "resolved"
866 describe "reblog muting" do
868 muter = insert(:user)
870 muted = insert(:user)
872 [muter: muter, muted: muted]
875 test "add a reblog mute", %{muter: muter, muted: muted} do
876 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
878 assert User.showing_reblogs?(muter, muted) == false
881 test "remove a reblog mute", %{muter: muter, muted: muted} do
882 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
883 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
885 assert User.showing_reblogs?(muter, muted) == true
889 describe "unfollow/2" do
890 test "also unsubscribes a user" do
891 [follower, followed] = insert_pair(:user)
892 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
893 {:ok, _subscription} = User.subscribe(follower, followed)
895 assert User.subscribed_to?(follower, followed)
897 {:ok, follower} = CommonAPI.unfollow(follower, followed)
899 refute User.subscribed_to?(follower, followed)
902 test "cancels a pending follow for a local user" do
903 follower = insert(:user)
904 followed = insert(:user, locked: true)
906 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
907 CommonAPI.follow(follower, followed)
909 assert User.get_follow_state(follower, followed) == :follow_pending
910 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
911 assert User.get_follow_state(follower, followed) == nil
913 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
914 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
919 "object" => %{"type" => "Follow", "state" => "cancelled"}
921 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
924 test "cancels a pending follow for a remote user" do
925 follower = insert(:user)
926 followed = insert(:user, locked: true, local: false, ap_enabled: true)
928 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
929 CommonAPI.follow(follower, followed)
931 assert User.get_follow_state(follower, followed) == :follow_pending
932 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
933 assert User.get_follow_state(follower, followed) == nil
935 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
936 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
941 "object" => %{"type" => "Follow", "state" => "cancelled"}
943 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
947 describe "accept_follow_request/2" do
948 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
949 user = insert(:user, locked: true)
950 follower = insert(:user)
951 follower_two = insert(:user)
953 {:ok, follow_activity} = ActivityPub.follow(follower, user)
954 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
955 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
957 assert follow_activity.data["state"] == "pending"
958 assert follow_activity_two.data["state"] == "pending"
959 assert follow_activity_three.data["state"] == "pending"
961 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
963 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
964 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
965 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
968 test "after rejection, it sets all existing pending follow request states to 'reject'" do
969 user = insert(:user, locked: true)
970 follower = insert(:user)
971 follower_two = insert(:user)
973 {:ok, follow_activity} = ActivityPub.follow(follower, user)
974 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
975 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
977 assert follow_activity.data["state"] == "pending"
978 assert follow_activity_two.data["state"] == "pending"
979 assert follow_activity_three.data["state"] == "pending"
981 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
983 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
984 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
985 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
988 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
989 user = insert(:user, locked: true)
990 not_follower = insert(:user)
991 CommonAPI.accept_follow_request(not_follower, user)
993 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
998 test "does not allow to vote twice" do
1000 other_user = insert(:user)
1003 CommonAPI.post(user, %{
1004 status: "Am I cute?",
1005 poll: %{options: ["Yes", "No"], expires_in: 20}
1008 object = Object.normalize(activity)
1010 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1012 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1016 describe "listen/2" do
1017 test "returns a valid activity" do
1018 user = insert(:user)
1021 CommonAPI.listen(user, %{
1022 title: "lain radio episode 1",
1023 album: "lain radio",
1028 object = Object.normalize(activity)
1030 assert object.data["title"] == "lain radio episode 1"
1032 assert Visibility.get_visibility(activity) == "public"
1035 test "respects visibility=private" do
1036 user = insert(:user)
1039 CommonAPI.listen(user, %{
1040 title: "lain radio episode 1",
1041 album: "lain radio",
1044 visibility: "private"
1047 object = Object.normalize(activity)
1049 assert object.data["title"] == "lain radio episode 1"
1051 assert Visibility.get_visibility(activity) == "private"