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')"
92 describe "reactions" do
93 test "repeating a status" do
95 other_user = insert(:user)
97 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
99 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
102 test "favoriting a status" do
104 other_user = insert(:user)
106 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
108 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
111 test "retweeting a status twice returns an error" do
113 other_user = insert(:user)
115 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
116 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
117 {:error, _} = CommonAPI.repeat(activity.id, user)
120 test "favoriting a status twice returns an error" do
122 other_user = insert(:user)
124 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
125 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
126 {:error, _} = CommonAPI.favorite(activity.id, user)
130 describe "pinned statuses" do
132 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
135 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
137 [user: user, activity: activity]
140 test "pin status", %{user: user, activity: activity} do
141 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
144 user = refresh_record(user)
146 assert %User{info: %{pinned_activities: [^id]}} = user
149 test "only self-authored can be pinned", %{activity: activity} do
152 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
155 test "max pinned statuses", %{user: user, activity: activity_one} do
156 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
158 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
160 user = refresh_record(user)
162 assert {:error, "You have already pinned the maximum number of statuses"} =
163 CommonAPI.pin(activity_two.id, user)
166 test "unpin status", %{user: user, activity: activity} do
167 {:ok, activity} = CommonAPI.pin(activity.id, user)
169 user = refresh_record(user)
171 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
173 user = refresh_record(user)
175 assert %User{info: %{pinned_activities: []}} = user
178 test "should unpin when deleting a status", %{user: user, activity: activity} do
179 {:ok, activity} = CommonAPI.pin(activity.id, user)
181 user = refresh_record(user)
183 assert {:ok, _} = CommonAPI.delete(activity.id, user)
185 user = refresh_record(user)
187 assert %User{info: %{pinned_activities: []}} = user
191 describe "mute tests" do
195 activity = insert(:note_activity)
197 [user: user, activity: activity]
200 test "add mute", %{user: user, activity: activity} do
201 {:ok, _} = CommonAPI.add_mute(user, activity)
202 assert CommonAPI.thread_muted?(user, activity)
205 test "remove mute", %{user: user, activity: activity} do
206 CommonAPI.add_mute(user, activity)
207 {:ok, _} = CommonAPI.remove_mute(user, activity)
208 refute CommonAPI.thread_muted?(user, activity)
211 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
212 CommonAPI.add_mute(user, activity)
213 {:error, _} = CommonAPI.add_mute(user, activity)
217 describe "reports" do
218 test "creates a report" do
219 reporter = insert(:user)
220 target_user = insert(:user)
222 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
224 reporter_ap_id = reporter.ap_id
225 target_ap_id = target_user.ap_id
226 activity_ap_id = activity.data["id"]
230 "account_id" => target_user.id,
231 "comment" => comment,
232 "status_ids" => [activity.id]
235 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
238 actor: ^reporter_ap_id,
241 "content" => ^comment,
242 "object" => [^target_ap_id, ^activity_ap_id]
248 describe "reblog muting" do
250 muter = insert(:user)
252 muted = insert(:user)
254 [muter: muter, muted: muted]
257 test "add a reblog mute", %{muter: muter, muted: muted} do
258 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
260 assert Pleroma.User.showing_reblogs?(muter, muted) == false
263 test "remove a reblog mute", %{muter: muter, muted: muted} do
264 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
265 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
267 assert Pleroma.User.showing_reblogs?(muter, muted) == true