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