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 require Pleroma.Constants
19 clear_config([:instance, :safe_dm_mentions])
20 clear_config([:instance, :limit])
21 clear_config([:instance, :max_pinned_statuses])
23 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
25 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
27 [participation] = Participation.for_user(user)
30 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
32 assert Visibility.is_direct?(convo_reply)
34 assert activity.data["context"] == convo_reply.data["context"]
37 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
39 jafnhar = insert(:user)
43 CommonAPI.post(har, %{
44 "status" => "@#{jafnhar.nickname} hey",
45 "visibility" => "direct"
48 assert har.ap_id in activity.recipients
49 assert jafnhar.ap_id in activity.recipients
51 [participation] = Participation.for_user(har)
54 CommonAPI.post(har, %{
55 "status" => "I don't really like @#{tridi.nickname}",
56 "visibility" => "direct",
57 "in_reply_to_status_id" => activity.id,
58 "in_reply_to_conversation_id" => participation.id
61 assert har.ap_id in activity.recipients
62 assert jafnhar.ap_id in activity.recipients
63 refute tridi.ap_id in activity.recipients
66 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
68 jafnhar = insert(:user)
70 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
73 CommonAPI.post(har, %{
74 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
75 "visibility" => "direct"
78 refute tridi.ap_id in activity.recipients
79 assert jafnhar.ap_id in activity.recipients
82 test "it de-duplicates tags" do
84 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
86 object = Object.normalize(activity)
88 assert object.data["tag"] == ["2hu"]
91 test "it adds emoji in the object" do
93 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
95 assert Object.normalize(activity).data["emoji"]["firefox"]
98 test "it adds emoji when updating profiles" do
99 user = insert(:user, %{name: ":firefox:"})
101 {:ok, activity} = CommonAPI.update(user)
102 user = User.get_cached_by_ap_id(user.ap_id)
103 [firefox] = user.info.source_data["tag"]
105 assert firefox["name"] == ":firefox:"
107 assert Pleroma.Constants.as_public() in activity.recipients
110 describe "posting" do
111 test "it supports explicit addressing" do
113 user_two = insert(:user)
114 user_three = insert(:user)
115 user_four = insert(:user)
118 CommonAPI.post(user, %{
120 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
121 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
124 assert user.ap_id in activity.recipients
125 assert user_two.ap_id in activity.recipients
126 assert user_four.ap_id in activity.recipients
127 refute user_three.ap_id in activity.recipients
130 test "it filters out obviously bad tags when accepting a post as HTML" do
133 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
136 CommonAPI.post(user, %{
138 "content_type" => "text/html"
141 object = Object.normalize(activity)
143 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
146 test "it filters out obviously bad tags when accepting a post as Markdown" do
149 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
152 CommonAPI.post(user, %{
154 "content_type" => "text/markdown"
157 object = Object.normalize(activity)
159 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
162 test "it does not allow replies to direct messages that are not direct messages themselves" do
165 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
168 CommonAPI.post(user, %{
169 "status" => "suya..",
170 "visibility" => "direct",
171 "in_reply_to_status_id" => activity.id
174 Enum.each(["public", "private", "unlisted"], fn visibility ->
175 assert {:error, "The message visibility must be direct"} =
176 CommonAPI.post(user, %{
177 "status" => "suya..",
178 "visibility" => visibility,
179 "in_reply_to_status_id" => activity.id
184 test "it allows to address a list" do
186 {:ok, list} = Pleroma.List.create("foo", user)
189 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
191 assert activity.data["bcc"] == [list.ap_id]
192 assert activity.recipients == [list.ap_id, user.ap_id]
193 assert activity.data["listMessage"] == list.ap_id
196 test "it returns error when status is empty and no attachments" do
199 assert {:error, "Cannot post an empty status without attachments"} =
200 CommonAPI.post(user, %{"status" => ""})
203 test "it returns error when character limit is exceeded" do
204 Pleroma.Config.put([:instance, :limit], 5)
208 assert {:error, "The status is over the character limit"} =
209 CommonAPI.post(user, %{"status" => "foobar"})
212 test "it can handle activities that expire" do
216 NaiveDateTime.utc_now()
217 |> NaiveDateTime.truncate(:second)
218 |> NaiveDateTime.add(1_000_000, :second)
220 assert {:ok, activity} =
221 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
223 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
224 assert expiration.scheduled_at == expires_at
228 describe "reactions" do
229 test "repeating a status" do
231 other_user = insert(:user)
233 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
235 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
238 test "favoriting a status" do
240 other_user = insert(:user)
242 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
244 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
247 test "retweeting a status twice returns an error" do
249 other_user = insert(:user)
251 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
252 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
253 {:error, _} = CommonAPI.repeat(activity.id, user)
256 test "favoriting a status twice returns an error" do
258 other_user = insert(:user)
260 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
261 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
262 {:error, _} = CommonAPI.favorite(activity.id, user)
266 describe "pinned statuses" do
268 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
271 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
273 [user: user, activity: activity]
276 test "pin status", %{user: user, activity: activity} do
277 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
280 user = refresh_record(user)
282 assert %User{info: %{pinned_activities: [^id]}} = user
285 test "unlisted statuses can be pinned", %{user: user} do
286 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
287 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
290 test "only self-authored can be pinned", %{activity: activity} do
293 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
296 test "max pinned statuses", %{user: user, activity: activity_one} do
297 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
299 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
301 user = refresh_record(user)
303 assert {:error, "You have already pinned the maximum number of statuses"} =
304 CommonAPI.pin(activity_two.id, user)
307 test "unpin status", %{user: user, activity: activity} do
308 {:ok, activity} = CommonAPI.pin(activity.id, user)
310 user = refresh_record(user)
312 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
314 user = refresh_record(user)
316 assert %User{info: %{pinned_activities: []}} = user
319 test "should unpin when deleting a status", %{user: user, activity: activity} do
320 {:ok, activity} = CommonAPI.pin(activity.id, user)
322 user = refresh_record(user)
324 assert {:ok, _} = CommonAPI.delete(activity.id, user)
326 user = refresh_record(user)
328 assert %User{info: %{pinned_activities: []}} = user
332 describe "mute tests" do
336 activity = insert(:note_activity)
338 [user: user, activity: activity]
341 test "add mute", %{user: user, activity: activity} do
342 {:ok, _} = CommonAPI.add_mute(user, activity)
343 assert CommonAPI.thread_muted?(user, activity)
346 test "remove mute", %{user: user, activity: activity} do
347 CommonAPI.add_mute(user, activity)
348 {:ok, _} = CommonAPI.remove_mute(user, activity)
349 refute CommonAPI.thread_muted?(user, activity)
352 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
353 CommonAPI.add_mute(user, activity)
354 {:error, _} = CommonAPI.add_mute(user, activity)
358 describe "reports" do
359 test "creates a report" do
360 reporter = insert(:user)
361 target_user = insert(:user)
363 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
365 reporter_ap_id = reporter.ap_id
366 target_ap_id = target_user.ap_id
367 activity_ap_id = activity.data["id"]
371 "account_id" => target_user.id,
372 "comment" => comment,
373 "status_ids" => [activity.id]
376 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
379 actor: ^reporter_ap_id,
382 "content" => ^comment,
383 "object" => [^target_ap_id, ^activity_ap_id],
389 test "updates report state" do
390 [reporter, target_user] = insert_pair(:user)
391 activity = insert(:note_activity, user: target_user)
393 {:ok, %Activity{id: report_id}} =
394 CommonAPI.report(reporter, %{
395 "account_id" => target_user.id,
396 "comment" => "I feel offended",
397 "status_ids" => [activity.id]
400 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
402 assert report.data["state"] == "resolved"
405 test "does not update report state when state is unsupported" do
406 [reporter, target_user] = insert_pair(:user)
407 activity = insert(:note_activity, user: target_user)
409 {:ok, %Activity{id: report_id}} =
410 CommonAPI.report(reporter, %{
411 "account_id" => target_user.id,
412 "comment" => "I feel offended",
413 "status_ids" => [activity.id]
416 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
420 describe "reblog muting" do
422 muter = insert(:user)
424 muted = insert(:user)
426 [muter: muter, muted: muted]
429 test "add a reblog mute", %{muter: muter, muted: muted} do
430 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
432 assert User.showing_reblogs?(muter, muted) == false
435 test "remove a reblog mute", %{muter: muter, muted: muted} do
436 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
437 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
439 assert User.showing_reblogs?(muter, muted) == true
443 describe "unfollow/2" do
444 test "also unsubscribes a user" do
445 [follower, followed] = insert_pair(:user)
446 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
447 {:ok, followed} = User.subscribe(follower, followed)
449 assert User.subscribed_to?(follower, followed)
451 {:ok, follower} = CommonAPI.unfollow(follower, followed)
453 refute User.subscribed_to?(follower, followed)
457 describe "accept_follow_request/2" do
458 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
459 user = insert(:user, info: %{locked: true})
460 follower = insert(:user)
461 follower_two = insert(:user)
463 {:ok, follow_activity} = ActivityPub.follow(follower, user)
464 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
465 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
467 assert follow_activity.data["state"] == "pending"
468 assert follow_activity_two.data["state"] == "pending"
469 assert follow_activity_three.data["state"] == "pending"
471 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
473 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
474 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
475 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
478 test "after rejection, it sets all existing pending follow request states to 'reject'" do
479 user = insert(:user, info: %{locked: true})
480 follower = insert(:user)
481 follower_two = insert(:user)
483 {:ok, follow_activity} = ActivityPub.follow(follower, user)
484 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
485 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
487 assert follow_activity.data["state"] == "pending"
488 assert follow_activity_two.data["state"] == "pending"
489 assert follow_activity_three.data["state"] == "pending"
491 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
493 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
494 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
495 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
500 test "does not allow to vote twice" do
502 other_user = insert(:user)
505 CommonAPI.post(user, %{
506 "status" => "Am I cute?",
507 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
510 object = Object.normalize(activity)
512 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
514 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])