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 describe "posting" do
129 test "it supports explicit addressing" do
131 user_two = insert(:user)
132 user_three = insert(:user)
133 user_four = insert(:user)
136 CommonAPI.post(user, %{
138 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
139 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
142 assert user.ap_id in activity.recipients
143 assert user_two.ap_id in activity.recipients
144 assert user_four.ap_id in activity.recipients
145 refute user_three.ap_id in activity.recipients
148 test "it filters out obviously bad tags when accepting a post as HTML" do
151 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
154 CommonAPI.post(user, %{
156 "content_type" => "text/html"
159 object = Object.normalize(activity)
161 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
164 test "it filters out obviously bad tags when accepting a post as Markdown" do
167 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
170 CommonAPI.post(user, %{
172 "content_type" => "text/markdown"
175 object = Object.normalize(activity)
177 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
180 test "it does not allow replies to direct messages that are not direct messages themselves" do
183 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
186 CommonAPI.post(user, %{
187 "status" => "suya..",
188 "visibility" => "direct",
189 "in_reply_to_status_id" => activity.id
192 Enum.each(["public", "private", "unlisted"], fn visibility ->
193 assert {:error, "The message visibility must be direct"} =
194 CommonAPI.post(user, %{
195 "status" => "suya..",
196 "visibility" => visibility,
197 "in_reply_to_status_id" => activity.id
202 test "it allows to address a list" do
204 {:ok, list} = Pleroma.List.create("foo", user)
207 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
209 assert activity.data["bcc"] == [list.ap_id]
210 assert activity.recipients == [list.ap_id, user.ap_id]
211 assert activity.data["listMessage"] == list.ap_id
214 test "it returns error when status is empty and no attachments" do
217 assert {:error, "Cannot post an empty status without attachments"} =
218 CommonAPI.post(user, %{"status" => ""})
221 test "it validates character limits are correctly enforced" do
222 Pleroma.Config.put([:instance, :limit], 5)
226 assert {:error, "The status is over the character limit"} =
227 CommonAPI.post(user, %{"status" => "foobar"})
229 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
232 test "it can handle activities that expire" do
236 NaiveDateTime.utc_now()
237 |> NaiveDateTime.truncate(:second)
238 |> NaiveDateTime.add(1_000_000, :second)
240 assert {:ok, activity} =
241 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
243 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
244 assert expiration.scheduled_at == expires_at
248 describe "reactions" do
249 test "reacting to a status with an emoji" do
251 other_user = insert(:user)
253 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
255 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
257 assert reaction.data["actor"] == user.ap_id
258 assert reaction.data["content"] == "👍"
260 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
262 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
265 test "unreacting to a status with an emoji" do
267 other_user = insert(:user)
269 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
270 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
272 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
274 assert unreaction.data["type"] == "Undo"
275 assert unreaction.data["object"] == reaction.data["id"]
278 test "repeating a status" do
280 other_user = insert(:user)
282 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
284 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
287 test "repeating a status privately" do
289 other_user = insert(:user)
291 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
293 {:ok, %Activity{} = announce_activity, _} =
294 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
296 assert Visibility.is_private?(announce_activity)
299 test "favoriting a status" do
301 other_user = insert(:user)
303 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
305 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
306 assert data["type"] == "Like"
307 assert data["actor"] == user.ap_id
308 assert data["object"] == post_activity.data["object"]
311 test "retweeting a status twice returns the status" do
313 other_user = insert(:user)
315 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
316 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
317 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
320 test "favoriting a status twice returns ok, but without the like activity" do
322 other_user = insert(:user)
324 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
325 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
326 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
330 describe "pinned statuses" do
332 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
335 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
337 [user: user, activity: activity]
340 test "pin status", %{user: user, activity: activity} do
341 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
344 user = refresh_record(user)
346 assert %User{pinned_activities: [^id]} = user
349 test "pin poll", %{user: user} do
351 CommonAPI.post(user, %{
352 "status" => "How is fediverse today?",
353 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
356 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
359 user = refresh_record(user)
361 assert %User{pinned_activities: [^id]} = user
364 test "unlisted statuses can be pinned", %{user: user} do
365 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
366 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
369 test "only self-authored can be pinned", %{activity: activity} do
372 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
375 test "max pinned statuses", %{user: user, activity: activity_one} do
376 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
378 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
380 user = refresh_record(user)
382 assert {:error, "You have already pinned the maximum number of statuses"} =
383 CommonAPI.pin(activity_two.id, user)
386 test "unpin status", %{user: user, activity: activity} do
387 {:ok, activity} = CommonAPI.pin(activity.id, user)
389 user = refresh_record(user)
391 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
393 user = refresh_record(user)
395 assert %User{pinned_activities: []} = user
398 test "should unpin when deleting a status", %{user: user, activity: activity} do
399 {:ok, activity} = CommonAPI.pin(activity.id, user)
401 user = refresh_record(user)
403 assert {:ok, _} = CommonAPI.delete(activity.id, user)
405 user = refresh_record(user)
407 assert %User{pinned_activities: []} = user
411 describe "mute tests" do
415 activity = insert(:note_activity)
417 [user: user, activity: activity]
420 test "add mute", %{user: user, activity: activity} do
421 {:ok, _} = CommonAPI.add_mute(user, activity)
422 assert CommonAPI.thread_muted?(user, activity)
425 test "remove mute", %{user: user, activity: activity} do
426 CommonAPI.add_mute(user, activity)
427 {:ok, _} = CommonAPI.remove_mute(user, activity)
428 refute CommonAPI.thread_muted?(user, activity)
431 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
432 CommonAPI.add_mute(user, activity)
433 {:error, _} = CommonAPI.add_mute(user, activity)
437 describe "reports" do
438 test "creates a report" do
439 reporter = insert(:user)
440 target_user = insert(:user)
442 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
444 reporter_ap_id = reporter.ap_id
445 target_ap_id = target_user.ap_id
446 activity_ap_id = activity.data["id"]
450 "account_id" => target_user.id,
451 "comment" => comment,
452 "status_ids" => [activity.id]
457 "id" => activity_ap_id,
458 "content" => "foobar",
459 "published" => activity.object.data["published"],
460 "actor" => AccountView.render("show.json", %{user: target_user})
463 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
466 actor: ^reporter_ap_id,
469 "content" => ^comment,
470 "object" => [^target_ap_id, ^note_obj],
476 test "updates report state" do
477 [reporter, target_user] = insert_pair(:user)
478 activity = insert(:note_activity, user: target_user)
480 {:ok, %Activity{id: report_id}} =
481 CommonAPI.report(reporter, %{
482 "account_id" => target_user.id,
483 "comment" => "I feel offended",
484 "status_ids" => [activity.id]
487 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
489 assert report.data["state"] == "resolved"
491 [reported_user, activity_id] = report.data["object"]
493 assert reported_user == target_user.ap_id
494 assert activity_id == activity.data["id"]
497 test "does not update report state when state is unsupported" do
498 [reporter, target_user] = insert_pair(:user)
499 activity = insert(:note_activity, user: target_user)
501 {:ok, %Activity{id: report_id}} =
502 CommonAPI.report(reporter, %{
503 "account_id" => target_user.id,
504 "comment" => "I feel offended",
505 "status_ids" => [activity.id]
508 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
511 test "updates state of multiple reports" do
512 [reporter, target_user] = insert_pair(:user)
513 activity = insert(:note_activity, user: target_user)
515 {:ok, %Activity{id: first_report_id}} =
516 CommonAPI.report(reporter, %{
517 "account_id" => target_user.id,
518 "comment" => "I feel offended",
519 "status_ids" => [activity.id]
522 {:ok, %Activity{id: second_report_id}} =
523 CommonAPI.report(reporter, %{
524 "account_id" => target_user.id,
525 "comment" => "I feel very offended!",
526 "status_ids" => [activity.id]
530 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
532 first_report = Activity.get_by_id(first_report_id)
533 second_report = Activity.get_by_id(second_report_id)
535 assert report_ids -- [first_report_id, second_report_id] == []
536 assert first_report.data["state"] == "resolved"
537 assert second_report.data["state"] == "resolved"
541 describe "reblog muting" do
543 muter = insert(:user)
545 muted = insert(:user)
547 [muter: muter, muted: muted]
550 test "add a reblog mute", %{muter: muter, muted: muted} do
551 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
553 assert User.showing_reblogs?(muter, muted) == false
556 test "remove a reblog mute", %{muter: muter, muted: muted} do
557 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
558 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
560 assert User.showing_reblogs?(muter, muted) == true
564 describe "unfollow/2" do
565 test "also unsubscribes a user" do
566 [follower, followed] = insert_pair(:user)
567 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
568 {:ok, _subscription} = User.subscribe(follower, followed)
570 assert User.subscribed_to?(follower, followed)
572 {:ok, follower} = CommonAPI.unfollow(follower, followed)
574 refute User.subscribed_to?(follower, followed)
577 test "cancels a pending follow for a local user" do
578 follower = insert(:user)
579 followed = insert(:user, locked: true)
581 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
582 CommonAPI.follow(follower, followed)
584 assert User.get_follow_state(follower, followed) == :follow_pending
585 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
586 assert User.get_follow_state(follower, followed) == nil
588 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
589 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
594 "object" => %{"type" => "Follow", "state" => "cancelled"}
596 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
599 test "cancels a pending follow for a remote user" do
600 follower = insert(:user)
601 followed = insert(:user, locked: true, local: false, ap_enabled: 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)
622 describe "accept_follow_request/2" do
623 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
624 user = insert(:user, locked: true)
625 follower = insert(:user)
626 follower_two = insert(:user)
628 {:ok, follow_activity} = ActivityPub.follow(follower, user)
629 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
630 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
632 assert follow_activity.data["state"] == "pending"
633 assert follow_activity_two.data["state"] == "pending"
634 assert follow_activity_three.data["state"] == "pending"
636 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
638 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
639 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
640 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
643 test "after rejection, it sets all existing pending follow request states to 'reject'" do
644 user = insert(:user, locked: true)
645 follower = insert(:user)
646 follower_two = insert(:user)
648 {:ok, follow_activity} = ActivityPub.follow(follower, user)
649 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
650 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
652 assert follow_activity.data["state"] == "pending"
653 assert follow_activity_two.data["state"] == "pending"
654 assert follow_activity_three.data["state"] == "pending"
656 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
658 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
659 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
660 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
665 test "does not allow to vote twice" do
667 other_user = insert(:user)
670 CommonAPI.post(user, %{
671 "status" => "Am I cute?",
672 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
675 object = Object.normalize(activity)
677 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
679 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
683 describe "listen/2" do
684 test "returns a valid activity" do
688 CommonAPI.listen(user, %{
689 "title" => "lain radio episode 1",
690 "album" => "lain radio",
695 object = Object.normalize(activity)
697 assert object.data["title"] == "lain radio episode 1"
699 assert Visibility.get_visibility(activity) == "public"
702 test "respects visibility=private" do
706 CommonAPI.listen(user, %{
707 "title" => "lain radio episode 1",
708 "album" => "lain radio",
711 "visibility" => "private"
714 object = Object.normalize(activity)
716 assert object.data["title"] == "lain radio episode 1"
718 assert Visibility.get_visibility(activity) == "private"