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 "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
103 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
105 [participation] = Participation.for_user(user)
108 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
110 assert Visibility.is_direct?(convo_reply)
112 assert activity.data["context"] == convo_reply.data["context"]
115 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
117 jafnhar = insert(:user)
118 tridi = insert(:user)
121 CommonAPI.post(har, %{
122 "status" => "@#{jafnhar.nickname} hey",
123 "visibility" => "direct"
126 assert har.ap_id in activity.recipients
127 assert jafnhar.ap_id in activity.recipients
129 [participation] = Participation.for_user(har)
132 CommonAPI.post(har, %{
133 "status" => "I don't really like @#{tridi.nickname}",
134 "visibility" => "direct",
135 "in_reply_to_status_id" => activity.id,
136 "in_reply_to_conversation_id" => participation.id
139 assert har.ap_id in activity.recipients
140 assert jafnhar.ap_id in activity.recipients
141 refute tridi.ap_id in activity.recipients
144 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
146 jafnhar = insert(:user)
147 tridi = insert(:user)
149 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
152 CommonAPI.post(har, %{
153 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
154 "visibility" => "direct"
157 refute tridi.ap_id in activity.recipients
158 assert jafnhar.ap_id in activity.recipients
161 test "it de-duplicates tags" do
163 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
165 object = Object.normalize(activity)
167 assert object.data["tag"] == ["2hu"]
170 test "it adds emoji in the object" do
172 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
174 assert Object.normalize(activity).data["emoji"]["firefox"]
177 describe "posting" do
178 test "it supports explicit addressing" do
180 user_two = insert(:user)
181 user_three = insert(:user)
182 user_four = insert(:user)
185 CommonAPI.post(user, %{
187 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
188 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
191 assert user.ap_id in activity.recipients
192 assert user_two.ap_id in activity.recipients
193 assert user_four.ap_id in activity.recipients
194 refute user_three.ap_id in activity.recipients
197 test "it filters out obviously bad tags when accepting a post as HTML" do
200 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
203 CommonAPI.post(user, %{
205 "content_type" => "text/html"
208 object = Object.normalize(activity)
210 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
213 test "it filters out obviously bad tags when accepting a post as Markdown" do
216 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
219 CommonAPI.post(user, %{
221 "content_type" => "text/markdown"
224 object = Object.normalize(activity)
226 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
229 test "it does not allow replies to direct messages that are not direct messages themselves" do
232 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
235 CommonAPI.post(user, %{
236 "status" => "suya..",
237 "visibility" => "direct",
238 "in_reply_to_status_id" => activity.id
241 Enum.each(["public", "private", "unlisted"], fn visibility ->
242 assert {:error, "The message visibility must be direct"} =
243 CommonAPI.post(user, %{
244 "status" => "suya..",
245 "visibility" => visibility,
246 "in_reply_to_status_id" => activity.id
251 test "it allows to address a list" do
253 {:ok, list} = Pleroma.List.create("foo", user)
256 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
258 assert activity.data["bcc"] == [list.ap_id]
259 assert activity.recipients == [list.ap_id, user.ap_id]
260 assert activity.data["listMessage"] == list.ap_id
263 test "it returns error when status is empty and no attachments" do
266 assert {:error, "Cannot post an empty status without attachments"} =
267 CommonAPI.post(user, %{"status" => ""})
270 test "it validates character limits are correctly enforced" do
271 Pleroma.Config.put([:instance, :limit], 5)
275 assert {:error, "The status is over the character limit"} =
276 CommonAPI.post(user, %{"status" => "foobar"})
278 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
281 test "it can handle activities that expire" do
285 NaiveDateTime.utc_now()
286 |> NaiveDateTime.truncate(:second)
287 |> NaiveDateTime.add(1_000_000, :second)
289 assert {:ok, activity} =
290 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
292 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
293 assert expiration.scheduled_at == expires_at
297 describe "reactions" do
298 test "reacting to a status with an emoji" do
300 other_user = insert(:user)
302 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
304 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
306 assert reaction.data["actor"] == user.ap_id
307 assert reaction.data["content"] == "👍"
309 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
311 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
314 test "unreacting to a status with an emoji" do
316 other_user = insert(:user)
318 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
319 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
321 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
323 assert unreaction.data["type"] == "Undo"
324 assert unreaction.data["object"] == reaction.data["id"]
327 test "repeating a status" do
329 other_user = insert(:user)
331 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
333 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
336 test "can't repeat a repeat" do
338 other_user = insert(:user)
339 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
341 {:ok, %Activity{} = announce, _} = CommonAPI.repeat(activity.id, other_user)
343 refute match?({:ok, %Activity{}, _}, CommonAPI.repeat(announce.id, user))
346 test "repeating a status privately" do
348 other_user = insert(:user)
350 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
352 {:ok, %Activity{} = announce_activity, _} =
353 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
355 assert Visibility.is_private?(announce_activity)
358 test "favoriting a status" do
360 other_user = insert(:user)
362 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
364 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
365 assert data["type"] == "Like"
366 assert data["actor"] == user.ap_id
367 assert data["object"] == post_activity.data["object"]
370 test "retweeting a status twice returns the status" do
372 other_user = insert(:user)
374 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
375 {:ok, %Activity{} = announce, object} = CommonAPI.repeat(activity.id, user)
376 {:ok, ^announce, ^object} = CommonAPI.repeat(activity.id, user)
379 test "favoriting a status twice returns ok, but without the like activity" do
381 other_user = insert(:user)
383 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
384 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
385 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
389 describe "pinned statuses" do
391 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
394 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
396 [user: user, activity: activity]
399 test "pin status", %{user: user, activity: activity} do
400 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
403 user = refresh_record(user)
405 assert %User{pinned_activities: [^id]} = user
408 test "pin poll", %{user: user} do
410 CommonAPI.post(user, %{
411 "status" => "How is fediverse today?",
412 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
415 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
418 user = refresh_record(user)
420 assert %User{pinned_activities: [^id]} = user
423 test "unlisted statuses can be pinned", %{user: user} do
424 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
425 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
428 test "only self-authored can be pinned", %{activity: activity} do
431 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
434 test "max pinned statuses", %{user: user, activity: activity_one} do
435 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
437 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
439 user = refresh_record(user)
441 assert {:error, "You have already pinned the maximum number of statuses"} =
442 CommonAPI.pin(activity_two.id, user)
445 test "unpin status", %{user: user, activity: activity} do
446 {:ok, activity} = CommonAPI.pin(activity.id, user)
448 user = refresh_record(user)
452 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
454 user = refresh_record(user)
456 assert %User{pinned_activities: []} = user
459 test "should unpin when deleting a status", %{user: user, activity: activity} do
460 {:ok, activity} = CommonAPI.pin(activity.id, user)
462 user = refresh_record(user)
464 assert {:ok, _} = CommonAPI.delete(activity.id, user)
466 user = refresh_record(user)
468 assert %User{pinned_activities: []} = user
472 describe "mute tests" do
476 activity = insert(:note_activity)
478 [user: user, activity: activity]
481 test "add mute", %{user: user, activity: activity} do
482 {:ok, _} = CommonAPI.add_mute(user, activity)
483 assert CommonAPI.thread_muted?(user, activity)
486 test "remove mute", %{user: user, activity: activity} do
487 CommonAPI.add_mute(user, activity)
488 {:ok, _} = CommonAPI.remove_mute(user, activity)
489 refute CommonAPI.thread_muted?(user, activity)
492 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
493 CommonAPI.add_mute(user, activity)
494 {:error, _} = CommonAPI.add_mute(user, activity)
498 describe "reports" do
499 test "creates a report" do
500 reporter = insert(:user)
501 target_user = insert(:user)
503 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
505 reporter_ap_id = reporter.ap_id
506 target_ap_id = target_user.ap_id
507 activity_ap_id = activity.data["id"]
511 "account_id" => target_user.id,
512 "comment" => comment,
513 "status_ids" => [activity.id]
518 "id" => activity_ap_id,
519 "content" => "foobar",
520 "published" => activity.object.data["published"],
521 "actor" => AccountView.render("show.json", %{user: target_user})
524 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
527 actor: ^reporter_ap_id,
530 "content" => ^comment,
531 "object" => [^target_ap_id, ^note_obj],
537 test "updates report state" do
538 [reporter, target_user] = insert_pair(:user)
539 activity = insert(:note_activity, user: target_user)
541 {:ok, %Activity{id: report_id}} =
542 CommonAPI.report(reporter, %{
543 "account_id" => target_user.id,
544 "comment" => "I feel offended",
545 "status_ids" => [activity.id]
548 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
550 assert report.data["state"] == "resolved"
552 [reported_user, activity_id] = report.data["object"]
554 assert reported_user == target_user.ap_id
555 assert activity_id == activity.data["id"]
558 test "does not update report state when state is unsupported" do
559 [reporter, target_user] = insert_pair(:user)
560 activity = insert(:note_activity, user: target_user)
562 {:ok, %Activity{id: report_id}} =
563 CommonAPI.report(reporter, %{
564 "account_id" => target_user.id,
565 "comment" => "I feel offended",
566 "status_ids" => [activity.id]
569 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
572 test "updates state of multiple reports" do
573 [reporter, target_user] = insert_pair(:user)
574 activity = insert(:note_activity, user: target_user)
576 {:ok, %Activity{id: first_report_id}} =
577 CommonAPI.report(reporter, %{
578 "account_id" => target_user.id,
579 "comment" => "I feel offended",
580 "status_ids" => [activity.id]
583 {:ok, %Activity{id: second_report_id}} =
584 CommonAPI.report(reporter, %{
585 "account_id" => target_user.id,
586 "comment" => "I feel very offended!",
587 "status_ids" => [activity.id]
591 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
593 first_report = Activity.get_by_id(first_report_id)
594 second_report = Activity.get_by_id(second_report_id)
596 assert report_ids -- [first_report_id, second_report_id] == []
597 assert first_report.data["state"] == "resolved"
598 assert second_report.data["state"] == "resolved"
602 describe "reblog muting" do
604 muter = insert(:user)
606 muted = insert(:user)
608 [muter: muter, muted: muted]
611 test "add a reblog mute", %{muter: muter, muted: muted} do
612 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
614 assert User.showing_reblogs?(muter, muted) == false
617 test "remove a reblog mute", %{muter: muter, muted: muted} do
618 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
619 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
621 assert User.showing_reblogs?(muter, muted) == true
625 describe "unfollow/2" do
626 test "also unsubscribes a user" do
627 [follower, followed] = insert_pair(:user)
628 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
629 {:ok, _subscription} = User.subscribe(follower, followed)
631 assert User.subscribed_to?(follower, followed)
633 {:ok, follower} = CommonAPI.unfollow(follower, followed)
635 refute User.subscribed_to?(follower, followed)
638 test "cancels a pending follow for a local user" do
639 follower = insert(:user)
640 followed = insert(:user, locked: 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)
660 test "cancels a pending follow for a remote user" do
661 follower = insert(:user)
662 followed = insert(:user, locked: true, local: false, ap_enabled: true)
664 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
665 CommonAPI.follow(follower, followed)
667 assert User.get_follow_state(follower, followed) == :follow_pending
668 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
669 assert User.get_follow_state(follower, followed) == nil
671 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
672 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
677 "object" => %{"type" => "Follow", "state" => "cancelled"}
679 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
683 describe "accept_follow_request/2" do
684 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
685 user = insert(:user, locked: true)
686 follower = insert(:user)
687 follower_two = insert(:user)
689 {:ok, follow_activity} = ActivityPub.follow(follower, user)
690 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
691 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
693 assert follow_activity.data["state"] == "pending"
694 assert follow_activity_two.data["state"] == "pending"
695 assert follow_activity_three.data["state"] == "pending"
697 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
699 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
700 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
701 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
704 test "after rejection, it sets all existing pending follow request states to 'reject'" do
705 user = insert(:user, locked: true)
706 follower = insert(:user)
707 follower_two = insert(:user)
709 {:ok, follow_activity} = ActivityPub.follow(follower, user)
710 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
711 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
713 assert follow_activity.data["state"] == "pending"
714 assert follow_activity_two.data["state"] == "pending"
715 assert follow_activity_three.data["state"] == "pending"
717 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
719 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
720 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
721 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
726 test "does not allow to vote twice" do
728 other_user = insert(:user)
731 CommonAPI.post(user, %{
732 "status" => "Am I cute?",
733 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
736 object = Object.normalize(activity)
738 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
740 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
744 describe "listen/2" do
745 test "returns a valid activity" do
749 CommonAPI.listen(user, %{
750 "title" => "lain radio episode 1",
751 "album" => "lain radio",
756 object = Object.normalize(activity)
758 assert object.data["title"] == "lain radio episode 1"
760 assert Visibility.get_visibility(activity) == "public"
763 test "respects visibility=private" do
767 CommonAPI.listen(user, %{
768 "title" => "lain radio episode 1",
769 "album" => "lain radio",
772 "visibility" => "private"
775 object = Object.normalize(activity)
777 assert object.data["title"] == "lain radio episode 1"
779 assert Visibility.get_visibility(activity) == "private"