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 "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
26 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
28 [participation] = Participation.for_user(user)
31 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
33 assert Visibility.is_direct?(convo_reply)
35 assert activity.data["context"] == convo_reply.data["context"]
38 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
40 jafnhar = insert(:user)
44 CommonAPI.post(har, %{
45 "status" => "@#{jafnhar.nickname} hey",
46 "visibility" => "direct"
49 assert har.ap_id in activity.recipients
50 assert jafnhar.ap_id in activity.recipients
52 [participation] = Participation.for_user(har)
55 CommonAPI.post(har, %{
56 "status" => "I don't really like @#{tridi.nickname}",
57 "visibility" => "direct",
58 "in_reply_to_status_id" => activity.id,
59 "in_reply_to_conversation_id" => participation.id
62 assert har.ap_id in activity.recipients
63 assert jafnhar.ap_id in activity.recipients
64 refute tridi.ap_id in activity.recipients
67 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
69 jafnhar = insert(:user)
72 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
75 CommonAPI.post(har, %{
76 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
77 "visibility" => "direct"
80 refute tridi.ap_id in activity.recipients
81 assert jafnhar.ap_id in activity.recipients
84 test "it de-duplicates tags" do
86 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
88 object = Object.normalize(activity)
90 assert object.data["tag"] == ["2hu"]
93 test "it adds emoji in the object" do
95 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
97 assert Object.normalize(activity).data["emoji"]["firefox"]
100 describe "posting" do
101 test "it supports explicit addressing" do
103 user_two = insert(:user)
104 user_three = insert(:user)
105 user_four = insert(:user)
108 CommonAPI.post(user, %{
110 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
111 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
114 assert user.ap_id in activity.recipients
115 assert user_two.ap_id in activity.recipients
116 assert user_four.ap_id in activity.recipients
117 refute user_three.ap_id in activity.recipients
120 test "it filters out obviously bad tags when accepting a post as HTML" do
123 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
126 CommonAPI.post(user, %{
128 "content_type" => "text/html"
131 object = Object.normalize(activity)
133 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
136 test "it filters out obviously bad tags when accepting a post as Markdown" do
139 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
142 CommonAPI.post(user, %{
144 "content_type" => "text/markdown"
147 object = Object.normalize(activity)
149 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
152 test "it does not allow replies to direct messages that are not direct messages themselves" do
155 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
158 CommonAPI.post(user, %{
159 "status" => "suya..",
160 "visibility" => "direct",
161 "in_reply_to_status_id" => activity.id
164 Enum.each(["public", "private", "unlisted"], fn visibility ->
165 assert {:error, "The message visibility must be direct"} =
166 CommonAPI.post(user, %{
167 "status" => "suya..",
168 "visibility" => visibility,
169 "in_reply_to_status_id" => activity.id
174 test "it allows to address a list" do
176 {:ok, list} = Pleroma.List.create("foo", user)
179 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
181 assert activity.data["bcc"] == [list.ap_id]
182 assert activity.recipients == [list.ap_id, user.ap_id]
183 assert activity.data["listMessage"] == list.ap_id
186 test "it returns error when status is empty and no attachments" do
189 assert {:error, "Cannot post an empty status without attachments"} =
190 CommonAPI.post(user, %{"status" => ""})
193 test "it validates character limits are correctly enforced" do
194 Pleroma.Config.put([:instance, :limit], 5)
198 assert {:error, "The status is over the character limit"} =
199 CommonAPI.post(user, %{"status" => "foobar"})
201 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
204 test "it can handle activities that expire" do
208 NaiveDateTime.utc_now()
209 |> NaiveDateTime.truncate(:second)
210 |> NaiveDateTime.add(1_000_000, :second)
212 assert {:ok, activity} =
213 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
215 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
216 assert expiration.scheduled_at == expires_at
220 describe "reactions" do
221 test "reacting to a status with an emoji" do
223 other_user = insert(:user)
225 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
227 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
229 assert reaction.data["actor"] == user.ap_id
230 assert reaction.data["content"] == "👍"
232 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
234 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
237 test "unreacting to a status with an emoji" do
239 other_user = insert(:user)
241 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
242 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
244 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
246 assert unreaction.data["type"] == "Undo"
247 assert unreaction.data["object"] == reaction.data["id"]
250 test "repeating a status" do
252 other_user = insert(:user)
254 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
256 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
259 test "repeating a status privately" do
261 other_user = insert(:user)
263 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
265 {:ok, %Activity{} = announce_activity, _} =
266 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
268 assert Visibility.is_private?(announce_activity)
271 test "favoriting a status" do
273 other_user = insert(:user)
275 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
277 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
278 assert data["type"] == "Like"
279 assert data["actor"] == user.ap_id
280 assert data["object"] == post_activity.data["object"]
283 test "retweeting a status twice returns the status" do
285 other_user = insert(:user)
287 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
288 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
289 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
292 test "favoriting a status twice returns ok, but without the like activity" do
294 other_user = insert(:user)
296 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
297 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
298 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
302 describe "pinned statuses" do
304 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
307 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
309 [user: user, activity: activity]
312 test "pin status", %{user: user, activity: activity} do
313 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
316 user = refresh_record(user)
318 assert %User{pinned_activities: [^id]} = user
321 test "pin poll", %{user: user} do
323 CommonAPI.post(user, %{
324 "status" => "How is fediverse today?",
325 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
328 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
331 user = refresh_record(user)
333 assert %User{pinned_activities: [^id]} = user
336 test "unlisted statuses can be pinned", %{user: user} do
337 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
338 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
341 test "only self-authored can be pinned", %{activity: activity} do
344 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
347 test "max pinned statuses", %{user: user, activity: activity_one} do
348 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
350 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
352 user = refresh_record(user)
354 assert {:error, "You have already pinned the maximum number of statuses"} =
355 CommonAPI.pin(activity_two.id, user)
358 test "unpin status", %{user: user, activity: activity} do
359 {:ok, activity} = CommonAPI.pin(activity.id, user)
361 user = refresh_record(user)
363 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
365 user = refresh_record(user)
367 assert %User{pinned_activities: []} = user
370 test "should unpin when deleting a status", %{user: user, activity: activity} do
371 {:ok, activity} = CommonAPI.pin(activity.id, user)
373 user = refresh_record(user)
375 assert {:ok, _} = CommonAPI.delete(activity.id, user)
377 user = refresh_record(user)
379 assert %User{pinned_activities: []} = user
383 describe "mute tests" do
387 activity = insert(:note_activity)
389 [user: user, activity: activity]
392 test "add mute", %{user: user, activity: activity} do
393 {:ok, _} = CommonAPI.add_mute(user, activity)
394 assert CommonAPI.thread_muted?(user, activity)
397 test "remove mute", %{user: user, activity: activity} do
398 CommonAPI.add_mute(user, activity)
399 {:ok, _} = CommonAPI.remove_mute(user, activity)
400 refute CommonAPI.thread_muted?(user, activity)
403 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
404 CommonAPI.add_mute(user, activity)
405 {:error, _} = CommonAPI.add_mute(user, activity)
409 describe "reports" do
410 test "creates a report" do
411 reporter = insert(:user)
412 target_user = insert(:user)
414 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
416 reporter_ap_id = reporter.ap_id
417 target_ap_id = target_user.ap_id
418 activity_ap_id = activity.data["id"]
422 "account_id" => target_user.id,
423 "comment" => comment,
424 "status_ids" => [activity.id]
429 "id" => activity_ap_id,
430 "content" => "foobar",
431 "published" => activity.object.data["published"],
432 "actor" => AccountView.render("show.json", %{user: target_user})
435 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
438 actor: ^reporter_ap_id,
441 "content" => ^comment,
442 "object" => [^target_ap_id, ^note_obj],
448 test "updates report state" do
449 [reporter, target_user] = insert_pair(:user)
450 activity = insert(:note_activity, user: target_user)
452 {:ok, %Activity{id: report_id}} =
453 CommonAPI.report(reporter, %{
454 "account_id" => target_user.id,
455 "comment" => "I feel offended",
456 "status_ids" => [activity.id]
459 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
461 assert report.data["state"] == "resolved"
463 [reported_user, activity_id] = report.data["object"]
465 assert reported_user == target_user.ap_id
466 assert activity_id == activity.data["id"]
469 test "does not update report state when state is unsupported" do
470 [reporter, target_user] = insert_pair(:user)
471 activity = insert(:note_activity, user: target_user)
473 {:ok, %Activity{id: report_id}} =
474 CommonAPI.report(reporter, %{
475 "account_id" => target_user.id,
476 "comment" => "I feel offended",
477 "status_ids" => [activity.id]
480 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
483 test "updates state of multiple reports" do
484 [reporter, target_user] = insert_pair(:user)
485 activity = insert(:note_activity, user: target_user)
487 {:ok, %Activity{id: first_report_id}} =
488 CommonAPI.report(reporter, %{
489 "account_id" => target_user.id,
490 "comment" => "I feel offended",
491 "status_ids" => [activity.id]
494 {:ok, %Activity{id: second_report_id}} =
495 CommonAPI.report(reporter, %{
496 "account_id" => target_user.id,
497 "comment" => "I feel very offended!",
498 "status_ids" => [activity.id]
502 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
504 first_report = Activity.get_by_id(first_report_id)
505 second_report = Activity.get_by_id(second_report_id)
507 assert report_ids -- [first_report_id, second_report_id] == []
508 assert first_report.data["state"] == "resolved"
509 assert second_report.data["state"] == "resolved"
513 describe "reblog muting" do
515 muter = insert(:user)
517 muted = insert(:user)
519 [muter: muter, muted: muted]
522 test "add a reblog mute", %{muter: muter, muted: muted} do
523 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
525 assert User.showing_reblogs?(muter, muted) == false
528 test "remove a reblog mute", %{muter: muter, muted: muted} do
529 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
530 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
532 assert User.showing_reblogs?(muter, muted) == true
536 describe "unfollow/2" do
537 test "also unsubscribes a user" do
538 [follower, followed] = insert_pair(:user)
539 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
540 {:ok, _subscription} = User.subscribe(follower, followed)
542 assert User.subscribed_to?(follower, followed)
544 {:ok, follower} = CommonAPI.unfollow(follower, followed)
546 refute User.subscribed_to?(follower, followed)
549 test "cancels a pending follow for a local user" do
550 follower = insert(:user)
551 followed = insert(:user, locked: true)
553 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
554 CommonAPI.follow(follower, followed)
556 assert User.get_follow_state(follower, followed) == :follow_pending
557 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
558 assert User.get_follow_state(follower, followed) == nil
560 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
561 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
566 "object" => %{"type" => "Follow", "state" => "cancelled"}
568 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
571 test "cancels a pending follow for a remote user" do
572 follower = insert(:user)
573 followed = insert(:user, locked: true, local: false, ap_enabled: true)
575 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
576 CommonAPI.follow(follower, followed)
578 assert User.get_follow_state(follower, followed) == :follow_pending
579 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
580 assert User.get_follow_state(follower, followed) == nil
582 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
583 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
588 "object" => %{"type" => "Follow", "state" => "cancelled"}
590 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
594 describe "accept_follow_request/2" do
595 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
596 user = insert(:user, locked: true)
597 follower = insert(:user)
598 follower_two = insert(:user)
600 {:ok, follow_activity} = ActivityPub.follow(follower, user)
601 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
602 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
604 assert follow_activity.data["state"] == "pending"
605 assert follow_activity_two.data["state"] == "pending"
606 assert follow_activity_three.data["state"] == "pending"
608 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
610 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
611 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
612 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
615 test "after rejection, it sets all existing pending follow request states to 'reject'" do
616 user = insert(:user, locked: true)
617 follower = insert(:user)
618 follower_two = insert(:user)
620 {:ok, follow_activity} = ActivityPub.follow(follower, user)
621 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
622 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
624 assert follow_activity.data["state"] == "pending"
625 assert follow_activity_two.data["state"] == "pending"
626 assert follow_activity_three.data["state"] == "pending"
628 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
630 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
631 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
632 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
637 test "does not allow to vote twice" do
639 other_user = insert(:user)
642 CommonAPI.post(user, %{
643 "status" => "Am I cute?",
644 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
647 object = Object.normalize(activity)
649 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
651 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
655 describe "listen/2" do
656 test "returns a valid activity" do
660 CommonAPI.listen(user, %{
661 "title" => "lain radio episode 1",
662 "album" => "lain radio",
667 object = Object.normalize(activity)
669 assert object.data["title"] == "lain radio episode 1"
671 assert Visibility.get_visibility(activity) == "public"
674 test "respects visibility=private" do
678 CommonAPI.listen(user, %{
679 "title" => "lain radio episode 1",
680 "album" => "lain radio",
683 "visibility" => "private"
686 object = Object.normalize(activity)
688 assert object.data["title"] == "lain radio episode 1"
690 assert Visibility.get_visibility(activity) == "private"