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 test "it posts a chat message" do
27 author = insert(:user)
28 recipient = insert(:user)
31 CommonAPI.post_chat_message(
34 "a test message <script>alert('uuu')</script>"
37 assert activity.data["type"] == "Create"
39 object = Object.normalize(activity)
41 assert object.data["type"] == "ChatMessage"
42 assert object.data["to"] == [recipient.ap_id]
44 assert object.data["content"] ==
45 "a test message <script>alert('uuu')</script>"
47 assert Chat.get(author.id, recipient.ap_id)
48 assert Chat.get(recipient.id, author.ap_id)
52 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
54 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
56 [participation] = Participation.for_user(user)
59 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
61 assert Visibility.is_direct?(convo_reply)
63 assert activity.data["context"] == convo_reply.data["context"]
66 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
68 jafnhar = insert(:user)
72 CommonAPI.post(har, %{
73 "status" => "@#{jafnhar.nickname} hey",
74 "visibility" => "direct"
77 assert har.ap_id in activity.recipients
78 assert jafnhar.ap_id in activity.recipients
80 [participation] = Participation.for_user(har)
83 CommonAPI.post(har, %{
84 "status" => "I don't really like @#{tridi.nickname}",
85 "visibility" => "direct",
86 "in_reply_to_status_id" => activity.id,
87 "in_reply_to_conversation_id" => participation.id
90 assert har.ap_id in activity.recipients
91 assert jafnhar.ap_id in activity.recipients
92 refute tridi.ap_id in activity.recipients
95 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
97 jafnhar = insert(:user)
100 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
103 CommonAPI.post(har, %{
104 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
105 "visibility" => "direct"
108 refute tridi.ap_id in activity.recipients
109 assert jafnhar.ap_id in activity.recipients
112 test "it de-duplicates tags" do
114 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
116 object = Object.normalize(activity)
118 assert object.data["tag"] == ["2hu"]
121 test "it adds emoji in the object" do
123 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
125 assert Object.normalize(activity).data["emoji"]["firefox"]
128 test "it adds emoji when updating profiles" do
129 user = insert(:user, %{name: ":firefox:"})
131 {:ok, activity} = CommonAPI.update(user)
132 user = User.get_cached_by_ap_id(user.ap_id)
133 [firefox] = user.source_data["tag"]
135 assert firefox["name"] == ":firefox:"
137 assert Pleroma.Constants.as_public() in activity.recipients
140 describe "posting" do
141 test "it supports explicit addressing" do
143 user_two = insert(:user)
144 user_three = insert(:user)
145 user_four = insert(:user)
148 CommonAPI.post(user, %{
150 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
151 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
154 assert user.ap_id in activity.recipients
155 assert user_two.ap_id in activity.recipients
156 assert user_four.ap_id in activity.recipients
157 refute user_three.ap_id in activity.recipients
160 test "it filters out obviously bad tags when accepting a post as HTML" do
163 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
166 CommonAPI.post(user, %{
168 "content_type" => "text/html"
171 object = Object.normalize(activity)
173 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
176 test "it filters out obviously bad tags when accepting a post as Markdown" do
179 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
182 CommonAPI.post(user, %{
184 "content_type" => "text/markdown"
187 object = Object.normalize(activity)
189 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
192 test "it does not allow replies to direct messages that are not direct messages themselves" do
195 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
198 CommonAPI.post(user, %{
199 "status" => "suya..",
200 "visibility" => "direct",
201 "in_reply_to_status_id" => activity.id
204 Enum.each(["public", "private", "unlisted"], fn visibility ->
205 assert {:error, "The message visibility must be direct"} =
206 CommonAPI.post(user, %{
207 "status" => "suya..",
208 "visibility" => visibility,
209 "in_reply_to_status_id" => activity.id
214 test "it allows to address a list" do
216 {:ok, list} = Pleroma.List.create("foo", user)
219 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
221 assert activity.data["bcc"] == [list.ap_id]
222 assert activity.recipients == [list.ap_id, user.ap_id]
223 assert activity.data["listMessage"] == list.ap_id
226 test "it returns error when status is empty and no attachments" do
229 assert {:error, "Cannot post an empty status without attachments"} =
230 CommonAPI.post(user, %{"status" => ""})
233 test "it validates character limits are correctly enforced" do
234 Pleroma.Config.put([:instance, :limit], 5)
238 assert {:error, "The status is over the character limit"} =
239 CommonAPI.post(user, %{"status" => "foobar"})
241 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
244 test "it can handle activities that expire" do
248 NaiveDateTime.utc_now()
249 |> NaiveDateTime.truncate(:second)
250 |> NaiveDateTime.add(1_000_000, :second)
252 assert {:ok, activity} =
253 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
255 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
256 assert expiration.scheduled_at == expires_at
260 describe "reactions" do
261 test "reacting to a status with an emoji" do
263 other_user = insert(:user)
265 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
267 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
269 assert reaction.data["actor"] == user.ap_id
270 assert reaction.data["content"] == "👍"
272 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
274 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
277 test "unreacting to a status with an emoji" do
279 other_user = insert(:user)
281 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
282 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
284 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
286 assert unreaction.data["type"] == "Undo"
287 assert unreaction.data["object"] == reaction.data["id"]
290 test "repeating a status" do
292 other_user = insert(:user)
294 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
296 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
299 test "repeating a status privately" do
301 other_user = insert(:user)
303 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
305 {:ok, %Activity{} = announce_activity, _} =
306 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
308 assert Visibility.is_private?(announce_activity)
311 test "favoriting a status" do
313 other_user = insert(:user)
315 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
317 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
318 assert data["type"] == "Like"
319 assert data["actor"] == user.ap_id
320 assert data["object"] == post_activity.data["object"]
323 test "retweeting a status twice returns the status" do
325 other_user = insert(:user)
327 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
328 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
329 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
332 test "favoriting a status twice returns ok, but without the like activity" do
334 other_user = insert(:user)
336 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
337 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
338 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
342 describe "pinned statuses" do
344 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
347 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
349 [user: user, activity: activity]
352 test "pin status", %{user: user, activity: activity} do
353 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
356 user = refresh_record(user)
358 assert %User{pinned_activities: [^id]} = user
361 test "pin poll", %{user: user} do
363 CommonAPI.post(user, %{
364 "status" => "How is fediverse today?",
365 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
368 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
371 user = refresh_record(user)
373 assert %User{pinned_activities: [^id]} = user
376 test "unlisted statuses can be pinned", %{user: user} do
377 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
378 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
381 test "only self-authored can be pinned", %{activity: activity} do
384 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
387 test "max pinned statuses", %{user: user, activity: activity_one} do
388 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
390 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
392 user = refresh_record(user)
394 assert {:error, "You have already pinned the maximum number of statuses"} =
395 CommonAPI.pin(activity_two.id, user)
398 test "unpin status", %{user: user, activity: activity} do
399 {:ok, activity} = CommonAPI.pin(activity.id, user)
401 user = refresh_record(user)
403 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
405 user = refresh_record(user)
407 assert %User{pinned_activities: []} = user
410 test "should unpin when deleting a status", %{user: user, activity: activity} do
411 {:ok, activity} = CommonAPI.pin(activity.id, user)
413 user = refresh_record(user)
415 assert {:ok, _} = CommonAPI.delete(activity.id, user)
417 user = refresh_record(user)
419 assert %User{pinned_activities: []} = user
423 describe "mute tests" do
427 activity = insert(:note_activity)
429 [user: user, activity: activity]
432 test "add mute", %{user: user, activity: activity} do
433 {:ok, _} = CommonAPI.add_mute(user, activity)
434 assert CommonAPI.thread_muted?(user, activity)
437 test "remove mute", %{user: user, activity: activity} do
438 CommonAPI.add_mute(user, activity)
439 {:ok, _} = CommonAPI.remove_mute(user, activity)
440 refute CommonAPI.thread_muted?(user, activity)
443 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
444 CommonAPI.add_mute(user, activity)
445 {:error, _} = CommonAPI.add_mute(user, activity)
449 describe "reports" do
450 test "creates a report" do
451 reporter = insert(:user)
452 target_user = insert(:user)
454 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
456 reporter_ap_id = reporter.ap_id
457 target_ap_id = target_user.ap_id
458 activity_ap_id = activity.data["id"]
462 "account_id" => target_user.id,
463 "comment" => comment,
464 "status_ids" => [activity.id]
469 "id" => activity_ap_id,
470 "content" => "foobar",
471 "published" => activity.object.data["published"],
472 "actor" => AccountView.render("show.json", %{user: target_user})
475 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
478 actor: ^reporter_ap_id,
481 "content" => ^comment,
482 "object" => [^target_ap_id, ^note_obj],
488 test "updates report state" do
489 [reporter, target_user] = insert_pair(:user)
490 activity = insert(:note_activity, user: target_user)
492 {:ok, %Activity{id: report_id}} =
493 CommonAPI.report(reporter, %{
494 "account_id" => target_user.id,
495 "comment" => "I feel offended",
496 "status_ids" => [activity.id]
499 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
501 assert report.data["state"] == "resolved"
503 [reported_user, activity_id] = report.data["object"]
505 assert reported_user == target_user.ap_id
506 assert activity_id == activity.data["id"]
509 test "does not update report state when state is unsupported" do
510 [reporter, target_user] = insert_pair(:user)
511 activity = insert(:note_activity, user: target_user)
513 {:ok, %Activity{id: report_id}} =
514 CommonAPI.report(reporter, %{
515 "account_id" => target_user.id,
516 "comment" => "I feel offended",
517 "status_ids" => [activity.id]
520 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
523 test "updates state of multiple reports" do
524 [reporter, target_user] = insert_pair(:user)
525 activity = insert(:note_activity, user: target_user)
527 {:ok, %Activity{id: first_report_id}} =
528 CommonAPI.report(reporter, %{
529 "account_id" => target_user.id,
530 "comment" => "I feel offended",
531 "status_ids" => [activity.id]
534 {:ok, %Activity{id: second_report_id}} =
535 CommonAPI.report(reporter, %{
536 "account_id" => target_user.id,
537 "comment" => "I feel very offended!",
538 "status_ids" => [activity.id]
542 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
544 first_report = Activity.get_by_id(first_report_id)
545 second_report = Activity.get_by_id(second_report_id)
547 assert report_ids -- [first_report_id, second_report_id] == []
548 assert first_report.data["state"] == "resolved"
549 assert second_report.data["state"] == "resolved"
553 describe "reblog muting" do
555 muter = insert(:user)
557 muted = insert(:user)
559 [muter: muter, muted: muted]
562 test "add a reblog mute", %{muter: muter, muted: muted} do
563 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
565 assert User.showing_reblogs?(muter, muted) == false
568 test "remove a reblog mute", %{muter: muter, muted: muted} do
569 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
570 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
572 assert User.showing_reblogs?(muter, muted) == true
576 describe "unfollow/2" do
577 test "also unsubscribes a user" do
578 [follower, followed] = insert_pair(:user)
579 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
580 {:ok, _subscription} = User.subscribe(follower, followed)
582 assert User.subscribed_to?(follower, followed)
584 {:ok, follower} = CommonAPI.unfollow(follower, followed)
586 refute User.subscribed_to?(follower, followed)
589 test "cancels a pending follow for a local user" do
590 follower = insert(:user)
591 followed = insert(:user, locked: true)
593 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
594 CommonAPI.follow(follower, followed)
596 assert User.get_follow_state(follower, followed) == :follow_pending
597 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
598 assert User.get_follow_state(follower, followed) == nil
600 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
601 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
606 "object" => %{"type" => "Follow", "state" => "cancelled"}
608 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
611 test "cancels a pending follow for a remote user" do
612 follower = insert(:user)
613 followed = insert(:user, locked: true, local: false, ap_enabled: true)
615 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
616 CommonAPI.follow(follower, followed)
618 assert User.get_follow_state(follower, followed) == :follow_pending
619 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
620 assert User.get_follow_state(follower, followed) == nil
622 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
623 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
628 "object" => %{"type" => "Follow", "state" => "cancelled"}
630 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
634 describe "accept_follow_request/2" do
635 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
636 user = insert(:user, locked: true)
637 follower = insert(:user)
638 follower_two = insert(:user)
640 {:ok, follow_activity} = ActivityPub.follow(follower, user)
641 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
642 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
644 assert follow_activity.data["state"] == "pending"
645 assert follow_activity_two.data["state"] == "pending"
646 assert follow_activity_three.data["state"] == "pending"
648 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
650 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
651 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
652 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
655 test "after rejection, it sets all existing pending follow request states to 'reject'" do
656 user = insert(:user, locked: true)
657 follower = insert(:user)
658 follower_two = insert(:user)
660 {:ok, follow_activity} = ActivityPub.follow(follower, user)
661 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
662 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
664 assert follow_activity.data["state"] == "pending"
665 assert follow_activity_two.data["state"] == "pending"
666 assert follow_activity_three.data["state"] == "pending"
668 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
670 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
671 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
672 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
677 test "does not allow to vote twice" do
679 other_user = insert(:user)
682 CommonAPI.post(user, %{
683 "status" => "Am I cute?",
684 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
687 object = Object.normalize(activity)
689 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
691 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
695 describe "listen/2" do
696 test "returns a valid activity" do
700 CommonAPI.listen(user, %{
701 "title" => "lain radio episode 1",
702 "album" => "lain radio",
707 object = Object.normalize(activity)
709 assert object.data["title"] == "lain radio episode 1"
711 assert Visibility.get_visibility(activity) == "public"
714 test "respects visibility=private" do
718 CommonAPI.listen(user, %{
719 "title" => "lain radio episode 1",
720 "album" => "lain radio",
723 "visibility" => "private"
726 object = Object.normalize(activity)
728 assert object.data["title"] == "lain radio episode 1"
730 assert Visibility.get_visibility(activity) == "private"