Merge branch 'fix/no-email-no-fail' 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 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
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 returns error when character limit is exceeded" 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 end
213
214 test "it can handle activities that expire" do
215 user = insert(:user)
216
217 expires_at =
218 NaiveDateTime.utc_now()
219 |> NaiveDateTime.truncate(:second)
220 |> NaiveDateTime.add(1_000_000, :second)
221
222 assert {:ok, activity} =
223 CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
224
225 assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
226 assert expiration.scheduled_at == expires_at
227 end
228 end
229
230 describe "reactions" do
231 test "reacting to a status with an emoji" do
232 user = insert(:user)
233 other_user = insert(:user)
234
235 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
236
237 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
238
239 assert reaction.data["actor"] == user.ap_id
240 assert reaction.data["content"] == "👍"
241
242 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
243
244 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
245 end
246
247 test "unreacting to a status with an emoji" do
248 user = insert(:user)
249 other_user = insert(:user)
250
251 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
252 {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
253
254 {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
255
256 assert unreaction.data["type"] == "Undo"
257 assert unreaction.data["object"] == reaction.data["id"]
258 end
259
260 test "repeating a status" do
261 user = insert(:user)
262 other_user = insert(:user)
263
264 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
265
266 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
267 end
268
269 test "repeating a status privately" do
270 user = insert(:user)
271 other_user = insert(:user)
272
273 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
274
275 {:ok, %Activity{} = announce_activity, _} =
276 CommonAPI.repeat(activity.id, user, %{"visibility" => "private"})
277
278 assert Visibility.is_private?(announce_activity)
279 end
280
281 test "favoriting a status" do
282 user = insert(:user)
283 other_user = insert(:user)
284
285 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
286
287 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
288 end
289
290 test "retweeting a status twice returns the status" do
291 user = insert(:user)
292 other_user = insert(:user)
293
294 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
295 {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
296 {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
297 end
298
299 test "favoriting a status twice returns the status" do
300 user = insert(:user)
301 other_user = insert(:user)
302
303 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
304 {:ok, %Activity{} = activity, object} = CommonAPI.favorite(activity.id, user)
305 {:ok, ^activity, ^object} = CommonAPI.favorite(activity.id, user)
306 end
307 end
308
309 describe "pinned statuses" do
310 setup do
311 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
312
313 user = insert(:user)
314 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
315
316 [user: user, activity: activity]
317 end
318
319 test "pin status", %{user: user, activity: activity} do
320 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
321
322 id = activity.id
323 user = refresh_record(user)
324
325 assert %User{pinned_activities: [^id]} = user
326 end
327
328 test "pin poll", %{user: user} do
329 {:ok, activity} =
330 CommonAPI.post(user, %{
331 "status" => "How is fediverse today?",
332 "poll" => %{"options" => ["Absolutely outstanding", "Not good"], "expires_in" => 20}
333 })
334
335 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
336
337 id = activity.id
338 user = refresh_record(user)
339
340 assert %User{pinned_activities: [^id]} = user
341 end
342
343 test "unlisted statuses can be pinned", %{user: user} do
344 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!", "visibility" => "unlisted"})
345 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
346 end
347
348 test "only self-authored can be pinned", %{activity: activity} do
349 user = insert(:user)
350
351 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
352 end
353
354 test "max pinned statuses", %{user: user, activity: activity_one} do
355 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
356
357 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
358
359 user = refresh_record(user)
360
361 assert {:error, "You have already pinned the maximum number of statuses"} =
362 CommonAPI.pin(activity_two.id, user)
363 end
364
365 test "unpin status", %{user: user, activity: activity} do
366 {:ok, activity} = CommonAPI.pin(activity.id, user)
367
368 user = refresh_record(user)
369
370 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
371
372 user = refresh_record(user)
373
374 assert %User{pinned_activities: []} = user
375 end
376
377 test "should unpin when deleting a status", %{user: user, activity: activity} do
378 {:ok, activity} = CommonAPI.pin(activity.id, user)
379
380 user = refresh_record(user)
381
382 assert {:ok, _} = CommonAPI.delete(activity.id, user)
383
384 user = refresh_record(user)
385
386 assert %User{pinned_activities: []} = user
387 end
388 end
389
390 describe "mute tests" do
391 setup do
392 user = insert(:user)
393
394 activity = insert(:note_activity)
395
396 [user: user, activity: activity]
397 end
398
399 test "add mute", %{user: user, activity: activity} do
400 {:ok, _} = CommonAPI.add_mute(user, activity)
401 assert CommonAPI.thread_muted?(user, activity)
402 end
403
404 test "remove mute", %{user: user, activity: activity} do
405 CommonAPI.add_mute(user, activity)
406 {:ok, _} = CommonAPI.remove_mute(user, activity)
407 refute CommonAPI.thread_muted?(user, activity)
408 end
409
410 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
411 CommonAPI.add_mute(user, activity)
412 {:error, _} = CommonAPI.add_mute(user, activity)
413 end
414 end
415
416 describe "reports" do
417 test "creates a report" do
418 reporter = insert(:user)
419 target_user = insert(:user)
420
421 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
422
423 reporter_ap_id = reporter.ap_id
424 target_ap_id = target_user.ap_id
425 activity_ap_id = activity.data["id"]
426 comment = "foobar"
427
428 report_data = %{
429 "account_id" => target_user.id,
430 "comment" => comment,
431 "status_ids" => [activity.id]
432 }
433
434 note_obj = %{
435 "type" => "Note",
436 "id" => activity_ap_id,
437 "content" => "foobar",
438 "published" => activity.object.data["published"],
439 "actor" => AccountView.render("show.json", %{user: target_user})
440 }
441
442 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
443
444 assert %Activity{
445 actor: ^reporter_ap_id,
446 data: %{
447 "type" => "Flag",
448 "content" => ^comment,
449 "object" => [^target_ap_id, ^note_obj],
450 "state" => "open"
451 }
452 } = flag_activity
453 end
454
455 test "updates report state" do
456 [reporter, target_user] = insert_pair(:user)
457 activity = insert(:note_activity, user: target_user)
458
459 {:ok, %Activity{id: report_id}} =
460 CommonAPI.report(reporter, %{
461 "account_id" => target_user.id,
462 "comment" => "I feel offended",
463 "status_ids" => [activity.id]
464 })
465
466 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
467
468 assert report.data["state"] == "resolved"
469
470 [reported_user, activity_id] = report.data["object"]
471
472 assert reported_user == target_user.ap_id
473 assert activity_id == activity.data["id"]
474 end
475
476 test "does not update report state when state is unsupported" do
477 [reporter, target_user] = insert_pair(:user)
478 activity = insert(:note_activity, user: target_user)
479
480 {:ok, %Activity{id: report_id}} =
481 CommonAPI.report(reporter, %{
482 "account_id" => target_user.id,
483 "comment" => "I feel offended",
484 "status_ids" => [activity.id]
485 })
486
487 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
488 end
489
490 test "updates state of multiple reports" do
491 [reporter, target_user] = insert_pair(:user)
492 activity = insert(:note_activity, user: target_user)
493
494 {:ok, %Activity{id: first_report_id}} =
495 CommonAPI.report(reporter, %{
496 "account_id" => target_user.id,
497 "comment" => "I feel offended",
498 "status_ids" => [activity.id]
499 })
500
501 {:ok, %Activity{id: second_report_id}} =
502 CommonAPI.report(reporter, %{
503 "account_id" => target_user.id,
504 "comment" => "I feel very offended!",
505 "status_ids" => [activity.id]
506 })
507
508 {:ok, report_ids} =
509 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
510
511 first_report = Activity.get_by_id(first_report_id)
512 second_report = Activity.get_by_id(second_report_id)
513
514 assert report_ids -- [first_report_id, second_report_id] == []
515 assert first_report.data["state"] == "resolved"
516 assert second_report.data["state"] == "resolved"
517 end
518 end
519
520 describe "reblog muting" do
521 setup do
522 muter = insert(:user)
523
524 muted = insert(:user)
525
526 [muter: muter, muted: muted]
527 end
528
529 test "add a reblog mute", %{muter: muter, muted: muted} do
530 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
531
532 assert User.showing_reblogs?(muter, muted) == false
533 end
534
535 test "remove a reblog mute", %{muter: muter, muted: muted} do
536 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
537 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
538
539 assert User.showing_reblogs?(muter, muted) == true
540 end
541 end
542
543 describe "unfollow/2" do
544 test "also unsubscribes a user" do
545 [follower, followed] = insert_pair(:user)
546 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
547 {:ok, _subscription} = User.subscribe(follower, followed)
548
549 assert User.subscribed_to?(follower, followed)
550
551 {:ok, follower} = CommonAPI.unfollow(follower, followed)
552
553 refute User.subscribed_to?(follower, followed)
554 end
555
556 test "cancels a pending follow for a local user" do
557 follower = insert(:user)
558 followed = insert(:user, locked: true)
559
560 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
561 CommonAPI.follow(follower, followed)
562
563 assert User.get_follow_state(follower, followed) == "pending"
564 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
565 assert User.get_follow_state(follower, followed) == nil
566
567 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
568 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
569
570 assert %{
571 data: %{
572 "type" => "Undo",
573 "object" => %{"type" => "Follow", "state" => "cancelled"}
574 }
575 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
576 end
577
578 test "cancels a pending follow for a remote user" do
579 follower = insert(:user)
580 followed = insert(:user, locked: true, local: false, ap_enabled: true)
581
582 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
583 CommonAPI.follow(follower, followed)
584
585 assert User.get_follow_state(follower, followed) == "pending"
586 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
587 assert User.get_follow_state(follower, followed) == nil
588
589 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
590 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
591
592 assert %{
593 data: %{
594 "type" => "Undo",
595 "object" => %{"type" => "Follow", "state" => "cancelled"}
596 }
597 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
598 end
599 end
600
601 describe "accept_follow_request/2" do
602 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
603 user = insert(:user, locked: true)
604 follower = insert(:user)
605 follower_two = insert(:user)
606
607 {:ok, follow_activity} = ActivityPub.follow(follower, user)
608 {:ok, follow_activity_two} = ActivityPub.follow(follower, user)
609 {:ok, follow_activity_three} = ActivityPub.follow(follower_two, user)
610
611 assert follow_activity.data["state"] == "pending"
612 assert follow_activity_two.data["state"] == "pending"
613 assert follow_activity_three.data["state"] == "pending"
614
615 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
616
617 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
618 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
619 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
620 end
621
622 test "after rejection, it sets all existing pending follow request states to 'reject'" 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.reject_follow_request(follower, user)
636
637 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
638 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
639 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
640 end
641 end
642
643 describe "vote/3" do
644 test "does not allow to vote twice" do
645 user = insert(:user)
646 other_user = insert(:user)
647
648 {:ok, activity} =
649 CommonAPI.post(user, %{
650 "status" => "Am I cute?",
651 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
652 })
653
654 object = Object.normalize(activity)
655
656 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
657
658 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
659 end
660 end
661
662 describe "listen/2" do
663 test "returns a valid activity" do
664 user = insert(:user)
665
666 {:ok, activity} =
667 CommonAPI.listen(user, %{
668 "title" => "lain radio episode 1",
669 "album" => "lain radio",
670 "artist" => "lain",
671 "length" => 180_000
672 })
673
674 object = Object.normalize(activity)
675
676 assert object.data["title"] == "lain radio episode 1"
677
678 assert Visibility.get_visibility(activity) == "public"
679 end
680
681 test "respects visibility=private" do
682 user = insert(:user)
683
684 {:ok, activity} =
685 CommonAPI.listen(user, %{
686 "title" => "lain radio episode 1",
687 "album" => "lain radio",
688 "artist" => "lain",
689 "length" => 180_000,
690 "visibility" => "private"
691 })
692
693 object = Object.normalize(activity)
694
695 assert object.data["title"] == "lain radio episode 1"
696
697 assert Visibility.get_visibility(activity) == "private"
698 end
699 end
700 end