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
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Visibility
14 alias Pleroma.Web.AdminAPI.AccountView
15 alias Pleroma.Web.CommonAPI
17 import Pleroma.Factory
19 require Pleroma.Constants
21 setup do: clear_config([:instance, :safe_dm_mentions])
22 setup do: clear_config([:instance, :limit])
23 setup do: clear_config([:instance, :max_pinned_statuses])
25 describe "posting chat messages" do
26 setup do: clear_config([:instance, :chat_limit])
28 test "it posts a chat message" do
29 author = insert(:user)
30 recipient = insert(:user)
33 CommonAPI.post_chat_message(
36 "a test message <script>alert('uuu')</script> :firefox:"
39 assert activity.data["type"] == "Create"
41 object = Object.normalize(activity)
43 assert object.data["type"] == "ChatMessage"
44 assert object.data["to"] == [recipient.ap_id]
46 assert object.data["content"] ==
47 "a test message <script>alert('uuu')</script> :firefox:"
49 assert object.data["emoji"] == %{
50 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
53 assert Chat.get(author.id, recipient.ap_id)
54 assert Chat.get(recipient.id, author.ap_id)
57 test "it reject messages over the local limit" do
58 Pleroma.Config.put([:instance, :chat_limit], 2)
60 author = insert(:user)
61 recipient = insert(:user)
64 CommonAPI.post_chat_message(
70 assert message == :content_too_long
74 test "favoriting race condition" do
76 users_serial = insert_list(10, :user)
77 users = insert_list(10, :user)
79 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
82 |> Enum.map(fn user ->
83 CommonAPI.favorite(user, activity.id)
86 object = Object.get_by_ap_id(activity.data["object"])
87 assert object.data["like_count"] == 10
90 |> Enum.map(fn user ->
92 CommonAPI.favorite(user, activity.id)
95 |> Enum.map(&Task.await/1)
97 object = Object.get_by_ap_id(activity.data["object"])
98 assert object.data["like_count"] == 20
101 test "repeating race condition" do
103 users_serial = insert_list(10, :user)
104 users = insert_list(10, :user)
106 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
109 |> Enum.map(fn user ->
110 CommonAPI.repeat(activity.id, user)
113 object = Object.get_by_ap_id(activity.data["object"])
114 assert object.data["announcement_count"] == 10
117 |> Enum.map(fn user ->
119 CommonAPI.repeat(activity.id, user)
122 |> Enum.map(&Task.await/1)
124 object = Object.get_by_ap_id(activity.data["object"])
125 assert object.data["announcement_count"] == 20
128 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
130 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
132 [participation] = Participation.for_user(user)
135 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
137 assert Visibility.is_direct?(convo_reply)
139 assert activity.data["context"] == convo_reply.data["context"]
142 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
144 jafnhar = insert(:user)
145 tridi = insert(:user)
148 CommonAPI.post(har, %{
149 "status" => "@#{jafnhar.nickname} hey",
150 "visibility" => "direct"
153 assert har.ap_id in activity.recipients
154 assert jafnhar.ap_id in activity.recipients
156 [participation] = Participation.for_user(har)
159 CommonAPI.post(har, %{
160 "status" => "I don't really like @#{tridi.nickname}",
161 "visibility" => "direct",
162 "in_reply_to_status_id" => activity.id,
163 "in_reply_to_conversation_id" => participation.id
166 assert har.ap_id in activity.recipients
167 assert jafnhar.ap_id in activity.recipients
168 refute tridi.ap_id in activity.recipients
171 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
173 jafnhar = insert(:user)
174 tridi = insert(:user)
176 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
179 CommonAPI.post(har, %{
180 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
181 "visibility" => "direct"
184 refute tridi.ap_id in activity.recipients
185 assert jafnhar.ap_id in activity.recipients
188 test "it de-duplicates tags" do
190 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
192 object = Object.normalize(activity)
194 assert object.data["tag"] == ["2hu"]
197 test "it adds emoji in the object" do
199 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
201 assert Object.normalize(activity).data["emoji"]["firefox"]
204 describe "posting" do
205 test "it supports explicit addressing" do
207 user_two = insert(:user)
208 user_three = insert(:user)
209 user_four = insert(:user)
212 CommonAPI.post(user, %{
214 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
215 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
218 assert user.ap_id in activity.recipients
219 assert user_two.ap_id in activity.recipients
220 assert user_four.ap_id in activity.recipients
221 refute user_three.ap_id in activity.recipients
224 test "it filters out obviously bad tags when accepting a post as HTML" do
227 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
230 CommonAPI.post(user, %{
232 "content_type" => "text/html"
235 object = Object.normalize(activity)
237 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
240 test "it filters out obviously bad tags when accepting a post as Markdown" do
243 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
246 CommonAPI.post(user, %{
248 "content_type" => "text/markdown"
251 object = Object.normalize(activity)
253 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
256 test "it does not allow replies to direct messages that are not direct messages themselves" do
259 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
262 CommonAPI.post(user, %{
263 "status" => "suya..",
264 "visibility" => "direct",
265 "in_reply_to_status_id" => activity.id
268 Enum.each(["public", "private", "unlisted"], fn visibility ->
269 assert {:error, "The message visibility must be direct"} =
270 CommonAPI.post(user, %{
271 "status" => "suya..",
272 "visibility" => visibility,
273 "in_reply_to_status_id" => activity.id
278 test "it allows to address a list" do
280 {:ok, list} = Pleroma.List.create("foo", user)
283 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
285 assert activity.data["bcc"] == [list.ap_id]
286 assert activity.recipients == [list.ap_id, user.ap_id]
287 assert activity.data["listMessage"] == list.ap_id
290 test "it returns error when status is empty and no attachments" do
293 assert {:error, "Cannot post an empty status without attachments"} =
294 CommonAPI.post(user, %{"status" => ""})
297 test "it validates character limits are correctly enforced" do
298 Pleroma.Config.put([:instance, :limit], 5)
302 assert {:error, "The status is over the character limit"} =
303 CommonAPI.post(user, %{"status" => "foobar"})
305 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
308 test "it can handle activities that expire" do
312 NaiveDateTime.utc_now()
313 |> NaiveDateTime.truncate(:second)
314 |> NaiveDateTime.add(1_000_000, :second)
316 assert {:ok, activity} =
317 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
319 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
320 assert expiration.scheduled_at == expires_at
324 describe "reactions" do
325 test "reacting to a status with an emoji" do
327 other_user = insert(:user)
329 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
331 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
333 assert reaction.data["actor"] == user.ap_id
334 assert reaction.data["content"] == "👍"
336 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
338 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
341 test "unreacting to a status with an emoji" do
343 other_user = insert(:user)
345 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
346 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
348 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
350 assert unreaction.data["type"] == "Undo"
351 assert unreaction.data["object"] == reaction.data["id"]
354 test "repeating a status" do
356 other_user = insert(:user)
358 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
360 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
363 test "can't repeat a repeat" do
365 other_user = insert(:user)
366 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
368 {:ok, %Activity{} = announce, _} = CommonAPI.repeat(activity.id, other_user)
370 refute match?({:ok, %Activity{}, _}, CommonAPI.repeat(announce.id, user))
373 test "repeating a status privately" do
375 other_user = insert(:user)
377 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
379 {:ok, %Activity{} = announce_activity, _} =
380 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
382 assert Visibility.is_private?(announce_activity)
385 test "favoriting a status" do
387 other_user = insert(:user)
389 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
391 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
392 assert data["type"] == "Like"
393 assert data["actor"] == user.ap_id
394 assert data["object"] == post_activity.data["object"]
397 test "retweeting a status twice returns the status" do
399 other_user = insert(:user)
401 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
402 {:ok, %Activity{} = announce, object} = CommonAPI.repeat(activity.id, user)
403 {:ok, ^announce, ^object} = CommonAPI.repeat(activity.id, user)
406 test "favoriting a status twice returns ok, but without the like activity" do
408 other_user = insert(:user)
410 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
411 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
412 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
416 describe "pinned statuses" do
418 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
421 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
423 [user: user, activity: activity]
426 test "pin status", %{user: user, activity: activity} do
427 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
430 user = refresh_record(user)
432 assert %User{pinned_activities: [^id]} = user
435 test "pin poll", %{user: user} do
437 CommonAPI.post(user, %{
438 "status" => "How is fediverse today?",
439 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
442 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
445 user = refresh_record(user)
447 assert %User{pinned_activities: [^id]} = user
450 test "unlisted statuses can be pinned", %{user: user} do
451 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
452 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
455 test "only self-authored can be pinned", %{activity: activity} do
458 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
461 test "max pinned statuses", %{user: user, activity: activity_one} do
462 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
464 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
466 user = refresh_record(user)
468 assert {:error, "You have already pinned the maximum number of statuses"} =
469 CommonAPI.pin(activity_two.id, user)
472 test "unpin status", %{user: user, activity: activity} do
473 {:ok, activity} = CommonAPI.pin(activity.id, user)
475 user = refresh_record(user)
479 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
481 user = refresh_record(user)
483 assert %User{pinned_activities: []} = user
486 test "should unpin when deleting a status", %{user: user, activity: activity} do
487 {:ok, activity} = CommonAPI.pin(activity.id, user)
489 user = refresh_record(user)
491 assert {:ok, _} = CommonAPI.delete(activity.id, user)
493 user = refresh_record(user)
495 assert %User{pinned_activities: []} = user
499 describe "mute tests" do
503 activity = insert(:note_activity)
505 [user: user, activity: activity]
508 test "add mute", %{user: user, activity: activity} do
509 {:ok, _} = CommonAPI.add_mute(user, activity)
510 assert CommonAPI.thread_muted?(user, activity)
513 test "remove mute", %{user: user, activity: activity} do
514 CommonAPI.add_mute(user, activity)
515 {:ok, _} = CommonAPI.remove_mute(user, activity)
516 refute CommonAPI.thread_muted?(user, activity)
519 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
520 CommonAPI.add_mute(user, activity)
521 {:error, _} = CommonAPI.add_mute(user, activity)
525 describe "reports" do
526 test "creates a report" do
527 reporter = insert(:user)
528 target_user = insert(:user)
530 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
532 reporter_ap_id = reporter.ap_id
533 target_ap_id = target_user.ap_id
534 activity_ap_id = activity.data["id"]
538 "account_id" => target_user.id,
539 "comment" => comment,
540 "status_ids" => [activity.id]
545 "id" => activity_ap_id,
546 "content" => "foobar",
547 "published" => activity.object.data["published"],
548 "actor" => AccountView.render("show.json", %{user: target_user})
551 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
554 actor: ^reporter_ap_id,
557 "content" => ^comment,
558 "object" => [^target_ap_id, ^note_obj],
564 test "updates report state" do
565 [reporter, target_user] = insert_pair(:user)
566 activity = insert(:note_activity, user: target_user)
568 {:ok, %Activity{id: report_id}} =
569 CommonAPI.report(reporter, %{
570 "account_id" => target_user.id,
571 "comment" => "I feel offended",
572 "status_ids" => [activity.id]
575 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
577 assert report.data["state"] == "resolved"
579 [reported_user, activity_id] = report.data["object"]
581 assert reported_user == target_user.ap_id
582 assert activity_id == activity.data["id"]
585 test "does not update report state when state is unsupported" do
586 [reporter, target_user] = insert_pair(:user)
587 activity = insert(:note_activity, user: target_user)
589 {:ok, %Activity{id: report_id}} =
590 CommonAPI.report(reporter, %{
591 "account_id" => target_user.id,
592 "comment" => "I feel offended",
593 "status_ids" => [activity.id]
596 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
599 test "updates state of multiple reports" do
600 [reporter, target_user] = insert_pair(:user)
601 activity = insert(:note_activity, user: target_user)
603 {:ok, %Activity{id: first_report_id}} =
604 CommonAPI.report(reporter, %{
605 "account_id" => target_user.id,
606 "comment" => "I feel offended",
607 "status_ids" => [activity.id]
610 {:ok, %Activity{id: second_report_id}} =
611 CommonAPI.report(reporter, %{
612 "account_id" => target_user.id,
613 "comment" => "I feel very offended!",
614 "status_ids" => [activity.id]
618 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
620 first_report = Activity.get_by_id(first_report_id)
621 second_report = Activity.get_by_id(second_report_id)
623 assert report_ids -- [first_report_id, second_report_id] == []
624 assert first_report.data["state"] == "resolved"
625 assert second_report.data["state"] == "resolved"
629 describe "reblog muting" do
631 muter = insert(:user)
633 muted = insert(:user)
635 [muter: muter, muted: muted]
638 test "add a reblog mute", %{muter: muter, muted: muted} do
639 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
641 assert User.showing_reblogs?(muter, muted) == false
644 test "remove a reblog mute", %{muter: muter, muted: muted} do
645 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
646 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
648 assert User.showing_reblogs?(muter, muted) == true
652 describe "unfollow/2" do
653 test "also unsubscribes a user" do
654 [follower, followed] = insert_pair(:user)
655 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
656 {:ok, _subscription} = User.subscribe(follower, followed)
658 assert User.subscribed_to?(follower, followed)
660 {:ok, follower} = CommonAPI.unfollow(follower, followed)
662 refute User.subscribed_to?(follower, followed)
665 test "cancels a pending follow for a local user" do
666 follower = insert(:user)
667 followed = insert(:user, locked: true)
669 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
670 CommonAPI.follow(follower, followed)
672 assert User.get_follow_state(follower, followed) == :follow_pending
673 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
674 assert User.get_follow_state(follower, followed) == nil
676 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
677 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
682 "object" => %{"type" => "Follow", "state" => "cancelled"}
684 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
687 test "cancels a pending follow for a remote user" do
688 follower = insert(:user)
689 followed = insert(:user, locked: true, local: false, ap_enabled: true)
691 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
692 CommonAPI.follow(follower, followed)
694 assert User.get_follow_state(follower, followed) == :follow_pending
695 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
696 assert User.get_follow_state(follower, followed) == nil
698 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
699 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
704 "object" => %{"type" => "Follow", "state" => "cancelled"}
706 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
710 describe "accept_follow_request/2" do
711 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
712 user = insert(:user, locked: true)
713 follower = insert(:user)
714 follower_two = insert(:user)
716 {:ok, follow_activity} = ActivityPub.follow(follower, user)
717 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
718 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
720 assert follow_activity.data["state"] == "pending"
721 assert follow_activity_two.data["state"] == "pending"
722 assert follow_activity_three.data["state"] == "pending"
724 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
726 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
727 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
728 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
731 test "after rejection, it sets all existing pending follow request states to 'reject'" do
732 user = insert(:user, locked: true)
733 follower = insert(:user)
734 follower_two = insert(:user)
736 {:ok, follow_activity} = ActivityPub.follow(follower, user)
737 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
738 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
740 assert follow_activity.data["state"] == "pending"
741 assert follow_activity_two.data["state"] == "pending"
742 assert follow_activity_three.data["state"] == "pending"
744 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
746 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
747 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
748 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
753 test "does not allow to vote twice" do
755 other_user = insert(:user)
758 CommonAPI.post(user, %{
759 "status" => "Am I cute?",
760 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
763 object = Object.normalize(activity)
765 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
767 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
771 describe "listen/2" do
772 test "returns a valid activity" do
776 CommonAPI.listen(user, %{
777 "title" => "lain radio episode 1",
778 "album" => "lain radio",
783 object = Object.normalize(activity)
785 assert object.data["title"] == "lain radio episode 1"
787 assert Visibility.get_visibility(activity) == "public"
790 test "respects visibility=private" do
794 CommonAPI.listen(user, %{
795 "title" => "lain radio episode 1",
796 "album" => "lain radio",
799 "visibility" => "private"
802 object = Object.normalize(activity)
804 assert object.data["title"] == "lain radio episode 1"
806 assert Visibility.get_visibility(activity) == "private"