1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.ActivityExpiration
11 alias Pleroma.Conversation.Participation
14 alias Pleroma.ScheduledActivity
15 alias Pleroma.Tests.ObanHelpers
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
22 clear_config([:instance, :federating])
23 clear_config([:instance, :allow_relay])
25 describe "posting statuses" do
31 |> assign(:user, user)
36 test "posting a status does not increment reblog_count when relaying", %{conn: conn} do
37 Pleroma.Config.put([:instance, :federating], true)
38 Pleroma.Config.get([:instance, :allow_relay], true)
43 |> assign(:user, user)
44 |> post("api/v1/statuses", %{
45 "content_type" => "text/plain",
46 "source" => "Pleroma FE",
47 "status" => "Hello world",
48 "visibility" => "public"
52 assert response["reblogs_count"] == 0
53 ObanHelpers.perform_all()
57 |> assign(:user, user)
58 |> get("api/v1/statuses/#{response["id"]}", %{})
61 assert response["reblogs_count"] == 0
64 test "posting a status", %{conn: conn} do
65 idempotency_key = "Pikachu rocks!"
69 |> put_req_header("idempotency-key", idempotency_key)
70 |> post("/api/v1/statuses", %{
72 "spoiler_text" => "2hu",
73 "sensitive" => "false"
76 {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
78 assert ttl > :timer.seconds(6 * 60 * 60 - 1)
80 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
81 json_response(conn_one, 200)
83 assert Activity.get_by_id(id)
87 |> put_req_header("idempotency-key", idempotency_key)
88 |> post("/api/v1/statuses", %{
90 "spoiler_text" => "2hu",
91 "sensitive" => "false"
94 assert %{"id" => second_id} = json_response(conn_two, 200)
95 assert id == second_id
99 |> post("/api/v1/statuses", %{
101 "spoiler_text" => "2hu",
102 "sensitive" => "false"
105 assert %{"id" => third_id} = json_response(conn_three, 200)
106 refute id == third_id
108 # An activity that will expire:
110 expires_in = 120 * 60
114 |> post("api/v1/statuses", %{
115 "status" => "oolong",
116 "expires_in" => expires_in
119 assert fourth_response = %{"id" => fourth_id} = json_response(conn_four, 200)
120 assert activity = Activity.get_by_id(fourth_id)
121 assert expiration = ActivityExpiration.get_by_activity_id(fourth_id)
123 estimated_expires_at =
124 NaiveDateTime.utc_now()
125 |> NaiveDateTime.add(expires_in)
126 |> NaiveDateTime.truncate(:second)
128 # This assert will fail if the test takes longer than a minute. I sure hope it never does:
129 assert abs(NaiveDateTime.diff(expiration.scheduled_at, estimated_expires_at, :second)) < 60
131 assert fourth_response["pleroma"]["expires_at"] ==
132 NaiveDateTime.to_iso8601(expiration.scheduled_at)
135 test "posting an undefined status with an attachment", %{conn: conn} do
139 content_type: "image/jpg",
140 path: Path.absname("test/fixtures/image.jpg"),
141 filename: "an_image.jpg"
144 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
148 |> assign(:user, user)
149 |> post("/api/v1/statuses", %{
150 "media_ids" => [to_string(upload.id)]
153 assert json_response(conn, 200)
156 test "replying to a status", %{conn: conn} do
158 {:ok, replied_to} = CommonAPI.post(user, %{"status" => "cofe"})
162 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
164 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
166 activity = Activity.get_by_id(id)
168 assert activity.data["context"] == replied_to.data["context"]
169 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
172 test "replying to a direct message with visibility other than direct", %{conn: conn} do
174 {:ok, replied_to} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
176 Enum.each(["public", "private", "unlisted"], fn visibility ->
179 |> post("/api/v1/statuses", %{
180 "status" => "@#{user.nickname} hey",
181 "in_reply_to_id" => replied_to.id,
182 "visibility" => visibility
185 assert json_response(conn, 422) == %{"error" => "The message visibility must be direct"}
189 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
192 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
194 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
195 assert Activity.get_by_id(id)
198 test "posting a sensitive status", %{conn: conn} do
201 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
203 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
204 assert Activity.get_by_id(id)
207 test "posting a fake status", %{conn: conn} do
210 |> post("/api/v1/statuses", %{
212 "\"Tenshi Eating a Corndog\" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it"
215 real_status = json_response(real_conn, 200)
218 assert Object.get_by_ap_id(real_status["uri"])
222 |> Map.put("id", nil)
223 |> Map.put("url", nil)
224 |> Map.put("uri", nil)
225 |> Map.put("created_at", nil)
226 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
230 |> post("/api/v1/statuses", %{
232 "\"Tenshi Eating a Corndog\" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it",
236 fake_status = json_response(fake_conn, 200)
239 refute Object.get_by_ap_id(fake_status["uri"])
243 |> Map.put("id", nil)
244 |> Map.put("url", nil)
245 |> Map.put("uri", nil)
246 |> Map.put("created_at", nil)
247 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
249 assert real_status == fake_status
252 test "posting a status with OGP link preview", %{conn: conn} do
253 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
254 Config.put([:rich_media, :enabled], true)
258 |> post("/api/v1/statuses", %{
259 "status" => "https://example.com/ogp"
262 assert %{"id" => id, "card" => %{"title" => "The Rock"}} = json_response(conn, 200)
263 assert Activity.get_by_id(id)
266 test "posting a direct status", %{conn: conn} do
267 user2 = insert(:user)
268 content = "direct cofe @#{user2.nickname}"
272 |> post("api/v1/statuses", %{"status" => content, "visibility" => "direct"})
274 assert %{"id" => id} = response = json_response(conn, 200)
275 assert response["visibility"] == "direct"
276 assert response["pleroma"]["direct_conversation_id"]
277 assert activity = Activity.get_by_id(id)
278 assert activity.recipients == [user2.ap_id, conn.assigns[:user].ap_id]
279 assert activity.data["to"] == [user2.ap_id]
280 assert activity.data["cc"] == []
284 describe "posting scheduled statuses" do
285 test "creates a scheduled activity", %{conn: conn} do
287 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
291 |> assign(:user, user)
292 |> post("/api/v1/statuses", %{
293 "status" => "scheduled",
294 "scheduled_at" => scheduled_at
297 assert %{"scheduled_at" => expected_scheduled_at} = json_response(conn, 200)
298 assert expected_scheduled_at == CommonAPI.Utils.to_masto_date(scheduled_at)
299 assert [] == Repo.all(Activity)
302 test "creates a scheduled activity with a media attachment", %{conn: conn} do
304 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
307 content_type: "image/jpg",
308 path: Path.absname("test/fixtures/image.jpg"),
309 filename: "an_image.jpg"
312 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
316 |> assign(:user, user)
317 |> post("/api/v1/statuses", %{
318 "media_ids" => [to_string(upload.id)],
319 "status" => "scheduled",
320 "scheduled_at" => scheduled_at
323 assert %{"media_attachments" => [media_attachment]} = json_response(conn, 200)
324 assert %{"type" => "image"} = media_attachment
327 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
332 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
336 |> assign(:user, user)
337 |> post("/api/v1/statuses", %{
338 "status" => "not scheduled",
339 "scheduled_at" => scheduled_at
342 assert %{"content" => "not scheduled"} = json_response(conn, 200)
343 assert [] == Repo.all(ScheduledActivity)
346 test "returns error when daily user limit is exceeded", %{conn: conn} do
350 NaiveDateTime.utc_now()
351 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
352 |> NaiveDateTime.to_iso8601()
354 attrs = %{params: %{}, scheduled_at: today}
355 {:ok, _} = ScheduledActivity.create(user, attrs)
356 {:ok, _} = ScheduledActivity.create(user, attrs)
360 |> assign(:user, user)
361 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
363 assert %{"error" => "daily limit exceeded"} == json_response(conn, 422)
366 test "returns error when total user limit is exceeded", %{conn: conn} do
370 NaiveDateTime.utc_now()
371 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
372 |> NaiveDateTime.to_iso8601()
375 NaiveDateTime.utc_now()
376 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
377 |> NaiveDateTime.to_iso8601()
379 attrs = %{params: %{}, scheduled_at: today}
380 {:ok, _} = ScheduledActivity.create(user, attrs)
381 {:ok, _} = ScheduledActivity.create(user, attrs)
382 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
386 |> assign(:user, user)
387 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
389 assert %{"error" => "total limit exceeded"} == json_response(conn, 422)
393 describe "posting polls" do
394 test "posting a poll", %{conn: conn} do
396 time = NaiveDateTime.utc_now()
400 |> assign(:user, user)
401 |> post("/api/v1/statuses", %{
402 "status" => "Who is the #bestgrill?",
403 "poll" => %{"options" => ["Rei", "Asuka", "Misato"], "expires_in" => 420}
406 response = json_response(conn, 200)
408 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
409 title in ["Rei", "Asuka", "Misato"]
412 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
413 refute response["poll"]["expred"]
416 test "option limit is enforced", %{conn: conn} do
418 limit = Config.get([:instance, :poll_limits, :max_options])
422 |> assign(:user, user)
423 |> post("/api/v1/statuses", %{
425 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
428 %{"error" => error} = json_response(conn, 422)
429 assert error == "Poll can't contain more than #{limit} options"
432 test "option character limit is enforced", %{conn: conn} do
434 limit = Config.get([:instance, :poll_limits, :max_option_chars])
438 |> assign(:user, user)
439 |> post("/api/v1/statuses", %{
442 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
447 %{"error" => error} = json_response(conn, 422)
448 assert error == "Poll options cannot be longer than #{limit} characters each"
451 test "minimal date limit is enforced", %{conn: conn} do
453 limit = Config.get([:instance, :poll_limits, :min_expiration])
457 |> assign(:user, user)
458 |> post("/api/v1/statuses", %{
459 "status" => "imagine arbitrary limits",
461 "options" => ["this post was made by pleroma gang"],
462 "expires_in" => limit - 1
466 %{"error" => error} = json_response(conn, 422)
467 assert error == "Expiration date is too soon"
470 test "maximum date limit is enforced", %{conn: conn} do
472 limit = Config.get([:instance, :poll_limits, :max_expiration])
476 |> assign(:user, user)
477 |> post("/api/v1/statuses", %{
478 "status" => "imagine arbitrary limits",
480 "options" => ["this post was made by pleroma gang"],
481 "expires_in" => limit + 1
485 %{"error" => error} = json_response(conn, 422)
486 assert error == "Expiration date is too far in the future"
490 test "get a status", %{conn: conn} do
491 activity = insert(:note_activity)
495 |> get("/api/v1/statuses/#{activity.id}")
497 assert %{"id" => id} = json_response(conn, 200)
498 assert id == to_string(activity.id)
501 test "get a direct status", %{conn: conn} do
503 other_user = insert(:user)
506 CommonAPI.post(user, %{"status" => "@#{other_user.nickname}", "visibility" => "direct"})
510 |> assign(:user, user)
511 |> get("/api/v1/statuses/#{activity.id}")
513 [participation] = Participation.for_user(user)
515 res = json_response(conn, 200)
516 assert res["pleroma"]["direct_conversation_id"] == participation.id
519 test "get statuses by IDs", %{conn: conn} do
520 %{id: id1} = insert(:note_activity)
521 %{id: id2} = insert(:note_activity)
523 query_string = "ids[]=#{id1}&ids[]=#{id2}"
524 conn = get(conn, "/api/v1/statuses/?#{query_string}")
526 assert [%{"id" => ^id1}, %{"id" => ^id2}] = Enum.sort_by(json_response(conn, :ok), & &1["id"])
529 describe "deleting a status" do
530 test "when you created it", %{conn: conn} do
531 activity = insert(:note_activity)
532 author = User.get_cached_by_ap_id(activity.data["actor"])
536 |> assign(:user, author)
537 |> delete("/api/v1/statuses/#{activity.id}")
539 assert %{} = json_response(conn, 200)
541 refute Activity.get_by_id(activity.id)
544 test "when you didn't create it", %{conn: conn} do
545 activity = insert(:note_activity)
550 |> assign(:user, user)
551 |> delete("/api/v1/statuses/#{activity.id}")
553 assert %{"error" => _} = json_response(conn, 403)
555 assert Activity.get_by_id(activity.id) == activity
558 test "when you're an admin or moderator", %{conn: conn} do
559 activity1 = insert(:note_activity)
560 activity2 = insert(:note_activity)
561 admin = insert(:user, info: %{is_admin: true})
562 moderator = insert(:user, info: %{is_moderator: true})
566 |> assign(:user, admin)
567 |> delete("/api/v1/statuses/#{activity1.id}")
569 assert %{} = json_response(res_conn, 200)
573 |> assign(:user, moderator)
574 |> delete("/api/v1/statuses/#{activity2.id}")
576 assert %{} = json_response(res_conn, 200)
578 refute Activity.get_by_id(activity1.id)
579 refute Activity.get_by_id(activity2.id)
583 describe "reblogging" do
584 test "reblogs and returns the reblogged status", %{conn: conn} do
585 activity = insert(:note_activity)
590 |> assign(:user, user)
591 |> post("/api/v1/statuses/#{activity.id}/reblog")
594 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
596 } = json_response(conn, 200)
598 assert to_string(activity.id) == id
601 test "reblogs privately and returns the reblogged status", %{conn: conn} do
602 activity = insert(:note_activity)
607 |> assign(:user, user)
608 |> post("/api/v1/statuses/#{activity.id}/reblog", %{"visibility" => "private"})
611 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
613 "visibility" => "private"
614 } = json_response(conn, 200)
616 assert to_string(activity.id) == id
619 test "reblogged status for another user", %{conn: conn} do
620 activity = insert(:note_activity)
621 user1 = insert(:user)
622 user2 = insert(:user)
623 user3 = insert(:user)
624 CommonAPI.favorite(activity.id, user2)
625 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
626 {:ok, reblog_activity1, _object} = CommonAPI.repeat(activity.id, user1)
627 {:ok, _, _object} = CommonAPI.repeat(activity.id, user2)
631 |> assign(:user, user3)
632 |> get("/api/v1/statuses/#{reblog_activity1.id}")
635 "reblog" => %{"id" => id, "reblogged" => false, "reblogs_count" => 2},
636 "reblogged" => false,
637 "favourited" => false,
638 "bookmarked" => false
639 } = json_response(conn_res, 200)
643 |> assign(:user, user2)
644 |> get("/api/v1/statuses/#{reblog_activity1.id}")
647 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
649 "favourited" => true,
651 } = json_response(conn_res, 200)
653 assert to_string(activity.id) == id
656 test "returns 400 error when activity is not exist", %{conn: conn} do
661 |> assign(:user, user)
662 |> post("/api/v1/statuses/foo/reblog")
664 assert json_response(conn, 400) == %{"error" => "Could not repeat"}
668 describe "unreblogging" do
669 test "unreblogs and returns the unreblogged status", %{conn: conn} do
670 activity = insert(:note_activity)
673 {:ok, _, _} = CommonAPI.repeat(activity.id, user)
677 |> assign(:user, user)
678 |> post("/api/v1/statuses/#{activity.id}/unreblog")
680 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
682 assert to_string(activity.id) == id
685 test "returns 400 error when activity is not exist", %{conn: conn} do
690 |> assign(:user, user)
691 |> post("/api/v1/statuses/foo/unreblog")
693 assert json_response(conn, 400) == %{"error" => "Could not unrepeat"}
697 describe "favoriting" do
698 test "favs a status and returns it", %{conn: conn} do
699 activity = insert(:note_activity)
704 |> assign(:user, user)
705 |> post("/api/v1/statuses/#{activity.id}/favourite")
707 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
708 json_response(conn, 200)
710 assert to_string(activity.id) == id
713 test "returns 400 error for a wrong id", %{conn: conn} do
718 |> assign(:user, user)
719 |> post("/api/v1/statuses/1/favourite")
721 assert json_response(conn, 400) == %{"error" => "Could not favorite"}
725 describe "unfavoriting" do
726 test "unfavorites a status and returns it", %{conn: conn} do
727 activity = insert(:note_activity)
730 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
734 |> assign(:user, user)
735 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
737 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
738 json_response(conn, 200)
740 assert to_string(activity.id) == id
743 test "returns 400 error for a wrong id", %{conn: conn} do
748 |> assign(:user, user)
749 |> post("/api/v1/statuses/1/unfavourite")
751 assert json_response(conn, 400) == %{"error" => "Could not unfavorite"}
755 describe "pinned statuses" do
758 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
760 [user: user, activity: activity]
763 clear_config([:instance, :max_pinned_statuses]) do
764 Config.put([:instance, :max_pinned_statuses], 1)
767 test "pin status", %{conn: conn, user: user, activity: activity} do
768 id_str = to_string(activity.id)
770 assert %{"id" => ^id_str, "pinned" => true} =
772 |> assign(:user, user)
773 |> post("/api/v1/statuses/#{activity.id}/pin")
774 |> json_response(200)
776 assert [%{"id" => ^id_str, "pinned" => true}] =
778 |> assign(:user, user)
779 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
780 |> json_response(200)
783 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
784 {:ok, dm} = CommonAPI.post(user, %{"status" => "test", "visibility" => "direct"})
788 |> assign(:user, user)
789 |> post("/api/v1/statuses/#{dm.id}/pin")
791 assert json_response(conn, 400) == %{"error" => "Could not pin"}
794 test "unpin status", %{conn: conn, user: user, activity: activity} do
795 {:ok, _} = CommonAPI.pin(activity.id, user)
797 id_str = to_string(activity.id)
798 user = refresh_record(user)
800 assert %{"id" => ^id_str, "pinned" => false} =
802 |> assign(:user, user)
803 |> post("/api/v1/statuses/#{activity.id}/unpin")
804 |> json_response(200)
808 |> assign(:user, user)
809 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
810 |> json_response(200)
813 test "/unpin: returns 400 error when activity is not exist", %{conn: conn, user: user} do
816 |> assign(:user, user)
817 |> post("/api/v1/statuses/1/unpin")
819 assert json_response(conn, 400) == %{"error" => "Could not unpin"}
822 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
823 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
825 id_str_one = to_string(activity_one.id)
827 assert %{"id" => ^id_str_one, "pinned" => true} =
829 |> assign(:user, user)
830 |> post("/api/v1/statuses/#{id_str_one}/pin")
831 |> json_response(200)
833 user = refresh_record(user)
835 assert %{"error" => "You have already pinned the maximum number of statuses"} =
837 |> assign(:user, user)
838 |> post("/api/v1/statuses/#{activity_two.id}/pin")
839 |> json_response(400)
845 Config.put([:rich_media, :enabled], true)
851 test "returns rich-media card", %{conn: conn, user: user} do
852 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
854 {:ok, activity} = CommonAPI.post(user, %{"status" => "https://example.com/ogp"})
857 "image" => "http://ia.media-imdb.com/images/rock.jpg",
858 "provider_name" => "example.com",
859 "provider_url" => "https://example.com",
860 "title" => "The Rock",
862 "url" => "https://example.com/ogp",
864 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
867 "image" => "http://ia.media-imdb.com/images/rock.jpg",
868 "title" => "The Rock",
869 "type" => "video.movie",
870 "url" => "https://example.com/ogp",
872 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
879 |> get("/api/v1/statuses/#{activity.id}/card")
880 |> json_response(200)
882 assert response == card_data
884 # works with private posts
886 CommonAPI.post(user, %{"status" => "https://example.com/ogp", "visibility" => "direct"})
890 |> assign(:user, user)
891 |> get("/api/v1/statuses/#{activity.id}/card")
892 |> json_response(200)
894 assert response_two == card_data
897 test "replaces missing description with an empty string", %{conn: conn, user: user} do
898 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
901 CommonAPI.post(user, %{"status" => "https://example.com/ogp-missing-data"})
905 |> get("/api/v1/statuses/#{activity.id}/card")
906 |> json_response(:ok)
908 assert response == %{
910 "title" => "Pleroma",
913 "provider_name" => "example.com",
914 "provider_url" => "https://example.com",
915 "url" => "https://example.com/ogp-missing-data",
918 "title" => "Pleroma",
920 "url" => "https://example.com/ogp-missing-data"
929 for_user = insert(:user)
932 CommonAPI.post(user, %{
933 "status" => "heweoo?"
937 CommonAPI.post(user, %{
938 "status" => "heweoo!"
943 |> assign(:user, for_user)
944 |> post("/api/v1/statuses/#{activity1.id}/bookmark")
946 assert json_response(response1, 200)["bookmarked"] == true
950 |> assign(:user, for_user)
951 |> post("/api/v1/statuses/#{activity2.id}/bookmark")
953 assert json_response(response2, 200)["bookmarked"] == true
957 |> assign(:user, for_user)
958 |> get("/api/v1/bookmarks")
960 assert [json_response(response2, 200), json_response(response1, 200)] ==
961 json_response(bookmarks, 200)
965 |> assign(:user, for_user)
966 |> post("/api/v1/statuses/#{activity1.id}/unbookmark")
968 assert json_response(response1, 200)["bookmarked"] == false
972 |> assign(:user, for_user)
973 |> get("/api/v1/bookmarks")
975 assert [json_response(response2, 200)] == json_response(bookmarks, 200)
978 describe "conversation muting" do
980 post_user = insert(:user)
983 {:ok, activity} = CommonAPI.post(post_user, %{"status" => "HIE"})
985 [user: user, activity: activity]
988 test "mute conversation", %{conn: conn, user: user, activity: activity} do
989 id_str = to_string(activity.id)
991 assert %{"id" => ^id_str, "muted" => true} =
993 |> assign(:user, user)
994 |> post("/api/v1/statuses/#{activity.id}/mute")
995 |> json_response(200)
998 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
999 {:ok, _} = CommonAPI.add_mute(user, activity)
1003 |> assign(:user, user)
1004 |> post("/api/v1/statuses/#{activity.id}/mute")
1006 assert json_response(conn, 400) == %{"error" => "conversation is already muted"}
1009 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
1010 {:ok, _} = CommonAPI.add_mute(user, activity)
1012 id_str = to_string(activity.id)
1013 user = refresh_record(user)
1015 assert %{"id" => ^id_str, "muted" => false} =
1017 |> assign(:user, user)
1018 |> post("/api/v1/statuses/#{activity.id}/unmute")
1019 |> json_response(200)
1023 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
1024 user1 = insert(:user)
1025 user2 = insert(:user)
1026 user3 = insert(:user)
1028 {:ok, replied_to} = CommonAPI.post(user1, %{"status" => "cofe"})
1030 # Reply to status from another user
1033 |> assign(:user, user2)
1034 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
1036 assert %{"content" => "xD", "id" => id} = json_response(conn1, 200)
1038 activity = Activity.get_by_id_with_object(id)
1040 assert Object.normalize(activity).data["inReplyTo"] == Object.normalize(replied_to).data["id"]
1041 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
1043 # Reblog from the third user
1046 |> assign(:user, user3)
1047 |> post("/api/v1/statuses/#{activity.id}/reblog")
1049 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
1050 json_response(conn2, 200)
1052 assert to_string(activity.id) == id
1054 # Getting third user status
1057 |> assign(:user, user3)
1058 |> get("api/v1/timelines/home")
1060 [reblogged_activity] = json_response(conn3, 200)
1062 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
1064 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
1065 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
1068 describe "GET /api/v1/statuses/:id/favourited_by" do
1070 user = insert(:user)
1071 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1075 |> assign(:user, user)
1077 [conn: conn, activity: activity, user: user]
1080 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
1081 other_user = insert(:user)
1082 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1086 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1087 |> json_response(:ok)
1089 [%{"id" => id}] = response
1091 assert id == other_user.id
1094 test "returns empty array when status has not been favorited yet", %{
1100 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1101 |> json_response(:ok)
1103 assert Enum.empty?(response)
1106 test "does not return users who have favorited the status but are blocked", %{
1107 conn: %{assigns: %{user: user}} = conn,
1110 other_user = insert(:user)
1111 {:ok, user} = User.block(user, other_user)
1113 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1117 |> assign(:user, user)
1118 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1119 |> json_response(:ok)
1121 assert Enum.empty?(response)
1124 test "does not fail on an unauthenticated request", %{conn: conn, activity: activity} do
1125 other_user = insert(:user)
1126 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1130 |> assign(:user, nil)
1131 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1132 |> json_response(:ok)
1134 [%{"id" => id}] = response
1135 assert id == other_user.id
1138 test "requires authentification for private posts", %{conn: conn, user: user} do
1139 other_user = insert(:user)
1142 CommonAPI.post(user, %{
1143 "status" => "@#{other_user.nickname} wanna get some #cofe together?",
1144 "visibility" => "direct"
1147 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1150 |> assign(:user, nil)
1151 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1152 |> json_response(404)
1156 |> assign(:user, other_user)
1157 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1158 |> json_response(200)
1160 [%{"id" => id}] = response
1161 assert id == other_user.id
1165 describe "GET /api/v1/statuses/:id/reblogged_by" do
1167 user = insert(:user)
1168 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1172 |> assign(:user, user)
1174 [conn: conn, activity: activity, user: user]
1177 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1178 other_user = insert(:user)
1179 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1183 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1184 |> json_response(:ok)
1186 [%{"id" => id}] = response
1188 assert id == other_user.id
1191 test "returns empty array when status has not been reblogged yet", %{
1197 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1198 |> json_response(:ok)
1200 assert Enum.empty?(response)
1203 test "does not return users who have reblogged the status but are blocked", %{
1204 conn: %{assigns: %{user: user}} = conn,
1207 other_user = insert(:user)
1208 {:ok, user} = User.block(user, other_user)
1210 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1214 |> assign(:user, user)
1215 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1216 |> json_response(:ok)
1218 assert Enum.empty?(response)
1221 test "does not return users who have reblogged the status privately", %{
1222 conn: %{assigns: %{user: user}} = conn,
1225 other_user = insert(:user)
1227 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user, %{"visibility" => "private"})
1231 |> assign(:user, user)
1232 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1233 |> json_response(:ok)
1235 assert Enum.empty?(response)
1238 test "does not fail on an unauthenticated request", %{conn: conn, activity: activity} do
1239 other_user = insert(:user)
1240 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1244 |> assign(:user, nil)
1245 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1246 |> json_response(:ok)
1248 [%{"id" => id}] = response
1249 assert id == other_user.id
1252 test "requires authentification for private posts", %{conn: conn, user: user} do
1253 other_user = insert(:user)
1256 CommonAPI.post(user, %{
1257 "status" => "@#{other_user.nickname} wanna get some #cofe together?",
1258 "visibility" => "direct"
1262 |> assign(:user, nil)
1263 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1264 |> json_response(404)
1268 |> assign(:user, other_user)
1269 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1270 |> json_response(200)
1272 assert [] == response
1277 user = insert(:user)
1279 {:ok, %{id: id1}} = CommonAPI.post(user, %{"status" => "1"})
1280 {:ok, %{id: id2}} = CommonAPI.post(user, %{"status" => "2", "in_reply_to_status_id" => id1})
1281 {:ok, %{id: id3}} = CommonAPI.post(user, %{"status" => "3", "in_reply_to_status_id" => id2})
1282 {:ok, %{id: id4}} = CommonAPI.post(user, %{"status" => "4", "in_reply_to_status_id" => id3})
1283 {:ok, %{id: id5}} = CommonAPI.post(user, %{"status" => "5", "in_reply_to_status_id" => id4})
1287 |> assign(:user, nil)
1288 |> get("/api/v1/statuses/#{id3}/context")
1289 |> json_response(:ok)
1292 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1293 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1297 test "returns the favorites of a user", %{conn: conn} do
1298 user = insert(:user)
1299 other_user = insert(:user)
1301 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
1302 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
1304 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
1308 |> assign(:user, user)
1309 |> get("/api/v1/favourites")
1311 assert [status] = json_response(first_conn, 200)
1312 assert status["id"] == to_string(activity.id)
1314 assert [{"link", _link_header}] =
1315 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1317 # Honours query params
1318 {:ok, second_activity} =
1319 CommonAPI.post(other_user, %{
1321 "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1324 {:ok, _, _} = CommonAPI.favorite(second_activity.id, user)
1326 last_like = status["id"]
1330 |> assign(:user, user)
1331 |> get("/api/v1/favourites?since_id=#{last_like}")
1333 assert [second_status] = json_response(second_conn, 200)
1334 assert second_status["id"] == to_string(second_activity.id)
1338 |> assign(:user, user)
1339 |> get("/api/v1/favourites?limit=0")
1341 assert [] = json_response(third_conn, 200)