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"]
304 test "repeating a status" do
306 other_user = insert(:user)
308 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
310 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
313 test "can't repeat a repeat" do
315 other_user = insert(:user)
316 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
318 {:ok, %Activity{} = announce, _} = CommonAPI.repeat(activity.id, other_user)
320 refute match?({:ok, %Activity{}, _}, CommonAPI.repeat(announce.id, user))
323 test "repeating a status privately" do
325 other_user = insert(:user)
327 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
329 {:ok, %Activity{} = announce_activity, _} =
330 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
332 assert Visibility.is_private?(announce_activity)
335 test "favoriting a status" do
337 other_user = insert(:user)
339 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
341 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
342 assert data["type"] == "Like"
343 assert data["actor"] == user.ap_id
344 assert data["object"] == post_activity.data["object"]
347 test "retweeting a status twice returns the status" do
349 other_user = insert(:user)
351 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
352 {:ok, %Activity{} = announce, object} = CommonAPI.repeat(activity.id, user)
353 {:ok, ^announce, ^object} = CommonAPI.repeat(activity.id, user)
356 test "favoriting a status twice returns ok, but without the like activity" do
358 other_user = insert(:user)
360 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
361 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
362 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
366 describe "pinned statuses" do
368 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
371 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
373 [user: user, activity: activity]
376 test "pin status", %{user: user, activity: activity} do
377 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
380 user = refresh_record(user)
382 assert %User{pinned_activities: [^id]} = user
385 test "pin poll", %{user: user} do
387 CommonAPI.post(user, %{
388 "status" => "How is fediverse today?",
389 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
392 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
395 user = refresh_record(user)
397 assert %User{pinned_activities: [^id]} = user
400 test "unlisted statuses can be pinned", %{user: user} do
401 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
402 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
405 test "only self-authored can be pinned", %{activity: activity} do
408 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
411 test "max pinned statuses", %{user: user, activity: activity_one} do
412 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
414 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
416 user = refresh_record(user)
418 assert {:error, "You have already pinned the maximum number of statuses"} =
419 CommonAPI.pin(activity_two.id, user)
422 test "unpin status", %{user: user, activity: activity} do
423 {:ok, activity} = CommonAPI.pin(activity.id, user)
425 user = refresh_record(user)
429 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
431 user = refresh_record(user)
433 assert %User{pinned_activities: []} = user
436 test "should unpin when deleting a status", %{user: user, activity: activity} do
437 {:ok, activity} = CommonAPI.pin(activity.id, user)
439 user = refresh_record(user)
441 assert {:ok, _} = CommonAPI.delete(activity.id, user)
443 user = refresh_record(user)
445 assert %User{pinned_activities: []} = user
449 describe "mute tests" do
453 activity = insert(:note_activity)
455 [user: user, activity: activity]
458 test "add mute", %{user: user, activity: activity} do
459 {:ok, _} = CommonAPI.add_mute(user, activity)
460 assert CommonAPI.thread_muted?(user, activity)
463 test "remove mute", %{user: user, activity: activity} do
464 CommonAPI.add_mute(user, activity)
465 {:ok, _} = CommonAPI.remove_mute(user, activity)
466 refute CommonAPI.thread_muted?(user, activity)
469 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
470 CommonAPI.add_mute(user, activity)
471 {:error, _} = CommonAPI.add_mute(user, activity)
475 describe "reports" do
476 test "creates a report" do
477 reporter = insert(:user)
478 target_user = insert(:user)
480 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
482 reporter_ap_id = reporter.ap_id
483 target_ap_id = target_user.ap_id
484 activity_ap_id = activity.data["id"]
488 account_id: target_user.id,
490 status_ids: [activity.id]
495 "id" => activity_ap_id,
496 "content" => "foobar",
497 "published" => activity.object.data["published"],
498 "actor" => AccountView.render("show.json", %{user: target_user})
501 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
504 actor: ^reporter_ap_id,
507 "content" => ^comment,
508 "object" => [^target_ap_id, ^note_obj],
514 test "updates report state" do
515 [reporter, target_user] = insert_pair(:user)
516 activity = insert(:note_activity, user: target_user)
518 {:ok, %Activity{id: report_id}} =
519 CommonAPI.report(reporter, %{
520 account_id: target_user.id,
521 comment: "I feel offended",
522 status_ids: [activity.id]
525 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
527 assert report.data["state"] == "resolved"
529 [reported_user, activity_id] = report.data["object"]
531 assert reported_user == target_user.ap_id
532 assert activity_id == activity.data["id"]
535 test "does not update report state when state is unsupported" do
536 [reporter, target_user] = insert_pair(:user)
537 activity = insert(:note_activity, user: target_user)
539 {:ok, %Activity{id: report_id}} =
540 CommonAPI.report(reporter, %{
541 account_id: target_user.id,
542 comment: "I feel offended",
543 status_ids: [activity.id]
546 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
549 test "updates state of multiple reports" do
550 [reporter, target_user] = insert_pair(:user)
551 activity = insert(:note_activity, user: target_user)
553 {:ok, %Activity{id: first_report_id}} =
554 CommonAPI.report(reporter, %{
555 account_id: target_user.id,
556 comment: "I feel offended",
557 status_ids: [activity.id]
560 {:ok, %Activity{id: second_report_id}} =
561 CommonAPI.report(reporter, %{
562 account_id: target_user.id,
563 comment: "I feel very offended!",
564 status_ids: [activity.id]
568 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
570 first_report = Activity.get_by_id(first_report_id)
571 second_report = Activity.get_by_id(second_report_id)
573 assert report_ids -- [first_report_id, second_report_id] == []
574 assert first_report.data["state"] == "resolved"
575 assert second_report.data["state"] == "resolved"
579 describe "reblog muting" do
581 muter = insert(:user)
583 muted = insert(:user)
585 [muter: muter, muted: muted]
588 test "add a reblog mute", %{muter: muter, muted: muted} do
589 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
591 assert User.showing_reblogs?(muter, muted) == false
594 test "remove a reblog mute", %{muter: muter, muted: muted} do
595 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
596 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
598 assert User.showing_reblogs?(muter, muted) == true
602 describe "unfollow/2" do
603 test "also unsubscribes a user" do
604 [follower, followed] = insert_pair(:user)
605 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
606 {:ok, _subscription} = User.subscribe(follower, followed)
608 assert User.subscribed_to?(follower, followed)
610 {:ok, follower} = CommonAPI.unfollow(follower, followed)
612 refute User.subscribed_to?(follower, followed)
615 test "cancels a pending follow for a local user" do
616 follower = insert(:user)
617 followed = insert(:user, locked: true)
619 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
620 CommonAPI.follow(follower, followed)
622 assert User.get_follow_state(follower, followed) == :follow_pending
623 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
624 assert User.get_follow_state(follower, followed) == nil
626 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
627 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
632 "object" => %{"type" => "Follow", "state" => "cancelled"}
634 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
637 test "cancels a pending follow for a remote user" do
638 follower = insert(:user)
639 followed = insert(:user, locked: true, local: false, ap_enabled: true)
641 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
642 CommonAPI.follow(follower, followed)
644 assert User.get_follow_state(follower, followed) == :follow_pending
645 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
646 assert User.get_follow_state(follower, followed) == nil
648 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
649 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
654 "object" => %{"type" => "Follow", "state" => "cancelled"}
656 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
660 describe "accept_follow_request/2" do
661 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
662 user = insert(:user, locked: true)
663 follower = insert(:user)
664 follower_two = insert(:user)
666 {:ok, follow_activity} = ActivityPub.follow(follower, user)
667 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
668 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
670 assert follow_activity.data["state"] == "pending"
671 assert follow_activity_two.data["state"] == "pending"
672 assert follow_activity_three.data["state"] == "pending"
674 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
676 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
677 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
678 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
681 test "after rejection, it sets all existing pending follow request states to 'reject'" do
682 user = insert(:user, locked: true)
683 follower = insert(:user)
684 follower_two = insert(:user)
686 {:ok, follow_activity} = ActivityPub.follow(follower, user)
687 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
688 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
690 assert follow_activity.data["state"] == "pending"
691 assert follow_activity_two.data["state"] == "pending"
692 assert follow_activity_three.data["state"] == "pending"
694 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
696 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
697 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
698 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
701 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
702 user = insert(:user, locked: true)
703 not_follower = insert(:user)
704 CommonAPI.accept_follow_request(not_follower, user)
706 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
711 test "does not allow to vote twice" do
713 other_user = insert(:user)
716 CommonAPI.post(user, %{
717 "status" => "Am I cute?",
718 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
721 object = Object.normalize(activity)
723 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
725 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
729 describe "listen/2" do
730 test "returns a valid activity" do
734 CommonAPI.listen(user, %{
735 "title" => "lain radio episode 1",
736 "album" => "lain radio",
741 object = Object.normalize(activity)
743 assert object.data["title"] == "lain radio episode 1"
745 assert Visibility.get_visibility(activity) == "public"
748 test "respects visibility=private" do
752 CommonAPI.listen(user, %{
753 "title" => "lain radio episode 1",
754 "album" => "lain radio",
757 "visibility" => "private"
760 object = Object.normalize(activity)
762 assert object.data["title"] == "lain radio episode 1"
764 assert Visibility.get_visibility(activity) == "private"