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
217 test "it reject messages via MRF" do
218 clear_config([:mrf_keyword, :reject], ["GNO"])
219 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
221 author = insert(:user)
222 recipient = insert(:user)
224 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
225 CommonAPI.post_chat_message(author, recipient, "GNO/Linux")
229 describe "unblocking" do
230 test "it works even without an existing block activity" do
231 blocked = insert(:user)
232 blocker = insert(:user)
233 User.block(blocker, blocked)
235 assert User.blocks?(blocker, blocked)
236 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
237 refute User.blocks?(blocker, blocked)
241 describe "deletion" do
242 test "it works with pruned objects" do
245 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
247 clear_config([:instance, :federating], true)
249 Object.normalize(post, false)
252 with_mock Pleroma.Web.Federator,
253 publish: fn _ -> nil end do
254 assert {:ok, delete} = CommonAPI.delete(post.id, user)
256 assert called(Pleroma.Web.Federator.publish(delete))
259 refute Activity.get_by_id(post.id)
262 test "it allows users to delete their posts" do
265 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
267 clear_config([:instance, :federating], true)
269 with_mock Pleroma.Web.Federator,
270 publish: fn _ -> nil end do
271 assert {:ok, delete} = CommonAPI.delete(post.id, user)
273 assert called(Pleroma.Web.Federator.publish(delete))
276 refute Activity.get_by_id(post.id)
279 test "it does not allow a user to delete their posts" do
281 other_user = insert(:user)
283 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
285 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
286 assert Activity.get_by_id(post.id)
289 test "it allows moderators to delete other user's posts" do
291 moderator = insert(:user, is_moderator: true)
293 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
295 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
298 refute Activity.get_by_id(post.id)
301 test "it allows admins to delete other user's posts" do
303 moderator = insert(:user, is_admin: true)
305 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
307 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
310 refute Activity.get_by_id(post.id)
313 test "superusers deleting non-local posts won't federate the delete" do
314 # This is the user of the ingested activity
318 ap_id: "http://mastodon.example.org/users/admin",
319 last_refreshed_at: NaiveDateTime.utc_now()
322 moderator = insert(:user, is_admin: true)
325 File.read!("test/fixtures/mastodon-post-activity.json")
328 {:ok, post} = Transmogrifier.handle_incoming(data)
330 with_mock Pleroma.Web.Federator,
331 publish: fn _ -> nil end do
332 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
334 refute called(Pleroma.Web.Federator.publish(:_))
337 refute Activity.get_by_id(post.id)
341 test "favoriting race condition" do
343 users_serial = insert_list(10, :user)
344 users = insert_list(10, :user)
346 {:ok, activity} = CommonAPI.post(user, %{status: "."})
349 |> Enum.map(fn user ->
350 CommonAPI.favorite(user, activity.id)
353 object = Object.get_by_ap_id(activity.data["object"])
354 assert object.data["like_count"] == 10
357 |> Enum.map(fn user ->
359 CommonAPI.favorite(user, activity.id)
362 |> Enum.map(&Task.await/1)
364 object = Object.get_by_ap_id(activity.data["object"])
365 assert object.data["like_count"] == 20
368 test "repeating race condition" do
370 users_serial = insert_list(10, :user)
371 users = insert_list(10, :user)
373 {:ok, activity} = CommonAPI.post(user, %{status: "."})
376 |> Enum.map(fn user ->
377 CommonAPI.repeat(activity.id, user)
380 object = Object.get_by_ap_id(activity.data["object"])
381 assert object.data["announcement_count"] == 10
384 |> Enum.map(fn user ->
386 CommonAPI.repeat(activity.id, user)
389 |> Enum.map(&Task.await/1)
391 object = Object.get_by_ap_id(activity.data["object"])
392 assert object.data["announcement_count"] == 20
395 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
397 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
399 [participation] = Participation.for_user(user)
402 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
404 assert Visibility.is_direct?(convo_reply)
406 assert activity.data["context"] == convo_reply.data["context"]
409 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
411 jafnhar = insert(:user)
412 tridi = insert(:user)
415 CommonAPI.post(har, %{
416 status: "@#{jafnhar.nickname} hey",
420 assert har.ap_id in activity.recipients
421 assert jafnhar.ap_id in activity.recipients
423 [participation] = Participation.for_user(har)
426 CommonAPI.post(har, %{
427 status: "I don't really like @#{tridi.nickname}",
428 visibility: "direct",
429 in_reply_to_status_id: activity.id,
430 in_reply_to_conversation_id: participation.id
433 assert har.ap_id in activity.recipients
434 assert jafnhar.ap_id in activity.recipients
435 refute tridi.ap_id in activity.recipients
438 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
440 jafnhar = insert(:user)
441 tridi = insert(:user)
443 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
446 CommonAPI.post(har, %{
447 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
451 refute tridi.ap_id in activity.recipients
452 assert jafnhar.ap_id in activity.recipients
455 test "it de-duplicates tags" do
457 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
459 object = Object.normalize(activity)
461 assert object.data["tag"] == ["2hu"]
464 test "it adds emoji in the object" do
466 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
468 assert Object.normalize(activity).data["emoji"]["firefox"]
471 describe "posting" do
472 test "deactivated users can't post" do
473 user = insert(:user, deactivated: true)
474 assert {:error, _} = CommonAPI.post(user, %{status: "ye"})
477 test "it supports explicit addressing" do
479 user_two = insert(:user)
480 user_three = insert(:user)
481 user_four = insert(:user)
484 CommonAPI.post(user, %{
486 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
487 to: [user_two.nickname, user_four.nickname, "nonexistent"]
490 assert user.ap_id in activity.recipients
491 assert user_two.ap_id in activity.recipients
492 assert user_four.ap_id in activity.recipients
493 refute user_three.ap_id in activity.recipients
496 test "it filters out obviously bad tags when accepting a post as HTML" do
499 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
502 CommonAPI.post(user, %{
504 content_type: "text/html"
507 object = Object.normalize(activity)
509 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
510 assert object.data["source"] == post
513 test "it filters out obviously bad tags when accepting a post as Markdown" do
516 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
519 CommonAPI.post(user, %{
521 content_type: "text/markdown"
524 object = Object.normalize(activity)
526 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
527 assert object.data["source"] == post
530 test "it does not allow replies to direct messages that are not direct messages themselves" do
533 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
536 CommonAPI.post(user, %{
538 visibility: "direct",
539 in_reply_to_status_id: activity.id
542 Enum.each(["public", "private", "unlisted"], fn visibility ->
543 assert {:error, "The message visibility must be direct"} =
544 CommonAPI.post(user, %{
546 visibility: visibility,
547 in_reply_to_status_id: activity.id
552 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
554 other_user = insert(:user)
555 third_user = insert(:user)
557 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
560 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
562 # The OP is implicitly added
563 assert user.ap_id in open_answer.recipients
565 {:ok, secret_answer} =
566 CommonAPI.post(other_user, %{
567 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
568 in_reply_to_status_id: post.id,
572 assert third_user.ap_id in secret_answer.recipients
574 # The OP is not added
575 refute user.ap_id in secret_answer.recipients
578 test "it allows to address a list" do
580 {:ok, list} = Pleroma.List.create("foo", user)
582 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
584 assert activity.data["bcc"] == [list.ap_id]
585 assert activity.recipients == [list.ap_id, user.ap_id]
586 assert activity.data["listMessage"] == list.ap_id
589 test "it returns error when status is empty and no attachments" do
592 assert {:error, "Cannot post an empty status without attachments"} =
593 CommonAPI.post(user, %{status: ""})
596 test "it validates character limits are correctly enforced" do
597 Pleroma.Config.put([:instance, :limit], 5)
601 assert {:error, "The status is over the character limit"} =
602 CommonAPI.post(user, %{status: "foobar"})
604 assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
607 test "it can handle activities that expire" do
611 NaiveDateTime.utc_now()
612 |> NaiveDateTime.truncate(:second)
613 |> NaiveDateTime.add(1_000_000, :second)
615 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
617 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
618 assert expiration.scheduled_at == expires_at
622 describe "reactions" do
623 test "reacting to a status with an emoji" do
625 other_user = insert(:user)
627 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
629 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
631 assert reaction.data["actor"] == user.ap_id
632 assert reaction.data["content"] == "👍"
634 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
636 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
639 test "unreacting to a status with an emoji" do
641 other_user = insert(:user)
643 clear_config([:instance, :federating], true)
645 with_mock Pleroma.Web.Federator,
646 publish: fn _ -> nil end do
647 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
648 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
650 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
652 assert unreaction.data["type"] == "Undo"
653 assert unreaction.data["object"] == reaction.data["id"]
654 assert unreaction.local
656 # On federation, it contains the undone (and deleted) object
657 unreaction_with_object = %{
659 | data: Map.put(unreaction.data, "object", reaction.data)
662 assert called(Pleroma.Web.Federator.publish(unreaction_with_object))
666 test "repeating a status" do
668 other_user = insert(:user)
670 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
672 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
673 assert Visibility.is_public?(announce_activity)
676 test "can't repeat a repeat" do
678 other_user = insert(:user)
679 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
681 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
683 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
686 test "repeating a status privately" do
688 other_user = insert(:user)
690 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
692 {:ok, %Activity{} = announce_activity} =
693 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
695 assert Visibility.is_private?(announce_activity)
696 refute Visibility.visible_for_user?(announce_activity, nil)
699 test "favoriting a status" do
701 other_user = insert(:user)
703 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
705 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
706 assert data["type"] == "Like"
707 assert data["actor"] == user.ap_id
708 assert data["object"] == post_activity.data["object"]
711 test "retweeting a status twice returns the status" do
713 other_user = insert(:user)
715 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
716 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
717 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
720 test "favoriting a status twice returns ok, but without the like activity" do
722 other_user = insert(:user)
724 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
725 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
726 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
730 describe "pinned statuses" do
732 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
735 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
737 [user: user, activity: activity]
740 test "pin status", %{user: user, activity: activity} do
741 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
744 user = refresh_record(user)
746 assert %User{pinned_activities: [^id]} = user
749 test "pin poll", %{user: user} do
751 CommonAPI.post(user, %{
752 status: "How is fediverse today?",
753 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
756 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
759 user = refresh_record(user)
761 assert %User{pinned_activities: [^id]} = user
764 test "unlisted statuses can be pinned", %{user: user} do
765 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
766 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
769 test "only self-authored can be pinned", %{activity: activity} do
772 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
775 test "max pinned statuses", %{user: user, activity: activity_one} do
776 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
778 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
780 user = refresh_record(user)
782 assert {:error, "You have already pinned the maximum number of statuses"} =
783 CommonAPI.pin(activity_two.id, user)
786 test "unpin status", %{user: user, activity: activity} do
787 {:ok, activity} = CommonAPI.pin(activity.id, user)
789 user = refresh_record(user)
793 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
795 user = refresh_record(user)
797 assert %User{pinned_activities: []} = user
800 test "should unpin when deleting a status", %{user: user, activity: activity} do
801 {:ok, activity} = CommonAPI.pin(activity.id, user)
803 user = refresh_record(user)
805 assert {:ok, _} = CommonAPI.delete(activity.id, user)
807 user = refresh_record(user)
809 assert %User{pinned_activities: []} = user
813 describe "mute tests" do
817 activity = insert(:note_activity)
819 [user: user, activity: activity]
822 test "add mute", %{user: user, activity: activity} do
823 {:ok, _} = CommonAPI.add_mute(user, activity)
824 assert CommonAPI.thread_muted?(user, activity)
827 test "remove mute", %{user: user, activity: activity} do
828 CommonAPI.add_mute(user, activity)
829 {:ok, _} = CommonAPI.remove_mute(user, activity)
830 refute CommonAPI.thread_muted?(user, activity)
833 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
834 CommonAPI.add_mute(user, activity)
835 {:error, _} = CommonAPI.add_mute(user, activity)
839 describe "reports" do
840 test "creates a report" do
841 reporter = insert(:user)
842 target_user = insert(:user)
844 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
846 reporter_ap_id = reporter.ap_id
847 target_ap_id = target_user.ap_id
848 activity_ap_id = activity.data["id"]
852 account_id: target_user.id,
854 status_ids: [activity.id]
859 "id" => activity_ap_id,
860 "content" => "foobar",
861 "published" => activity.object.data["published"],
862 "actor" => AccountView.render("show.json", %{user: target_user})
865 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
868 actor: ^reporter_ap_id,
871 "content" => ^comment,
872 "object" => [^target_ap_id, ^note_obj],
878 test "updates report state" do
879 [reporter, target_user] = insert_pair(:user)
880 activity = insert(:note_activity, user: target_user)
882 {:ok, %Activity{id: report_id}} =
883 CommonAPI.report(reporter, %{
884 account_id: target_user.id,
885 comment: "I feel offended",
886 status_ids: [activity.id]
889 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
891 assert report.data["state"] == "resolved"
893 [reported_user, activity_id] = report.data["object"]
895 assert reported_user == target_user.ap_id
896 assert activity_id == activity.data["id"]
899 test "does not update report state when state is unsupported" do
900 [reporter, target_user] = insert_pair(:user)
901 activity = insert(:note_activity, user: target_user)
903 {:ok, %Activity{id: report_id}} =
904 CommonAPI.report(reporter, %{
905 account_id: target_user.id,
906 comment: "I feel offended",
907 status_ids: [activity.id]
910 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
913 test "updates state of multiple reports" do
914 [reporter, target_user] = insert_pair(:user)
915 activity = insert(:note_activity, user: target_user)
917 {:ok, %Activity{id: first_report_id}} =
918 CommonAPI.report(reporter, %{
919 account_id: target_user.id,
920 comment: "I feel offended",
921 status_ids: [activity.id]
924 {:ok, %Activity{id: second_report_id}} =
925 CommonAPI.report(reporter, %{
926 account_id: target_user.id,
927 comment: "I feel very offended!",
928 status_ids: [activity.id]
932 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
934 first_report = Activity.get_by_id(first_report_id)
935 second_report = Activity.get_by_id(second_report_id)
937 assert report_ids -- [first_report_id, second_report_id] == []
938 assert first_report.data["state"] == "resolved"
939 assert second_report.data["state"] == "resolved"
943 describe "reblog muting" do
945 muter = insert(:user)
947 muted = insert(:user)
949 [muter: muter, muted: muted]
952 test "add a reblog mute", %{muter: muter, muted: muted} do
953 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
955 assert User.showing_reblogs?(muter, muted) == false
958 test "remove a reblog mute", %{muter: muter, muted: muted} do
959 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
960 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
962 assert User.showing_reblogs?(muter, muted) == true
966 describe "follow/2" do
967 test "directly follows a non-locked local user" do
968 [follower, followed] = insert_pair(:user)
969 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
971 assert User.following?(follower, followed)
975 describe "unfollow/2" do
976 test "also unsubscribes a user" do
977 [follower, followed] = insert_pair(:user)
978 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
979 {:ok, _subscription} = User.subscribe(follower, followed)
981 assert User.subscribed_to?(follower, followed)
983 {:ok, follower} = CommonAPI.unfollow(follower, followed)
985 refute User.subscribed_to?(follower, followed)
988 test "cancels a pending follow for a local user" do
989 follower = insert(:user)
990 followed = insert(:user, locked: true)
992 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
993 CommonAPI.follow(follower, followed)
995 assert User.get_follow_state(follower, followed) == :follow_pending
996 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
997 assert User.get_follow_state(follower, followed) == nil
999 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1000 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1005 "object" => %{"type" => "Follow", "state" => "cancelled"}
1007 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1010 test "cancels a pending follow for a remote user" do
1011 follower = insert(:user)
1012 followed = insert(:user, locked: true, local: false, ap_enabled: true)
1014 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1015 CommonAPI.follow(follower, followed)
1017 assert User.get_follow_state(follower, followed) == :follow_pending
1018 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1019 assert User.get_follow_state(follower, followed) == nil
1021 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1022 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1027 "object" => %{"type" => "Follow", "state" => "cancelled"}
1029 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1033 describe "accept_follow_request/2" do
1034 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
1035 user = insert(:user, locked: true)
1036 follower = insert(:user)
1037 follower_two = insert(:user)
1039 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1040 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1041 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1043 assert follow_activity.data["state"] == "pending"
1044 assert follow_activity_two.data["state"] == "pending"
1045 assert follow_activity_three.data["state"] == "pending"
1047 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
1049 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
1050 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
1051 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1054 test "after rejection, it sets all existing pending follow request states to 'reject'" do
1055 user = insert(:user, locked: true)
1056 follower = insert(:user)
1057 follower_two = insert(:user)
1059 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1060 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1061 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1063 assert follow_activity.data["state"] == "pending"
1064 assert follow_activity_two.data["state"] == "pending"
1065 assert follow_activity_three.data["state"] == "pending"
1067 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
1069 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
1070 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
1071 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1074 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
1075 user = insert(:user, locked: true)
1076 not_follower = insert(:user)
1077 CommonAPI.accept_follow_request(not_follower, user)
1079 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
1083 describe "vote/3" do
1084 test "does not allow to vote twice" do
1085 user = insert(:user)
1086 other_user = insert(:user)
1089 CommonAPI.post(user, %{
1090 status: "Am I cute?",
1091 poll: %{options: ["Yes", "No"], expires_in: 20}
1094 object = Object.normalize(activity)
1096 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1098 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1102 describe "listen/2" do
1103 test "returns a valid activity" do
1104 user = insert(:user)
1107 CommonAPI.listen(user, %{
1108 title: "lain radio episode 1",
1109 album: "lain radio",
1114 object = Object.normalize(activity)
1116 assert object.data["title"] == "lain radio episode 1"
1118 assert Visibility.get_visibility(activity) == "public"
1121 test "respects visibility=private" do
1122 user = insert(:user)
1125 CommonAPI.listen(user, %{
1126 title: "lain radio episode 1",
1127 album: "lain radio",
1130 visibility: "private"
1133 object = Object.normalize(activity)
1135 assert object.data["title"] == "lain radio episode 1"
1137 assert Visibility.get_visibility(activity) == "private"