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 "reacting to a status with an emoji" do
227 other_user = insert(:user)
229 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
231 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
233 assert reaction.data["actor"] == user.ap_id
234 assert reaction.data["content"] == "👍"
236 # TODO: test error case.
239 test "repeating a status" do
241 other_user = insert(:user)
243 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
245 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
248 test "favoriting a status" do
250 other_user = insert(:user)
252 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
254 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
257 test "retweeting a status twice returns an error" do
259 other_user = insert(:user)
261 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
262 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
263 {:error, _} = CommonAPI.repeat(activity.id, user)
266 test "favoriting a status twice returns an error" do
268 other_user = insert(:user)
270 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
271 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
272 {:error, _} = CommonAPI.favorite(activity.id, user)
276 describe "pinned statuses" do
278 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
281 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
283 [user: user, activity: activity]
286 test "pin status", %{user: user, activity: activity} do
287 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
290 user = refresh_record(user)
292 assert %User{info: %{pinned_activities: [^id]}} = user
295 test "unlisted statuses can be pinned", %{user: user} do
296 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
297 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
300 test "only self-authored can be pinned", %{activity: activity} do
303 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
306 test "max pinned statuses", %{user: user, activity: activity_one} do
307 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
309 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
311 user = refresh_record(user)
313 assert {:error, "You have already pinned the maximum number of statuses"} =
314 CommonAPI.pin(activity_two.id, user)
317 test "unpin status", %{user: user, activity: activity} do
318 {:ok, activity} = CommonAPI.pin(activity.id, user)
320 user = refresh_record(user)
322 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
324 user = refresh_record(user)
326 assert %User{info: %{pinned_activities: []}} = user
329 test "should unpin when deleting a status", %{user: user, activity: activity} do
330 {:ok, activity} = CommonAPI.pin(activity.id, user)
332 user = refresh_record(user)
334 assert {:ok, _} = CommonAPI.delete(activity.id, user)
336 user = refresh_record(user)
338 assert %User{info: %{pinned_activities: []}} = user
342 describe "mute tests" do
346 activity = insert(:note_activity)
348 [user: user, activity: activity]
351 test "add mute", %{user: user, activity: activity} do
352 {:ok, _} = CommonAPI.add_mute(user, activity)
353 assert CommonAPI.thread_muted?(user, activity)
356 test "remove mute", %{user: user, activity: activity} do
357 CommonAPI.add_mute(user, activity)
358 {:ok, _} = CommonAPI.remove_mute(user, activity)
359 refute CommonAPI.thread_muted?(user, activity)
362 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
363 CommonAPI.add_mute(user, activity)
364 {:error, _} = CommonAPI.add_mute(user, activity)
368 describe "reports" do
369 test "creates a report" do
370 reporter = insert(:user)
371 target_user = insert(:user)
373 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
375 reporter_ap_id = reporter.ap_id
376 target_ap_id = target_user.ap_id
377 activity_ap_id = activity.data["id"]
381 "account_id" => target_user.id,
382 "comment" => comment,
383 "status_ids" => [activity.id]
386 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
389 actor: ^reporter_ap_id,
392 "content" => ^comment,
393 "object" => [^target_ap_id, ^activity_ap_id],
399 test "updates report state" do
400 [reporter, target_user] = insert_pair(:user)
401 activity = insert(:note_activity, user: target_user)
403 {:ok, %Activity{id: report_id}} =
404 CommonAPI.report(reporter, %{
405 "account_id" => target_user.id,
406 "comment" => "I feel offended",
407 "status_ids" => [activity.id]
410 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
412 assert report.data["state"] == "resolved"
415 test "does not update report state when state is unsupported" do
416 [reporter, target_user] = insert_pair(:user)
417 activity = insert(:note_activity, user: target_user)
419 {:ok, %Activity{id: report_id}} =
420 CommonAPI.report(reporter, %{
421 "account_id" => target_user.id,
422 "comment" => "I feel offended",
423 "status_ids" => [activity.id]
426 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
430 describe "reblog muting" do
432 muter = insert(:user)
434 muted = insert(:user)
436 [muter: muter, muted: muted]
439 test "add a reblog mute", %{muter: muter, muted: muted} do
440 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
442 assert User.showing_reblogs?(muter, muted) == false
445 test "remove a reblog mute", %{muter: muter, muted: muted} do
446 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
447 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
449 assert User.showing_reblogs?(muter, muted) == true
453 describe "unfollow/2" do
454 test "also unsubscribes a user" do
455 [follower, followed] = insert_pair(:user)
456 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
457 {:ok, followed} = User.subscribe(follower, followed)
459 assert User.subscribed_to?(follower, followed)
461 {:ok, follower} = CommonAPI.unfollow(follower, followed)
463 refute User.subscribed_to?(follower, followed)
467 describe "accept_follow_request/2" do
468 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
469 user = insert(:user, info: %{locked: true})
470 follower = insert(:user)
471 follower_two = insert(:user)
473 {:ok, follow_activity} = ActivityPub.follow(follower, user)
474 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
475 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
477 assert follow_activity.data["state"] == "pending"
478 assert follow_activity_two.data["state"] == "pending"
479 assert follow_activity_three.data["state"] == "pending"
481 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
483 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
484 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
485 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
488 test "after rejection, it sets all existing pending follow request states to 'reject'" do
489 user = insert(:user, info: %{locked: true})
490 follower = insert(:user)
491 follower_two = insert(:user)
493 {:ok, follow_activity} = ActivityPub.follow(follower, user)
494 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
495 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
497 assert follow_activity.data["state"] == "pending"
498 assert follow_activity_two.data["state"] == "pending"
499 assert follow_activity_three.data["state"] == "pending"
501 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
503 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
504 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
505 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
510 test "does not allow to vote twice" do
512 other_user = insert(:user)
515 CommonAPI.post(user, %{
516 "status" => "Am I cute?",
517 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
520 object = Object.normalize(activity)
522 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
524 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])