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 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
19 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
21 [participation] = Participation.for_user(user)
24 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
26 assert Visibility.is_direct?(convo_reply)
28 assert activity.data["context"] == convo_reply.data["context"]
31 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
33 jafnhar = insert(:user)
37 CommonAPI.post(har, %{
38 "status" => "@#{jafnhar.nickname} hey",
39 "visibility" => "direct"
42 assert har.ap_id in activity.recipients
43 assert jafnhar.ap_id in activity.recipients
45 [participation] = Participation.for_user(har)
48 CommonAPI.post(har, %{
49 "status" => "I don't really like @#{tridi.nickname}",
50 "visibility" => "direct",
51 "in_reply_to_status_id" => activity.id,
52 "in_reply_to_conversation_id" => participation.id
55 assert har.ap_id in activity.recipients
56 assert jafnhar.ap_id in activity.recipients
57 refute tridi.ap_id in activity.recipients
60 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
62 jafnhar = insert(:user)
64 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
65 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
68 CommonAPI.post(har, %{
69 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
70 "visibility" => "direct"
73 refute tridi.ap_id in activity.recipients
74 assert jafnhar.ap_id in activity.recipients
75 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
78 test "it de-duplicates tags" do
80 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
82 object = Object.normalize(activity)
84 assert object.data["tag"] == ["2hu"]
87 test "it adds emoji in the object" do
89 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
91 assert Object.normalize(activity).data["emoji"]["firefox"]
94 test "it adds emoji when updating profiles" do
95 user = insert(:user, %{name: ":firefox:"})
97 CommonAPI.update(user)
98 user = User.get_cached_by_ap_id(user.ap_id)
99 [firefox] = user.info.source_data["tag"]
101 assert firefox["name"] == ":firefox:"
104 describe "posting" do
105 test "it supports explicit addressing" do
107 user_two = insert(:user)
108 user_three = insert(:user)
109 user_four = insert(:user)
112 CommonAPI.post(user, %{
114 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
115 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
118 assert user.ap_id in activity.recipients
119 assert user_two.ap_id in activity.recipients
120 assert user_four.ap_id in activity.recipients
121 refute user_three.ap_id in activity.recipients
124 test "it filters out obviously bad tags when accepting a post as HTML" do
127 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
130 CommonAPI.post(user, %{
132 "content_type" => "text/html"
135 object = Object.normalize(activity)
137 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
140 test "it filters out obviously bad tags when accepting a post as Markdown" do
143 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
146 CommonAPI.post(user, %{
148 "content_type" => "text/markdown"
151 object = Object.normalize(activity)
153 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
156 test "it does not allow replies to direct messages that are not direct messages themselves" do
159 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
162 CommonAPI.post(user, %{
163 "status" => "suya..",
164 "visibility" => "direct",
165 "in_reply_to_status_id" => activity.id
168 Enum.each(["public", "private", "unlisted"], fn visibility ->
169 assert {:error, "The message visibility must be direct"} =
170 CommonAPI.post(user, %{
171 "status" => "suya..",
172 "visibility" => visibility,
173 "in_reply_to_status_id" => activity.id
178 test "it allows to address a list" do
180 {:ok, list} = Pleroma.List.create("foo", user)
183 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
185 assert activity.data["bcc"] == [list.ap_id]
186 assert activity.recipients == [list.ap_id, user.ap_id]
187 assert activity.data["listMessage"] == list.ap_id
190 test "it returns error when status is empty and no attachments" do
193 assert {:error, "Cannot post an empty status without attachments"} =
194 CommonAPI.post(user, %{"status" => ""})
197 test "it returns error when character limit is exceeded" do
198 limit = Pleroma.Config.get([:instance, :limit])
199 Pleroma.Config.put([:instance, :limit], 5)
203 assert {:error, "The status is over the character limit"} =
204 CommonAPI.post(user, %{"status" => "foobar"})
206 Pleroma.Config.put([:instance, :limit], limit)
210 describe "reactions" do
211 test "repeating a status" do
213 other_user = insert(:user)
215 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
217 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
220 test "favoriting a status" do
222 other_user = insert(:user)
224 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
226 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
229 test "retweeting a status twice returns an error" do
231 other_user = insert(:user)
233 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
234 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
235 {:error, _} = CommonAPI.repeat(activity.id, user)
238 test "favoriting a status twice returns an error" do
240 other_user = insert(:user)
242 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
243 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
244 {:error, _} = CommonAPI.favorite(activity.id, user)
248 describe "pinned statuses" do
250 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
253 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
255 [user: user, activity: activity]
258 test "pin status", %{user: user, activity: activity} do
259 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
262 user = refresh_record(user)
264 assert %User{info: %{pinned_activities: [^id]}} = user
267 test "unlisted statuses can be pinned", %{user: user} do
268 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
269 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
272 test "only self-authored can be pinned", %{activity: activity} do
275 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
278 test "max pinned statuses", %{user: user, activity: activity_one} do
279 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
281 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
283 user = refresh_record(user)
285 assert {:error, "You have already pinned the maximum number of statuses"} =
286 CommonAPI.pin(activity_two.id, user)
289 test "unpin status", %{user: user, activity: activity} do
290 {:ok, activity} = CommonAPI.pin(activity.id, user)
292 user = refresh_record(user)
294 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
296 user = refresh_record(user)
298 assert %User{info: %{pinned_activities: []}} = user
301 test "should unpin when deleting a status", %{user: user, activity: activity} do
302 {:ok, activity} = CommonAPI.pin(activity.id, user)
304 user = refresh_record(user)
306 assert {:ok, _} = CommonAPI.delete(activity.id, user)
308 user = refresh_record(user)
310 assert %User{info: %{pinned_activities: []}} = user
314 describe "mute tests" do
318 activity = insert(:note_activity)
320 [user: user, activity: activity]
323 test "add mute", %{user: user, activity: activity} do
324 {:ok, _} = CommonAPI.add_mute(user, activity)
325 assert CommonAPI.thread_muted?(user, activity)
328 test "remove mute", %{user: user, activity: activity} do
329 CommonAPI.add_mute(user, activity)
330 {:ok, _} = CommonAPI.remove_mute(user, activity)
331 refute CommonAPI.thread_muted?(user, activity)
334 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
335 CommonAPI.add_mute(user, activity)
336 {:error, _} = CommonAPI.add_mute(user, activity)
340 describe "reports" do
341 test "creates a report" do
342 reporter = insert(:user)
343 target_user = insert(:user)
345 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
347 reporter_ap_id = reporter.ap_id
348 target_ap_id = target_user.ap_id
349 activity_ap_id = activity.data["id"]
353 "account_id" => target_user.id,
354 "comment" => comment,
355 "status_ids" => [activity.id]
358 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
361 actor: ^reporter_ap_id,
364 "content" => ^comment,
365 "object" => [^target_ap_id, ^activity_ap_id],
371 test "updates report state" do
372 [reporter, target_user] = insert_pair(:user)
373 activity = insert(:note_activity, user: target_user)
375 {:ok, %Activity{id: report_id}} =
376 CommonAPI.report(reporter, %{
377 "account_id" => target_user.id,
378 "comment" => "I feel offended",
379 "status_ids" => [activity.id]
382 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
384 assert report.data["state"] == "resolved"
387 test "does not update report state when state is unsupported" do
388 [reporter, target_user] = insert_pair(:user)
389 activity = insert(:note_activity, user: target_user)
391 {:ok, %Activity{id: report_id}} =
392 CommonAPI.report(reporter, %{
393 "account_id" => target_user.id,
394 "comment" => "I feel offended",
395 "status_ids" => [activity.id]
398 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
402 describe "reblog muting" do
404 muter = insert(:user)
406 muted = insert(:user)
408 [muter: muter, muted: muted]
411 test "add a reblog mute", %{muter: muter, muted: muted} do
412 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
414 assert User.showing_reblogs?(muter, muted) == false
417 test "remove a reblog mute", %{muter: muter, muted: muted} do
418 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
419 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
421 assert User.showing_reblogs?(muter, muted) == true
425 describe "unfollow/2" do
426 test "also unsubscribes a user" do
427 [follower, followed] = insert_pair(:user)
428 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
429 {:ok, followed} = User.subscribe(follower, followed)
431 assert User.subscribed_to?(follower, followed)
433 {:ok, follower} = CommonAPI.unfollow(follower, followed)
435 refute User.subscribed_to?(follower, followed)
439 describe "accept_follow_request/2" do
440 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
441 user = insert(:user, info: %{locked: true})
442 follower = insert(:user)
443 follower_two = insert(:user)
445 {:ok, follow_activity} = ActivityPub.follow(follower, user)
446 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
447 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
449 assert follow_activity.data["state"] == "pending"
450 assert follow_activity_two.data["state"] == "pending"
451 assert follow_activity_three.data["state"] == "pending"
453 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
455 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
456 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
457 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
460 test "after rejection, it sets all existing pending follow request states to 'reject'" do
461 user = insert(:user, info: %{locked: true})
462 follower = insert(:user)
463 follower_two = insert(:user)
465 {:ok, follow_activity} = ActivityPub.follow(follower, user)
466 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
467 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
469 assert follow_activity.data["state"] == "pending"
470 assert follow_activity_two.data["state"] == "pending"
471 assert follow_activity_three.data["state"] == "pending"
473 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
475 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
476 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
477 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
482 test "does not allow to vote twice" do
484 other_user = insert(:user)
487 CommonAPI.post(user, %{
488 "status" => "Am I cute?",
489 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
492 object = Object.normalize(activity)
494 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
496 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])