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>"
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>"
49 assert Chat.get(author.id, recipient.ap_id)
50 assert Chat.get(recipient.id, author.ap_id)
53 test "it reject messages over the local limit" do
54 Pleroma.Config.put([:instance, :chat_limit], 2)
56 author = insert(:user)
57 recipient = insert(:user)
60 CommonAPI.post_chat_message(
66 assert message == :content_too_long
70 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
72 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
74 [participation] = Participation.for_user(user)
77 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
79 assert Visibility.is_direct?(convo_reply)
81 assert activity.data["context"] == convo_reply.data["context"]
84 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
86 jafnhar = insert(:user)
90 CommonAPI.post(har, %{
91 "status" => "@#{jafnhar.nickname} hey",
92 "visibility" => "direct"
95 assert har.ap_id in activity.recipients
96 assert jafnhar.ap_id in activity.recipients
98 [participation] = Participation.for_user(har)
101 CommonAPI.post(har, %{
102 "status" => "I don't really like @#{tridi.nickname}",
103 "visibility" => "direct",
104 "in_reply_to_status_id" => activity.id,
105 "in_reply_to_conversation_id" => participation.id
108 assert har.ap_id in activity.recipients
109 assert jafnhar.ap_id in activity.recipients
110 refute tridi.ap_id in activity.recipients
113 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
115 jafnhar = insert(:user)
116 tridi = insert(:user)
118 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
121 CommonAPI.post(har, %{
122 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
123 "visibility" => "direct"
126 refute tridi.ap_id in activity.recipients
127 assert jafnhar.ap_id in activity.recipients
130 test "it de-duplicates tags" do
132 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
134 object = Object.normalize(activity)
136 assert object.data["tag"] == ["2hu"]
139 test "it adds emoji in the object" do
141 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
143 assert Object.normalize(activity).data["emoji"]["firefox"]
146 describe "posting" do
147 test "it supports explicit addressing" do
149 user_two = insert(:user)
150 user_three = insert(:user)
151 user_four = insert(:user)
154 CommonAPI.post(user, %{
156 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
157 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
160 assert user.ap_id in activity.recipients
161 assert user_two.ap_id in activity.recipients
162 assert user_four.ap_id in activity.recipients
163 refute user_three.ap_id in activity.recipients
166 test "it filters out obviously bad tags when accepting a post as HTML" do
169 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
172 CommonAPI.post(user, %{
174 "content_type" => "text/html"
177 object = Object.normalize(activity)
179 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
182 test "it filters out obviously bad tags when accepting a post as Markdown" do
185 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
188 CommonAPI.post(user, %{
190 "content_type" => "text/markdown"
193 object = Object.normalize(activity)
195 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
198 test "it does not allow replies to direct messages that are not direct messages themselves" do
201 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
204 CommonAPI.post(user, %{
205 "status" => "suya..",
206 "visibility" => "direct",
207 "in_reply_to_status_id" => activity.id
210 Enum.each(["public", "private", "unlisted"], fn visibility ->
211 assert {:error, "The message visibility must be direct"} =
212 CommonAPI.post(user, %{
213 "status" => "suya..",
214 "visibility" => visibility,
215 "in_reply_to_status_id" => activity.id
220 test "it allows to address a list" do
222 {:ok, list} = Pleroma.List.create("foo", user)
225 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
227 assert activity.data["bcc"] == [list.ap_id]
228 assert activity.recipients == [list.ap_id, user.ap_id]
229 assert activity.data["listMessage"] == list.ap_id
232 test "it returns error when status is empty and no attachments" do
235 assert {:error, "Cannot post an empty status without attachments"} =
236 CommonAPI.post(user, %{"status" => ""})
239 test "it validates character limits are correctly enforced" do
240 Pleroma.Config.put([:instance, :limit], 5)
244 assert {:error, "The status is over the character limit"} =
245 CommonAPI.post(user, %{"status" => "foobar"})
247 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
250 test "it can handle activities that expire" do
254 NaiveDateTime.utc_now()
255 |> NaiveDateTime.truncate(:second)
256 |> NaiveDateTime.add(1_000_000, :second)
258 assert {:ok, activity} =
259 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
261 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
262 assert expiration.scheduled_at == expires_at
266 describe "reactions" do
267 test "reacting to a status with an emoji" do
269 other_user = insert(:user)
271 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
273 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
275 assert reaction.data["actor"] == user.ap_id
276 assert reaction.data["content"] == "👍"
278 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
280 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
283 test "unreacting to a status with an emoji" do
285 other_user = insert(:user)
287 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
288 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
290 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
292 assert unreaction.data["type"] == "Undo"
293 assert unreaction.data["object"] == reaction.data["id"]
296 test "repeating a status" do
298 other_user = insert(:user)
300 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
302 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
305 test "repeating a status privately" do
307 other_user = insert(:user)
309 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
311 {:ok, %Activity{} = announce_activity, _} =
312 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
314 assert Visibility.is_private?(announce_activity)
317 test "favoriting a status" do
319 other_user = insert(:user)
321 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
323 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
324 assert data["type"] == "Like"
325 assert data["actor"] == user.ap_id
326 assert data["object"] == post_activity.data["object"]
329 test "retweeting a status twice returns the status" do
331 other_user = insert(:user)
333 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
334 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
335 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
338 test "favoriting a status twice returns ok, but without the like activity" do
340 other_user = insert(:user)
342 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
343 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
344 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
348 describe "pinned statuses" do
350 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
353 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
355 [user: user, activity: activity]
358 test "pin status", %{user: user, activity: activity} do
359 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
362 user = refresh_record(user)
364 assert %User{pinned_activities: [^id]} = user
367 test "pin poll", %{user: user} do
369 CommonAPI.post(user, %{
370 "status" => "How is fediverse today?",
371 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
374 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
377 user = refresh_record(user)
379 assert %User{pinned_activities: [^id]} = user
382 test "unlisted statuses can be pinned", %{user: user} do
383 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
384 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
387 test "only self-authored can be pinned", %{activity: activity} do
390 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
393 test "max pinned statuses", %{user: user, activity: activity_one} do
394 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
396 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
398 user = refresh_record(user)
400 assert {:error, "You have already pinned the maximum number of statuses"} =
401 CommonAPI.pin(activity_two.id, user)
404 test "unpin status", %{user: user, activity: activity} do
405 {:ok, activity} = CommonAPI.pin(activity.id, user)
407 user = refresh_record(user)
409 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
411 user = refresh_record(user)
413 assert %User{pinned_activities: []} = user
416 test "should unpin when deleting a status", %{user: user, activity: activity} do
417 {:ok, activity} = CommonAPI.pin(activity.id, user)
419 user = refresh_record(user)
421 assert {:ok, _} = CommonAPI.delete(activity.id, user)
423 user = refresh_record(user)
425 assert %User{pinned_activities: []} = user
429 describe "mute tests" do
433 activity = insert(:note_activity)
435 [user: user, activity: activity]
438 test "add mute", %{user: user, activity: activity} do
439 {:ok, _} = CommonAPI.add_mute(user, activity)
440 assert CommonAPI.thread_muted?(user, activity)
443 test "remove mute", %{user: user, activity: activity} do
444 CommonAPI.add_mute(user, activity)
445 {:ok, _} = CommonAPI.remove_mute(user, activity)
446 refute CommonAPI.thread_muted?(user, activity)
449 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
450 CommonAPI.add_mute(user, activity)
451 {:error, _} = CommonAPI.add_mute(user, activity)
455 describe "reports" do
456 test "creates a report" do
457 reporter = insert(:user)
458 target_user = insert(:user)
460 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
462 reporter_ap_id = reporter.ap_id
463 target_ap_id = target_user.ap_id
464 activity_ap_id = activity.data["id"]
468 "account_id" => target_user.id,
469 "comment" => comment,
470 "status_ids" => [activity.id]
475 "id" => activity_ap_id,
476 "content" => "foobar",
477 "published" => activity.object.data["published"],
478 "actor" => AccountView.render("show.json", %{user: target_user})
481 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
484 actor: ^reporter_ap_id,
487 "content" => ^comment,
488 "object" => [^target_ap_id, ^note_obj],
494 test "updates report state" do
495 [reporter, target_user] = insert_pair(:user)
496 activity = insert(:note_activity, user: target_user)
498 {:ok, %Activity{id: report_id}} =
499 CommonAPI.report(reporter, %{
500 "account_id" => target_user.id,
501 "comment" => "I feel offended",
502 "status_ids" => [activity.id]
505 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
507 assert report.data["state"] == "resolved"
509 [reported_user, activity_id] = report.data["object"]
511 assert reported_user == target_user.ap_id
512 assert activity_id == activity.data["id"]
515 test "does not update report state when state is unsupported" 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 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
529 test "updates state of multiple reports" do
530 [reporter, target_user] = insert_pair(:user)
531 activity = insert(:note_activity, user: target_user)
533 {:ok, %Activity{id: first_report_id}} =
534 CommonAPI.report(reporter, %{
535 "account_id" => target_user.id,
536 "comment" => "I feel offended",
537 "status_ids" => [activity.id]
540 {:ok, %Activity{id: second_report_id}} =
541 CommonAPI.report(reporter, %{
542 "account_id" => target_user.id,
543 "comment" => "I feel very offended!",
544 "status_ids" => [activity.id]
548 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
550 first_report = Activity.get_by_id(first_report_id)
551 second_report = Activity.get_by_id(second_report_id)
553 assert report_ids -- [first_report_id, second_report_id] == []
554 assert first_report.data["state"] == "resolved"
555 assert second_report.data["state"] == "resolved"
559 describe "reblog muting" do
561 muter = insert(:user)
563 muted = insert(:user)
565 [muter: muter, muted: muted]
568 test "add a reblog mute", %{muter: muter, muted: muted} do
569 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
571 assert User.showing_reblogs?(muter, muted) == false
574 test "remove a reblog mute", %{muter: muter, muted: muted} do
575 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
576 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
578 assert User.showing_reblogs?(muter, muted) == true
582 describe "unfollow/2" do
583 test "also unsubscribes a user" do
584 [follower, followed] = insert_pair(:user)
585 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
586 {:ok, _subscription} = User.subscribe(follower, followed)
588 assert User.subscribed_to?(follower, followed)
590 {:ok, follower} = CommonAPI.unfollow(follower, followed)
592 refute User.subscribed_to?(follower, followed)
595 test "cancels a pending follow for a local user" do
596 follower = insert(:user)
597 followed = insert(:user, locked: true)
599 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
600 CommonAPI.follow(follower, followed)
602 assert User.get_follow_state(follower, followed) == :follow_pending
603 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
604 assert User.get_follow_state(follower, followed) == nil
606 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
607 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
612 "object" => %{"type" => "Follow", "state" => "cancelled"}
614 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
617 test "cancels a pending follow for a remote user" do
618 follower = insert(:user)
619 followed = insert(:user, locked: true, local: false, ap_enabled: true)
621 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
622 CommonAPI.follow(follower, followed)
624 assert User.get_follow_state(follower, followed) == :follow_pending
625 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
626 assert User.get_follow_state(follower, followed) == nil
628 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
629 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
634 "object" => %{"type" => "Follow", "state" => "cancelled"}
636 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
640 describe "accept_follow_request/2" do
641 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
642 user = insert(:user, locked: true)
643 follower = insert(:user)
644 follower_two = insert(:user)
646 {:ok, follow_activity} = ActivityPub.follow(follower, user)
647 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
648 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
650 assert follow_activity.data["state"] == "pending"
651 assert follow_activity_two.data["state"] == "pending"
652 assert follow_activity_three.data["state"] == "pending"
654 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
656 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
657 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
658 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
661 test "after rejection, it sets all existing pending follow request states to 'reject'" 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.reject_follow_request(follower, user)
676 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
677 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
678 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
683 test "does not allow to vote twice" do
685 other_user = insert(:user)
688 CommonAPI.post(user, %{
689 "status" => "Am I cute?",
690 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
693 object = Object.normalize(activity)
695 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
697 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
701 describe "listen/2" do
702 test "returns a valid activity" do
706 CommonAPI.listen(user, %{
707 "title" => "lain radio episode 1",
708 "album" => "lain radio",
713 object = Object.normalize(activity)
715 assert object.data["title"] == "lain radio episode 1"
717 assert Visibility.get_visibility(activity) == "public"
720 test "respects visibility=private" do
724 CommonAPI.listen(user, %{
725 "title" => "lain radio episode 1",
726 "album" => "lain radio",
729 "visibility" => "private"
732 object = Object.normalize(activity)
734 assert object.data["title"] == "lain radio episode 1"
736 assert Visibility.get_visibility(activity) == "private"