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