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
8 alias Pleroma.Conversation.Participation
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Visibility
13 alias Pleroma.Web.AdminAPI.AccountView
14 alias Pleroma.Web.CommonAPI
16 import Pleroma.Factory
18 require Pleroma.Constants
20 setup do: clear_config([:instance, :safe_dm_mentions])
21 setup do: clear_config([:instance, :limit])
22 setup do: clear_config([:instance, :max_pinned_statuses])
24 test "favoriting race condition" do
26 users_serial = insert_list(10, :user)
27 users = insert_list(10, :user)
29 {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
32 |> Enum.map(fn user ->
33 CommonAPI.favorite(user, activity.id)
36 object = Object.get_by_ap_id(activity.data["object"])
37 assert object.data["like_count"] == 10
40 |> Enum.map(fn user ->
42 CommonAPI.favorite(user, activity.id)
45 |> Enum.map(&Task.await/1)
47 object = Object.get_by_ap_id(activity.data["object"])
48 assert object.data["like_count"] == 20
51 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
53 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
55 [participation] = Participation.for_user(user)
58 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
60 assert Visibility.is_direct?(convo_reply)
62 assert activity.data["context"] == convo_reply.data["context"]
65 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
67 jafnhar = insert(:user)
71 CommonAPI.post(har, %{
72 "status" => "@#{jafnhar.nickname} hey",
73 "visibility" => "direct"
76 assert har.ap_id in activity.recipients
77 assert jafnhar.ap_id in activity.recipients
79 [participation] = Participation.for_user(har)
82 CommonAPI.post(har, %{
83 "status" => "I don't really like @#{tridi.nickname}",
84 "visibility" => "direct",
85 "in_reply_to_status_id" => activity.id,
86 "in_reply_to_conversation_id" => participation.id
89 assert har.ap_id in activity.recipients
90 assert jafnhar.ap_id in activity.recipients
91 refute tridi.ap_id in activity.recipients
94 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
96 jafnhar = insert(:user)
99 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
102 CommonAPI.post(har, %{
103 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
104 "visibility" => "direct"
107 refute tridi.ap_id in activity.recipients
108 assert jafnhar.ap_id in activity.recipients
111 test "it de-duplicates tags" do
113 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
115 object = Object.normalize(activity)
117 assert object.data["tag"] == ["2hu"]
120 test "it adds emoji in the object" do
122 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
124 assert Object.normalize(activity).data["emoji"]["firefox"]
127 describe "posting" do
128 test "it supports explicit addressing" do
130 user_two = insert(:user)
131 user_three = insert(:user)
132 user_four = insert(:user)
135 CommonAPI.post(user, %{
137 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
138 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
141 assert user.ap_id in activity.recipients
142 assert user_two.ap_id in activity.recipients
143 assert user_four.ap_id in activity.recipients
144 refute user_three.ap_id in activity.recipients
147 test "it filters out obviously bad tags when accepting a post as HTML" do
150 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
153 CommonAPI.post(user, %{
155 "content_type" => "text/html"
158 object = Object.normalize(activity)
160 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
163 test "it filters out obviously bad tags when accepting a post as Markdown" do
166 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
169 CommonAPI.post(user, %{
171 "content_type" => "text/markdown"
174 object = Object.normalize(activity)
176 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
179 test "it does not allow replies to direct messages that are not direct messages themselves" do
182 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
185 CommonAPI.post(user, %{
186 "status" => "suya..",
187 "visibility" => "direct",
188 "in_reply_to_status_id" => activity.id
191 Enum.each(["public", "private", "unlisted"], fn visibility ->
192 assert {:error, "The message visibility must be direct"} =
193 CommonAPI.post(user, %{
194 "status" => "suya..",
195 "visibility" => visibility,
196 "in_reply_to_status_id" => activity.id
201 test "it allows to address a list" do
203 {:ok, list} = Pleroma.List.create("foo", user)
206 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
208 assert activity.data["bcc"] == [list.ap_id]
209 assert activity.recipients == [list.ap_id, user.ap_id]
210 assert activity.data["listMessage"] == list.ap_id
213 test "it returns error when status is empty and no attachments" do
216 assert {:error, "Cannot post an empty status without attachments"} =
217 CommonAPI.post(user, %{"status" => ""})
220 test "it validates character limits are correctly enforced" do
221 Pleroma.Config.put([:instance, :limit], 5)
225 assert {:error, "The status is over the character limit"} =
226 CommonAPI.post(user, %{"status" => "foobar"})
228 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
231 test "it can handle activities that expire" do
235 NaiveDateTime.utc_now()
236 |> NaiveDateTime.truncate(:second)
237 |> NaiveDateTime.add(1_000_000, :second)
239 assert {:ok, activity} =
240 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
242 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
243 assert expiration.scheduled_at == expires_at
247 describe "reactions" do
248 test "reacting to a status with an emoji" do
250 other_user = insert(:user)
252 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
254 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
256 assert reaction.data["actor"] == user.ap_id
257 assert reaction.data["content"] == "👍"
259 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
261 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
264 test "unreacting to a status with an emoji" do
266 other_user = insert(:user)
268 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
269 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
271 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
273 assert unreaction.data["type"] == "Undo"
274 assert unreaction.data["object"] == reaction.data["id"]
277 test "repeating a status" do
279 other_user = insert(:user)
281 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
283 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
286 test "repeating a status privately" do
288 other_user = insert(:user)
290 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
292 {:ok, %Activity{} = announce_activity, _} =
293 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
295 assert Visibility.is_private?(announce_activity)
298 test "favoriting a status" do
300 other_user = insert(:user)
302 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
304 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
305 assert data["type"] == "Like"
306 assert data["actor"] == user.ap_id
307 assert data["object"] == post_activity.data["object"]
310 test "retweeting a status twice returns the status" do
312 other_user = insert(:user)
314 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
315 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
316 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
319 test "favoriting a status twice returns ok, but without the like activity" do
321 other_user = insert(:user)
323 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
324 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
325 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
329 describe "pinned statuses" do
331 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
334 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
336 [user: user, activity: activity]
339 test "pin status", %{user: user, activity: activity} do
340 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
343 user = refresh_record(user)
345 assert %User{pinned_activities: [^id]} = user
348 test "pin poll", %{user: user} do
350 CommonAPI.post(user, %{
351 "status" => "How is fediverse today?",
352 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
355 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
358 user = refresh_record(user)
360 assert %User{pinned_activities: [^id]} = user
363 test "unlisted statuses can be pinned", %{user: user} do
364 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
365 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
368 test "only self-authored can be pinned", %{activity: activity} do
371 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
374 test "max pinned statuses", %{user: user, activity: activity_one} do
375 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
377 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
379 user = refresh_record(user)
381 assert {:error, "You have already pinned the maximum number of statuses"} =
382 CommonAPI.pin(activity_two.id, user)
385 test "unpin status", %{user: user, activity: activity} do
386 {:ok, activity} = CommonAPI.pin(activity.id, user)
388 user = refresh_record(user)
390 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
392 user = refresh_record(user)
394 assert %User{pinned_activities: []} = user
397 test "should unpin when deleting a status", %{user: user, activity: activity} do
398 {:ok, activity} = CommonAPI.pin(activity.id, user)
400 user = refresh_record(user)
402 assert {:ok, _} = CommonAPI.delete(activity.id, user)
404 user = refresh_record(user)
406 assert %User{pinned_activities: []} = user
410 describe "mute tests" do
414 activity = insert(:note_activity)
416 [user: user, activity: activity]
419 test "add mute", %{user: user, activity: activity} do
420 {:ok, _} = CommonAPI.add_mute(user, activity)
421 assert CommonAPI.thread_muted?(user, activity)
424 test "remove mute", %{user: user, activity: activity} do
425 CommonAPI.add_mute(user, activity)
426 {:ok, _} = CommonAPI.remove_mute(user, activity)
427 refute CommonAPI.thread_muted?(user, activity)
430 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
431 CommonAPI.add_mute(user, activity)
432 {:error, _} = CommonAPI.add_mute(user, activity)
436 describe "reports" do
437 test "creates a report" do
438 reporter = insert(:user)
439 target_user = insert(:user)
441 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
443 reporter_ap_id = reporter.ap_id
444 target_ap_id = target_user.ap_id
445 activity_ap_id = activity.data["id"]
449 "account_id" => target_user.id,
450 "comment" => comment,
451 "status_ids" => [activity.id]
456 "id" => activity_ap_id,
457 "content" => "foobar",
458 "published" => activity.object.data["published"],
459 "actor" => AccountView.render("show.json", %{user: target_user})
462 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
465 actor: ^reporter_ap_id,
468 "content" => ^comment,
469 "object" => [^target_ap_id, ^note_obj],
475 test "updates report state" do
476 [reporter, target_user] = insert_pair(:user)
477 activity = insert(:note_activity, user: target_user)
479 {:ok, %Activity{id: report_id}} =
480 CommonAPI.report(reporter, %{
481 "account_id" => target_user.id,
482 "comment" => "I feel offended",
483 "status_ids" => [activity.id]
486 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
488 assert report.data["state"] == "resolved"
490 [reported_user, activity_id] = report.data["object"]
492 assert reported_user == target_user.ap_id
493 assert activity_id == activity.data["id"]
496 test "does not update report state when state is unsupported" do
497 [reporter, target_user] = insert_pair(:user)
498 activity = insert(:note_activity, user: target_user)
500 {:ok, %Activity{id: report_id}} =
501 CommonAPI.report(reporter, %{
502 "account_id" => target_user.id,
503 "comment" => "I feel offended",
504 "status_ids" => [activity.id]
507 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
510 test "updates state of multiple reports" do
511 [reporter, target_user] = insert_pair(:user)
512 activity = insert(:note_activity, user: target_user)
514 {:ok, %Activity{id: first_report_id}} =
515 CommonAPI.report(reporter, %{
516 "account_id" => target_user.id,
517 "comment" => "I feel offended",
518 "status_ids" => [activity.id]
521 {:ok, %Activity{id: second_report_id}} =
522 CommonAPI.report(reporter, %{
523 "account_id" => target_user.id,
524 "comment" => "I feel very offended!",
525 "status_ids" => [activity.id]
529 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
531 first_report = Activity.get_by_id(first_report_id)
532 second_report = Activity.get_by_id(second_report_id)
534 assert report_ids -- [first_report_id, second_report_id] == []
535 assert first_report.data["state"] == "resolved"
536 assert second_report.data["state"] == "resolved"
540 describe "reblog muting" do
542 muter = insert(:user)
544 muted = insert(:user)
546 [muter: muter, muted: muted]
549 test "add a reblog mute", %{muter: muter, muted: muted} do
550 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
552 assert User.showing_reblogs?(muter, muted) == false
555 test "remove a reblog mute", %{muter: muter, muted: muted} do
556 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
557 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
559 assert User.showing_reblogs?(muter, muted) == true
563 describe "unfollow/2" do
564 test "also unsubscribes a user" do
565 [follower, followed] = insert_pair(:user)
566 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
567 {:ok, _subscription} = User.subscribe(follower, followed)
569 assert User.subscribed_to?(follower, followed)
571 {:ok, follower} = CommonAPI.unfollow(follower, followed)
573 refute User.subscribed_to?(follower, followed)
576 test "cancels a pending follow for a local user" do
577 follower = insert(:user)
578 followed = insert(:user, locked: true)
580 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
581 CommonAPI.follow(follower, followed)
583 assert User.get_follow_state(follower, followed) == :follow_pending
584 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
585 assert User.get_follow_state(follower, followed) == nil
587 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
588 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
593 "object" => %{"type" => "Follow", "state" => "cancelled"}
595 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
598 test "cancels a pending follow for a remote user" do
599 follower = insert(:user)
600 followed = insert(:user, locked: true, local: false, ap_enabled: true)
602 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
603 CommonAPI.follow(follower, followed)
605 assert User.get_follow_state(follower, followed) == :follow_pending
606 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
607 assert User.get_follow_state(follower, followed) == nil
609 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
610 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
615 "object" => %{"type" => "Follow", "state" => "cancelled"}
617 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
621 describe "accept_follow_request/2" do
622 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
623 user = insert(:user, locked: true)
624 follower = insert(:user)
625 follower_two = insert(:user)
627 {:ok, follow_activity} = ActivityPub.follow(follower, user)
628 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
629 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
631 assert follow_activity.data["state"] == "pending"
632 assert follow_activity_two.data["state"] == "pending"
633 assert follow_activity_three.data["state"] == "pending"
635 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
637 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
638 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
639 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
642 test "after rejection, it sets all existing pending follow request states to 'reject'" do
643 user = insert(:user, locked: true)
644 follower = insert(:user)
645 follower_two = insert(:user)
647 {:ok, follow_activity} = ActivityPub.follow(follower, user)
648 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
649 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
651 assert follow_activity.data["state"] == "pending"
652 assert follow_activity_two.data["state"] == "pending"
653 assert follow_activity_three.data["state"] == "pending"
655 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
657 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
658 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
659 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
664 test "does not allow to vote twice" do
666 other_user = insert(:user)
669 CommonAPI.post(user, %{
670 "status" => "Am I cute?",
671 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
674 object = Object.normalize(activity)
676 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
678 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
682 describe "listen/2" do
683 test "returns a valid activity" do
687 CommonAPI.listen(user, %{
688 "title" => "lain radio episode 1",
689 "album" => "lain radio",
694 object = Object.normalize(activity)
696 assert object.data["title"] == "lain radio episode 1"
698 assert Visibility.get_visibility(activity) == "public"
701 test "respects visibility=private" do
705 CommonAPI.listen(user, %{
706 "title" => "lain radio episode 1",
707 "album" => "lain radio",
710 "visibility" => "private"
713 object = Object.normalize(activity)
715 assert object.data["title"] == "lain radio episode 1"
717 assert Visibility.get_visibility(activity) == "private"