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