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')"
448 assert object.data["source"] == post
451 test "it filters out obviously bad tags when accepting a post as Markdown" do
454 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
457 CommonAPI.post(user, %{
459 content_type: "text/markdown"
462 object = Object.normalize(activity)
464 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
465 assert object.data["source"] == post
468 test "it does not allow replies to direct messages that are not direct messages themselves" do
471 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
474 CommonAPI.post(user, %{
476 visibility: "direct",
477 in_reply_to_status_id: activity.id
480 Enum.each(["public", "private", "unlisted"], fn visibility ->
481 assert {:error, "The message visibility must be direct"} =
482 CommonAPI.post(user, %{
484 visibility: visibility,
485 in_reply_to_status_id: activity.id
490 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
492 other_user = insert(:user)
493 third_user = insert(:user)
495 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
498 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
500 # The OP is implicitly added
501 assert user.ap_id in open_answer.recipients
503 {:ok, secret_answer} =
504 CommonAPI.post(other_user, %{
505 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
506 in_reply_to_status_id: post.id,
510 assert third_user.ap_id in secret_answer.recipients
512 # The OP is not added
513 refute user.ap_id in secret_answer.recipients
516 test "it allows to address a list" do
518 {:ok, list} = Pleroma.List.create("foo", user)
520 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
522 assert activity.data["bcc"] == [list.ap_id]
523 assert activity.recipients == [list.ap_id, user.ap_id]
524 assert activity.data["listMessage"] == list.ap_id
527 test "it returns error when status is empty and no attachments" do
530 assert {:error, "Cannot post an empty status without attachments"} =
531 CommonAPI.post(user, %{status: ""})
534 test "it validates character limits are correctly enforced" do
535 Pleroma.Config.put([:instance, :limit], 5)
539 assert {:error, "The status is over the character limit"} =
540 CommonAPI.post(user, %{status: "foobar"})
542 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
545 test "it can handle activities that expire" do
549 NaiveDateTime.utc_now()
550 |> NaiveDateTime.truncate(:second)
551 |> NaiveDateTime.add(1_000_000, :second)
553 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
555 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
556 assert expiration.scheduled_at == expires_at
560 describe "reactions" do
561 test "reacting to a status with an emoji" do
563 other_user = insert(:user)
565 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
567 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
569 assert reaction.data["actor"] == user.ap_id
570 assert reaction.data["content"] == "👍"
572 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
574 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
577 test "unreacting to a status with an emoji" do
579 other_user = insert(:user)
581 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
582 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
584 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
586 assert unreaction.data["type"] == "Undo"
587 assert unreaction.data["object"] == reaction.data["id"]
588 assert unreaction.local
591 test "repeating a status" do
593 other_user = insert(:user)
595 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
597 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
598 assert Visibility.is_public?(announce_activity)
601 test "can't repeat a repeat" do
603 other_user = insert(:user)
604 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
606 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
608 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
611 test "repeating a status privately" do
613 other_user = insert(:user)
615 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
617 {:ok, %Activity{} = announce_activity} =
618 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
620 assert Visibility.is_private?(announce_activity)
621 refute Visibility.visible_for_user?(announce_activity, nil)
624 test "favoriting a status" do
626 other_user = insert(:user)
628 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
630 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
631 assert data["type"] == "Like"
632 assert data["actor"] == user.ap_id
633 assert data["object"] == post_activity.data["object"]
636 test "retweeting a status twice returns the status" do
638 other_user = insert(:user)
640 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
641 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
642 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
645 test "favoriting a status twice returns ok, but without the like activity" do
647 other_user = insert(:user)
649 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
650 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
651 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
655 describe "pinned statuses" do
657 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
660 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
662 [user: user, activity: activity]
665 test "pin status", %{user: user, activity: activity} do
666 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
669 user = refresh_record(user)
671 assert %User{pinned_activities: [^id]} = user
674 test "pin poll", %{user: user} do
676 CommonAPI.post(user, %{
677 status: "How is fediverse today?",
678 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
681 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
684 user = refresh_record(user)
686 assert %User{pinned_activities: [^id]} = user
689 test "unlisted statuses can be pinned", %{user: user} do
690 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
691 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
694 test "only self-authored can be pinned", %{activity: activity} do
697 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
700 test "max pinned statuses", %{user: user, activity: activity_one} do
701 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
703 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
705 user = refresh_record(user)
707 assert {:error, "You have already pinned the maximum number of statuses"} =
708 CommonAPI.pin(activity_two.id, user)
711 test "unpin status", %{user: user, activity: activity} do
712 {:ok, activity} = CommonAPI.pin(activity.id, user)
714 user = refresh_record(user)
718 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
720 user = refresh_record(user)
722 assert %User{pinned_activities: []} = user
725 test "should unpin when deleting a status", %{user: user, activity: activity} do
726 {:ok, activity} = CommonAPI.pin(activity.id, user)
728 user = refresh_record(user)
730 assert {:ok, _} = CommonAPI.delete(activity.id, user)
732 user = refresh_record(user)
734 assert %User{pinned_activities: []} = user
738 describe "mute tests" do
742 activity = insert(:note_activity)
744 [user: user, activity: activity]
747 test "add mute", %{user: user, activity: activity} do
748 {:ok, _} = CommonAPI.add_mute(user, activity)
749 assert CommonAPI.thread_muted?(user, activity)
752 test "remove mute", %{user: user, activity: activity} do
753 CommonAPI.add_mute(user, activity)
754 {:ok, _} = CommonAPI.remove_mute(user, activity)
755 refute CommonAPI.thread_muted?(user, activity)
758 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
759 CommonAPI.add_mute(user, activity)
760 {:error, _} = CommonAPI.add_mute(user, activity)
764 describe "reports" do
765 test "creates a report" do
766 reporter = insert(:user)
767 target_user = insert(:user)
769 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
771 reporter_ap_id = reporter.ap_id
772 target_ap_id = target_user.ap_id
773 activity_ap_id = activity.data["id"]
777 account_id: target_user.id,
779 status_ids: [activity.id]
784 "id" => activity_ap_id,
785 "content" => "foobar",
786 "published" => activity.object.data["published"],
787 "actor" => AccountView.render("show.json", %{user: target_user})
790 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
793 actor: ^reporter_ap_id,
796 "content" => ^comment,
797 "object" => [^target_ap_id, ^note_obj],
803 test "updates report state" do
804 [reporter, target_user] = insert_pair(:user)
805 activity = insert(:note_activity, user: target_user)
807 {:ok, %Activity{id: report_id}} =
808 CommonAPI.report(reporter, %{
809 account_id: target_user.id,
810 comment: "I feel offended",
811 status_ids: [activity.id]
814 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
816 assert report.data["state"] == "resolved"
818 [reported_user, activity_id] = report.data["object"]
820 assert reported_user == target_user.ap_id
821 assert activity_id == activity.data["id"]
824 test "does not update report state when state is unsupported" do
825 [reporter, target_user] = insert_pair(:user)
826 activity = insert(:note_activity, user: target_user)
828 {:ok, %Activity{id: report_id}} =
829 CommonAPI.report(reporter, %{
830 account_id: target_user.id,
831 comment: "I feel offended",
832 status_ids: [activity.id]
835 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
838 test "updates state of multiple reports" do
839 [reporter, target_user] = insert_pair(:user)
840 activity = insert(:note_activity, user: target_user)
842 {:ok, %Activity{id: first_report_id}} =
843 CommonAPI.report(reporter, %{
844 account_id: target_user.id,
845 comment: "I feel offended",
846 status_ids: [activity.id]
849 {:ok, %Activity{id: second_report_id}} =
850 CommonAPI.report(reporter, %{
851 account_id: target_user.id,
852 comment: "I feel very offended!",
853 status_ids: [activity.id]
857 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
859 first_report = Activity.get_by_id(first_report_id)
860 second_report = Activity.get_by_id(second_report_id)
862 assert report_ids -- [first_report_id, second_report_id] == []
863 assert first_report.data["state"] == "resolved"
864 assert second_report.data["state"] == "resolved"
868 describe "reblog muting" do
870 muter = insert(:user)
872 muted = insert(:user)
874 [muter: muter, muted: muted]
877 test "add a reblog mute", %{muter: muter, muted: muted} do
878 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
880 assert User.showing_reblogs?(muter, muted) == false
883 test "remove a reblog mute", %{muter: muter, muted: muted} do
884 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
885 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
887 assert User.showing_reblogs?(muter, muted) == true
891 describe "unfollow/2" do
892 test "also unsubscribes a user" do
893 [follower, followed] = insert_pair(:user)
894 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
895 {:ok, _subscription} = User.subscribe(follower, followed)
897 assert User.subscribed_to?(follower, followed)
899 {:ok, follower} = CommonAPI.unfollow(follower, followed)
901 refute User.subscribed_to?(follower, followed)
904 test "cancels a pending follow for a local user" do
905 follower = insert(:user)
906 followed = insert(:user, locked: true)
908 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
909 CommonAPI.follow(follower, followed)
911 assert User.get_follow_state(follower, followed) == :follow_pending
912 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
913 assert User.get_follow_state(follower, followed) == nil
915 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
916 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
921 "object" => %{"type" => "Follow", "state" => "cancelled"}
923 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
926 test "cancels a pending follow for a remote user" do
927 follower = insert(:user)
928 followed = insert(:user, locked: true, local: false, ap_enabled: true)
930 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
931 CommonAPI.follow(follower, followed)
933 assert User.get_follow_state(follower, followed) == :follow_pending
934 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
935 assert User.get_follow_state(follower, followed) == nil
937 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
938 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
943 "object" => %{"type" => "Follow", "state" => "cancelled"}
945 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
949 describe "accept_follow_request/2" do
950 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
951 user = insert(:user, locked: true)
952 follower = insert(:user)
953 follower_two = insert(:user)
955 {:ok, follow_activity} = ActivityPub.follow(follower, user)
956 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
957 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
959 assert follow_activity.data["state"] == "pending"
960 assert follow_activity_two.data["state"] == "pending"
961 assert follow_activity_three.data["state"] == "pending"
963 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
965 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
966 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
967 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
970 test "after rejection, it sets all existing pending follow request states to 'reject'" do
971 user = insert(:user, locked: true)
972 follower = insert(:user)
973 follower_two = insert(:user)
975 {:ok, follow_activity} = ActivityPub.follow(follower, user)
976 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
977 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
979 assert follow_activity.data["state"] == "pending"
980 assert follow_activity_two.data["state"] == "pending"
981 assert follow_activity_three.data["state"] == "pending"
983 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
985 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
986 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
987 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
990 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
991 user = insert(:user, locked: true)
992 not_follower = insert(:user)
993 CommonAPI.accept_follow_request(not_follower, user)
995 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
1000 test "does not allow to vote twice" do
1001 user = insert(:user)
1002 other_user = insert(:user)
1005 CommonAPI.post(user, %{
1006 status: "Am I cute?",
1007 poll: %{options: ["Yes", "No"], expires_in: 20}
1010 object = Object.normalize(activity)
1012 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1014 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1018 describe "listen/2" do
1019 test "returns a valid activity" do
1020 user = insert(:user)
1023 CommonAPI.listen(user, %{
1024 title: "lain radio episode 1",
1025 album: "lain radio",
1030 object = Object.normalize(activity)
1032 assert object.data["title"] == "lain radio episode 1"
1034 assert Visibility.get_visibility(activity) == "public"
1037 test "respects visibility=private" do
1038 user = insert(:user)
1041 CommonAPI.listen(user, %{
1042 title: "lain radio episode 1",
1043 album: "lain radio",
1046 visibility: "private"
1049 object = Object.normalize(activity)
1051 assert object.data["title"] == "lain radio episode 1"
1053 assert Visibility.get_visibility(activity) == "private"