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