Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / common_api / common_api_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.AdminAPI.AccountView
14 alias Pleroma.Web.CommonAPI
15
16 import Pleroma.Factory
17
18 require Pleroma.Constants
19
20 setup do: clear_config([:instance, :safe_dm_mentions])
21 setup do: clear_config([:instance, :limit])
22 setup do: clear_config([:instance, :max_pinned_statuses])
23
24 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
25 user = insert(:user)
26 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
27
28 [participation] = Participation.for_user(user)
29
30 {:ok, convo_reply} =
31 CommonAPI.post(user, %{"status" => ".", "in_reply_to_conversation_id" => participation.id})
32
33 assert Visibility.is_direct?(convo_reply)
34
35 assert activity.data["context"] == convo_reply.data["context"]
36 end
37
38 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
39 har = insert(:user)
40 jafnhar = insert(:user)
41 tridi = insert(:user)
42
43 {:ok, activity} =
44 CommonAPI.post(har, %{
45 "status" => "@#{jafnhar.nickname} hey",
46 "visibility" => "direct"
47 })
48
49 assert har.ap_id in activity.recipients
50 assert jafnhar.ap_id in activity.recipients
51
52 [participation] = Participation.for_user(har)
53
54 {:ok, activity} =
55 CommonAPI.post(har, %{
56 "status" => "I don't really like @#{tridi.nickname}",
57 "visibility" => "direct",
58 "in_reply_to_status_id" => activity.id,
59 "in_reply_to_conversation_id" => participation.id
60 })
61
62 assert har.ap_id in activity.recipients
63 assert jafnhar.ap_id in activity.recipients
64 refute tridi.ap_id in activity.recipients
65 end
66
67 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
68 har = insert(:user)
69 jafnhar = insert(:user)
70 tridi = insert(:user)
71
72 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
73
74 {:ok, activity} =
75 CommonAPI.post(har, %{
76 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
77 "visibility" => "direct"
78 })
79
80 refute tridi.ap_id in activity.recipients
81 assert jafnhar.ap_id in activity.recipients
82 end
83
84 test "it de-duplicates tags" do
85 user = insert(:user)
86 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
87
88 object = Object.normalize(activity)
89
90 assert object.data["tag"] == ["2hu"]
91 end
92
93 test "it adds emoji in the object" do
94 user = insert(:user)
95 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
96
97 assert Object.normalize(activity).data["emoji"]["firefox"]
98 end
99
100 describe "posting" do
101 test "it supports explicit addressing" do
102 user = insert(:user)
103 user_two = insert(:user)
104 user_three = insert(:user)
105 user_four = insert(:user)
106
107 {:ok, activity} =
108 CommonAPI.post(user, %{
109 "status" =>
110 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
111 "to" => [user_two.nickname, user_four.nickname, "nonexistent"]
112 })
113
114 assert user.ap_id in activity.recipients
115 assert user_two.ap_id in activity.recipients
116 assert user_four.ap_id in activity.recipients
117 refute user_three.ap_id in activity.recipients
118 end
119
120 test "it filters out obviously bad tags when accepting a post as HTML" do
121 user = insert(:user)
122
123 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
124
125 {:ok, activity} =
126 CommonAPI.post(user, %{
127 "status" => post,
128 "content_type" => "text/html"
129 })
130
131 object = Object.normalize(activity)
132
133 assert object.data["content"] == "<p><b>2hu</b></p>alert(&#39;xss&#39;)"
134 end
135
136 test "it filters out obviously bad tags when accepting a post as Markdown" do
137 user = insert(:user)
138
139 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
140
141 {:ok, activity} =
142 CommonAPI.post(user, %{
143 "status" => post,
144 "content_type" => "text/markdown"
145 })
146
147 object = Object.normalize(activity)
148
149 assert object.data["content"] == "<p><b>2hu</b></p>alert(&#39;xss&#39;)"
150 end
151
152 test "it does not allow replies to direct messages that are not direct messages themselves" do
153 user = insert(:user)
154
155 {:ok, activity} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
156
157 assert {:ok, _} =
158 CommonAPI.post(user, %{
159 "status" => "suya..",
160 "visibility" => "direct",
161 "in_reply_to_status_id" => activity.id
162 })
163
164 Enum.each(["public", "private", "unlisted"], fn visibility ->
165 assert {:error, "The message visibility must be direct"} =
166 CommonAPI.post(user, %{
167 "status" => "suya..",
168 "visibility" => visibility,
169 "in_reply_to_status_id" => activity.id
170 })
171 end)
172 end
173
174 test "it allows to address a list" do
175 user = insert(:user)
176 {:ok, list} = Pleroma.List.create("foo", user)
177
178 {:ok, activity} =
179 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
180
181 assert activity.data["bcc"] == [list.ap_id]
182 assert activity.recipients == [list.ap_id, user.ap_id]
183 assert activity.data["listMessage"] == list.ap_id
184 end
185
186 test "it returns error when status is empty and no attachments" do
187 user = insert(:user)
188
189 assert {:error, "Cannot post an empty status without attachments"} =
190 CommonAPI.post(user, %{"status" => ""})
191 end
192
193 test "it validates character limits are correctly enforced" do
194 Pleroma.Config.put([:instance, :limit], 5)
195
196 user = insert(:user)
197
198 assert {:error, "The status is over the character limit"} =
199 CommonAPI.post(user, %{"status" => "foobar"})
200
201 assert {:ok, activity} = CommonAPI.post(user, %{"status" => "12345"})
202 end
203
204 test "it can handle activities that expire" do
205 user = insert(:user)
206
207 expires_at =
208 NaiveDateTime.utc_now()
209 |> NaiveDateTime.truncate(:second)
210 |> NaiveDateTime.add(1_000_000, :second)
211
212 assert {:ok, activity} =
213 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
214
215 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
216 assert expiration.scheduled_at == expires_at
217 end
218 end
219
220 describe "reactions" do
221 test "reacting to a status with an emoji" do
222 user = insert(:user)
223 other_user = insert(:user)
224
225 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
226
227 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
228
229 assert reaction.data["actor"] == user.ap_id
230 assert reaction.data["content"] == "👍"
231
232 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
233
234 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
235 end
236
237 test "unreacting to a status with an emoji" do
238 user = insert(:user)
239 other_user = insert(:user)
240
241 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
242 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
243
244 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
245
246 assert unreaction.data["type"] == "Undo"
247 assert unreaction.data["object"] == reaction.data["id"]
248 end
249
250 test "repeating a status" do
251 user = insert(:user)
252 other_user = insert(:user)
253
254 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
255
256 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
257 end
258
259 test "repeating a status privately" do
260 user = insert(:user)
261 other_user = insert(:user)
262
263 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
264
265 {:ok, %Activity{} = announce_activity, _} =
266 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
267
268 assert Visibility.is_private?(announce_activity)
269 end
270
271 test "favoriting a status" do
272 user = insert(:user)
273 other_user = insert(:user)
274
275 {:ok, post_activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
276
277 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
278 assert data["type"] == "Like"
279 assert data["actor"] == user.ap_id
280 assert data["object"] == post_activity.data["object"]
281 end
282
283 test "retweeting a status twice returns the status" do
284 user = insert(:user)
285 other_user = insert(:user)
286
287 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
288 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
289 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
290 end
291
292 test "favoriting a status twice returns ok, but without the like activity" do
293 user = insert(:user)
294 other_user = insert(:user)
295
296 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
297 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
298 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
299 end
300 end
301
302 describe "pinned statuses" do
303 setup do
304 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
305
306 user = insert(:user)
307 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
308
309 [user: user, activity: activity]
310 end
311
312 test "pin status", %{user: user, activity: activity} do
313 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
314
315 id = activity.id
316 user = refresh_record(user)
317
318 assert %User{pinned_activities: [^id]} = user
319 end
320
321 test "pin poll", %{user: user} do
322 {:ok, activity} =
323 CommonAPI.post(user, %{
324 "status" => "How is fediverse today?",
325 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
326 })
327
328 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
329
330 id = activity.id
331 user = refresh_record(user)
332
333 assert %User{pinned_activities: [^id]} = user
334 end
335
336 test "unlisted statuses can be pinned", %{user: user} do
337 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
338 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
339 end
340
341 test "only self-authored can be pinned", %{activity: activity} do
342 user = insert(:user)
343
344 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
345 end
346
347 test "max pinned statuses", %{user: user, activity: activity_one} do
348 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
349
350 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
351
352 user = refresh_record(user)
353
354 assert {:error, "You have already pinned the maximum number of statuses"} =
355 CommonAPI.pin(activity_two.id, user)
356 end
357
358 test "unpin status", %{user: user, activity: activity} do
359 {:ok, activity} = CommonAPI.pin(activity.id, user)
360
361 user = refresh_record(user)
362
363 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
364
365 user = refresh_record(user)
366
367 assert %User{pinned_activities: []} = user
368 end
369
370 test "should unpin when deleting a status", %{user: user, activity: activity} do
371 {:ok, activity} = CommonAPI.pin(activity.id, user)
372
373 user = refresh_record(user)
374
375 assert {:ok, _} = CommonAPI.delete(activity.id, user)
376
377 user = refresh_record(user)
378
379 assert %User{pinned_activities: []} = user
380 end
381 end
382
383 describe "mute tests" do
384 setup do
385 user = insert(:user)
386
387 activity = insert(:note_activity)
388
389 [user: user, activity: activity]
390 end
391
392 test "add mute", %{user: user, activity: activity} do
393 {:ok, _} = CommonAPI.add_mute(user, activity)
394 assert CommonAPI.thread_muted?(user, activity)
395 end
396
397 test "remove mute", %{user: user, activity: activity} do
398 CommonAPI.add_mute(user, activity)
399 {:ok, _} = CommonAPI.remove_mute(user, activity)
400 refute CommonAPI.thread_muted?(user, activity)
401 end
402
403 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
404 CommonAPI.add_mute(user, activity)
405 {:error, _} = CommonAPI.add_mute(user, activity)
406 end
407 end
408
409 describe "reports" do
410 test "creates a report" do
411 reporter = insert(:user)
412 target_user = insert(:user)
413
414 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
415
416 reporter_ap_id = reporter.ap_id
417 target_ap_id = target_user.ap_id
418 activity_ap_id = activity.data["id"]
419 comment = "foobar"
420
421 report_data = %{
422 "account_id" => target_user.id,
423 "comment" => comment,
424 "status_ids" => [activity.id]
425 }
426
427 note_obj = %{
428 "type" => "Note",
429 "id" => activity_ap_id,
430 "content" => "foobar",
431 "published" => activity.object.data["published"],
432 "actor" => AccountView.render("show.json", %{user: target_user})
433 }
434
435 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
436
437 assert %Activity{
438 actor: ^reporter_ap_id,
439 data: %{
440 "type" => "Flag",
441 "content" => ^comment,
442 "object" => [^target_ap_id, ^note_obj],
443 "state" => "open"
444 }
445 } = flag_activity
446 end
447
448 test "updates report state" do
449 [reporter, target_user] = insert_pair(:user)
450 activity = insert(:note_activity, user: target_user)
451
452 {:ok, %Activity{id: report_id}} =
453 CommonAPI.report(reporter, %{
454 "account_id" => target_user.id,
455 "comment" => "I feel offended",
456 "status_ids" => [activity.id]
457 })
458
459 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
460
461 assert report.data["state"] == "resolved"
462
463 [reported_user, activity_id] = report.data["object"]
464
465 assert reported_user == target_user.ap_id
466 assert activity_id == activity.data["id"]
467 end
468
469 test "does not update report state when state is unsupported" do
470 [reporter, target_user] = insert_pair(:user)
471 activity = insert(:note_activity, user: target_user)
472
473 {:ok, %Activity{id: report_id}} =
474 CommonAPI.report(reporter, %{
475 "account_id" => target_user.id,
476 "comment" => "I feel offended",
477 "status_ids" => [activity.id]
478 })
479
480 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
481 end
482
483 test "updates state of multiple reports" do
484 [reporter, target_user] = insert_pair(:user)
485 activity = insert(:note_activity, user: target_user)
486
487 {:ok, %Activity{id: first_report_id}} =
488 CommonAPI.report(reporter, %{
489 "account_id" => target_user.id,
490 "comment" => "I feel offended",
491 "status_ids" => [activity.id]
492 })
493
494 {:ok, %Activity{id: second_report_id}} =
495 CommonAPI.report(reporter, %{
496 "account_id" => target_user.id,
497 "comment" => "I feel very offended!",
498 "status_ids" => [activity.id]
499 })
500
501 {:ok, report_ids} =
502 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
503
504 first_report = Activity.get_by_id(first_report_id)
505 second_report = Activity.get_by_id(second_report_id)
506
507 assert report_ids -- [first_report_id, second_report_id] == []
508 assert first_report.data["state"] == "resolved"
509 assert second_report.data["state"] == "resolved"
510 end
511 end
512
513 describe "reblog muting" do
514 setup do
515 muter = insert(:user)
516
517 muted = insert(:user)
518
519 [muter: muter, muted: muted]
520 end
521
522 test "add a reblog mute", %{muter: muter, muted: muted} do
523 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
524
525 assert User.showing_reblogs?(muter, muted) == false
526 end
527
528 test "remove a reblog mute", %{muter: muter, muted: muted} do
529 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
530 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
531
532 assert User.showing_reblogs?(muter, muted) == true
533 end
534 end
535
536 describe "unfollow/2" do
537 test "also unsubscribes a user" do
538 [follower, followed] = insert_pair(:user)
539 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
540 {:ok, _subscription} = User.subscribe(follower, followed)
541
542 assert User.subscribed_to?(follower, followed)
543
544 {:ok, follower} = CommonAPI.unfollow(follower, followed)
545
546 refute User.subscribed_to?(follower, followed)
547 end
548
549 test "cancels a pending follow for a local user" do
550 follower = insert(:user)
551 followed = insert(:user, locked: true)
552
553 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
554 CommonAPI.follow(follower, followed)
555
556 assert User.get_follow_state(follower, followed) == :follow_pending
557 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
558 assert User.get_follow_state(follower, followed) == nil
559
560 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
561 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
562
563 assert %{
564 data: %{
565 "type" => "Undo",
566 "object" => %{"type" => "Follow", "state" => "cancelled"}
567 }
568 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
569 end
570
571 test "cancels a pending follow for a remote user" do
572 follower = insert(:user)
573 followed = insert(:user, locked: true, local: false, ap_enabled: true)
574
575 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
576 CommonAPI.follow(follower, followed)
577
578 assert User.get_follow_state(follower, followed) == :follow_pending
579 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
580 assert User.get_follow_state(follower, followed) == nil
581
582 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
583 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
584
585 assert %{
586 data: %{
587 "type" => "Undo",
588 "object" => %{"type" => "Follow", "state" => "cancelled"}
589 }
590 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
591 end
592 end
593
594 describe "accept_follow_request/2" do
595 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
596 user = insert(:user, locked: true)
597 follower = insert(:user)
598 follower_two = insert(:user)
599
600 {:ok, follow_activity} = ActivityPub.follow(follower, user)
601 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
602 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
603
604 assert follow_activity.data["state"] == "pending"
605 assert follow_activity_two.data["state"] == "pending"
606 assert follow_activity_three.data["state"] == "pending"
607
608 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
609
610 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
611 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
612 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
613 end
614
615 test "after rejection, it sets all existing pending follow request states to 'reject'" do
616 user = insert(:user, locked: true)
617 follower = insert(:user)
618 follower_two = insert(:user)
619
620 {:ok, follow_activity} = ActivityPub.follow(follower, user)
621 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
622 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
623
624 assert follow_activity.data["state"] == "pending"
625 assert follow_activity_two.data["state"] == "pending"
626 assert follow_activity_three.data["state"] == "pending"
627
628 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
629
630 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
631 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
632 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
633 end
634 end
635
636 describe "vote/3" do
637 test "does not allow to vote twice" do
638 user = insert(:user)
639 other_user = insert(:user)
640
641 {:ok, activity} =
642 CommonAPI.post(user, %{
643 "status" => "Am I cute?",
644 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
645 })
646
647 object = Object.normalize(activity)
648
649 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
650
651 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
652 end
653 end
654
655 describe "listen/2" do
656 test "returns a valid activity" do
657 user = insert(:user)
658
659 {:ok, activity} =
660 CommonAPI.listen(user, %{
661 "title" => "lain radio episode 1",
662 "album" => "lain radio",
663 "artist" => "lain",
664 "length" => 180_000
665 })
666
667 object = Object.normalize(activity)
668
669 assert object.data["title"] == "lain radio episode 1"
670
671 assert Visibility.get_visibility(activity) == "public"
672 end
673
674 test "respects visibility=private" do
675 user = insert(:user)
676
677 {:ok, activity} =
678 CommonAPI.listen(user, %{
679 "title" => "lain radio episode 1",
680 "album" => "lain radio",
681 "artist" => "lain",
682 "length" => 180_000,
683 "visibility" => "private"
684 })
685
686 object = Object.normalize(activity)
687
688 assert object.data["title"] == "lain radio episode 1"
689
690 assert Visibility.get_visibility(activity) == "private"
691 end
692 end
693 end