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