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