Merge branch 'develop' into 'fix/reverse-proxy-body-too-large'
[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.Conversation.Participation
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Visibility
13 alias Pleroma.Web.CommonAPI
14
15 import Pleroma.Factory
16
17 clear_config([:instance, :safe_dm_mentions])
18 clear_config([:instance, :limit])
19 clear_config([:instance, :max_pinned_statuses])
20
21 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
22 user = insert(:user)
23 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
24
25 [participation] = Participation.for_user(user)
26
27 {:ok, convo_reply} =
28 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
29
30 assert Visibility.is_direct?(convo_reply)
31
32 assert activity.data["context"] == convo_reply.data["context"]
33 end
34
35 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
36 har = insert(:user)
37 jafnhar = insert(:user)
38 tridi = insert(:user)
39
40 {:ok, activity} =
41 CommonAPI.post(har, %{
42 "status" => "@#{jafnhar.nickname} hey",
43 "visibility" => "direct"
44 })
45
46 assert har.ap_id in activity.recipients
47 assert jafnhar.ap_id in activity.recipients
48
49 [participation] = Participation.for_user(har)
50
51 {:ok, activity} =
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
57 })
58
59 assert har.ap_id in activity.recipients
60 assert jafnhar.ap_id in activity.recipients
61 refute tridi.ap_id in activity.recipients
62 end
63
64 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
65 har = insert(:user)
66 jafnhar = insert(:user)
67 tridi = insert(:user)
68 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
69
70 {:ok, activity} =
71 CommonAPI.post(har, %{
72 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
73 "visibility" => "direct"
74 })
75
76 refute tridi.ap_id in activity.recipients
77 assert jafnhar.ap_id in activity.recipients
78 end
79
80 test "it de-duplicates tags" do
81 user = insert(:user)
82 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
83
84 object = Object.normalize(activity)
85
86 assert object.data["tag"] == ["2hu"]
87 end
88
89 test "it adds emoji in the object" do
90 user = insert(:user)
91 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
92
93 assert Object.normalize(activity).data["emoji"]["firefox"]
94 end
95
96 test "it adds emoji when updating profiles" do
97 user = insert(:user, %{name: ":firefox:"})
98
99 CommonAPI.update(user)
100 user = User.get_cached_by_ap_id(user.ap_id)
101 [firefox] = user.info.source_data["tag"]
102
103 assert firefox["name"] == ":firefox:"
104 end
105
106 describe "posting" do
107 test "it supports explicit addressing" do
108 user = insert(:user)
109 user_two = insert(:user)
110 user_three = insert(:user)
111 user_four = insert(:user)
112
113 {:ok, activity} =
114 CommonAPI.post(user, %{
115 "status" =>
116 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
117 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
118 })
119
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
124 end
125
126 test "it filters out obviously bad tags when accepting a post as HTML" do
127 user = insert(:user)
128
129 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
130
131 {:ok, activity} =
132 CommonAPI.post(user, %{
133 "status" => post,
134 "content_type" => "text/html"
135 })
136
137 object = Object.normalize(activity)
138
139 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
140 end
141
142 test "it filters out obviously bad tags when accepting a post as Markdown" do
143 user = insert(:user)
144
145 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
146
147 {:ok, activity} =
148 CommonAPI.post(user, %{
149 "status" => post,
150 "content_type" => "text/markdown"
151 })
152
153 object = Object.normalize(activity)
154
155 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
156 end
157
158 test "it does not allow replies to direct messages that are not direct messages themselves" do
159 user = insert(:user)
160
161 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
162
163 assert {:ok, _} =
164 CommonAPI.post(user, %{
165 "status" => "suya..",
166 "visibility" => "direct",
167 "in_reply_to_status_id" => activity.id
168 })
169
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
176 })
177 end)
178 end
179
180 test "it allows to address a list" do
181 user = insert(:user)
182 {:ok, list} = Pleroma.List.create("foo", user)
183
184 {:ok, activity} =
185 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
186
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
190 end
191
192 test "it returns error when status is empty and no attachments" do
193 user = insert(:user)
194
195 assert {:error, "Cannot post an empty status without attachments"} =
196 CommonAPI.post(user, %{"status" => ""})
197 end
198
199 test "it returns error when character limit is exceeded" do
200 Pleroma.Config.put([:instance, :limit], 5)
201
202 user = insert(:user)
203
204 assert {:error, "The status is over the character limit"} =
205 CommonAPI.post(user, %{"status" => "foobar"})
206 end
207 end
208
209 describe "reactions" do
210 test "repeating a status" do
211 user = insert(:user)
212 other_user = insert(:user)
213
214 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
215
216 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
217 end
218
219 test "favoriting a status" do
220 user = insert(:user)
221 other_user = insert(:user)
222
223 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
224
225 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
226 end
227
228 test "retweeting a status twice returns an error" do
229 user = insert(:user)
230 other_user = insert(:user)
231
232 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
233 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
234 {:error, _} = CommonAPI.repeat(activity.id, user)
235 end
236
237 test "favoriting a status twice returns an error" do
238 user = insert(:user)
239 other_user = insert(:user)
240
241 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
242 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
243 {:error, _} = CommonAPI.favorite(activity.id, user)
244 end
245 end
246
247 describe "pinned statuses" do
248 setup do
249 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
250
251 user = insert(:user)
252 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
253
254 [user: user, activity: activity]
255 end
256
257 test "pin status", %{user: user, activity: activity} do
258 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
259
260 id = activity.id
261 user = refresh_record(user)
262
263 assert %User{info: %{pinned_activities: [^id]}} = user
264 end
265
266 test "unlisted statuses can be pinned", %{user: user} do
267 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
268 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
269 end
270
271 test "only self-authored can be pinned", %{activity: activity} do
272 user = insert(:user)
273
274 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
275 end
276
277 test "max pinned statuses", %{user: user, activity: activity_one} do
278 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
279
280 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
281
282 user = refresh_record(user)
283
284 assert {:error, "You have already pinned the maximum number of statuses"} =
285 CommonAPI.pin(activity_two.id, user)
286 end
287
288 test "unpin status", %{user: user, activity: activity} do
289 {:ok, activity} = CommonAPI.pin(activity.id, user)
290
291 user = refresh_record(user)
292
293 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
294
295 user = refresh_record(user)
296
297 assert %User{info: %{pinned_activities: []}} = user
298 end
299
300 test "should unpin when deleting a status", %{user: user, activity: activity} do
301 {:ok, activity} = CommonAPI.pin(activity.id, user)
302
303 user = refresh_record(user)
304
305 assert {:ok, _} = CommonAPI.delete(activity.id, user)
306
307 user = refresh_record(user)
308
309 assert %User{info: %{pinned_activities: []}} = user
310 end
311 end
312
313 describe "mute tests" do
314 setup do
315 user = insert(:user)
316
317 activity = insert(:note_activity)
318
319 [user: user, activity: activity]
320 end
321
322 test "add mute", %{user: user, activity: activity} do
323 {:ok, _} = CommonAPI.add_mute(user, activity)
324 assert CommonAPI.thread_muted?(user, activity)
325 end
326
327 test "remove mute", %{user: user, activity: activity} do
328 CommonAPI.add_mute(user, activity)
329 {:ok, _} = CommonAPI.remove_mute(user, activity)
330 refute CommonAPI.thread_muted?(user, activity)
331 end
332
333 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
334 CommonAPI.add_mute(user, activity)
335 {:error, _} = CommonAPI.add_mute(user, activity)
336 end
337 end
338
339 describe "reports" do
340 test "creates a report" do
341 reporter = insert(:user)
342 target_user = insert(:user)
343
344 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
345
346 reporter_ap_id = reporter.ap_id
347 target_ap_id = target_user.ap_id
348 activity_ap_id = activity.data["id"]
349 comment = "foobar"
350
351 report_data = %{
352 "account_id" => target_user.id,
353 "comment" => comment,
354 "status_ids" => [activity.id]
355 }
356
357 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
358
359 assert %Activity{
360 actor: ^reporter_ap_id,
361 data: %{
362 "type" => "Flag",
363 "content" => ^comment,
364 "object" => [^target_ap_id, ^activity_ap_id],
365 "state" => "open"
366 }
367 } = flag_activity
368 end
369
370 test "updates report state" do
371 [reporter, target_user] = insert_pair(:user)
372 activity = insert(:note_activity, user: target_user)
373
374 {:ok, %Activity{id: report_id}} =
375 CommonAPI.report(reporter, %{
376 "account_id" => target_user.id,
377 "comment" => "I feel offended",
378 "status_ids" => [activity.id]
379 })
380
381 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
382
383 assert report.data["state"] == "resolved"
384 end
385
386 test "does not update report state when state is unsupported" do
387 [reporter, target_user] = insert_pair(:user)
388 activity = insert(:note_activity, user: target_user)
389
390 {:ok, %Activity{id: report_id}} =
391 CommonAPI.report(reporter, %{
392 "account_id" => target_user.id,
393 "comment" => "I feel offended",
394 "status_ids" => [activity.id]
395 })
396
397 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
398 end
399 end
400
401 describe "reblog muting" do
402 setup do
403 muter = insert(:user)
404
405 muted = insert(:user)
406
407 [muter: muter, muted: muted]
408 end
409
410 test "add a reblog mute", %{muter: muter, muted: muted} do
411 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
412
413 assert User.showing_reblogs?(muter, muted) == false
414 end
415
416 test "remove a reblog mute", %{muter: muter, muted: muted} do
417 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
418 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
419
420 assert User.showing_reblogs?(muter, muted) == true
421 end
422 end
423
424 describe "unfollow/2" do
425 test "also unsubscribes a user" do
426 [follower, followed] = insert_pair(:user)
427 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
428 {:ok, followed} = User.subscribe(follower, followed)
429
430 assert User.subscribed_to?(follower, followed)
431
432 {:ok, follower} = CommonAPI.unfollow(follower, followed)
433
434 refute User.subscribed_to?(follower, followed)
435 end
436 end
437
438 describe "accept_follow_request/2" do
439 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
440 user = insert(:user, info: %{locked: true})
441 follower = insert(:user)
442 follower_two = insert(:user)
443
444 {:ok, follow_activity} = ActivityPub.follow(follower, user)
445 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
446 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
447
448 assert follow_activity.data["state"] == "pending"
449 assert follow_activity_two.data["state"] == "pending"
450 assert follow_activity_three.data["state"] == "pending"
451
452 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
453
454 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
455 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
456 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
457 end
458
459 test "after rejection, it sets all existing pending follow request states to 'reject'" do
460 user = insert(:user, info: %{locked: true})
461 follower = insert(:user)
462 follower_two = insert(:user)
463
464 {:ok, follow_activity} = ActivityPub.follow(follower, user)
465 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
466 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
467
468 assert follow_activity.data["state"] == "pending"
469 assert follow_activity_two.data["state"] == "pending"
470 assert follow_activity_three.data["state"] == "pending"
471
472 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
473
474 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
475 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
476 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
477 end
478 end
479
480 describe "vote/3" do
481 test "does not allow to vote twice" do
482 user = insert(:user)
483 other_user = insert(:user)
484
485 {:ok, activity} =
486 CommonAPI.post(user, %{
487 "status" => "Am I cute?",
488 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
489 })
490
491 object = Object.normalize(activity)
492
493 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
494
495 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
496 end
497 end
498 end