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