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.CommonAPI
12 import Pleroma.Factory
14 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
16 jafnhar = insert(:user)
18 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
19 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
22 CommonAPI.post(har, %{
23 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
24 "visibility" => "direct"
27 refute tridi.ap_id in activity.recipients
28 assert jafnhar.ap_id in activity.recipients
29 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
32 test "it de-duplicates tags" do
34 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
36 object = Object.normalize(activity.data["object"])
38 assert object.data["tag"] == ["2hu"]
41 test "it adds emoji in the object" do
43 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
45 assert Object.normalize(activity).data["emoji"]["firefox"]
48 test "it adds emoji when updating profiles" do
49 user = insert(:user, %{name: ":firefox:"})
51 CommonAPI.update(user)
52 user = User.get_cached_by_ap_id(user.ap_id)
53 [firefox] = user.info.source_data["tag"]
55 assert firefox["name"] == ":firefox:"
59 test "it filters out obviously bad tags when accepting a post as HTML" do
62 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
65 CommonAPI.post(user, %{
67 "content_type" => "text/html"
70 object = Object.normalize(activity.data["object"])
72 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
75 test "it filters out obviously bad tags when accepting a post as Markdown" do
78 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
81 CommonAPI.post(user, %{
83 "content_type" => "text/markdown"
86 object = Object.normalize(activity.data["object"])
88 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
91 test "it does not allow replies to direct messages that are not direct messages themselves" do
94 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
97 CommonAPI.post(user, %{
99 "visibility" => "direct",
100 "in_reply_to_status_id" => activity.id
103 Enum.each(["public", "private", "unlisted"], fn visibility ->
104 assert {:error, {:private_to_public, _}} =
105 CommonAPI.post(user, %{
106 "status" => "suya..",
107 "visibility" => visibility,
108 "in_reply_to_status_id" => activity.id
114 describe "reactions" do
115 test "repeating a status" do
117 other_user = insert(:user)
119 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
121 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
124 test "favoriting a status" do
126 other_user = insert(:user)
128 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
130 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
133 test "retweeting a status twice returns an error" do
135 other_user = insert(:user)
137 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
138 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
139 {:error, _} = CommonAPI.repeat(activity.id, user)
142 test "favoriting a status twice returns an error" do
144 other_user = insert(:user)
146 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
147 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
148 {:error, _} = CommonAPI.favorite(activity.id, user)
152 describe "pinned statuses" do
154 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
157 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
159 [user: user, activity: activity]
162 test "pin status", %{user: user, activity: activity} do
163 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
166 user = refresh_record(user)
168 assert %User{info: %{pinned_activities: [^id]}} = user
171 test "only self-authored can be pinned", %{activity: activity} do
174 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
177 test "max pinned statuses", %{user: user, activity: activity_one} do
178 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
180 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
182 user = refresh_record(user)
184 assert {:error, "You have already pinned the maximum number of statuses"} =
185 CommonAPI.pin(activity_two.id, user)
188 test "unpin status", %{user: user, activity: activity} do
189 {:ok, activity} = CommonAPI.pin(activity.id, user)
191 user = refresh_record(user)
193 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
195 user = refresh_record(user)
197 assert %User{info: %{pinned_activities: []}} = user
200 test "should unpin when deleting a status", %{user: user, activity: activity} do
201 {:ok, activity} = CommonAPI.pin(activity.id, user)
203 user = refresh_record(user)
205 assert {:ok, _} = CommonAPI.delete(activity.id, user)
207 user = refresh_record(user)
209 assert %User{info: %{pinned_activities: []}} = user
213 describe "mute tests" do
217 activity = insert(:note_activity)
219 [user: user, activity: activity]
222 test "add mute", %{user: user, activity: activity} do
223 {:ok, _} = CommonAPI.add_mute(user, activity)
224 assert CommonAPI.thread_muted?(user, activity)
227 test "remove mute", %{user: user, activity: activity} do
228 CommonAPI.add_mute(user, activity)
229 {:ok, _} = CommonAPI.remove_mute(user, activity)
230 refute CommonAPI.thread_muted?(user, activity)
233 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
234 CommonAPI.add_mute(user, activity)
235 {:error, _} = CommonAPI.add_mute(user, activity)
239 describe "reports" do
240 test "creates a report" do
241 reporter = insert(:user)
242 target_user = insert(:user)
244 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
246 reporter_ap_id = reporter.ap_id
247 target_ap_id = target_user.ap_id
248 activity_ap_id = activity.data["id"]
252 "account_id" => target_user.id,
253 "comment" => comment,
254 "status_ids" => [activity.id]
257 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
260 actor: ^reporter_ap_id,
263 "content" => ^comment,
264 "object" => [^target_ap_id, ^activity_ap_id],
270 test "updates report state" do
271 [reporter, target_user] = insert_pair(:user)
272 activity = insert(:note_activity, user: target_user)
274 {:ok, %Activity{id: report_id}} =
275 CommonAPI.report(reporter, %{
276 "account_id" => target_user.id,
277 "comment" => "I feel offended",
278 "status_ids" => [activity.id]
281 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
283 assert report.data["state"] == "resolved"
286 test "does not update report state when state is unsupported" do
287 [reporter, target_user] = insert_pair(:user)
288 activity = insert(:note_activity, user: target_user)
290 {:ok, %Activity{id: report_id}} =
291 CommonAPI.report(reporter, %{
292 "account_id" => target_user.id,
293 "comment" => "I feel offended",
294 "status_ids" => [activity.id]
297 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
301 describe "reblog muting" do
303 muter = insert(:user)
305 muted = insert(:user)
307 [muter: muter, muted: muted]
310 test "add a reblog mute", %{muter: muter, muted: muted} do
311 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
313 assert Pleroma.User.showing_reblogs?(muter, muted) == false
316 test "remove a reblog mute", %{muter: muter, muted: muted} do
317 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
318 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
320 assert Pleroma.User.showing_reblogs?(muter, muted) == true