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
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
17 jafnhar = insert(:user)
19 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
20 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
23 CommonAPI.post(har, %{
24 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
25 "visibility" => "direct"
28 refute tridi.ap_id in activity.recipients
29 assert jafnhar.ap_id in activity.recipients
30 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
33 test "it de-duplicates tags" do
35 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
37 object = Object.normalize(activity.data["object"])
39 assert object.data["tag"] == ["2hu"]
42 test "it adds emoji in the object" do
44 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
46 assert Object.normalize(activity).data["emoji"]["firefox"]
49 test "it adds emoji when updating profiles" do
50 user = insert(:user, %{name: ":firefox:"})
52 CommonAPI.update(user)
53 user = User.get_cached_by_ap_id(user.ap_id)
54 [firefox] = user.info.source_data["tag"]
56 assert firefox["name"] == ":firefox:"
60 test "it supports explicit addressing" do
62 user_two = insert(:user)
63 user_three = insert(:user)
64 user_four = insert(:user)
67 CommonAPI.post(user, %{
69 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
70 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
73 assert user.ap_id in activity.recipients
74 assert user_two.ap_id in activity.recipients
75 assert user_four.ap_id in activity.recipients
76 refute user_three.ap_id in activity.recipients
79 test "it filters out obviously bad tags when accepting a post as HTML" do
82 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
85 CommonAPI.post(user, %{
87 "content_type" => "text/html"
90 object = Object.normalize(activity.data["object"])
92 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
95 test "it filters out obviously bad tags when accepting a post as Markdown" do
98 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
101 CommonAPI.post(user, %{
103 "content_type" => "text/markdown"
106 object = Object.normalize(activity.data["object"])
108 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
111 test "it does not allow replies to direct messages that are not direct messages themselves" do
114 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
117 CommonAPI.post(user, %{
118 "status" => "suya..",
119 "visibility" => "direct",
120 "in_reply_to_status_id" => activity.id
123 Enum.each(["public", "private", "unlisted"], fn visibility ->
124 assert {:error, "The message visibility must be direct"} =
125 CommonAPI.post(user, %{
126 "status" => "suya..",
127 "visibility" => visibility,
128 "in_reply_to_status_id" => activity.id
134 describe "reactions" do
135 test "repeating a status" do
137 other_user = insert(:user)
139 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
141 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
144 test "favoriting a status" do
146 other_user = insert(:user)
148 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
150 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
153 test "retweeting a status twice returns an error" do
155 other_user = insert(:user)
157 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
158 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
159 {:error, _} = CommonAPI.repeat(activity.id, user)
162 test "favoriting a status twice returns an error" do
164 other_user = insert(:user)
166 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
167 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
168 {:error, _} = CommonAPI.favorite(activity.id, user)
172 describe "pinned statuses" do
174 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
177 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
179 [user: user, activity: activity]
182 test "pin status", %{user: user, activity: activity} do
183 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
186 user = refresh_record(user)
188 assert %User{info: %{pinned_activities: [^id]}} = user
191 test "only self-authored can be pinned", %{activity: activity} do
194 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
197 test "max pinned statuses", %{user: user, activity: activity_one} do
198 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
200 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
202 user = refresh_record(user)
204 assert {:error, "You have already pinned the maximum number of statuses"} =
205 CommonAPI.pin(activity_two.id, user)
208 test "unpin status", %{user: user, activity: activity} do
209 {:ok, activity} = CommonAPI.pin(activity.id, user)
211 user = refresh_record(user)
213 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
215 user = refresh_record(user)
217 assert %User{info: %{pinned_activities: []}} = user
220 test "should unpin when deleting a status", %{user: user, activity: activity} do
221 {:ok, activity} = CommonAPI.pin(activity.id, user)
223 user = refresh_record(user)
225 assert {:ok, _} = CommonAPI.delete(activity.id, user)
227 user = refresh_record(user)
229 assert %User{info: %{pinned_activities: []}} = user
233 describe "mute tests" do
237 activity = insert(:note_activity)
239 [user: user, activity: activity]
242 test "add mute", %{user: user, activity: activity} do
243 {:ok, _} = CommonAPI.add_mute(user, activity)
244 assert CommonAPI.thread_muted?(user, activity)
247 test "remove mute", %{user: user, activity: activity} do
248 CommonAPI.add_mute(user, activity)
249 {:ok, _} = CommonAPI.remove_mute(user, activity)
250 refute CommonAPI.thread_muted?(user, activity)
253 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
254 CommonAPI.add_mute(user, activity)
255 {:error, _} = CommonAPI.add_mute(user, activity)
259 describe "reports" do
260 test "creates a report" do
261 reporter = insert(:user)
262 target_user = insert(:user)
264 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
266 reporter_ap_id = reporter.ap_id
267 target_ap_id = target_user.ap_id
268 activity_ap_id = activity.data["id"]
272 "account_id" => target_user.id,
273 "comment" => comment,
274 "status_ids" => [activity.id]
277 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
280 actor: ^reporter_ap_id,
283 "content" => ^comment,
284 "object" => [^target_ap_id, ^activity_ap_id],
290 test "updates report state" do
291 [reporter, target_user] = insert_pair(:user)
292 activity = insert(:note_activity, user: target_user)
294 {:ok, %Activity{id: report_id}} =
295 CommonAPI.report(reporter, %{
296 "account_id" => target_user.id,
297 "comment" => "I feel offended",
298 "status_ids" => [activity.id]
301 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
303 assert report.data["state"] == "resolved"
306 test "does not update report state when state is unsupported" do
307 [reporter, target_user] = insert_pair(:user)
308 activity = insert(:note_activity, user: target_user)
310 {:ok, %Activity{id: report_id}} =
311 CommonAPI.report(reporter, %{
312 "account_id" => target_user.id,
313 "comment" => "I feel offended",
314 "status_ids" => [activity.id]
317 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
321 describe "reblog muting" do
323 muter = insert(:user)
325 muted = insert(:user)
327 [muter: muter, muted: muted]
330 test "add a reblog mute", %{muter: muter, muted: muted} do
331 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
333 assert User.showing_reblogs?(muter, muted) == false
336 test "remove a reblog mute", %{muter: muter, muted: muted} do
337 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
338 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
340 assert User.showing_reblogs?(muter, muted) == true
344 describe "accept_follow_request/2" do
345 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
346 user = insert(:user, info: %{locked: true})
347 follower = insert(:user)
348 follower_two = insert(:user)
350 {:ok, follow_activity} = ActivityPub.follow(follower, user)
351 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
352 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
354 assert follow_activity.data["state"] == "pending"
355 assert follow_activity_two.data["state"] == "pending"
356 assert follow_activity_three.data["state"] == "pending"
358 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
360 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
361 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
362 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
365 test "after rejection, it sets all existing pending follow request states to 'reject'" do
366 user = insert(:user, info: %{locked: true})
367 follower = insert(:user)
368 follower_two = insert(:user)
370 {:ok, follow_activity} = ActivityPub.follow(follower, user)
371 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
372 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
374 assert follow_activity.data["state"] == "pending"
375 assert follow_activity_two.data["state"] == "pending"
376 assert follow_activity_three.data["state"] == "pending"
378 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
380 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
381 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
382 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"