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
8 alias Pleroma.Conversation.Participation
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Visibility
13 alias Pleroma.Web.AdminAPI.AccountView
14 alias Pleroma.Web.CommonAPI
16 import Pleroma.Factory
18 require Pleroma.Constants
20 setup do: clear_config([:instance, :safe_dm_mentions])
21 setup do: clear_config([:instance, :limit])
22 setup do: clear_config([:instance, :max_pinned_statuses])
24 test "favoriting race condition" do
26 users_serial = insert_list(10, :user)
27 users = insert_list(10, :user)
29 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
32 |> Enum.map(fn user ->
33 CommonAPI.favorite(user, activity.id)
36 object = Object.get_by_ap_id(activity.data["object"])
37 assert object.data["like_count"] == 10
40 |> Enum.map(fn user ->
42 CommonAPI.favorite(user, activity.id)
45 |> Enum.map(&Task.await/1)
47 object = Object.get_by_ap_id(activity.data["object"])
48 assert object.data["like_count"] == 20
51 test "repeating race condition" do
53 users_serial = insert_list(10, :user)
54 users = insert_list(10, :user)
56 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
59 |> Enum.map(fn user ->
60 CommonAPI.repeat(activity.id, user)
63 object = Object.get_by_ap_id(activity.data["object"])
64 assert object.data["announcement_count"] == 10
67 |> Enum.map(fn user ->
69 CommonAPI.repeat(activity.id, user)
72 |> Enum.map(&Task.await/1)
74 object = Object.get_by_ap_id(activity.data["object"])
75 assert object.data["announcement_count"] == 20
78 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
80 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
82 [participation] = Participation.for_user(user)
85 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
87 assert Visibility.is_direct?(convo_reply)
89 assert activity.data["context"] == convo_reply.data["context"]
92 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
94 jafnhar = insert(:user)
98 CommonAPI.post(har, %{
99 "status" => "@#{jafnhar.nickname} hey",
100 "visibility" => "direct"
103 assert har.ap_id in activity.recipients
104 assert jafnhar.ap_id in activity.recipients
106 [participation] = Participation.for_user(har)
109 CommonAPI.post(har, %{
110 "status" => "I don't really like @#{tridi.nickname}",
111 "visibility" => "direct",
112 "in_reply_to_status_id" => activity.id,
113 "in_reply_to_conversation_id" => participation.id
116 assert har.ap_id in activity.recipients
117 assert jafnhar.ap_id in activity.recipients
118 refute tridi.ap_id in activity.recipients
121 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
123 jafnhar = insert(:user)
124 tridi = insert(:user)
126 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
129 CommonAPI.post(har, %{
130 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
131 "visibility" => "direct"
134 refute tridi.ap_id in activity.recipients
135 assert jafnhar.ap_id in activity.recipients
138 test "it de-duplicates tags" do
140 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
142 object = Object.normalize(activity)
144 assert object.data["tag"] == ["2hu"]
147 test "it adds emoji in the object" do
149 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
151 assert Object.normalize(activity).data["emoji"]["firefox"]
154 describe "posting" do
155 test "it supports explicit addressing" do
157 user_two = insert(:user)
158 user_three = insert(:user)
159 user_four = insert(:user)
162 CommonAPI.post(user, %{
164 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
165 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
168 assert user.ap_id in activity.recipients
169 assert user_two.ap_id in activity.recipients
170 assert user_four.ap_id in activity.recipients
171 refute user_three.ap_id in activity.recipients
174 test "it filters out obviously bad tags when accepting a post as HTML" do
177 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
180 CommonAPI.post(user, %{
182 "content_type" => "text/html"
185 object = Object.normalize(activity)
187 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
190 test "it filters out obviously bad tags when accepting a post as Markdown" do
193 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
196 CommonAPI.post(user, %{
198 "content_type" => "text/markdown"
201 object = Object.normalize(activity)
203 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
206 test "it does not allow replies to direct messages that are not direct messages themselves" do
209 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
212 CommonAPI.post(user, %{
213 "status" => "suya..",
214 "visibility" => "direct",
215 "in_reply_to_status_id" => activity.id
218 Enum.each(["public", "private", "unlisted"], fn visibility ->
219 assert {:error, "The message visibility must be direct"} =
220 CommonAPI.post(user, %{
221 "status" => "suya..",
222 "visibility" => visibility,
223 "in_reply_to_status_id" => activity.id
228 test "it allows to address a list" do
230 {:ok, list} = Pleroma.List.create("foo", user)
233 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
235 assert activity.data["bcc"] == [list.ap_id]
236 assert activity.recipients == [list.ap_id, user.ap_id]
237 assert activity.data["listMessage"] == list.ap_id
240 test "it returns error when status is empty and no attachments" do
243 assert {:error, "Cannot post an empty status without attachments"} =
244 CommonAPI.post(user, %{"status" => ""})
247 test "it validates character limits are correctly enforced" do
248 Pleroma.Config.put([:instance, :limit], 5)
252 assert {:error, "The status is over the character limit"} =
253 CommonAPI.post(user, %{"status" => "foobar"})
255 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
258 test "it can handle activities that expire" do
262 NaiveDateTime.utc_now()
263 |> NaiveDateTime.truncate(:second)
264 |> NaiveDateTime.add(1_000_000, :second)
266 assert {:ok, activity} =
267 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
269 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
270 assert expiration.scheduled_at == expires_at
274 describe "reactions" do
275 test "reacting to a status with an emoji" do
277 other_user = insert(:user)
279 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
281 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
283 assert reaction.data["actor"] == user.ap_id
284 assert reaction.data["content"] == "👍"
286 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
288 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
291 test "unreacting to a status with an emoji" do
293 other_user = insert(:user)
295 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
296 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
298 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
300 assert unreaction.data["type"] == "Undo"
301 assert unreaction.data["object"] == reaction.data["id"]
302 assert unreaction.local
305 test "repeating a status" do
307 other_user = insert(:user)
309 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
311 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
314 test "can't repeat a repeat" do
316 other_user = insert(:user)
317 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
319 {:ok, %Activity{} = announce, _} = CommonAPI.repeat(activity.id, other_user)
321 refute match?({:ok, %Activity{}, _}, CommonAPI.repeat(announce.id, user))
324 test "repeating a status privately" do
326 other_user = insert(:user)
328 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
330 {:ok, %Activity{} = announce_activity, _} =
331 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
333 assert Visibility.is_private?(announce_activity)
336 test "favoriting a status" do
338 other_user = insert(:user)
340 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
342 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
343 assert data["type"] == "Like"
344 assert data["actor"] == user.ap_id
345 assert data["object"] == post_activity.data["object"]
348 test "retweeting a status twice returns the status" do
350 other_user = insert(:user)
352 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
353 {:ok, %Activity{} = announce, object} = CommonAPI.repeat(activity.id, user)
354 {:ok, ^announce, ^object} = CommonAPI.repeat(activity.id, user)
357 test "favoriting a status twice returns ok, but without the like activity" do
359 other_user = insert(:user)
361 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
362 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
363 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
367 describe "pinned statuses" do
369 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
372 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
374 [user: user, activity: activity]
377 test "pin status", %{user: user, activity: activity} do
378 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
381 user = refresh_record(user)
383 assert %User{pinned_activities: [^id]} = user
386 test "pin poll", %{user: user} do
388 CommonAPI.post(user, %{
389 "status" => "How is fediverse today?",
390 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
393 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
396 user = refresh_record(user)
398 assert %User{pinned_activities: [^id]} = user
401 test "unlisted statuses can be pinned", %{user: user} do
402 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
403 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
406 test "only self-authored can be pinned", %{activity: activity} do
409 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
412 test "max pinned statuses", %{user: user, activity: activity_one} do
413 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
415 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
417 user = refresh_record(user)
419 assert {:error, "You have already pinned the maximum number of statuses"} =
420 CommonAPI.pin(activity_two.id, user)
423 test "unpin status", %{user: user, activity: activity} do
424 {:ok, activity} = CommonAPI.pin(activity.id, user)
426 user = refresh_record(user)
430 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
432 user = refresh_record(user)
434 assert %User{pinned_activities: []} = user
437 test "should unpin when deleting a status", %{user: user, activity: activity} do
438 {:ok, activity} = CommonAPI.pin(activity.id, user)
440 user = refresh_record(user)
442 assert {:ok, _} = CommonAPI.delete(activity.id, user)
444 user = refresh_record(user)
446 assert %User{pinned_activities: []} = user
450 describe "mute tests" do
454 activity = insert(:note_activity)
456 [user: user, activity: activity]
459 test "add mute", %{user: user, activity: activity} do
460 {:ok, _} = CommonAPI.add_mute(user, activity)
461 assert CommonAPI.thread_muted?(user, activity)
464 test "remove mute", %{user: user, activity: activity} do
465 CommonAPI.add_mute(user, activity)
466 {:ok, _} = CommonAPI.remove_mute(user, activity)
467 refute CommonAPI.thread_muted?(user, activity)
470 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
471 CommonAPI.add_mute(user, activity)
472 {:error, _} = CommonAPI.add_mute(user, activity)
476 describe "reports" do
477 test "creates a report" do
478 reporter = insert(:user)
479 target_user = insert(:user)
481 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
483 reporter_ap_id = reporter.ap_id
484 target_ap_id = target_user.ap_id
485 activity_ap_id = activity.data["id"]
489 account_id: target_user.id,
491 status_ids: [activity.id]
496 "id" => activity_ap_id,
497 "content" => "foobar",
498 "published" => activity.object.data["published"],
499 "actor" => AccountView.render("show.json", %{user: target_user})
502 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
505 actor: ^reporter_ap_id,
508 "content" => ^comment,
509 "object" => [^target_ap_id, ^note_obj],
515 test "updates report state" do
516 [reporter, target_user] = insert_pair(:user)
517 activity = insert(:note_activity, user: target_user)
519 {:ok, %Activity{id: report_id}} =
520 CommonAPI.report(reporter, %{
521 account_id: target_user.id,
522 comment: "I feel offended",
523 status_ids: [activity.id]
526 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
528 assert report.data["state"] == "resolved"
530 [reported_user, activity_id] = report.data["object"]
532 assert reported_user == target_user.ap_id
533 assert activity_id == activity.data["id"]
536 test "does not update report state when state is unsupported" do
537 [reporter, target_user] = insert_pair(:user)
538 activity = insert(:note_activity, user: target_user)
540 {:ok, %Activity{id: report_id}} =
541 CommonAPI.report(reporter, %{
542 account_id: target_user.id,
543 comment: "I feel offended",
544 status_ids: [activity.id]
547 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
550 test "updates state of multiple reports" do
551 [reporter, target_user] = insert_pair(:user)
552 activity = insert(:note_activity, user: target_user)
554 {:ok, %Activity{id: first_report_id}} =
555 CommonAPI.report(reporter, %{
556 account_id: target_user.id,
557 comment: "I feel offended",
558 status_ids: [activity.id]
561 {:ok, %Activity{id: second_report_id}} =
562 CommonAPI.report(reporter, %{
563 account_id: target_user.id,
564 comment: "I feel very offended!",
565 status_ids: [activity.id]
569 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
571 first_report = Activity.get_by_id(first_report_id)
572 second_report = Activity.get_by_id(second_report_id)
574 assert report_ids -- [first_report_id, second_report_id] == []
575 assert first_report.data["state"] == "resolved"
576 assert second_report.data["state"] == "resolved"
580 describe "reblog muting" do
582 muter = insert(:user)
584 muted = insert(:user)
586 [muter: muter, muted: muted]
589 test "add a reblog mute", %{muter: muter, muted: muted} do
590 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
592 assert User.showing_reblogs?(muter, muted) == false
595 test "remove a reblog mute", %{muter: muter, muted: muted} do
596 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
597 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
599 assert User.showing_reblogs?(muter, muted) == true
603 describe "unfollow/2" do
604 test "also unsubscribes a user" do
605 [follower, followed] = insert_pair(:user)
606 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
607 {:ok, _subscription} = User.subscribe(follower, followed)
609 assert User.subscribed_to?(follower, followed)
611 {:ok, follower} = CommonAPI.unfollow(follower, followed)
613 refute User.subscribed_to?(follower, followed)
616 test "cancels a pending follow for a local user" do
617 follower = insert(:user)
618 followed = insert(:user, locked: true)
620 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
621 CommonAPI.follow(follower, followed)
623 assert User.get_follow_state(follower, followed) == :follow_pending
624 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
625 assert User.get_follow_state(follower, followed) == nil
627 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
628 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
633 "object" => %{"type" => "Follow", "state" => "cancelled"}
635 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
638 test "cancels a pending follow for a remote user" do
639 follower = insert(:user)
640 followed = insert(:user, locked: true, local: false, ap_enabled: true)
642 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
643 CommonAPI.follow(follower, followed)
645 assert User.get_follow_state(follower, followed) == :follow_pending
646 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
647 assert User.get_follow_state(follower, followed) == nil
649 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
650 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
655 "object" => %{"type" => "Follow", "state" => "cancelled"}
657 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
661 describe "accept_follow_request/2" do
662 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
663 user = insert(:user, locked: true)
664 follower = insert(:user)
665 follower_two = insert(:user)
667 {:ok, follow_activity} = ActivityPub.follow(follower, user)
668 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
669 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
671 assert follow_activity.data["state"] == "pending"
672 assert follow_activity_two.data["state"] == "pending"
673 assert follow_activity_three.data["state"] == "pending"
675 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
677 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
678 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
679 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
682 test "after rejection, it sets all existing pending follow request states to 'reject'" do
683 user = insert(:user, locked: true)
684 follower = insert(:user)
685 follower_two = insert(:user)
687 {:ok, follow_activity} = ActivityPub.follow(follower, user)
688 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
689 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
691 assert follow_activity.data["state"] == "pending"
692 assert follow_activity_two.data["state"] == "pending"
693 assert follow_activity_three.data["state"] == "pending"
695 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
697 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
698 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
699 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
702 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
703 user = insert(:user, locked: true)
704 not_follower = insert(:user)
705 CommonAPI.accept_follow_request(not_follower, user)
707 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
712 test "does not allow to vote twice" do
714 other_user = insert(:user)
717 CommonAPI.post(user, %{
718 "status" => "Am I cute?",
719 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
722 object = Object.normalize(activity)
724 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
726 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
730 describe "listen/2" do
731 test "returns a valid activity" do
735 CommonAPI.listen(user, %{
736 "title" => "lain radio episode 1",
737 "album" => "lain radio",
742 object = Object.normalize(activity)
744 assert object.data["title"] == "lain radio episode 1"
746 assert Visibility.get_visibility(activity) == "public"
749 test "respects visibility=private" do
753 CommonAPI.listen(user, %{
754 "title" => "lain radio episode 1",
755 "album" => "lain radio",
758 "visibility" => "private"
761 object = Object.normalize(activity)
763 assert object.data["title"] == "lain radio episode 1"
765 assert Visibility.get_visibility(activity) == "private"