Add Reports to Admin API
[akkoma] / test / web / common_api / common_api_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.CommonAPITest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Object
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI
11
12 import Pleroma.Factory
13
14 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
15 har = insert(:user)
16 jafnhar = insert(:user)
17 tridi = insert(:user)
18 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
19 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
20
21 {:ok, activity} =
22 CommonAPI.post(har, %{
23 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
24 "visibility" => "direct"
25 })
26
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)
30 end
31
32 test "it de-duplicates tags" do
33 user = insert(:user)
34 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
35
36 object = Object.normalize(activity.data["object"])
37
38 assert object.data["tag"] == ["2hu"]
39 end
40
41 test "it adds emoji in the object" do
42 user = insert(:user)
43 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
44
45 assert Object.normalize(activity).data["emoji"]["firefox"]
46 end
47
48 test "it adds emoji when updating profiles" do
49 user = insert(:user, %{name: ":firefox:"})
50
51 CommonAPI.update(user)
52 user = User.get_cached_by_ap_id(user.ap_id)
53 [firefox] = user.info.source_data["tag"]
54
55 assert firefox["name"] == ":firefox:"
56 end
57
58 describe "posting" do
59 test "it filters out obviously bad tags when accepting a post as HTML" do
60 user = insert(:user)
61
62 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
63
64 {:ok, activity} =
65 CommonAPI.post(user, %{
66 "status" => post,
67 "content_type" => "text/html"
68 })
69
70 object = Object.normalize(activity.data["object"])
71
72 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
73 end
74
75 test "it filters out obviously bad tags when accepting a post as Markdown" do
76 user = insert(:user)
77
78 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
79
80 {:ok, activity} =
81 CommonAPI.post(user, %{
82 "status" => post,
83 "content_type" => "text/markdown"
84 })
85
86 object = Object.normalize(activity.data["object"])
87
88 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
89 end
90
91 test "it does not allow replies to direct messages that are not direct messages themselves" do
92 user = insert(:user)
93
94 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
95
96 assert {:ok, _} =
97 CommonAPI.post(user, %{
98 "status" => "suya..",
99 "visibility" => "direct",
100 "in_reply_to_status_id" => activity.id
101 })
102
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
109 })
110 end)
111 end
112 end
113
114 describe "reactions" do
115 test "repeating a status" do
116 user = insert(:user)
117 other_user = insert(:user)
118
119 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
120
121 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
122 end
123
124 test "favoriting a status" do
125 user = insert(:user)
126 other_user = insert(:user)
127
128 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
129
130 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
131 end
132
133 test "retweeting a status twice returns an error" do
134 user = insert(:user)
135 other_user = insert(:user)
136
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)
140 end
141
142 test "favoriting a status twice returns an error" do
143 user = insert(:user)
144 other_user = insert(:user)
145
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)
149 end
150 end
151
152 describe "pinned statuses" do
153 setup do
154 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
155
156 user = insert(:user)
157 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
158
159 [user: user, activity: activity]
160 end
161
162 test "pin status", %{user: user, activity: activity} do
163 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
164
165 id = activity.id
166 user = refresh_record(user)
167
168 assert %User{info: %{pinned_activities: [^id]}} = user
169 end
170
171 test "only self-authored can be pinned", %{activity: activity} do
172 user = insert(:user)
173
174 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
175 end
176
177 test "max pinned statuses", %{user: user, activity: activity_one} do
178 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
179
180 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
181
182 user = refresh_record(user)
183
184 assert {:error, "You have already pinned the maximum number of statuses"} =
185 CommonAPI.pin(activity_two.id, user)
186 end
187
188 test "unpin status", %{user: user, activity: activity} do
189 {:ok, activity} = CommonAPI.pin(activity.id, user)
190
191 user = refresh_record(user)
192
193 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
194
195 user = refresh_record(user)
196
197 assert %User{info: %{pinned_activities: []}} = user
198 end
199
200 test "should unpin when deleting a status", %{user: user, activity: activity} do
201 {:ok, activity} = CommonAPI.pin(activity.id, user)
202
203 user = refresh_record(user)
204
205 assert {:ok, _} = CommonAPI.delete(activity.id, user)
206
207 user = refresh_record(user)
208
209 assert %User{info: %{pinned_activities: []}} = user
210 end
211 end
212
213 describe "mute tests" do
214 setup do
215 user = insert(:user)
216
217 activity = insert(:note_activity)
218
219 [user: user, activity: activity]
220 end
221
222 test "add mute", %{user: user, activity: activity} do
223 {:ok, _} = CommonAPI.add_mute(user, activity)
224 assert CommonAPI.thread_muted?(user, activity)
225 end
226
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)
231 end
232
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)
236 end
237 end
238
239 describe "reports" do
240 test "creates a report" do
241 reporter = insert(:user)
242 target_user = insert(:user)
243
244 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
245
246 reporter_ap_id = reporter.ap_id
247 target_ap_id = target_user.ap_id
248 activity_ap_id = activity.data["id"]
249 comment = "foobar"
250
251 report_data = %{
252 "account_id" => target_user.id,
253 "comment" => comment,
254 "status_ids" => [activity.id]
255 }
256
257 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
258
259 assert %Activity{
260 actor: ^reporter_ap_id,
261 data: %{
262 "type" => "Flag",
263 "content" => ^comment,
264 "object" => [^target_ap_id, ^activity_ap_id],
265 "state" => "open"
266 }
267 } = flag_activity
268 end
269
270 test "updates report state" do
271 [reporter, target_user] = insert_pair(:user)
272 activity = insert(:note_activity, user: target_user)
273
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]
279 })
280
281 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
282
283 assert report.data["state"] == "resolved"
284 end
285
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)
289
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]
295 })
296
297 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
298 end
299 end
300
301 describe "reblog muting" do
302 setup do
303 muter = insert(:user)
304
305 muted = insert(:user)
306
307 [muter: muter, muted: muted]
308 end
309
310 test "add a reblog mute", %{muter: muter, muted: muted} do
311 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
312
313 assert Pleroma.User.showing_reblogs?(muter, muted) == false
314 end
315
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)
319
320 assert Pleroma.User.showing_reblogs?(muter, muted) == true
321 end
322 end
323 end