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))
80 test "it adds html newlines" do
81 author = insert(:user)
82 recipient = insert(:user)
84 other_user = insert(:user)
87 CommonAPI.post_chat_message(
93 assert other_user.ap_id not in activity.recipients
95 object = Object.normalize(activity, false)
97 assert object.data["content"] == "uguu<br/>uguuu"
100 test "it linkifies" do
101 author = insert(:user)
102 recipient = insert(:user)
104 other_user = insert(:user)
107 CommonAPI.post_chat_message(
110 "https://example.org is the site of @#{other_user.nickname} #2hu"
113 assert other_user.ap_id not in activity.recipients
115 object = Object.normalize(activity, false)
117 assert object.data["content"] ==
118 "<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=\"#{
120 }\" 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>"
123 test "it posts a chat message" do
124 author = insert(:user)
125 recipient = insert(:user)
128 CommonAPI.post_chat_message(
131 "a test message <script>alert('uuu')</script> :firefox:"
134 assert activity.data["type"] == "Create"
135 assert activity.local
136 object = Object.normalize(activity)
138 assert object.data["type"] == "ChatMessage"
139 assert object.data["to"] == [recipient.ap_id]
141 assert object.data["content"] ==
142 "a test message <script>alert('uuu')</script> :firefox:"
144 assert object.data["emoji"] == %{
145 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
148 assert Chat.get(author.id, recipient.ap_id)
149 assert Chat.get(recipient.id, author.ap_id)
151 assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
154 test "it reject messages over the local limit" do
155 Pleroma.Config.put([:instance, :chat_limit], 2)
157 author = insert(:user)
158 recipient = insert(:user)
161 CommonAPI.post_chat_message(
167 assert message == :content_too_long
171 describe "unblocking" do
172 test "it works even without an existing block activity" do
173 blocked = insert(:user)
174 blocker = insert(:user)
175 User.block(blocker, blocked)
177 assert User.blocks?(blocker, blocked)
178 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
179 refute User.blocks?(blocker, blocked)
183 describe "deletion" do
184 test "it works with pruned objects" do
187 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
189 clear_config([:instance, :federating], true)
191 Object.normalize(post, false)
194 with_mock Pleroma.Web.Federator,
195 publish: fn _ -> nil end do
196 assert {:ok, delete} = CommonAPI.delete(post.id, user)
198 assert called(Pleroma.Web.Federator.publish(delete))
201 refute Activity.get_by_id(post.id)
204 test "it allows users to delete their posts" do
207 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
209 clear_config([:instance, :federating], true)
211 with_mock Pleroma.Web.Federator,
212 publish: fn _ -> nil end do
213 assert {:ok, delete} = CommonAPI.delete(post.id, user)
215 assert called(Pleroma.Web.Federator.publish(delete))
218 refute Activity.get_by_id(post.id)
221 test "it does not allow a user to delete their posts" do
223 other_user = insert(:user)
225 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
227 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
228 assert Activity.get_by_id(post.id)
231 test "it allows moderators to delete other user's posts" do
233 moderator = insert(:user, is_moderator: true)
235 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
237 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
240 refute Activity.get_by_id(post.id)
243 test "it allows admins to delete other user's posts" do
245 moderator = insert(:user, is_admin: true)
247 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
249 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
252 refute Activity.get_by_id(post.id)
255 test "superusers deleting non-local posts won't federate the delete" do
256 # This is the user of the ingested activity
260 ap_id: "http://mastodon.example.org/users/admin",
261 last_refreshed_at: NaiveDateTime.utc_now()
264 moderator = insert(:user, is_admin: true)
267 File.read!("test/fixtures/mastodon-post-activity.json")
270 {:ok, post} = Transmogrifier.handle_incoming(data)
272 with_mock Pleroma.Web.Federator,
273 publish: fn _ -> nil end do
274 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
276 refute called(Pleroma.Web.Federator.publish(:_))
279 refute Activity.get_by_id(post.id)
283 test "favoriting 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.favorite(user, activity.id)
295 object = Object.get_by_ap_id(activity.data["object"])
296 assert object.data["like_count"] == 10
299 |> Enum.map(fn user ->
301 CommonAPI.favorite(user, activity.id)
304 |> Enum.map(&Task.await/1)
306 object = Object.get_by_ap_id(activity.data["object"])
307 assert object.data["like_count"] == 20
310 test "repeating race condition" do
312 users_serial = insert_list(10, :user)
313 users = insert_list(10, :user)
315 {:ok, activity} = CommonAPI.post(user, %{status: "."})
318 |> Enum.map(fn user ->
319 CommonAPI.repeat(activity.id, user)
322 object = Object.get_by_ap_id(activity.data["object"])
323 assert object.data["announcement_count"] == 10
326 |> Enum.map(fn user ->
328 CommonAPI.repeat(activity.id, user)
331 |> Enum.map(&Task.await/1)
333 object = Object.get_by_ap_id(activity.data["object"])
334 assert object.data["announcement_count"] == 20
337 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
339 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
341 [participation] = Participation.for_user(user)
344 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
346 assert Visibility.is_direct?(convo_reply)
348 assert activity.data["context"] == convo_reply.data["context"]
351 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
353 jafnhar = insert(:user)
354 tridi = insert(:user)
357 CommonAPI.post(har, %{
358 status: "@#{jafnhar.nickname} hey",
362 assert har.ap_id in activity.recipients
363 assert jafnhar.ap_id in activity.recipients
365 [participation] = Participation.for_user(har)
368 CommonAPI.post(har, %{
369 status: "I don't really like @#{tridi.nickname}",
370 visibility: "direct",
371 in_reply_to_status_id: activity.id,
372 in_reply_to_conversation_id: participation.id
375 assert har.ap_id in activity.recipients
376 assert jafnhar.ap_id in activity.recipients
377 refute tridi.ap_id in activity.recipients
380 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
382 jafnhar = insert(:user)
383 tridi = insert(:user)
385 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
388 CommonAPI.post(har, %{
389 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
393 refute tridi.ap_id in activity.recipients
394 assert jafnhar.ap_id in activity.recipients
397 test "it de-duplicates tags" do
399 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
401 object = Object.normalize(activity)
403 assert object.data["tag"] == ["2hu"]
406 test "it adds emoji in the object" do
408 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
410 assert Object.normalize(activity).data["emoji"]["firefox"]
413 describe "posting" do
414 test "it supports explicit addressing" do
416 user_two = insert(:user)
417 user_three = insert(:user)
418 user_four = insert(:user)
421 CommonAPI.post(user, %{
423 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
424 to: [user_two.nickname, user_four.nickname, "nonexistent"]
427 assert user.ap_id in activity.recipients
428 assert user_two.ap_id in activity.recipients
429 assert user_four.ap_id in activity.recipients
430 refute user_three.ap_id in activity.recipients
433 test "it filters out obviously bad tags when accepting a post as HTML" do
436 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
439 CommonAPI.post(user, %{
441 content_type: "text/html"
444 object = Object.normalize(activity)
446 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
449 test "it filters out obviously bad tags when accepting a post as Markdown" do
452 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
455 CommonAPI.post(user, %{
457 content_type: "text/markdown"
460 object = Object.normalize(activity)
462 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
465 test "it does not allow replies to direct messages that are not direct messages themselves" do
468 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
471 CommonAPI.post(user, %{
473 visibility: "direct",
474 in_reply_to_status_id: activity.id
477 Enum.each(["public", "private", "unlisted"], fn visibility ->
478 assert {:error, "The message visibility must be direct"} =
479 CommonAPI.post(user, %{
481 visibility: visibility,
482 in_reply_to_status_id: activity.id
487 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
489 other_user = insert(:user)
490 third_user = insert(:user)
492 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
495 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
497 # The OP is implicitly added
498 assert user.ap_id in open_answer.recipients
500 {:ok, secret_answer} =
501 CommonAPI.post(other_user, %{
502 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
503 in_reply_to_status_id: post.id,
507 assert third_user.ap_id in secret_answer.recipients
509 # The OP is not added
510 refute user.ap_id in secret_answer.recipients
513 test "it allows to address a list" do
515 {:ok, list} = Pleroma.List.create("foo", user)
517 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
519 assert activity.data["bcc"] == [list.ap_id]
520 assert activity.recipients == [list.ap_id, user.ap_id]
521 assert activity.data["listMessage"] == list.ap_id
524 test "it returns error when status is empty and no attachments" do
527 assert {:error, "Cannot post an empty status without attachments"} =
528 CommonAPI.post(user, %{status: ""})
531 test "it validates character limits are correctly enforced" do
532 Pleroma.Config.put([:instance, :limit], 5)
536 assert {:error, "The status is over the character limit"} =
537 CommonAPI.post(user, %{status: "foobar"})
539 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
542 test "it can handle activities that expire" do
546 NaiveDateTime.utc_now()
547 |> NaiveDateTime.truncate(:second)
548 |> NaiveDateTime.add(1_000_000, :second)
550 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
552 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
553 assert expiration.scheduled_at == expires_at
557 describe "reactions" do
558 test "reacting to a status with an emoji" do
560 other_user = insert(:user)
562 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
564 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
566 assert reaction.data["actor"] == user.ap_id
567 assert reaction.data["content"] == "👍"
569 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
571 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
574 test "unreacting to a status with an emoji" do
576 other_user = insert(:user)
578 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
579 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
581 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
583 assert unreaction.data["type"] == "Undo"
584 assert unreaction.data["object"] == reaction.data["id"]
585 assert unreaction.local
588 test "repeating a status" do
590 other_user = insert(:user)
592 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
594 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
595 assert Visibility.is_public?(announce_activity)
598 test "can't repeat a repeat" do
600 other_user = insert(:user)
601 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
603 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
605 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
608 test "repeating a status privately" do
610 other_user = insert(:user)
612 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
614 {:ok, %Activity{} = announce_activity} =
615 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
617 assert Visibility.is_private?(announce_activity)
618 refute Visibility.visible_for_user?(announce_activity, nil)
621 test "favoriting a status" do
623 other_user = insert(:user)
625 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
627 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
628 assert data["type"] == "Like"
629 assert data["actor"] == user.ap_id
630 assert data["object"] == post_activity.data["object"]
633 test "retweeting a status twice returns the status" do
635 other_user = insert(:user)
637 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
638 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
639 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
642 test "favoriting a status twice returns ok, but without the like activity" do
644 other_user = insert(:user)
646 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
647 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
648 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
652 describe "pinned statuses" do
654 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
657 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
659 [user: user, activity: activity]
662 test "pin status", %{user: user, activity: activity} do
663 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
666 user = refresh_record(user)
668 assert %User{pinned_activities: [^id]} = user
671 test "pin poll", %{user: user} do
673 CommonAPI.post(user, %{
674 status: "How is fediverse today?",
675 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
678 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
681 user = refresh_record(user)
683 assert %User{pinned_activities: [^id]} = user
686 test "unlisted statuses can be pinned", %{user: user} do
687 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
688 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
691 test "only self-authored can be pinned", %{activity: activity} do
694 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
697 test "max pinned statuses", %{user: user, activity: activity_one} do
698 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
700 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
702 user = refresh_record(user)
704 assert {:error, "You have already pinned the maximum number of statuses"} =
705 CommonAPI.pin(activity_two.id, user)
708 test "unpin status", %{user: user, activity: activity} do
709 {:ok, activity} = CommonAPI.pin(activity.id, user)
711 user = refresh_record(user)
715 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
717 user = refresh_record(user)
719 assert %User{pinned_activities: []} = user
722 test "should unpin when deleting a status", %{user: user, activity: activity} do
723 {:ok, activity} = CommonAPI.pin(activity.id, user)
725 user = refresh_record(user)
727 assert {:ok, _} = CommonAPI.delete(activity.id, user)
729 user = refresh_record(user)
731 assert %User{pinned_activities: []} = user
735 describe "mute tests" do
739 activity = insert(:note_activity)
741 [user: user, activity: activity]
744 test "add mute", %{user: user, activity: activity} do
745 {:ok, _} = CommonAPI.add_mute(user, activity)
746 assert CommonAPI.thread_muted?(user, activity)
749 test "remove mute", %{user: user, activity: activity} do
750 CommonAPI.add_mute(user, activity)
751 {:ok, _} = CommonAPI.remove_mute(user, activity)
752 refute CommonAPI.thread_muted?(user, activity)
755 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
756 CommonAPI.add_mute(user, activity)
757 {:error, _} = CommonAPI.add_mute(user, activity)
761 describe "reports" do
762 test "creates a report" do
763 reporter = insert(:user)
764 target_user = insert(:user)
766 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
768 reporter_ap_id = reporter.ap_id
769 target_ap_id = target_user.ap_id
770 activity_ap_id = activity.data["id"]
774 account_id: target_user.id,
776 status_ids: [activity.id]
781 "id" => activity_ap_id,
782 "content" => "foobar",
783 "published" => activity.object.data["published"],
784 "actor" => AccountView.render("show.json", %{user: target_user})
787 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
790 actor: ^reporter_ap_id,
793 "content" => ^comment,
794 "object" => [^target_ap_id, ^note_obj],
800 test "updates report state" do
801 [reporter, target_user] = insert_pair(:user)
802 activity = insert(:note_activity, user: target_user)
804 {:ok, %Activity{id: report_id}} =
805 CommonAPI.report(reporter, %{
806 account_id: target_user.id,
807 comment: "I feel offended",
808 status_ids: [activity.id]
811 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
813 assert report.data["state"] == "resolved"
815 [reported_user, activity_id] = report.data["object"]
817 assert reported_user == target_user.ap_id
818 assert activity_id == activity.data["id"]
821 test "does not update report state when state is unsupported" do
822 [reporter, target_user] = insert_pair(:user)
823 activity = insert(:note_activity, user: target_user)
825 {:ok, %Activity{id: report_id}} =
826 CommonAPI.report(reporter, %{
827 account_id: target_user.id,
828 comment: "I feel offended",
829 status_ids: [activity.id]
832 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
835 test "updates state of multiple reports" do
836 [reporter, target_user] = insert_pair(:user)
837 activity = insert(:note_activity, user: target_user)
839 {:ok, %Activity{id: first_report_id}} =
840 CommonAPI.report(reporter, %{
841 account_id: target_user.id,
842 comment: "I feel offended",
843 status_ids: [activity.id]
846 {:ok, %Activity{id: second_report_id}} =
847 CommonAPI.report(reporter, %{
848 account_id: target_user.id,
849 comment: "I feel very offended!",
850 status_ids: [activity.id]
854 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
856 first_report = Activity.get_by_id(first_report_id)
857 second_report = Activity.get_by_id(second_report_id)
859 assert report_ids -- [first_report_id, second_report_id] == []
860 assert first_report.data["state"] == "resolved"
861 assert second_report.data["state"] == "resolved"
865 describe "reblog muting" do
867 muter = insert(:user)
869 muted = insert(:user)
871 [muter: muter, muted: muted]
874 test "add a reblog mute", %{muter: muter, muted: muted} do
875 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
877 assert User.showing_reblogs?(muter, muted) == false
880 test "remove a reblog mute", %{muter: muter, muted: muted} do
881 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
882 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
884 assert User.showing_reblogs?(muter, muted) == true
888 describe "unfollow/2" do
889 test "also unsubscribes a user" do
890 [follower, followed] = insert_pair(:user)
891 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
892 {:ok, _subscription} = User.subscribe(follower, followed)
894 assert User.subscribed_to?(follower, followed)
896 {:ok, follower} = CommonAPI.unfollow(follower, followed)
898 refute User.subscribed_to?(follower, followed)
901 test "cancels a pending follow for a local user" do
902 follower = insert(:user)
903 followed = insert(:user, locked: true)
905 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
906 CommonAPI.follow(follower, followed)
908 assert User.get_follow_state(follower, followed) == :follow_pending
909 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
910 assert User.get_follow_state(follower, followed) == nil
912 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
913 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
918 "object" => %{"type" => "Follow", "state" => "cancelled"}
920 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
923 test "cancels a pending follow for a remote user" do
924 follower = insert(:user)
925 followed = insert(:user, locked: true, local: false, ap_enabled: true)
927 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
928 CommonAPI.follow(follower, followed)
930 assert User.get_follow_state(follower, followed) == :follow_pending
931 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
932 assert User.get_follow_state(follower, followed) == nil
934 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
935 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
940 "object" => %{"type" => "Follow", "state" => "cancelled"}
942 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
946 describe "accept_follow_request/2" do
947 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
948 user = insert(:user, locked: true)
949 follower = insert(:user)
950 follower_two = insert(:user)
952 {:ok, follow_activity} = ActivityPub.follow(follower, user)
953 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
954 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
956 assert follow_activity.data["state"] == "pending"
957 assert follow_activity_two.data["state"] == "pending"
958 assert follow_activity_three.data["state"] == "pending"
960 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
962 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
963 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
964 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
967 test "after rejection, it sets all existing pending follow request states to 'reject'" do
968 user = insert(:user, locked: true)
969 follower = insert(:user)
970 follower_two = insert(:user)
972 {:ok, follow_activity} = ActivityPub.follow(follower, user)
973 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
974 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
976 assert follow_activity.data["state"] == "pending"
977 assert follow_activity_two.data["state"] == "pending"
978 assert follow_activity_three.data["state"] == "pending"
980 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
982 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
983 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
984 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
987 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
988 user = insert(:user, locked: true)
989 not_follower = insert(:user)
990 CommonAPI.accept_follow_request(not_follower, user)
992 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
997 test "does not allow to vote twice" do
999 other_user = insert(:user)
1002 CommonAPI.post(user, %{
1003 status: "Am I cute?",
1004 poll: %{options: ["Yes", "No"], expires_in: 20}
1007 object = Object.normalize(activity)
1009 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1011 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1015 describe "listen/2" do
1016 test "returns a valid activity" do
1017 user = insert(:user)
1020 CommonAPI.listen(user, %{
1021 title: "lain radio episode 1",
1022 album: "lain radio",
1027 object = Object.normalize(activity)
1029 assert object.data["title"] == "lain radio episode 1"
1031 assert Visibility.get_visibility(activity) == "public"
1034 test "respects visibility=private" do
1035 user = insert(:user)
1038 CommonAPI.listen(user, %{
1039 title: "lain radio episode 1",
1040 album: "lain radio",
1043 visibility: "private"
1046 object = Object.normalize(activity)
1048 assert object.data["title"] == "lain radio episode 1"
1050 assert Visibility.get_visibility(activity) == "private"