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.CommonAPI
14 import Pleroma.Factory
16 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
18 jafnhar = insert(:user)
22 CommonAPI.post(har, %{
23 "status" => "@#{jafnhar.nickname} hey",
24 "visibility" => "direct"
27 assert har.ap_id in activity.recipients
28 assert jafnhar.ap_id in activity.recipients
30 [participation] = Participation.for_user(har)
33 CommonAPI.post(har, %{
34 "status" => "I don't really like @#{tridi.nickname}",
35 "visibility" => "direct",
36 "in_reply_to_status_id" => activity.id,
37 "in_reply_to_conversation_id" => participation.id
40 assert har.ap_id in activity.recipients
41 assert jafnhar.ap_id in activity.recipients
42 refute tridi.ap_id in activity.recipients
45 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
47 jafnhar = insert(:user)
49 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
50 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
53 CommonAPI.post(har, %{
54 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
55 "visibility" => "direct"
58 refute tridi.ap_id in activity.recipients
59 assert jafnhar.ap_id in activity.recipients
60 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
63 test "it de-duplicates tags" do
65 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
67 object = Object.normalize(activity)
69 assert object.data["tag"] == ["2hu"]
72 test "it adds emoji in the object" do
74 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
76 assert Object.normalize(activity).data["emoji"]["firefox"]
79 test "it adds emoji when updating profiles" do
80 user = insert(:user, %{name: ":firefox:"})
82 CommonAPI.update(user)
83 user = User.get_cached_by_ap_id(user.ap_id)
84 [firefox] = user.info.source_data["tag"]
86 assert firefox["name"] == ":firefox:"
90 test "it supports explicit addressing" do
92 user_two = insert(:user)
93 user_three = insert(:user)
94 user_four = insert(:user)
97 CommonAPI.post(user, %{
99 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
100 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
103 assert user.ap_id in activity.recipients
104 assert user_two.ap_id in activity.recipients
105 assert user_four.ap_id in activity.recipients
106 refute user_three.ap_id in activity.recipients
109 test "it filters out obviously bad tags when accepting a post as HTML" do
112 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
115 CommonAPI.post(user, %{
117 "content_type" => "text/html"
120 object = Object.normalize(activity)
122 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
125 test "it filters out obviously bad tags when accepting a post as Markdown" do
128 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
131 CommonAPI.post(user, %{
133 "content_type" => "text/markdown"
136 object = Object.normalize(activity)
138 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
141 test "it does not allow replies to direct messages that are not direct messages themselves" do
144 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
147 CommonAPI.post(user, %{
148 "status" => "suya..",
149 "visibility" => "direct",
150 "in_reply_to_status_id" => activity.id
153 Enum.each(["public", "private", "unlisted"], fn visibility ->
154 assert {:error, "The message visibility must be direct"} =
155 CommonAPI.post(user, %{
156 "status" => "suya..",
157 "visibility" => visibility,
158 "in_reply_to_status_id" => activity.id
163 test "it allows to address a list" do
165 {:ok, list} = Pleroma.List.create("foo", user)
168 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
170 assert activity.data["bcc"] == [list.ap_id]
171 assert activity.recipients == [list.ap_id, user.ap_id]
172 assert activity.data["listMessage"] == list.ap_id
175 test "it returns error when status is empty and no attachments" do
178 assert {:error, "Cannot post an empty status without attachments"} =
179 CommonAPI.post(user, %{"status" => ""})
182 test "it returns error when character limit is exceeded" do
183 limit = Pleroma.Config.get([:instance, :limit])
184 Pleroma.Config.put([:instance, :limit], 5)
188 assert {:error, "The status is over the character limit"} =
189 CommonAPI.post(user, %{"status" => "foobar"})
191 Pleroma.Config.put([:instance, :limit], limit)
195 describe "reactions" do
196 test "repeating a status" do
198 other_user = insert(:user)
200 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
202 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
205 test "favoriting a status" do
207 other_user = insert(:user)
209 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
211 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
214 test "retweeting a status twice returns an error" do
216 other_user = insert(:user)
218 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
219 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
220 {:error, _} = CommonAPI.repeat(activity.id, user)
223 test "favoriting a status twice returns an error" do
225 other_user = insert(:user)
227 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
228 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
229 {:error, _} = CommonAPI.favorite(activity.id, user)
233 describe "pinned statuses" do
235 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
238 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
240 [user: user, activity: activity]
243 test "pin status", %{user: user, activity: activity} do
244 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
247 user = refresh_record(user)
249 assert %User{info: %{pinned_activities: [^id]}} = user
252 test "unlisted statuses can be pinned", %{user: user} do
253 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
254 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
257 test "only self-authored can be pinned", %{activity: activity} do
260 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
263 test "max pinned statuses", %{user: user, activity: activity_one} do
264 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
266 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
268 user = refresh_record(user)
270 assert {:error, "You have already pinned the maximum number of statuses"} =
271 CommonAPI.pin(activity_two.id, user)
274 test "unpin status", %{user: user, activity: activity} do
275 {:ok, activity} = CommonAPI.pin(activity.id, user)
277 user = refresh_record(user)
279 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
281 user = refresh_record(user)
283 assert %User{info: %{pinned_activities: []}} = user
286 test "should unpin when deleting a status", %{user: user, activity: activity} do
287 {:ok, activity} = CommonAPI.pin(activity.id, user)
289 user = refresh_record(user)
291 assert {:ok, _} = CommonAPI.delete(activity.id, user)
293 user = refresh_record(user)
295 assert %User{info: %{pinned_activities: []}} = user
299 describe "mute tests" do
303 activity = insert(:note_activity)
305 [user: user, activity: activity]
308 test "add mute", %{user: user, activity: activity} do
309 {:ok, _} = CommonAPI.add_mute(user, activity)
310 assert CommonAPI.thread_muted?(user, activity)
313 test "remove mute", %{user: user, activity: activity} do
314 CommonAPI.add_mute(user, activity)
315 {:ok, _} = CommonAPI.remove_mute(user, activity)
316 refute CommonAPI.thread_muted?(user, activity)
319 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
320 CommonAPI.add_mute(user, activity)
321 {:error, _} = CommonAPI.add_mute(user, activity)
325 describe "reports" do
326 test "creates a report" do
327 reporter = insert(:user)
328 target_user = insert(:user)
330 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
332 reporter_ap_id = reporter.ap_id
333 target_ap_id = target_user.ap_id
334 activity_ap_id = activity.data["id"]
338 "account_id" => target_user.id,
339 "comment" => comment,
340 "status_ids" => [activity.id]
343 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
346 actor: ^reporter_ap_id,
349 "content" => ^comment,
350 "object" => [^target_ap_id, ^activity_ap_id],
356 test "updates report state" do
357 [reporter, target_user] = insert_pair(:user)
358 activity = insert(:note_activity, user: target_user)
360 {:ok, %Activity{id: report_id}} =
361 CommonAPI.report(reporter, %{
362 "account_id" => target_user.id,
363 "comment" => "I feel offended",
364 "status_ids" => [activity.id]
367 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
369 assert report.data["state"] == "resolved"
372 test "does not update report state when state is unsupported" do
373 [reporter, target_user] = insert_pair(:user)
374 activity = insert(:note_activity, user: target_user)
376 {:ok, %Activity{id: report_id}} =
377 CommonAPI.report(reporter, %{
378 "account_id" => target_user.id,
379 "comment" => "I feel offended",
380 "status_ids" => [activity.id]
383 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
387 describe "reblog muting" do
389 muter = insert(:user)
391 muted = insert(:user)
393 [muter: muter, muted: muted]
396 test "add a reblog mute", %{muter: muter, muted: muted} do
397 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
399 assert User.showing_reblogs?(muter, muted) == false
402 test "remove a reblog mute", %{muter: muter, muted: muted} do
403 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
404 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
406 assert User.showing_reblogs?(muter, muted) == true
410 describe "unfollow/2" do
411 test "also unsubscribes a user" do
412 [follower, followed] = insert_pair(:user)
413 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
414 {:ok, followed} = User.subscribe(follower, followed)
416 assert User.subscribed_to?(follower, followed)
418 {:ok, follower} = CommonAPI.unfollow(follower, followed)
420 refute User.subscribed_to?(follower, followed)
424 describe "accept_follow_request/2" do
425 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
426 user = insert(:user, info: %{locked: true})
427 follower = insert(:user)
428 follower_two = insert(:user)
430 {:ok, follow_activity} = ActivityPub.follow(follower, user)
431 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
432 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
434 assert follow_activity.data["state"] == "pending"
435 assert follow_activity_two.data["state"] == "pending"
436 assert follow_activity_three.data["state"] == "pending"
438 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
440 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
441 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
442 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
445 test "after rejection, it sets all existing pending follow request states to 'reject'" do
446 user = insert(:user, info: %{locked: true})
447 follower = insert(:user)
448 follower_two = insert(:user)
450 {:ok, follow_activity} = ActivityPub.follow(follower, user)
451 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
452 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
454 assert follow_activity.data["state"] == "pending"
455 assert follow_activity_two.data["state"] == "pending"
456 assert follow_activity_three.data["state"] == "pending"
458 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
460 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
461 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
462 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
467 test "does not allow to vote twice" do
469 other_user = insert(:user)
472 CommonAPI.post(user, %{
473 "status" => "Am I cute?",
474 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
477 object = Object.normalize(activity)
479 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
481 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])