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)
30 {:ok, activity} = CommonAPI.post_chat_message(author, recipient, "a test message")
32 assert activity.data["type"] == "Create"
34 object = Object.normalize(activity)
36 assert object.data["type"] == "ChatMessage"
37 assert object.data["to"] == [recipient.ap_id]
38 assert object.data["content"] == "a test message"
40 assert Chat.get(author.id, recipient.ap_id)
41 assert Chat.get(recipient.id, author.ap_id)
45 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
47 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
49 [participation] = Participation.for_user(user)
52 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
54 assert Visibility.is_direct?(convo_reply)
56 assert activity.data["context"] == convo_reply.data["context"]
59 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
61 jafnhar = insert(:user)
65 CommonAPI.post(har, %{
66 "status" => "@#{jafnhar.nickname} hey",
67 "visibility" => "direct"
70 assert har.ap_id in activity.recipients
71 assert jafnhar.ap_id in activity.recipients
73 [participation] = Participation.for_user(har)
76 CommonAPI.post(har, %{
77 "status" => "I don't really like @#{tridi.nickname}",
78 "visibility" => "direct",
79 "in_reply_to_status_id" => activity.id,
80 "in_reply_to_conversation_id" => participation.id
83 assert har.ap_id in activity.recipients
84 assert jafnhar.ap_id in activity.recipients
85 refute tridi.ap_id in activity.recipients
88 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
90 jafnhar = insert(:user)
93 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
96 CommonAPI.post(har, %{
97 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
98 "visibility" => "direct"
101 refute tridi.ap_id in activity.recipients
102 assert jafnhar.ap_id in activity.recipients
105 test "it de-duplicates tags" do
107 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
109 object = Object.normalize(activity)
111 assert object.data["tag"] == ["2hu"]
114 test "it adds emoji in the object" do
116 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
118 assert Object.normalize(activity).data["emoji"]["firefox"]
121 test "it adds emoji when updating profiles" do
122 user = insert(:user, %{name: ":firefox:"})
124 {:ok, activity} = CommonAPI.update(user)
125 user = User.get_cached_by_ap_id(user.ap_id)
126 [firefox] = user.source_data["tag"]
128 assert firefox["name"] == ":firefox:"
130 assert Pleroma.Constants.as_public() in activity.recipients
133 describe "posting" do
134 test "it supports explicit addressing" do
136 user_two = insert(:user)
137 user_three = insert(:user)
138 user_four = insert(:user)
141 CommonAPI.post(user, %{
143 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
144 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
147 assert user.ap_id in activity.recipients
148 assert user_two.ap_id in activity.recipients
149 assert user_four.ap_id in activity.recipients
150 refute user_three.ap_id in activity.recipients
153 test "it filters out obviously bad tags when accepting a post as HTML" do
156 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
159 CommonAPI.post(user, %{
161 "content_type" => "text/html"
164 object = Object.normalize(activity)
166 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
169 test "it filters out obviously bad tags when accepting a post as Markdown" do
172 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
175 CommonAPI.post(user, %{
177 "content_type" => "text/markdown"
180 object = Object.normalize(activity)
182 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
185 test "it does not allow replies to direct messages that are not direct messages themselves" do
188 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
191 CommonAPI.post(user, %{
192 "status" => "suya..",
193 "visibility" => "direct",
194 "in_reply_to_status_id" => activity.id
197 Enum.each(["public", "private", "unlisted"], fn visibility ->
198 assert {:error, "The message visibility must be direct"} =
199 CommonAPI.post(user, %{
200 "status" => "suya..",
201 "visibility" => visibility,
202 "in_reply_to_status_id" => activity.id
207 test "it allows to address a list" do
209 {:ok, list} = Pleroma.List.create("foo", user)
212 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
214 assert activity.data["bcc"] == [list.ap_id]
215 assert activity.recipients == [list.ap_id, user.ap_id]
216 assert activity.data["listMessage"] == list.ap_id
219 test "it returns error when status is empty and no attachments" do
222 assert {:error, "Cannot post an empty status without attachments"} =
223 CommonAPI.post(user, %{"status" => ""})
226 test "it validates character limits are correctly enforced" do
227 Pleroma.Config.put([:instance, :limit], 5)
231 assert {:error, "The status is over the character limit"} =
232 CommonAPI.post(user, %{"status" => "foobar"})
234 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
237 test "it can handle activities that expire" do
241 NaiveDateTime.utc_now()
242 |> NaiveDateTime.truncate(:second)
243 |> NaiveDateTime.add(1_000_000, :second)
245 assert {:ok, activity} =
246 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
248 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
249 assert expiration.scheduled_at == expires_at
253 describe "reactions" do
254 test "reacting to a status with an emoji" do
256 other_user = insert(:user)
258 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
260 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
262 assert reaction.data["actor"] == user.ap_id
263 assert reaction.data["content"] == "👍"
265 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
267 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
270 test "unreacting to a status with an emoji" do
272 other_user = insert(:user)
274 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
275 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
277 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
279 assert unreaction.data["type"] == "Undo"
280 assert unreaction.data["object"] == reaction.data["id"]
283 test "repeating a status" do
285 other_user = insert(:user)
287 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
289 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
292 test "repeating a status privately" do
294 other_user = insert(:user)
296 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
298 {:ok, %Activity{} = announce_activity, _} =
299 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
301 assert Visibility.is_private?(announce_activity)
304 test "favoriting a status" do
306 other_user = insert(:user)
308 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
310 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
311 assert data["type"] == "Like"
312 assert data["actor"] == user.ap_id
313 assert data["object"] == post_activity.data["object"]
316 test "retweeting a status twice returns the status" do
318 other_user = insert(:user)
320 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
321 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
322 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
325 test "favoriting a status twice returns ok, but without the like activity" do
327 other_user = insert(:user)
329 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
330 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
331 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
335 describe "pinned statuses" do
337 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
340 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
342 [user: user, activity: activity]
345 test "pin status", %{user: user, activity: activity} do
346 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
349 user = refresh_record(user)
351 assert %User{pinned_activities: [^id]} = user
354 test "pin poll", %{user: user} do
356 CommonAPI.post(user, %{
357 "status" => "How is fediverse today?",
358 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
361 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
364 user = refresh_record(user)
366 assert %User{pinned_activities: [^id]} = user
369 test "unlisted statuses can be pinned", %{user: user} do
370 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
371 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
374 test "only self-authored can be pinned", %{activity: activity} do
377 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
380 test "max pinned statuses", %{user: user, activity: activity_one} do
381 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
383 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
385 user = refresh_record(user)
387 assert {:error, "You have already pinned the maximum number of statuses"} =
388 CommonAPI.pin(activity_two.id, user)
391 test "unpin status", %{user: user, activity: activity} do
392 {:ok, activity} = CommonAPI.pin(activity.id, user)
394 user = refresh_record(user)
396 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
398 user = refresh_record(user)
400 assert %User{pinned_activities: []} = user
403 test "should unpin when deleting a status", %{user: user, activity: activity} do
404 {:ok, activity} = CommonAPI.pin(activity.id, user)
406 user = refresh_record(user)
408 assert {:ok, _} = CommonAPI.delete(activity.id, user)
410 user = refresh_record(user)
412 assert %User{pinned_activities: []} = user
416 describe "mute tests" do
420 activity = insert(:note_activity)
422 [user: user, activity: activity]
425 test "add mute", %{user: user, activity: activity} do
426 {:ok, _} = CommonAPI.add_mute(user, activity)
427 assert CommonAPI.thread_muted?(user, activity)
430 test "remove mute", %{user: user, activity: activity} do
431 CommonAPI.add_mute(user, activity)
432 {:ok, _} = CommonAPI.remove_mute(user, activity)
433 refute CommonAPI.thread_muted?(user, activity)
436 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
437 CommonAPI.add_mute(user, activity)
438 {:error, _} = CommonAPI.add_mute(user, activity)
442 describe "reports" do
443 test "creates a report" do
444 reporter = insert(:user)
445 target_user = insert(:user)
447 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
449 reporter_ap_id = reporter.ap_id
450 target_ap_id = target_user.ap_id
451 activity_ap_id = activity.data["id"]
455 "account_id" => target_user.id,
456 "comment" => comment,
457 "status_ids" => [activity.id]
462 "id" => activity_ap_id,
463 "content" => "foobar",
464 "published" => activity.object.data["published"],
465 "actor" => AccountView.render("show.json", %{user: target_user})
468 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
471 actor: ^reporter_ap_id,
474 "content" => ^comment,
475 "object" => [^target_ap_id, ^note_obj],
481 test "updates report state" do
482 [reporter, target_user] = insert_pair(:user)
483 activity = insert(:note_activity, user: target_user)
485 {:ok, %Activity{id: report_id}} =
486 CommonAPI.report(reporter, %{
487 "account_id" => target_user.id,
488 "comment" => "I feel offended",
489 "status_ids" => [activity.id]
492 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
494 assert report.data["state"] == "resolved"
496 [reported_user, activity_id] = report.data["object"]
498 assert reported_user == target_user.ap_id
499 assert activity_id == activity.data["id"]
502 test "does not update report state when state is unsupported" do
503 [reporter, target_user] = insert_pair(:user)
504 activity = insert(:note_activity, user: target_user)
506 {:ok, %Activity{id: report_id}} =
507 CommonAPI.report(reporter, %{
508 "account_id" => target_user.id,
509 "comment" => "I feel offended",
510 "status_ids" => [activity.id]
513 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
516 test "updates state of multiple reports" do
517 [reporter, target_user] = insert_pair(:user)
518 activity = insert(:note_activity, user: target_user)
520 {:ok, %Activity{id: first_report_id}} =
521 CommonAPI.report(reporter, %{
522 "account_id" => target_user.id,
523 "comment" => "I feel offended",
524 "status_ids" => [activity.id]
527 {:ok, %Activity{id: second_report_id}} =
528 CommonAPI.report(reporter, %{
529 "account_id" => target_user.id,
530 "comment" => "I feel very offended!",
531 "status_ids" => [activity.id]
535 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
537 first_report = Activity.get_by_id(first_report_id)
538 second_report = Activity.get_by_id(second_report_id)
540 assert report_ids -- [first_report_id, second_report_id] == []
541 assert first_report.data["state"] == "resolved"
542 assert second_report.data["state"] == "resolved"
546 describe "reblog muting" do
548 muter = insert(:user)
550 muted = insert(:user)
552 [muter: muter, muted: muted]
555 test "add a reblog mute", %{muter: muter, muted: muted} do
556 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
558 assert User.showing_reblogs?(muter, muted) == false
561 test "remove a reblog mute", %{muter: muter, muted: muted} do
562 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
563 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
565 assert User.showing_reblogs?(muter, muted) == true
569 describe "unfollow/2" do
570 test "also unsubscribes a user" do
571 [follower, followed] = insert_pair(:user)
572 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
573 {:ok, _subscription} = User.subscribe(follower, followed)
575 assert User.subscribed_to?(follower, followed)
577 {:ok, follower} = CommonAPI.unfollow(follower, followed)
579 refute User.subscribed_to?(follower, followed)
582 test "cancels a pending follow for a local user" do
583 follower = insert(:user)
584 followed = insert(:user, locked: true)
586 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
587 CommonAPI.follow(follower, followed)
589 assert User.get_follow_state(follower, followed) == "pending"
590 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
591 assert User.get_follow_state(follower, followed) == nil
593 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
594 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
599 "object" => %{"type" => "Follow", "state" => "cancelled"}
601 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
604 test "cancels a pending follow for a remote user" do
605 follower = insert(:user)
606 followed = insert(:user, locked: true, local: false, ap_enabled: true)
608 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
609 CommonAPI.follow(follower, followed)
611 assert User.get_follow_state(follower, followed) == "pending"
612 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
613 assert User.get_follow_state(follower, followed) == nil
615 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
616 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
621 "object" => %{"type" => "Follow", "state" => "cancelled"}
623 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
627 describe "accept_follow_request/2" do
628 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
629 user = insert(:user, locked: true)
630 follower = insert(:user)
631 follower_two = insert(:user)
633 {:ok, follow_activity} = ActivityPub.follow(follower, user)
634 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
635 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
637 assert follow_activity.data["state"] == "pending"
638 assert follow_activity_two.data["state"] == "pending"
639 assert follow_activity_three.data["state"] == "pending"
641 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
643 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
644 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
645 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
648 test "after rejection, it sets all existing pending follow request states to 'reject'" do
649 user = insert(:user, locked: true)
650 follower = insert(:user)
651 follower_two = insert(:user)
653 {:ok, follow_activity} = ActivityPub.follow(follower, user)
654 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
655 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
657 assert follow_activity.data["state"] == "pending"
658 assert follow_activity_two.data["state"] == "pending"
659 assert follow_activity_three.data["state"] == "pending"
661 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
663 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
664 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
665 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
670 test "does not allow to vote twice" do
672 other_user = insert(:user)
675 CommonAPI.post(user, %{
676 "status" => "Am I cute?",
677 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
680 object = Object.normalize(activity)
682 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
684 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
688 describe "listen/2" do
689 test "returns a valid activity" do
693 CommonAPI.listen(user, %{
694 "title" => "lain radio episode 1",
695 "album" => "lain radio",
700 object = Object.normalize(activity)
702 assert object.data["title"] == "lain radio episode 1"
704 assert Visibility.get_visibility(activity) == "public"
707 test "respects visibility=private" do
711 CommonAPI.listen(user, %{
712 "title" => "lain radio episode 1",
713 "album" => "lain radio",
716 "visibility" => "private"
719 object = Object.normalize(activity)
721 assert object.data["title"] == "lain radio episode 1"
723 assert Visibility.get_visibility(activity) == "private"