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 "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
76 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
78 [participation] = Participation.for_user(user)
81 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
83 assert Visibility.is_direct?(convo_reply)
85 assert activity.data["context"] == convo_reply.data["context"]
88 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
90 jafnhar = insert(:user)
94 CommonAPI.post(har, %{
95 "status" => "@#{jafnhar.nickname} hey",
96 "visibility" => "direct"
99 assert har.ap_id in activity.recipients
100 assert jafnhar.ap_id in activity.recipients
102 [participation] = Participation.for_user(har)
105 CommonAPI.post(har, %{
106 "status" => "I don't really like @#{tridi.nickname}",
107 "visibility" => "direct",
108 "in_reply_to_status_id" => activity.id,
109 "in_reply_to_conversation_id" => participation.id
112 assert har.ap_id in activity.recipients
113 assert jafnhar.ap_id in activity.recipients
114 refute tridi.ap_id in activity.recipients
117 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
119 jafnhar = insert(:user)
120 tridi = insert(:user)
122 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
125 CommonAPI.post(har, %{
126 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
127 "visibility" => "direct"
130 refute tridi.ap_id in activity.recipients
131 assert jafnhar.ap_id in activity.recipients
134 test "it de-duplicates tags" do
136 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
138 object = Object.normalize(activity)
140 assert object.data["tag"] == ["2hu"]
143 test "it adds emoji in the object" do
145 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
147 assert Object.normalize(activity).data["emoji"]["firefox"]
150 describe "posting" do
151 test "it supports explicit addressing" do
153 user_two = insert(:user)
154 user_three = insert(:user)
155 user_four = insert(:user)
158 CommonAPI.post(user, %{
160 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
161 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
164 assert user.ap_id in activity.recipients
165 assert user_two.ap_id in activity.recipients
166 assert user_four.ap_id in activity.recipients
167 refute user_three.ap_id in activity.recipients
170 test "it filters out obviously bad tags when accepting a post as HTML" do
173 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
176 CommonAPI.post(user, %{
178 "content_type" => "text/html"
181 object = Object.normalize(activity)
183 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
186 test "it filters out obviously bad tags when accepting a post as Markdown" do
189 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
192 CommonAPI.post(user, %{
194 "content_type" => "text/markdown"
197 object = Object.normalize(activity)
199 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
202 test "it does not allow replies to direct messages that are not direct messages themselves" do
205 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
208 CommonAPI.post(user, %{
209 "status" => "suya..",
210 "visibility" => "direct",
211 "in_reply_to_status_id" => activity.id
214 Enum.each(["public", "private", "unlisted"], fn visibility ->
215 assert {:error, "The message visibility must be direct"} =
216 CommonAPI.post(user, %{
217 "status" => "suya..",
218 "visibility" => visibility,
219 "in_reply_to_status_id" => activity.id
224 test "it allows to address a list" do
226 {:ok, list} = Pleroma.List.create("foo", user)
229 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
231 assert activity.data["bcc"] == [list.ap_id]
232 assert activity.recipients == [list.ap_id, user.ap_id]
233 assert activity.data["listMessage"] == list.ap_id
236 test "it returns error when status is empty and no attachments" do
239 assert {:error, "Cannot post an empty status without attachments"} =
240 CommonAPI.post(user, %{"status" => ""})
243 test "it validates character limits are correctly enforced" do
244 Pleroma.Config.put([:instance, :limit], 5)
248 assert {:error, "The status is over the character limit"} =
249 CommonAPI.post(user, %{"status" => "foobar"})
251 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
254 test "it can handle activities that expire" do
258 NaiveDateTime.utc_now()
259 |> NaiveDateTime.truncate(:second)
260 |> NaiveDateTime.add(1_000_000, :second)
262 assert {:ok, activity} =
263 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
265 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
266 assert expiration.scheduled_at == expires_at
270 describe "reactions" do
271 test "reacting to a status with an emoji" do
273 other_user = insert(:user)
275 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
277 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
279 assert reaction.data["actor"] == user.ap_id
280 assert reaction.data["content"] == "👍"
282 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
284 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
287 test "unreacting to a status with an emoji" do
289 other_user = insert(:user)
291 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
292 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
294 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
296 assert unreaction.data["type"] == "Undo"
297 assert unreaction.data["object"] == reaction.data["id"]
300 test "repeating a status" do
302 other_user = insert(:user)
304 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
306 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
309 test "repeating a status privately" do
311 other_user = insert(:user)
313 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
315 {:ok, %Activity{} = announce_activity, _} =
316 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
318 assert Visibility.is_private?(announce_activity)
321 test "favoriting a status" do
323 other_user = insert(:user)
325 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
327 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
328 assert data["type"] == "Like"
329 assert data["actor"] == user.ap_id
330 assert data["object"] == post_activity.data["object"]
333 test "retweeting a status twice returns the status" do
335 other_user = insert(:user)
337 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
338 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
339 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
342 test "favoriting a status twice returns ok, but without the like activity" do
344 other_user = insert(:user)
346 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
347 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
348 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
352 describe "pinned statuses" do
354 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
357 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
359 [user: user, activity: activity]
362 test "pin status", %{user: user, activity: activity} do
363 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
366 user = refresh_record(user)
368 assert %User{pinned_activities: [^id]} = user
371 test "pin poll", %{user: user} do
373 CommonAPI.post(user, %{
374 "status" => "How is fediverse today?",
375 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
378 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
381 user = refresh_record(user)
383 assert %User{pinned_activities: [^id]} = user
386 test "unlisted statuses can be pinned", %{user: user} do
387 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
388 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
391 test "only self-authored can be pinned", %{activity: activity} do
394 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
397 test "max pinned statuses", %{user: user, activity: activity_one} do
398 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
400 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
402 user = refresh_record(user)
404 assert {:error, "You have already pinned the maximum number of statuses"} =
405 CommonAPI.pin(activity_two.id, user)
408 test "unpin status", %{user: user, activity: activity} do
409 {:ok, activity} = CommonAPI.pin(activity.id, user)
411 user = refresh_record(user)
413 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
415 user = refresh_record(user)
417 assert %User{pinned_activities: []} = user
420 test "should unpin when deleting a status", %{user: user, activity: activity} do
421 {:ok, activity} = CommonAPI.pin(activity.id, user)
423 user = refresh_record(user)
425 assert {:ok, _} = CommonAPI.delete(activity.id, user)
427 user = refresh_record(user)
429 assert %User{pinned_activities: []} = user
433 describe "mute tests" do
437 activity = insert(:note_activity)
439 [user: user, activity: activity]
442 test "add mute", %{user: user, activity: activity} do
443 {:ok, _} = CommonAPI.add_mute(user, activity)
444 assert CommonAPI.thread_muted?(user, activity)
447 test "remove mute", %{user: user, activity: activity} do
448 CommonAPI.add_mute(user, activity)
449 {:ok, _} = CommonAPI.remove_mute(user, activity)
450 refute CommonAPI.thread_muted?(user, activity)
453 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
454 CommonAPI.add_mute(user, activity)
455 {:error, _} = CommonAPI.add_mute(user, activity)
459 describe "reports" do
460 test "creates a report" do
461 reporter = insert(:user)
462 target_user = insert(:user)
464 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
466 reporter_ap_id = reporter.ap_id
467 target_ap_id = target_user.ap_id
468 activity_ap_id = activity.data["id"]
472 "account_id" => target_user.id,
473 "comment" => comment,
474 "status_ids" => [activity.id]
479 "id" => activity_ap_id,
480 "content" => "foobar",
481 "published" => activity.object.data["published"],
482 "actor" => AccountView.render("show.json", %{user: target_user})
485 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
488 actor: ^reporter_ap_id,
491 "content" => ^comment,
492 "object" => [^target_ap_id, ^note_obj],
498 test "updates report state" do
499 [reporter, target_user] = insert_pair(:user)
500 activity = insert(:note_activity, user: target_user)
502 {:ok, %Activity{id: report_id}} =
503 CommonAPI.report(reporter, %{
504 "account_id" => target_user.id,
505 "comment" => "I feel offended",
506 "status_ids" => [activity.id]
509 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
511 assert report.data["state"] == "resolved"
513 [reported_user, activity_id] = report.data["object"]
515 assert reported_user == target_user.ap_id
516 assert activity_id == activity.data["id"]
519 test "does not update report state when state is unsupported" do
520 [reporter, target_user] = insert_pair(:user)
521 activity = insert(:note_activity, user: target_user)
523 {:ok, %Activity{id: report_id}} =
524 CommonAPI.report(reporter, %{
525 "account_id" => target_user.id,
526 "comment" => "I feel offended",
527 "status_ids" => [activity.id]
530 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
533 test "updates state of multiple reports" do
534 [reporter, target_user] = insert_pair(:user)
535 activity = insert(:note_activity, user: target_user)
537 {:ok, %Activity{id: first_report_id}} =
538 CommonAPI.report(reporter, %{
539 "account_id" => target_user.id,
540 "comment" => "I feel offended",
541 "status_ids" => [activity.id]
544 {:ok, %Activity{id: second_report_id}} =
545 CommonAPI.report(reporter, %{
546 "account_id" => target_user.id,
547 "comment" => "I feel very offended!",
548 "status_ids" => [activity.id]
552 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
554 first_report = Activity.get_by_id(first_report_id)
555 second_report = Activity.get_by_id(second_report_id)
557 assert report_ids -- [first_report_id, second_report_id] == []
558 assert first_report.data["state"] == "resolved"
559 assert second_report.data["state"] == "resolved"
563 describe "reblog muting" do
565 muter = insert(:user)
567 muted = insert(:user)
569 [muter: muter, muted: muted]
572 test "add a reblog mute", %{muter: muter, muted: muted} do
573 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
575 assert User.showing_reblogs?(muter, muted) == false
578 test "remove a reblog mute", %{muter: muter, muted: muted} do
579 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
580 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
582 assert User.showing_reblogs?(muter, muted) == true
586 describe "unfollow/2" do
587 test "also unsubscribes a user" do
588 [follower, followed] = insert_pair(:user)
589 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
590 {:ok, _subscription} = User.subscribe(follower, followed)
592 assert User.subscribed_to?(follower, followed)
594 {:ok, follower} = CommonAPI.unfollow(follower, followed)
596 refute User.subscribed_to?(follower, followed)
599 test "cancels a pending follow for a local user" do
600 follower = insert(:user)
601 followed = insert(:user, locked: true)
603 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
604 CommonAPI.follow(follower, followed)
606 assert User.get_follow_state(follower, followed) == :follow_pending
607 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
608 assert User.get_follow_state(follower, followed) == nil
610 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
611 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
616 "object" => %{"type" => "Follow", "state" => "cancelled"}
618 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
621 test "cancels a pending follow for a remote user" do
622 follower = insert(:user)
623 followed = insert(:user, locked: true, local: false, ap_enabled: true)
625 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
626 CommonAPI.follow(follower, followed)
628 assert User.get_follow_state(follower, followed) == :follow_pending
629 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
630 assert User.get_follow_state(follower, followed) == nil
632 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
633 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
638 "object" => %{"type" => "Follow", "state" => "cancelled"}
640 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
644 describe "accept_follow_request/2" do
645 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
646 user = insert(:user, locked: true)
647 follower = insert(:user)
648 follower_two = insert(:user)
650 {:ok, follow_activity} = ActivityPub.follow(follower, user)
651 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
652 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
654 assert follow_activity.data["state"] == "pending"
655 assert follow_activity_two.data["state"] == "pending"
656 assert follow_activity_three.data["state"] == "pending"
658 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
660 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
661 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
662 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
665 test "after rejection, it sets all existing pending follow request states to 'reject'" do
666 user = insert(:user, locked: true)
667 follower = insert(:user)
668 follower_two = insert(:user)
670 {:ok, follow_activity} = ActivityPub.follow(follower, user)
671 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
672 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
674 assert follow_activity.data["state"] == "pending"
675 assert follow_activity_two.data["state"] == "pending"
676 assert follow_activity_three.data["state"] == "pending"
678 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
680 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
681 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
682 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
687 test "does not allow to vote twice" do
689 other_user = insert(:user)
692 CommonAPI.post(user, %{
693 "status" => "Am I cute?",
694 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
697 object = Object.normalize(activity)
699 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
701 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
705 describe "listen/2" do
706 test "returns a valid activity" do
710 CommonAPI.listen(user, %{
711 "title" => "lain radio episode 1",
712 "album" => "lain radio",
717 object = Object.normalize(activity)
719 assert object.data["title"] == "lain radio episode 1"
721 assert Visibility.get_visibility(activity) == "public"
724 test "respects visibility=private" do
728 CommonAPI.listen(user, %{
729 "title" => "lain radio episode 1",
730 "album" => "lain radio",
733 "visibility" => "private"
736 object = Object.normalize(activity)
738 assert object.data["title"] == "lain radio episode 1"
740 assert Visibility.get_visibility(activity) == "private"