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