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 "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
53 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
55 [participation] = Participation.for_user(user)
58 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
60 assert Visibility.is_direct?(convo_reply)
62 assert activity.data["context"] == convo_reply.data["context"]
65 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
67 jafnhar = insert(:user)
71 CommonAPI.post(har, %{
72 "status" => "@#{jafnhar.nickname} hey",
73 "visibility" => "direct"
76 assert har.ap_id in activity.recipients
77 assert jafnhar.ap_id in activity.recipients
79 [participation] = Participation.for_user(har)
82 CommonAPI.post(har, %{
83 "status" => "I don't really like @#{tridi.nickname}",
84 "visibility" => "direct",
85 "in_reply_to_status_id" => activity.id,
86 "in_reply_to_conversation_id" => participation.id
89 assert har.ap_id in activity.recipients
90 assert jafnhar.ap_id in activity.recipients
91 refute tridi.ap_id in activity.recipients
94 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
96 jafnhar = insert(:user)
99 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
102 CommonAPI.post(har, %{
103 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
104 "visibility" => "direct"
107 refute tridi.ap_id in activity.recipients
108 assert jafnhar.ap_id in activity.recipients
111 test "it de-duplicates tags" do
113 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
115 object = Object.normalize(activity)
117 assert object.data["tag"] == ["2hu"]
120 test "it adds emoji in the object" do
122 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
124 assert Object.normalize(activity).data["emoji"]["firefox"]
127 describe "posting" do
128 test "it supports explicit addressing" do
130 user_two = insert(:user)
131 user_three = insert(:user)
132 user_four = insert(:user)
135 CommonAPI.post(user, %{
137 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
138 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
141 assert user.ap_id in activity.recipients
142 assert user_two.ap_id in activity.recipients
143 assert user_four.ap_id in activity.recipients
144 refute user_three.ap_id in activity.recipients
147 test "it filters out obviously bad tags when accepting a post as HTML" do
150 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
153 CommonAPI.post(user, %{
155 "content_type" => "text/html"
158 object = Object.normalize(activity)
160 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
163 test "it filters out obviously bad tags when accepting a post as Markdown" do
166 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
169 CommonAPI.post(user, %{
171 "content_type" => "text/markdown"
174 object = Object.normalize(activity)
176 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
179 test "it does not allow replies to direct messages that are not direct messages themselves" do
182 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
185 CommonAPI.post(user, %{
186 "status" => "suya..",
187 "visibility" => "direct",
188 "in_reply_to_status_id" => activity.id
191 Enum.each(["public", "private", "unlisted"], fn visibility ->
192 assert {:error, "The message visibility must be direct"} =
193 CommonAPI.post(user, %{
194 "status" => "suya..",
195 "visibility" => visibility,
196 "in_reply_to_status_id" => activity.id
201 test "it allows to address a list" do
203 {:ok, list} = Pleroma.List.create("foo", user)
206 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
208 assert activity.data["bcc"] == [list.ap_id]
209 assert activity.recipients == [list.ap_id, user.ap_id]
210 assert activity.data["listMessage"] == list.ap_id
213 test "it returns error when status is empty and no attachments" do
216 assert {:error, "Cannot post an empty status without attachments"} =
217 CommonAPI.post(user, %{"status" => ""})
220 test "it validates character limits are correctly enforced" do
221 Pleroma.Config.put([:instance, :limit], 5)
225 assert {:error, "The status is over the character limit"} =
226 CommonAPI.post(user, %{"status" => "foobar"})
228 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
231 test "it can handle activities that expire" do
235 NaiveDateTime.utc_now()
236 |> NaiveDateTime.truncate(:second)
237 |> NaiveDateTime.add(1_000_000, :second)
239 assert {:ok, activity} =
240 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
242 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
243 assert expiration.scheduled_at == expires_at
247 describe "reactions" do
248 test "reacting to a status with an emoji" do
250 other_user = insert(:user)
252 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
254 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
256 assert reaction.data["actor"] == user.ap_id
257 assert reaction.data["content"] == "👍"
259 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
261 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
264 test "unreacting to a status with an emoji" do
266 other_user = insert(:user)
268 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
269 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
271 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
273 assert unreaction.data["type"] == "Undo"
274 assert unreaction.data["object"] == reaction.data["id"]
277 test "repeating a status" do
279 other_user = insert(:user)
281 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
283 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
286 test "can't repeat a repeat" do
288 other_user = insert(:user)
289 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
291 {:ok, %Activity{} = announce, _} = CommonAPI.repeat(activity.id, other_user)
293 refute match?({:ok, %Activity{}, _}, CommonAPI.repeat(announce.id, user))
296 test "repeating a status privately" do
298 other_user = insert(:user)
300 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
302 {:ok, %Activity{} = announce_activity, _} =
303 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
305 assert Visibility.is_private?(announce_activity)
308 test "favoriting a status" do
310 other_user = insert(:user)
312 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
314 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
315 assert data["type"] == "Like"
316 assert data["actor"] == user.ap_id
317 assert data["object"] == post_activity.data["object"]
320 test "retweeting a status twice returns the status" do
322 other_user = insert(:user)
324 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
325 {:ok, %Activity{} = announce, object} = CommonAPI.repeat(activity.id, user)
326 {:ok, ^announce, ^object} = CommonAPI.repeat(activity.id, user)
329 test "favoriting a status twice returns ok, but without the like activity" do
331 other_user = insert(:user)
333 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
334 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
335 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
339 describe "pinned statuses" do
341 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
344 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
346 [user: user, activity: activity]
349 test "pin status", %{user: user, activity: activity} do
350 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
353 user = refresh_record(user)
355 assert %User{pinned_activities: [^id]} = user
358 test "pin poll", %{user: user} do
360 CommonAPI.post(user, %{
361 "status" => "How is fediverse today?",
362 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
365 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
368 user = refresh_record(user)
370 assert %User{pinned_activities: [^id]} = user
373 test "unlisted statuses can be pinned", %{user: user} do
374 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
375 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
378 test "only self-authored can be pinned", %{activity: activity} do
381 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
384 test "max pinned statuses", %{user: user, activity: activity_one} do
385 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
387 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
389 user = refresh_record(user)
391 assert {:error, "You have already pinned the maximum number of statuses"} =
392 CommonAPI.pin(activity_two.id, user)
395 test "unpin status", %{user: user, activity: activity} do
396 {:ok, activity} = CommonAPI.pin(activity.id, user)
398 user = refresh_record(user)
402 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
404 user = refresh_record(user)
406 assert %User{pinned_activities: []} = user
409 test "should unpin when deleting a status", %{user: user, activity: activity} do
410 {:ok, activity} = CommonAPI.pin(activity.id, user)
412 user = refresh_record(user)
414 assert {:ok, _} = CommonAPI.delete(activity.id, user)
416 user = refresh_record(user)
418 assert %User{pinned_activities: []} = user
422 describe "mute tests" do
426 activity = insert(:note_activity)
428 [user: user, activity: activity]
431 test "add mute", %{user: user, activity: activity} do
432 {:ok, _} = CommonAPI.add_mute(user, activity)
433 assert CommonAPI.thread_muted?(user, activity)
436 test "remove mute", %{user: user, activity: activity} do
437 CommonAPI.add_mute(user, activity)
438 {:ok, _} = CommonAPI.remove_mute(user, activity)
439 refute CommonAPI.thread_muted?(user, activity)
442 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
443 CommonAPI.add_mute(user, activity)
444 {:error, _} = CommonAPI.add_mute(user, activity)
448 describe "reports" do
449 test "creates a report" do
450 reporter = insert(:user)
451 target_user = insert(:user)
453 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
455 reporter_ap_id = reporter.ap_id
456 target_ap_id = target_user.ap_id
457 activity_ap_id = activity.data["id"]
461 "account_id" => target_user.id,
462 "comment" => comment,
463 "status_ids" => [activity.id]
468 "id" => activity_ap_id,
469 "content" => "foobar",
470 "published" => activity.object.data["published"],
471 "actor" => AccountView.render("show.json", %{user: target_user})
474 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
477 actor: ^reporter_ap_id,
480 "content" => ^comment,
481 "object" => [^target_ap_id, ^note_obj],
487 test "updates report state" do
488 [reporter, target_user] = insert_pair(:user)
489 activity = insert(:note_activity, user: target_user)
491 {:ok, %Activity{id: report_id}} =
492 CommonAPI.report(reporter, %{
493 "account_id" => target_user.id,
494 "comment" => "I feel offended",
495 "status_ids" => [activity.id]
498 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
500 assert report.data["state"] == "resolved"
502 [reported_user, activity_id] = report.data["object"]
504 assert reported_user == target_user.ap_id
505 assert activity_id == activity.data["id"]
508 test "does not update report state when state is unsupported" do
509 [reporter, target_user] = insert_pair(:user)
510 activity = insert(:note_activity, user: target_user)
512 {:ok, %Activity{id: report_id}} =
513 CommonAPI.report(reporter, %{
514 "account_id" => target_user.id,
515 "comment" => "I feel offended",
516 "status_ids" => [activity.id]
519 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
522 test "updates state of multiple reports" do
523 [reporter, target_user] = insert_pair(:user)
524 activity = insert(:note_activity, user: target_user)
526 {:ok, %Activity{id: first_report_id}} =
527 CommonAPI.report(reporter, %{
528 "account_id" => target_user.id,
529 "comment" => "I feel offended",
530 "status_ids" => [activity.id]
533 {:ok, %Activity{id: second_report_id}} =
534 CommonAPI.report(reporter, %{
535 "account_id" => target_user.id,
536 "comment" => "I feel very offended!",
537 "status_ids" => [activity.id]
541 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
543 first_report = Activity.get_by_id(first_report_id)
544 second_report = Activity.get_by_id(second_report_id)
546 assert report_ids -- [first_report_id, second_report_id] == []
547 assert first_report.data["state"] == "resolved"
548 assert second_report.data["state"] == "resolved"
552 describe "reblog muting" do
554 muter = insert(:user)
556 muted = insert(:user)
558 [muter: muter, muted: muted]
561 test "add a reblog mute", %{muter: muter, muted: muted} do
562 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
564 assert User.showing_reblogs?(muter, muted) == false
567 test "remove a reblog mute", %{muter: muter, muted: muted} do
568 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
569 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
571 assert User.showing_reblogs?(muter, muted) == true
575 describe "unfollow/2" do
576 test "also unsubscribes a user" do
577 [follower, followed] = insert_pair(:user)
578 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
579 {:ok, _subscription} = User.subscribe(follower, followed)
581 assert User.subscribed_to?(follower, followed)
583 {:ok, follower} = CommonAPI.unfollow(follower, followed)
585 refute User.subscribed_to?(follower, followed)
588 test "cancels a pending follow for a local user" do
589 follower = insert(:user)
590 followed = insert(:user, locked: true)
592 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
593 CommonAPI.follow(follower, followed)
595 assert User.get_follow_state(follower, followed) == :follow_pending
596 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
597 assert User.get_follow_state(follower, followed) == nil
599 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
600 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
605 "object" => %{"type" => "Follow", "state" => "cancelled"}
607 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
610 test "cancels a pending follow for a remote user" do
611 follower = insert(:user)
612 followed = insert(:user, locked: true, local: false, ap_enabled: true)
614 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
615 CommonAPI.follow(follower, followed)
617 assert User.get_follow_state(follower, followed) == :follow_pending
618 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
619 assert User.get_follow_state(follower, followed) == nil
621 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
622 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
627 "object" => %{"type" => "Follow", "state" => "cancelled"}
629 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
633 describe "accept_follow_request/2" do
634 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
635 user = insert(:user, locked: true)
636 follower = insert(:user)
637 follower_two = insert(:user)
639 {:ok, follow_activity} = ActivityPub.follow(follower, user)
640 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
641 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
643 assert follow_activity.data["state"] == "pending"
644 assert follow_activity_two.data["state"] == "pending"
645 assert follow_activity_three.data["state"] == "pending"
647 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
649 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
650 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
651 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
654 test "after rejection, it sets all existing pending follow request states to 'reject'" do
655 user = insert(:user, locked: true)
656 follower = insert(:user)
657 follower_two = insert(:user)
659 {:ok, follow_activity} = ActivityPub.follow(follower, user)
660 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
661 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
663 assert follow_activity.data["state"] == "pending"
664 assert follow_activity_two.data["state"] == "pending"
665 assert follow_activity_three.data["state"] == "pending"
667 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
669 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
670 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
671 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
676 test "does not allow to vote twice" do
678 other_user = insert(:user)
681 CommonAPI.post(user, %{
682 "status" => "Am I cute?",
683 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
686 object = Object.normalize(activity)
688 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
690 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
694 describe "listen/2" do
695 test "returns a valid activity" do
699 CommonAPI.listen(user, %{
700 "title" => "lain radio episode 1",
701 "album" => "lain radio",
706 object = Object.normalize(activity)
708 assert object.data["title"] == "lain radio episode 1"
710 assert Visibility.get_visibility(activity) == "public"
713 test "respects visibility=private" do
717 CommonAPI.listen(user, %{
718 "title" => "lain radio episode 1",
719 "album" => "lain radio",
722 "visibility" => "private"
725 object = Object.normalize(activity)
727 assert object.data["title"] == "lain radio episode 1"
729 assert Visibility.get_visibility(activity) == "private"