[#878] Merge remote-tracking branch 'remotes/upstream/develop' into 878-activity...
[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.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
16 har = insert(:user)
17 jafnhar = insert(:user)
18 tridi = insert(:user)
19 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
20 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
21
22 {:ok, activity} =
23 CommonAPI.post(har, %{
24 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
25 "visibility" => "direct"
26 })
27
28 refute tridi.ap_id in activity.recipients
29 assert jafnhar.ap_id in activity.recipients
30 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
31 end
32
33 test "it de-duplicates tags" do
34 user = insert(:user)
35 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
36
37 object = Object.normalize(activity)
38
39 assert object.data["tag"] == ["2hu"]
40 end
41
42 test "it adds emoji in the object" do
43 user = insert(:user)
44 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
45
46 assert Object.normalize(activity).data["emoji"]["firefox"]
47 end
48
49 test "it adds emoji when updating profiles" do
50 user = insert(:user, %{name: ":firefox:"})
51
52 CommonAPI.update(user)
53 user = User.get_cached_by_ap_id(user.ap_id)
54 [firefox] = user.info.source_data["tag"]
55
56 assert firefox["name"] == ":firefox:"
57 end
58
59 describe "posting" do
60 test "it supports explicit addressing" do
61 user = insert(:user)
62 user_two = insert(:user)
63 user_three = insert(:user)
64 user_four = insert(:user)
65
66 {:ok, activity} =
67 CommonAPI.post(user, %{
68 "status" =>
69 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
70 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
71 })
72
73 assert user.ap_id in activity.recipients
74 assert user_two.ap_id in activity.recipients
75 assert user_four.ap_id in activity.recipients
76 refute user_three.ap_id in activity.recipients
77 end
78
79 test "it filters out obviously bad tags when accepting a post as HTML" do
80 user = insert(:user)
81
82 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
83
84 {:ok, activity} =
85 CommonAPI.post(user, %{
86 "status" => post,
87 "content_type" => "text/html"
88 })
89
90 object = Object.normalize(activity)
91
92 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
93 end
94
95 test "it filters out obviously bad tags when accepting a post as Markdown" do
96 user = insert(:user)
97
98 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
99
100 {:ok, activity} =
101 CommonAPI.post(user, %{
102 "status" => post,
103 "content_type" => "text/markdown"
104 })
105
106 object = Object.normalize(activity)
107
108 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
109 end
110
111 test "it does not allow replies to direct messages that are not direct messages themselves" do
112 user = insert(:user)
113
114 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
115
116 assert {:ok, _} =
117 CommonAPI.post(user, %{
118 "status" => "suya..",
119 "visibility" => "direct",
120 "in_reply_to_status_id" => activity.id
121 })
122
123 Enum.each(["public", "private", "unlisted"], fn visibility ->
124 assert {:error, "The message visibility must be direct"} =
125 CommonAPI.post(user, %{
126 "status" => "suya..",
127 "visibility" => visibility,
128 "in_reply_to_status_id" => activity.id
129 })
130 end)
131 end
132 end
133
134 describe "reactions" do
135 test "repeating a status" do
136 user = insert(:user)
137 other_user = insert(:user)
138
139 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
140
141 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
142 end
143
144 test "favoriting a status" do
145 user = insert(:user)
146 other_user = insert(:user)
147
148 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
149
150 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
151 end
152
153 test "retweeting a status twice returns an error" do
154 user = insert(:user)
155 other_user = insert(:user)
156
157 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
158 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
159 {:error, _} = CommonAPI.repeat(activity.id, user)
160 end
161
162 test "favoriting a status twice returns an error" do
163 user = insert(:user)
164 other_user = insert(:user)
165
166 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
167 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
168 {:error, _} = CommonAPI.favorite(activity.id, user)
169 end
170 end
171
172 describe "pinned statuses" do
173 setup do
174 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
175
176 user = insert(:user)
177 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
178
179 [user: user, activity: activity]
180 end
181
182 test "pin status", %{user: user, activity: activity} do
183 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
184
185 id = activity.id
186 user = refresh_record(user)
187
188 assert %User{info: %{pinned_activities: [^id]}} = user
189 end
190
191 test "unlisted statuses can be pinned", %{user: user} do
192 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
193 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
194 end
195
196 test "only self-authored can be pinned", %{activity: activity} do
197 user = insert(:user)
198
199 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
200 end
201
202 test "max pinned statuses", %{user: user, activity: activity_one} do
203 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
204
205 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
206
207 user = refresh_record(user)
208
209 assert {:error, "You have already pinned the maximum number of statuses"} =
210 CommonAPI.pin(activity_two.id, user)
211 end
212
213 test "unpin status", %{user: user, activity: activity} do
214 {:ok, activity} = CommonAPI.pin(activity.id, user)
215
216 user = refresh_record(user)
217
218 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
219
220 user = refresh_record(user)
221
222 assert %User{info: %{pinned_activities: []}} = user
223 end
224
225 test "should unpin when deleting a status", %{user: user, activity: activity} do
226 {:ok, activity} = CommonAPI.pin(activity.id, user)
227
228 user = refresh_record(user)
229
230 assert {:ok, _} = CommonAPI.delete(activity.id, user)
231
232 user = refresh_record(user)
233
234 assert %User{info: %{pinned_activities: []}} = user
235 end
236 end
237
238 describe "mute tests" do
239 setup do
240 user = insert(:user)
241
242 activity = insert(:note_activity)
243
244 [user: user, activity: activity]
245 end
246
247 test "add mute", %{user: user, activity: activity} do
248 {:ok, _} = CommonAPI.add_mute(user, activity)
249 assert CommonAPI.thread_muted?(user, activity)
250 end
251
252 test "remove mute", %{user: user, activity: activity} do
253 CommonAPI.add_mute(user, activity)
254 {:ok, _} = CommonAPI.remove_mute(user, activity)
255 refute CommonAPI.thread_muted?(user, activity)
256 end
257
258 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
259 CommonAPI.add_mute(user, activity)
260 {:error, _} = CommonAPI.add_mute(user, activity)
261 end
262 end
263
264 describe "reports" do
265 test "creates a report" do
266 reporter = insert(:user)
267 target_user = insert(:user)
268
269 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
270
271 reporter_ap_id = reporter.ap_id
272 target_ap_id = target_user.ap_id
273 activity_ap_id = activity.data["id"]
274 comment = "foobar"
275
276 report_data = %{
277 "account_id" => target_user.id,
278 "comment" => comment,
279 "status_ids" => [activity.id]
280 }
281
282 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
283
284 assert %Activity{
285 actor: ^reporter_ap_id,
286 data: %{
287 "type" => "Flag",
288 "content" => ^comment,
289 "object" => [^target_ap_id, ^activity_ap_id],
290 "state" => "open"
291 }
292 } = flag_activity
293 end
294
295 test "updates report state" do
296 [reporter, target_user] = insert_pair(:user)
297 activity = insert(:note_activity, user: target_user)
298
299 {:ok, %Activity{id: report_id}} =
300 CommonAPI.report(reporter, %{
301 "account_id" => target_user.id,
302 "comment" => "I feel offended",
303 "status_ids" => [activity.id]
304 })
305
306 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
307
308 assert report.data["state"] == "resolved"
309 end
310
311 test "does not update report state when state is unsupported" do
312 [reporter, target_user] = insert_pair(:user)
313 activity = insert(:note_activity, user: target_user)
314
315 {:ok, %Activity{id: report_id}} =
316 CommonAPI.report(reporter, %{
317 "account_id" => target_user.id,
318 "comment" => "I feel offended",
319 "status_ids" => [activity.id]
320 })
321
322 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
323 end
324 end
325
326 describe "reblog muting" do
327 setup do
328 muter = insert(:user)
329
330 muted = insert(:user)
331
332 [muter: muter, muted: muted]
333 end
334
335 test "add a reblog mute", %{muter: muter, muted: muted} do
336 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
337
338 assert User.showing_reblogs?(muter, muted) == false
339 end
340
341 test "remove a reblog mute", %{muter: muter, muted: muted} do
342 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
343 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
344
345 assert User.showing_reblogs?(muter, muted) == true
346 end
347 end
348
349 describe "accept_follow_request/2" do
350 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
351 user = insert(:user, info: %{locked: true})
352 follower = insert(:user)
353 follower_two = insert(:user)
354
355 {:ok, follow_activity} = ActivityPub.follow(follower, user)
356 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
357 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
358
359 assert follow_activity.data["state"] == "pending"
360 assert follow_activity_two.data["state"] == "pending"
361 assert follow_activity_three.data["state"] == "pending"
362
363 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
364
365 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
366 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
367 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
368 end
369
370 test "after rejection, it sets all existing pending follow request states to 'reject'" do
371 user = insert(:user, info: %{locked: true})
372 follower = insert(:user)
373 follower_two = insert(:user)
374
375 {:ok, follow_activity} = ActivityPub.follow(follower, user)
376 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
377 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
378
379 assert follow_activity.data["state"] == "pending"
380 assert follow_activity_two.data["state"] == "pending"
381 assert follow_activity_three.data["state"] == "pending"
382
383 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
384
385 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
386 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
387 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
388 end
389 end
390 end