1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.CommonAPI
15 import Pleroma.Factory
17 clear_config([:instance, :safe_dm_mentions])
18 clear_config([:instance, :limit])
19 clear_config([:instance, :max_pinned_statuses])
21 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
23 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
25 [participation] = Participation.for_user(user)
28 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
30 assert Visibility.is_direct?(convo_reply)
32 assert activity.data["context"] == convo_reply.data["context"]
35 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
37 jafnhar = insert(:user)
41 CommonAPI.post(har, %{
42 "status" => "@#{jafnhar.nickname} hey",
43 "visibility" => "direct"
46 assert har.ap_id in activity.recipients
47 assert jafnhar.ap_id in activity.recipients
49 [participation] = Participation.for_user(har)
52 CommonAPI.post(har, %{
53 "status" => "I don't really like @#{tridi.nickname}",
54 "visibility" => "direct",
55 "in_reply_to_status_id" => activity.id,
56 "in_reply_to_conversation_id" => participation.id
59 assert har.ap_id in activity.recipients
60 assert jafnhar.ap_id in activity.recipients
61 refute tridi.ap_id in activity.recipients
64 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
66 jafnhar = insert(:user)
68 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
71 CommonAPI.post(har, %{
72 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
73 "visibility" => "direct"
76 refute tridi.ap_id in activity.recipients
77 assert jafnhar.ap_id in activity.recipients
80 test "it de-duplicates tags" do
82 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
84 object = Object.normalize(activity)
86 assert object.data["tag"] == ["2hu"]
89 test "it adds emoji in the object" do
91 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
93 assert Object.normalize(activity).data["emoji"]["firefox"]
96 test "it adds emoji when updating profiles" do
97 user = insert(:user, %{name: ":firefox:"})
99 CommonAPI.update(user)
100 user = User.get_cached_by_ap_id(user.ap_id)
101 [firefox] = user.info.source_data["tag"]
103 assert firefox["name"] == ":firefox:"
106 describe "posting" do
107 test "it supports explicit addressing" do
109 user_two = insert(:user)
110 user_three = insert(:user)
111 user_four = insert(:user)
114 CommonAPI.post(user, %{
116 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
117 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
120 assert user.ap_id in activity.recipients
121 assert user_two.ap_id in activity.recipients
122 assert user_four.ap_id in activity.recipients
123 refute user_three.ap_id in activity.recipients
126 test "it filters out obviously bad tags when accepting a post as HTML" do
129 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
132 CommonAPI.post(user, %{
134 "content_type" => "text/html"
137 object = Object.normalize(activity)
139 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
142 test "it filters out obviously bad tags when accepting a post as Markdown" do
145 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
148 CommonAPI.post(user, %{
150 "content_type" => "text/markdown"
153 object = Object.normalize(activity)
155 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
158 test "it does not allow replies to direct messages that are not direct messages themselves" do
161 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
164 CommonAPI.post(user, %{
165 "status" => "suya..",
166 "visibility" => "direct",
167 "in_reply_to_status_id" => activity.id
170 Enum.each(["public", "private", "unlisted"], fn visibility ->
171 assert {:error, "The message visibility must be direct"} =
172 CommonAPI.post(user, %{
173 "status" => "suya..",
174 "visibility" => visibility,
175 "in_reply_to_status_id" => activity.id
180 test "it allows to address a list" do
182 {:ok, list} = Pleroma.List.create("foo", user)
185 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
187 assert activity.data["bcc"] == [list.ap_id]
188 assert activity.recipients == [list.ap_id, user.ap_id]
189 assert activity.data["listMessage"] == list.ap_id
192 test "it returns error when status is empty and no attachments" do
195 assert {:error, "Cannot post an empty status without attachments"} =
196 CommonAPI.post(user, %{"status" => ""})
199 test "it returns error when character limit is exceeded" do
200 Pleroma.Config.put([:instance, :limit], 5)
204 assert {:error, "The status is over the character limit"} =
205 CommonAPI.post(user, %{"status" => "foobar"})
208 test "it can handle activities that expire" do
212 NaiveDateTime.utc_now()
213 |> NaiveDateTime.truncate(:second)
214 |> NaiveDateTime.add(1_000_000, :second)
216 assert {:ok, activity} =
217 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
219 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
220 assert expiration.scheduled_at == expires_at
224 describe "reactions" do
225 test "repeating a status" do
227 other_user = insert(:user)
229 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
231 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
234 test "favoriting a status" do
236 other_user = insert(:user)
238 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
240 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
243 test "retweeting a status twice returns an error" do
245 other_user = insert(:user)
247 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
248 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
249 {:error, _} = CommonAPI.repeat(activity.id, user)
252 test "favoriting a status twice returns an error" do
254 other_user = insert(:user)
256 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
257 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
258 {:error, _} = CommonAPI.favorite(activity.id, user)
262 describe "pinned statuses" do
264 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
267 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
269 [user: user, activity: activity]
272 test "pin status", %{user: user, activity: activity} do
273 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
276 user = refresh_record(user)
278 assert %User{info: %{pinned_activities: [^id]}} = user
281 test "unlisted statuses can be pinned", %{user: user} do
282 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
283 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
286 test "only self-authored can be pinned", %{activity: activity} do
289 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
292 test "max pinned statuses", %{user: user, activity: activity_one} do
293 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
295 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
297 user = refresh_record(user)
299 assert {:error, "You have already pinned the maximum number of statuses"} =
300 CommonAPI.pin(activity_two.id, user)
303 test "unpin status", %{user: user, activity: activity} do
304 {:ok, activity} = CommonAPI.pin(activity.id, user)
306 user = refresh_record(user)
308 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
310 user = refresh_record(user)
312 assert %User{info: %{pinned_activities: []}} = user
315 test "should unpin when deleting a status", %{user: user, activity: activity} do
316 {:ok, activity} = CommonAPI.pin(activity.id, user)
318 user = refresh_record(user)
320 assert {:ok, _} = CommonAPI.delete(activity.id, user)
322 user = refresh_record(user)
324 assert %User{info: %{pinned_activities: []}} = user
328 describe "mute tests" do
332 activity = insert(:note_activity)
334 [user: user, activity: activity]
337 test "add mute", %{user: user, activity: activity} do
338 {:ok, _} = CommonAPI.add_mute(user, activity)
339 assert CommonAPI.thread_muted?(user, activity)
342 test "remove mute", %{user: user, activity: activity} do
343 CommonAPI.add_mute(user, activity)
344 {:ok, _} = CommonAPI.remove_mute(user, activity)
345 refute CommonAPI.thread_muted?(user, activity)
348 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
349 CommonAPI.add_mute(user, activity)
350 {:error, _} = CommonAPI.add_mute(user, activity)
354 describe "reports" do
355 test "creates a report" do
356 reporter = insert(:user)
357 target_user = insert(:user)
359 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
361 reporter_ap_id = reporter.ap_id
362 target_ap_id = target_user.ap_id
363 activity_ap_id = activity.data["id"]
367 "account_id" => target_user.id,
368 "comment" => comment,
369 "status_ids" => [activity.id]
372 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
375 actor: ^reporter_ap_id,
378 "content" => ^comment,
379 "object" => [^target_ap_id, ^activity_ap_id],
385 test "updates report state" do
386 [reporter, target_user] = insert_pair(:user)
387 activity = insert(:note_activity, user: target_user)
389 {:ok, %Activity{id: report_id}} =
390 CommonAPI.report(reporter, %{
391 "account_id" => target_user.id,
392 "comment" => "I feel offended",
393 "status_ids" => [activity.id]
396 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
398 assert report.data["state"] == "resolved"
401 test "does not update report state when state is unsupported" do
402 [reporter, target_user] = insert_pair(:user)
403 activity = insert(:note_activity, user: target_user)
405 {:ok, %Activity{id: report_id}} =
406 CommonAPI.report(reporter, %{
407 "account_id" => target_user.id,
408 "comment" => "I feel offended",
409 "status_ids" => [activity.id]
412 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
416 describe "reblog muting" do
418 muter = insert(:user)
420 muted = insert(:user)
422 [muter: muter, muted: muted]
425 test "add a reblog mute", %{muter: muter, muted: muted} do
426 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
428 assert User.showing_reblogs?(muter, muted) == false
431 test "remove a reblog mute", %{muter: muter, muted: muted} do
432 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
433 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
435 assert User.showing_reblogs?(muter, muted) == true
439 describe "unfollow/2" do
440 test "also unsubscribes a user" do
441 [follower, followed] = insert_pair(:user)
442 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
443 {:ok, followed} = User.subscribe(follower, followed)
445 assert User.subscribed_to?(follower, followed)
447 {:ok, follower} = CommonAPI.unfollow(follower, followed)
449 refute User.subscribed_to?(follower, followed)
453 describe "accept_follow_request/2" do
454 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
455 user = insert(:user, info: %{locked: true})
456 follower = insert(:user)
457 follower_two = insert(:user)
459 {:ok, follow_activity} = ActivityPub.follow(follower, user)
460 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
461 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
463 assert follow_activity.data["state"] == "pending"
464 assert follow_activity_two.data["state"] == "pending"
465 assert follow_activity_three.data["state"] == "pending"
467 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
469 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
470 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
471 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
474 test "after rejection, it sets all existing pending follow request states to 'reject'" do
475 user = insert(:user, info: %{locked: true})
476 follower = insert(:user)
477 follower_two = insert(:user)
479 {:ok, follow_activity} = ActivityPub.follow(follower, user)
480 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
481 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
483 assert follow_activity.data["state"] == "pending"
484 assert follow_activity_two.data["state"] == "pending"
485 assert follow_activity_three.data["state"] == "pending"
487 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
489 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
490 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
491 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
496 test "does not allow to vote twice" do
498 other_user = insert(:user)
501 CommonAPI.post(user, %{
502 "status" => "Am I cute?",
503 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
506 object = Object.normalize(activity)
508 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
510 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])