1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Conversation.Participation
13 alias Pleroma.ScheduledActivity
14 alias Pleroma.Tests.ObanHelpers
16 alias Pleroma.Web.ActivityPub.ActivityPub
17 alias Pleroma.Web.CommonAPI
19 import Pleroma.Factory
21 setup do: clear_config([:instance, :federating])
22 setup do: clear_config([:instance, :allow_relay])
23 setup do: clear_config([:rich_media, :enabled])
24 setup do: clear_config([:mrf, :policies])
25 setup do: clear_config([:mrf_keyword, :reject])
27 describe "posting statuses" do
28 setup do: oauth_access(["write:statuses"])
30 test "posting a status does not increment reblog_count when relaying", %{conn: conn} do
31 clear_config([:instance, :federating], true)
32 Config.get([:instance, :allow_relay], true)
36 |> put_req_header("content-type", "application/json")
37 |> post("api/v1/statuses", %{
38 "content_type" => "text/plain",
39 "source" => "Pleroma FE",
40 "status" => "Hello world",
41 "visibility" => "public"
43 |> json_response_and_validate_schema(200)
45 assert response["reblogs_count"] == 0
46 ObanHelpers.perform_all()
50 |> get("api/v1/statuses/#{response["id"]}", %{})
51 |> json_response_and_validate_schema(200)
53 assert response["reblogs_count"] == 0
56 test "posting a status", %{conn: conn} do
57 idempotency_key = "Pikachu rocks!"
61 |> put_req_header("content-type", "application/json")
62 |> put_req_header("idempotency-key", idempotency_key)
63 |> post("/api/v1/statuses", %{
65 "spoiler_text" => "2hu",
69 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
70 json_response_and_validate_schema(conn_one, 200)
72 assert Activity.get_by_id(id)
76 |> put_req_header("content-type", "application/json")
77 |> put_req_header("idempotency-key", idempotency_key)
78 |> post("/api/v1/statuses", %{
80 "spoiler_text" => "2hu",
84 assert %{"id" => second_id} = json_response(conn_two, 200)
85 assert id == second_id
89 |> put_req_header("content-type", "application/json")
90 |> post("/api/v1/statuses", %{
92 "spoiler_text" => "2hu",
93 "sensitive" => "False"
96 assert %{"id" => third_id} = json_response_and_validate_schema(conn_three, 200)
99 # An activity that will expire:
101 expires_in = 2 * 60 * 60
103 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
107 |> put_req_header("content-type", "application/json")
108 |> post("api/v1/statuses", %{
109 "status" => "oolong",
110 "expires_in" => expires_in
113 assert %{"id" => fourth_id} = json_response_and_validate_schema(conn_four, 200)
115 assert Activity.get_by_id(fourth_id)
118 worker: Pleroma.Workers.PurgeExpiredActivity,
119 args: %{activity_id: fourth_id},
120 scheduled_at: expires_at
124 test "it fails to create a status if `expires_in` is less or equal than an hour", %{
130 assert %{"error" => "Expiry date is too soon"} =
132 |> put_req_header("content-type", "application/json")
133 |> post("api/v1/statuses", %{
134 "status" => "oolong",
135 "expires_in" => expires_in
137 |> json_response_and_validate_schema(422)
142 assert %{"error" => "Expiry date is too soon"} =
144 |> put_req_header("content-type", "application/json")
145 |> post("api/v1/statuses", %{
146 "status" => "oolong",
147 "expires_in" => expires_in
149 |> json_response_and_validate_schema(422)
152 test "Get MRF reason when posting a status is rejected by one", %{conn: conn} do
153 clear_config([:mrf_keyword, :reject], ["GNO"])
154 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
156 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} =
158 |> put_req_header("content-type", "application/json")
159 |> post("api/v1/statuses", %{"status" => "GNO/Linux"})
160 |> json_response_and_validate_schema(422)
163 test "posting an undefined status with an attachment", %{user: user, conn: conn} do
165 content_type: "image/jpeg",
166 path: Path.absname("test/fixtures/image.jpg"),
167 filename: "an_image.jpg"
170 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
174 |> put_req_header("content-type", "application/json")
175 |> post("/api/v1/statuses", %{
176 "media_ids" => [to_string(upload.id)]
179 assert json_response_and_validate_schema(conn, 200)
182 test "replying to a status", %{user: user, conn: conn} do
183 {:ok, replied_to} = CommonAPI.post(user, %{status: "cofe"})
187 |> put_req_header("content-type", "application/json")
188 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
190 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn, 200)
192 activity = Activity.get_by_id(id)
194 assert activity.data["context"] == replied_to.data["context"]
195 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
198 test "replying to a direct message with visibility other than direct", %{
202 {:ok, replied_to} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
204 Enum.each(["public", "private", "unlisted"], fn visibility ->
207 |> put_req_header("content-type", "application/json")
208 |> post("/api/v1/statuses", %{
209 "status" => "@#{user.nickname} hey",
210 "in_reply_to_id" => replied_to.id,
211 "visibility" => visibility
214 assert json_response_and_validate_schema(conn, 422) == %{
215 "error" => "The message visibility must be direct"
220 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
223 |> put_req_header("content-type", "application/json")
224 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
226 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn, 200)
227 assert Activity.get_by_id(id)
230 test "posting a sensitive status", %{conn: conn} do
233 |> put_req_header("content-type", "application/json")
234 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
236 assert %{"content" => "cofe", "id" => id, "sensitive" => true} =
237 json_response_and_validate_schema(conn, 200)
239 assert Activity.get_by_id(id)
242 test "posting a fake status", %{conn: conn} do
245 |> put_req_header("content-type", "application/json")
246 |> post("/api/v1/statuses", %{
248 "\"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"
251 real_status = json_response_and_validate_schema(real_conn, 200)
254 assert Object.get_by_ap_id(real_status["uri"])
258 |> Map.put("id", nil)
259 |> Map.put("url", nil)
260 |> Map.put("uri", nil)
261 |> Map.put("created_at", nil)
262 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
266 |> assign(:user, refresh_record(conn.assigns.user))
267 |> put_req_header("content-type", "application/json")
268 |> post("/api/v1/statuses", %{
270 "\"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",
274 fake_status = json_response_and_validate_schema(fake_conn, 200)
277 refute Object.get_by_ap_id(fake_status["uri"])
281 |> Map.put("id", nil)
282 |> Map.put("url", nil)
283 |> Map.put("uri", nil)
284 |> Map.put("created_at", nil)
285 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
287 assert real_status == fake_status
290 test "fake statuses' preview card is not cached", %{conn: conn} do
291 clear_config([:rich_media, :enabled], true)
296 url: "https://example.com/twitter-card"
298 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
301 apply(HttpRequestMock, :request, [env])
306 |> put_req_header("content-type", "application/json")
307 |> post("/api/v1/statuses", %{
308 "status" => "https://example.com/ogp",
314 |> put_req_header("content-type", "application/json")
315 |> post("/api/v1/statuses", %{
316 "status" => "https://example.com/twitter-card",
320 assert %{"card" => %{"title" => "The Rock"}} = json_response_and_validate_schema(conn1, 200)
322 assert %{"card" => %{"title" => "Small Island Developing States Photo Submission"}} =
323 json_response_and_validate_schema(conn2, 200)
326 test "posting a status with OGP link preview", %{conn: conn} do
327 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
328 clear_config([:rich_media, :enabled], true)
332 |> put_req_header("content-type", "application/json")
333 |> post("/api/v1/statuses", %{
334 "status" => "https://example.com/ogp"
337 assert %{"id" => id, "card" => %{"title" => "The Rock"}} =
338 json_response_and_validate_schema(conn, 200)
340 assert Activity.get_by_id(id)
343 test "posting a direct status", %{conn: conn} do
344 user2 = insert(:user)
345 content = "direct cofe @#{user2.nickname}"
349 |> put_req_header("content-type", "application/json")
350 |> post("api/v1/statuses", %{"status" => content, "visibility" => "direct"})
352 assert %{"id" => id} = response = json_response_and_validate_schema(conn, 200)
353 assert response["visibility"] == "direct"
354 assert response["pleroma"]["direct_conversation_id"]
355 assert activity = Activity.get_by_id(id)
356 assert activity.recipients == [user2.ap_id, conn.assigns[:user].ap_id]
357 assert activity.data["to"] == [user2.ap_id]
358 assert activity.data["cc"] == []
361 test "discloses application metadata when enabled" do
362 user = insert(:user, disclose_client: true)
363 %{user: _user, token: token, conn: conn} = oauth_access(["write:statuses"], user: user)
365 %Pleroma.Web.OAuth.Token{
366 app: %Pleroma.Web.OAuth.App{
367 client_name: app_name,
374 |> put_req_header("content-type", "application/json")
375 |> post("/api/v1/statuses", %{
376 "status" => "cofe is my copilot"
380 "content" => "cofe is my copilot",
383 "website" => ^app_website
385 } = json_response_and_validate_schema(result, 200)
388 test "hides application metadata when disabled" do
389 user = insert(:user, disclose_client: false)
390 %{user: _user, token: _token, conn: conn} = oauth_access(["write:statuses"], user: user)
394 |> put_req_header("content-type", "application/json")
395 |> post("/api/v1/statuses", %{
396 "status" => "club mate is my wingman"
400 "content" => "club mate is my wingman",
402 } = json_response_and_validate_schema(result, 200)
406 describe "posting scheduled statuses" do
407 setup do: oauth_access(["write:statuses"])
409 test "creates a scheduled activity", %{conn: conn} do
411 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
412 |> NaiveDateTime.to_iso8601()
417 |> put_req_header("content-type", "application/json")
418 |> post("/api/v1/statuses", %{
419 "status" => "scheduled",
420 "scheduled_at" => scheduled_at
423 assert %{"scheduled_at" => expected_scheduled_at} =
424 json_response_and_validate_schema(conn, 200)
426 assert expected_scheduled_at == CommonAPI.Utils.to_masto_date(scheduled_at)
427 assert [] == Repo.all(Activity)
430 test "with expiration" do
431 %{conn: conn} = oauth_access(["write:statuses", "read:statuses"])
434 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(6), :millisecond)
435 |> NaiveDateTime.to_iso8601()
438 assert %{"id" => status_id, "params" => %{"expires_in" => 300}} =
440 |> put_req_header("content-type", "application/json")
441 |> post("/api/v1/statuses", %{
442 "status" => "scheduled",
443 "scheduled_at" => scheduled_at,
446 |> json_response_and_validate_schema(200)
448 assert %{"id" => ^status_id, "params" => %{"expires_in" => 300}} =
450 |> put_req_header("content-type", "application/json")
451 |> get("/api/v1/scheduled_statuses/#{status_id}")
452 |> json_response_and_validate_schema(200)
455 test "ignores nil values", %{conn: conn} do
458 |> put_req_header("content-type", "application/json")
459 |> post("/api/v1/statuses", %{
460 "status" => "not scheduled",
461 "scheduled_at" => nil
464 assert result = json_response_and_validate_schema(conn, 200)
465 assert Activity.get_by_id(result["id"])
468 test "creates a scheduled activity with a media attachment", %{user: user, conn: conn} do
470 NaiveDateTime.utc_now()
471 |> NaiveDateTime.add(:timer.minutes(120), :millisecond)
472 |> NaiveDateTime.to_iso8601()
476 content_type: "image/jpeg",
477 path: Path.absname("test/fixtures/image.jpg"),
478 filename: "an_image.jpg"
481 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
485 |> put_req_header("content-type", "application/json")
486 |> post("/api/v1/statuses", %{
487 "media_ids" => [to_string(upload.id)],
488 "status" => "scheduled",
489 "scheduled_at" => scheduled_at
492 assert %{"media_attachments" => [media_attachment]} =
493 json_response_and_validate_schema(conn, 200)
495 assert %{"type" => "image"} = media_attachment
498 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
501 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
502 |> NaiveDateTime.to_iso8601()
507 |> put_req_header("content-type", "application/json")
508 |> post("/api/v1/statuses", %{
509 "status" => "not scheduled",
510 "scheduled_at" => scheduled_at
513 assert %{"content" => "not scheduled"} = json_response_and_validate_schema(conn, 200)
514 assert [] == Repo.all(ScheduledActivity)
517 test "returns error when daily user limit is exceeded", %{user: user, conn: conn} do
519 NaiveDateTime.utc_now()
520 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
521 |> NaiveDateTime.to_iso8601()
525 attrs = %{params: %{}, scheduled_at: today}
526 {:ok, _} = ScheduledActivity.create(user, attrs)
527 {:ok, _} = ScheduledActivity.create(user, attrs)
531 |> put_req_header("content-type", "application/json")
532 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
534 assert %{"error" => "daily limit exceeded"} == json_response_and_validate_schema(conn, 422)
537 test "returns error when total user limit is exceeded", %{user: user, conn: conn} do
539 NaiveDateTime.utc_now()
540 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
541 |> NaiveDateTime.to_iso8601()
545 NaiveDateTime.utc_now()
546 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
547 |> NaiveDateTime.to_iso8601()
550 attrs = %{params: %{}, scheduled_at: today}
551 {:ok, _} = ScheduledActivity.create(user, attrs)
552 {:ok, _} = ScheduledActivity.create(user, attrs)
553 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
557 |> put_req_header("content-type", "application/json")
558 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
560 assert %{"error" => "total limit exceeded"} == json_response_and_validate_schema(conn, 422)
564 describe "posting polls" do
565 setup do: oauth_access(["write:statuses"])
567 test "posting a poll", %{conn: conn} do
568 time = NaiveDateTime.utc_now()
572 |> put_req_header("content-type", "application/json")
573 |> post("/api/v1/statuses", %{
574 "status" => "Who is the #bestgrill?",
576 "options" => ["Rei", "Asuka", "Misato"],
581 response = json_response_and_validate_schema(conn, 200)
583 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
584 title in ["Rei", "Asuka", "Misato"]
587 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
588 assert response["poll"]["expired"] == false
590 question = Object.get_by_id(response["poll"]["id"])
592 # closed contains utc timezone
593 assert question.data["closed"] =~ "Z"
596 test "option limit is enforced", %{conn: conn} do
597 limit = Config.get([:instance, :poll_limits, :max_options])
601 |> put_req_header("content-type", "application/json")
602 |> post("/api/v1/statuses", %{
604 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
607 %{"error" => error} = json_response_and_validate_schema(conn, 422)
608 assert error == "Poll can't contain more than #{limit} options"
611 test "option character limit is enforced", %{conn: conn} do
612 limit = Config.get([:instance, :poll_limits, :max_option_chars])
616 |> put_req_header("content-type", "application/json")
617 |> post("/api/v1/statuses", %{
620 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
625 %{"error" => error} = json_response_and_validate_schema(conn, 422)
626 assert error == "Poll options cannot be longer than #{limit} characters each"
629 test "minimal date limit is enforced", %{conn: conn} do
630 limit = Config.get([:instance, :poll_limits, :min_expiration])
634 |> put_req_header("content-type", "application/json")
635 |> post("/api/v1/statuses", %{
636 "status" => "imagine arbitrary limits",
638 "options" => ["this post was made by pleroma gang"],
639 "expires_in" => limit - 1
643 %{"error" => error} = json_response_and_validate_schema(conn, 422)
644 assert error == "Expiration date is too soon"
647 test "maximum date limit is enforced", %{conn: conn} do
648 limit = Config.get([:instance, :poll_limits, :max_expiration])
652 |> put_req_header("content-type", "application/json")
653 |> post("/api/v1/statuses", %{
654 "status" => "imagine arbitrary limits",
656 "options" => ["this post was made by pleroma gang"],
657 "expires_in" => limit + 1
661 %{"error" => error} = json_response_and_validate_schema(conn, 422)
662 assert error == "Expiration date is too far in the future"
665 test "scheduled poll", %{conn: conn} do
666 clear_config([ScheduledActivity, :enabled], true)
669 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(6), :millisecond)
670 |> NaiveDateTime.to_iso8601()
673 %{"id" => scheduled_id} =
675 |> put_req_header("content-type", "application/json")
676 |> post("/api/v1/statuses", %{
677 "status" => "very cool poll",
679 "options" => ~w(a b c),
682 "scheduled_at" => scheduled_at
684 |> json_response_and_validate_schema(200)
686 assert {:ok, %{id: activity_id}} =
687 perform_job(Pleroma.Workers.ScheduledActivityWorker, %{
688 activity_id: scheduled_id
691 assert Repo.all(Oban.Job) == []
695 |> Repo.get(activity_id)
696 |> Object.normalize()
698 assert object.data["content"] == "very cool poll"
699 assert object.data["type"] == "Question"
700 assert length(object.data["oneOf"]) == 3
704 test "get a status" do
705 %{conn: conn} = oauth_access(["read:statuses"])
706 activity = insert(:note_activity)
708 conn = get(conn, "/api/v1/statuses/#{activity.id}")
710 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
711 assert id == to_string(activity.id)
714 defp local_and_remote_activities do
715 local = insert(:note_activity)
716 remote = insert(:note_activity, local: false)
717 {:ok, local: local, remote: remote}
720 describe "status with restrict unauthenticated activities for local and remote" do
721 setup do: local_and_remote_activities()
723 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
725 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
727 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
728 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
730 assert json_response_and_validate_schema(res_conn, :not_found) == %{
731 "error" => "Record not found"
734 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
736 assert json_response_and_validate_schema(res_conn, :not_found) == %{
737 "error" => "Record not found"
741 test "if user is authenticated", %{local: local, remote: remote} do
742 %{conn: conn} = oauth_access(["read"])
743 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
744 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
746 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
747 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
751 describe "status with restrict unauthenticated activities for local" do
752 setup do: local_and_remote_activities()
754 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
756 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
757 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
759 assert json_response_and_validate_schema(res_conn, :not_found) == %{
760 "error" => "Record not found"
763 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
764 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
767 test "if user is authenticated", %{local: local, remote: remote} do
768 %{conn: conn} = oauth_access(["read"])
769 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
770 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
772 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
773 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
777 describe "status with restrict unauthenticated activities for remote" do
778 setup do: local_and_remote_activities()
780 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
782 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
783 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
784 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
786 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
788 assert json_response_and_validate_schema(res_conn, :not_found) == %{
789 "error" => "Record not found"
793 test "if user is authenticated", %{local: local, remote: remote} do
794 %{conn: conn} = oauth_access(["read"])
795 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
796 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
798 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
799 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
803 test "getting a status that doesn't exist returns 404" do
804 %{conn: conn} = oauth_access(["read:statuses"])
805 activity = insert(:note_activity)
807 conn = get(conn, "/api/v1/statuses/#{String.downcase(activity.id)}")
809 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
812 test "get a direct status" do
813 %{user: user, conn: conn} = oauth_access(["read:statuses"])
814 other_user = insert(:user)
817 CommonAPI.post(user, %{status: "@#{other_user.nickname}", visibility: "direct"})
821 |> assign(:user, user)
822 |> get("/api/v1/statuses/#{activity.id}")
824 [participation] = Participation.for_user(user)
826 res = json_response_and_validate_schema(conn, 200)
827 assert res["pleroma"]["direct_conversation_id"] == participation.id
830 test "get statuses by IDs" do
831 %{conn: conn} = oauth_access(["read:statuses"])
832 %{id: id1} = insert(:note_activity)
833 %{id: id2} = insert(:note_activity)
835 query_string = "ids[]=#{id1}&ids[]=#{id2}"
836 conn = get(conn, "/api/v1/statuses/?#{query_string}")
838 assert [%{"id" => ^id1}, %{"id" => ^id2}] =
839 Enum.sort_by(json_response_and_validate_schema(conn, :ok), & &1["id"])
842 describe "getting statuses by ids with restricted unauthenticated for local and remote" do
843 setup do: local_and_remote_activities()
845 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
847 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
849 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
850 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
852 assert json_response_and_validate_schema(res_conn, 200) == []
855 test "if user is authenticated", %{local: local, remote: remote} do
856 %{conn: conn} = oauth_access(["read"])
858 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
860 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
864 describe "getting statuses by ids with restricted unauthenticated for local" do
865 setup do: local_and_remote_activities()
867 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
869 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
870 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
872 remote_id = remote.id
873 assert [%{"id" => ^remote_id}] = json_response_and_validate_schema(res_conn, 200)
876 test "if user is authenticated", %{local: local, remote: remote} do
877 %{conn: conn} = oauth_access(["read"])
879 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
881 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
885 describe "getting statuses by ids with restricted unauthenticated for remote" do
886 setup do: local_and_remote_activities()
888 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
890 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
891 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
894 assert [%{"id" => ^local_id}] = json_response_and_validate_schema(res_conn, 200)
897 test "if user is authenticated", %{local: local, remote: remote} do
898 %{conn: conn} = oauth_access(["read"])
900 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
902 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
906 describe "deleting a status" do
907 test "when you created it" do
908 %{user: author, conn: conn} = oauth_access(["write:statuses"])
909 activity = insert(:note_activity, user: author)
910 object = Object.normalize(activity, fetch: false)
912 content = object.data["content"]
913 source = object.data["source"]
917 |> assign(:user, author)
918 |> delete("/api/v1/statuses/#{activity.id}")
919 |> json_response_and_validate_schema(200)
921 assert match?(%{"content" => ^content, "text" => ^source}, result)
923 refute Activity.get_by_id(activity.id)
926 test "when it doesn't exist" do
927 %{user: author, conn: conn} = oauth_access(["write:statuses"])
928 activity = insert(:note_activity, user: author)
932 |> assign(:user, author)
933 |> delete("/api/v1/statuses/#{String.downcase(activity.id)}")
935 assert %{"error" => "Record not found"} == json_response_and_validate_schema(conn, 404)
938 test "when you didn't create it" do
939 %{conn: conn} = oauth_access(["write:statuses"])
940 activity = insert(:note_activity)
942 conn = delete(conn, "/api/v1/statuses/#{activity.id}")
944 assert %{"error" => "Record not found"} == json_response_and_validate_schema(conn, 404)
946 assert Activity.get_by_id(activity.id) == activity
949 test "when you're an admin or moderator", %{conn: conn} do
950 activity1 = insert(:note_activity)
951 activity2 = insert(:note_activity)
952 admin = insert(:user, is_admin: true)
953 moderator = insert(:user, is_moderator: true)
957 |> assign(:user, admin)
958 |> assign(:token, insert(:oauth_token, user: admin, scopes: ["write:statuses"]))
959 |> delete("/api/v1/statuses/#{activity1.id}")
961 assert %{} = json_response_and_validate_schema(res_conn, 200)
965 |> assign(:user, moderator)
966 |> assign(:token, insert(:oauth_token, user: moderator, scopes: ["write:statuses"]))
967 |> delete("/api/v1/statuses/#{activity2.id}")
969 assert %{} = json_response_and_validate_schema(res_conn, 200)
971 refute Activity.get_by_id(activity1.id)
972 refute Activity.get_by_id(activity2.id)
976 describe "reblogging" do
977 setup do: oauth_access(["write:statuses"])
979 test "reblogs and returns the reblogged status", %{conn: conn} do
980 activity = insert(:note_activity)
984 |> put_req_header("content-type", "application/json")
985 |> post("/api/v1/statuses/#{activity.id}/reblog")
988 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
990 } = json_response_and_validate_schema(conn, 200)
992 assert to_string(activity.id) == id
995 test "returns 404 if the reblogged status doesn't exist", %{conn: conn} do
996 activity = insert(:note_activity)
1000 |> put_req_header("content-type", "application/json")
1001 |> post("/api/v1/statuses/#{String.downcase(activity.id)}/reblog")
1003 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
1006 test "reblogs privately and returns the reblogged status", %{conn: conn} do
1007 activity = insert(:note_activity)
1011 |> put_req_header("content-type", "application/json")
1013 "/api/v1/statuses/#{activity.id}/reblog",
1014 %{"visibility" => "private"}
1018 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1019 "reblogged" => true,
1020 "visibility" => "private"
1021 } = json_response_and_validate_schema(conn, 200)
1023 assert to_string(activity.id) == id
1026 test "reblogged status for another user" do
1027 activity = insert(:note_activity)
1028 user1 = insert(:user)
1029 user2 = insert(:user)
1030 user3 = insert(:user)
1031 {:ok, _} = CommonAPI.favorite(user2, activity.id)
1032 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
1033 {:ok, reblog_activity1} = CommonAPI.repeat(activity.id, user1)
1034 {:ok, _} = CommonAPI.repeat(activity.id, user2)
1038 |> assign(:user, user3)
1039 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1040 |> get("/api/v1/statuses/#{reblog_activity1.id}")
1043 "reblog" => %{"id" => _id, "reblogged" => false, "reblogs_count" => 2},
1044 "reblogged" => false,
1045 "favourited" => false,
1046 "bookmarked" => false
1047 } = json_response_and_validate_schema(conn_res, 200)
1051 |> assign(:user, user2)
1052 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["read:statuses"]))
1053 |> get("/api/v1/statuses/#{reblog_activity1.id}")
1056 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
1057 "reblogged" => true,
1058 "favourited" => true,
1059 "bookmarked" => true
1060 } = json_response_and_validate_schema(conn_res, 200)
1062 assert to_string(activity.id) == id
1065 test "author can reblog own private status", %{conn: conn, user: user} do
1066 {:ok, activity} = CommonAPI.post(user, %{status: "cofe", visibility: "private"})
1070 |> put_req_header("content-type", "application/json")
1071 |> post("/api/v1/statuses/#{activity.id}/reblog")
1074 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1075 "reblogged" => true,
1076 "visibility" => "private"
1077 } = json_response_and_validate_schema(conn, 200)
1079 assert to_string(activity.id) == id
1083 describe "unreblogging" do
1084 setup do: oauth_access(["write:statuses"])
1086 test "unreblogs and returns the unreblogged status", %{user: user, conn: conn} do
1087 activity = insert(:note_activity)
1089 {:ok, _} = CommonAPI.repeat(activity.id, user)
1093 |> put_req_header("content-type", "application/json")
1094 |> post("/api/v1/statuses/#{activity.id}/unreblog")
1096 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} =
1097 json_response_and_validate_schema(conn, 200)
1099 assert to_string(activity.id) == id
1102 test "returns 404 error when activity does not exist", %{conn: conn} do
1105 |> put_req_header("content-type", "application/json")
1106 |> post("/api/v1/statuses/foo/unreblog")
1108 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1112 describe "favoriting" do
1113 setup do: oauth_access(["write:favourites"])
1115 test "favs a status and returns it", %{conn: conn} do
1116 activity = insert(:note_activity)
1120 |> put_req_header("content-type", "application/json")
1121 |> post("/api/v1/statuses/#{activity.id}/favourite")
1123 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
1124 json_response_and_validate_schema(conn, 200)
1126 assert to_string(activity.id) == id
1129 test "favoriting twice will just return 200", %{conn: conn} do
1130 activity = insert(:note_activity)
1133 |> put_req_header("content-type", "application/json")
1134 |> post("/api/v1/statuses/#{activity.id}/favourite")
1137 |> put_req_header("content-type", "application/json")
1138 |> post("/api/v1/statuses/#{activity.id}/favourite")
1139 |> json_response_and_validate_schema(200)
1142 test "returns 404 error for a wrong id", %{conn: conn} do
1145 |> put_req_header("content-type", "application/json")
1146 |> post("/api/v1/statuses/1/favourite")
1148 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1152 describe "unfavoriting" do
1153 setup do: oauth_access(["write:favourites"])
1155 test "unfavorites a status and returns it", %{user: user, conn: conn} do
1156 activity = insert(:note_activity)
1158 {:ok, _} = CommonAPI.favorite(user, activity.id)
1162 |> put_req_header("content-type", "application/json")
1163 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
1165 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
1166 json_response_and_validate_schema(conn, 200)
1168 assert to_string(activity.id) == id
1171 test "returns 404 error for a wrong id", %{conn: conn} do
1174 |> put_req_header("content-type", "application/json")
1175 |> post("/api/v1/statuses/1/unfavourite")
1177 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1181 describe "pinned statuses" do
1182 setup do: oauth_access(["write:accounts"])
1184 setup %{user: user} do
1185 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1187 %{activity: activity}
1190 setup do: clear_config([:instance, :max_pinned_statuses], 1)
1192 test "pin status", %{conn: conn, user: user, activity: activity} do
1193 id_str = to_string(activity.id)
1195 assert %{"id" => ^id_str, "pinned" => true} =
1197 |> put_req_header("content-type", "application/json")
1198 |> post("/api/v1/statuses/#{activity.id}/pin")
1199 |> json_response_and_validate_schema(200)
1201 assert [%{"id" => ^id_str, "pinned" => true}] =
1203 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1204 |> json_response_and_validate_schema(200)
1207 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
1208 {:ok, dm} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
1212 |> put_req_header("content-type", "application/json")
1213 |> post("/api/v1/statuses/#{dm.id}/pin")
1215 assert json_response_and_validate_schema(conn, 400) == %{"error" => "Could not pin"}
1218 test "unpin status", %{conn: conn, user: user, activity: activity} do
1219 {:ok, _} = CommonAPI.pin(activity.id, user)
1220 user = refresh_record(user)
1222 id_str = to_string(activity.id)
1224 assert %{"id" => ^id_str, "pinned" => false} =
1226 |> assign(:user, user)
1227 |> post("/api/v1/statuses/#{activity.id}/unpin")
1228 |> json_response_and_validate_schema(200)
1232 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1233 |> json_response_and_validate_schema(200)
1236 test "/unpin: returns 400 error when activity is not exist", %{conn: conn} do
1239 |> put_req_header("content-type", "application/json")
1240 |> post("/api/v1/statuses/1/unpin")
1242 assert json_response_and_validate_schema(conn, 400) == %{"error" => "Could not unpin"}
1245 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
1246 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
1248 id_str_one = to_string(activity_one.id)
1250 assert %{"id" => ^id_str_one, "pinned" => true} =
1252 |> put_req_header("content-type", "application/json")
1253 |> post("/api/v1/statuses/#{id_str_one}/pin")
1254 |> json_response_and_validate_schema(200)
1256 user = refresh_record(user)
1258 assert %{"error" => "You have already pinned the maximum number of statuses"} =
1260 |> assign(:user, user)
1261 |> post("/api/v1/statuses/#{activity_two.id}/pin")
1262 |> json_response_and_validate_schema(400)
1265 test "on pin removes deletion job, on unpin reschedule deletion" do
1266 %{conn: conn} = oauth_access(["write:accounts", "write:statuses"])
1267 expires_in = 2 * 60 * 60
1269 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
1271 assert %{"id" => id} =
1273 |> put_req_header("content-type", "application/json")
1274 |> post("api/v1/statuses", %{
1275 "status" => "oolong",
1276 "expires_in" => expires_in
1278 |> json_response_and_validate_schema(200)
1281 worker: Pleroma.Workers.PurgeExpiredActivity,
1282 args: %{activity_id: id},
1283 scheduled_at: expires_at
1286 assert %{"id" => ^id, "pinned" => true} =
1288 |> put_req_header("content-type", "application/json")
1289 |> post("/api/v1/statuses/#{id}/pin")
1290 |> json_response_and_validate_schema(200)
1293 worker: Pleroma.Workers.PurgeExpiredActivity,
1294 args: %{activity_id: id},
1295 scheduled_at: expires_at
1298 assert %{"id" => ^id, "pinned" => false} =
1300 |> put_req_header("content-type", "application/json")
1301 |> post("/api/v1/statuses/#{id}/unpin")
1302 |> json_response_and_validate_schema(200)
1305 worker: Pleroma.Workers.PurgeExpiredActivity,
1306 args: %{activity_id: id},
1307 scheduled_at: expires_at
1314 clear_config([:rich_media, :enabled], true)
1316 oauth_access(["read:statuses"])
1319 test "returns rich-media card", %{conn: conn, user: user} do
1320 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
1322 {:ok, activity} = CommonAPI.post(user, %{status: "https://example.com/ogp"})
1325 "image" => "http://ia.media-imdb.com/images/rock.jpg",
1326 "provider_name" => "example.com",
1327 "provider_url" => "https://example.com",
1328 "title" => "The Rock",
1330 "url" => "https://example.com/ogp",
1332 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
1335 "image" => "http://ia.media-imdb.com/images/rock.jpg",
1336 "title" => "The Rock",
1337 "type" => "video.movie",
1338 "url" => "https://example.com/ogp",
1340 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
1347 |> get("/api/v1/statuses/#{activity.id}/card")
1348 |> json_response_and_validate_schema(200)
1350 assert response == card_data
1352 # works with private posts
1354 CommonAPI.post(user, %{status: "https://example.com/ogp", visibility: "direct"})
1358 |> get("/api/v1/statuses/#{activity.id}/card")
1359 |> json_response_and_validate_schema(200)
1361 assert response_two == card_data
1364 test "replaces missing description with an empty string", %{conn: conn, user: user} do
1365 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
1367 {:ok, activity} = CommonAPI.post(user, %{status: "https://example.com/ogp-missing-data"})
1371 |> get("/api/v1/statuses/#{activity.id}/card")
1372 |> json_response_and_validate_schema(:ok)
1374 assert response == %{
1376 "title" => "Pleroma",
1377 "description" => "",
1379 "provider_name" => "example.com",
1380 "provider_url" => "https://example.com",
1381 "url" => "https://example.com/ogp-missing-data",
1384 "title" => "Pleroma",
1385 "type" => "website",
1386 "url" => "https://example.com/ogp-missing-data"
1394 bookmarks_uri = "/api/v1/bookmarks"
1396 %{conn: conn} = oauth_access(["write:bookmarks", "read:bookmarks"])
1397 author = insert(:user)
1399 {:ok, activity1} = CommonAPI.post(author, %{status: "heweoo?"})
1400 {:ok, activity2} = CommonAPI.post(author, %{status: "heweoo!"})
1404 |> put_req_header("content-type", "application/json")
1405 |> post("/api/v1/statuses/#{activity1.id}/bookmark")
1407 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == true
1411 |> put_req_header("content-type", "application/json")
1412 |> post("/api/v1/statuses/#{activity2.id}/bookmark")
1414 assert json_response_and_validate_schema(response2, 200)["bookmarked"] == true
1416 bookmarks = get(conn, bookmarks_uri)
1419 json_response_and_validate_schema(response2, 200),
1420 json_response_and_validate_schema(response1, 200)
1422 json_response_and_validate_schema(bookmarks, 200)
1426 |> put_req_header("content-type", "application/json")
1427 |> post("/api/v1/statuses/#{activity1.id}/unbookmark")
1429 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == false
1431 bookmarks = get(conn, bookmarks_uri)
1433 assert [json_response_and_validate_schema(response2, 200)] ==
1434 json_response_and_validate_schema(bookmarks, 200)
1437 describe "conversation muting" do
1438 setup do: oauth_access(["write:mutes"])
1441 post_user = insert(:user)
1442 {:ok, activity} = CommonAPI.post(post_user, %{status: "HIE"})
1443 %{activity: activity}
1446 test "mute conversation", %{conn: conn, activity: activity} do
1447 id_str = to_string(activity.id)
1449 assert %{"id" => ^id_str, "muted" => true} =
1451 |> put_req_header("content-type", "application/json")
1452 |> post("/api/v1/statuses/#{activity.id}/mute")
1453 |> json_response_and_validate_schema(200)
1456 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
1457 {:ok, _} = CommonAPI.add_mute(user, activity)
1461 |> put_req_header("content-type", "application/json")
1462 |> post("/api/v1/statuses/#{activity.id}/mute")
1464 assert json_response_and_validate_schema(conn, 400) == %{
1465 "error" => "conversation is already muted"
1469 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
1470 {:ok, _} = CommonAPI.add_mute(user, activity)
1472 id_str = to_string(activity.id)
1474 assert %{"id" => ^id_str, "muted" => false} =
1476 # |> assign(:user, user)
1477 |> post("/api/v1/statuses/#{activity.id}/unmute")
1478 |> json_response_and_validate_schema(200)
1482 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
1483 user1 = insert(:user)
1484 user2 = insert(:user)
1485 user3 = insert(:user)
1487 {:ok, replied_to} = CommonAPI.post(user1, %{status: "cofe"})
1489 # Reply to status from another user
1492 |> assign(:user, user2)
1493 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["write:statuses"]))
1494 |> put_req_header("content-type", "application/json")
1495 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
1497 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn1, 200)
1499 activity = Activity.get_by_id_with_object(id)
1501 assert Object.normalize(activity, fetch: false).data["inReplyTo"] ==
1502 Object.normalize(replied_to, fetch: false).data["id"]
1504 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
1506 # Reblog from the third user
1509 |> assign(:user, user3)
1510 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["write:statuses"]))
1511 |> put_req_header("content-type", "application/json")
1512 |> post("/api/v1/statuses/#{activity.id}/reblog")
1514 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
1515 json_response_and_validate_schema(conn2, 200)
1517 assert to_string(activity.id) == id
1519 # Getting third user status
1522 |> assign(:user, user3)
1523 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1524 |> get("api/v1/timelines/home")
1526 [reblogged_activity] = json_response(conn3, 200)
1528 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
1530 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
1531 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
1534 describe "GET /api/v1/statuses/:id/favourited_by" do
1535 setup do: oauth_access(["read:accounts"])
1537 setup %{user: user} do
1538 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1540 %{activity: activity}
1543 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
1544 other_user = insert(:user)
1545 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1549 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1550 |> json_response_and_validate_schema(:ok)
1552 [%{"id" => id}] = response
1554 assert id == other_user.id
1557 test "returns empty array when status has not been favorited yet", %{
1563 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1564 |> json_response_and_validate_schema(:ok)
1566 assert Enum.empty?(response)
1569 test "does not return users who have favorited the status but are blocked", %{
1570 conn: %{assigns: %{user: user}} = conn,
1573 other_user = insert(:user)
1574 {:ok, _user_relationship} = User.block(user, other_user)
1576 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1580 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1581 |> json_response_and_validate_schema(:ok)
1583 assert Enum.empty?(response)
1586 test "does not fail on an unauthenticated request", %{activity: activity} do
1587 other_user = insert(:user)
1588 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1592 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1593 |> json_response_and_validate_schema(:ok)
1595 [%{"id" => id}] = response
1596 assert id == other_user.id
1599 test "requires authentication for private posts", %{user: user} do
1600 other_user = insert(:user)
1603 CommonAPI.post(user, %{
1604 status: "@#{other_user.nickname} wanna get some #cofe together?",
1605 visibility: "direct"
1608 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1610 favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by"
1613 |> get(favourited_by_url)
1614 |> json_response_and_validate_schema(404)
1618 |> assign(:user, other_user)
1619 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1622 |> assign(:token, nil)
1623 |> get(favourited_by_url)
1624 |> json_response_and_validate_schema(404)
1628 |> get(favourited_by_url)
1629 |> json_response_and_validate_schema(200)
1631 [%{"id" => id}] = response
1632 assert id == other_user.id
1635 test "returns empty array when :show_reactions is disabled", %{conn: conn, activity: activity} do
1636 clear_config([:instance, :show_reactions], false)
1638 other_user = insert(:user)
1639 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1643 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1644 |> json_response_and_validate_schema(:ok)
1646 assert Enum.empty?(response)
1650 describe "GET /api/v1/statuses/:id/reblogged_by" do
1651 setup do: oauth_access(["read:accounts"])
1653 setup %{user: user} do
1654 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1656 %{activity: activity}
1659 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1660 other_user = insert(:user)
1661 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1665 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1666 |> json_response_and_validate_schema(:ok)
1668 [%{"id" => id}] = response
1670 assert id == other_user.id
1673 test "returns empty array when status has not been reblogged yet", %{
1679 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1680 |> json_response_and_validate_schema(:ok)
1682 assert Enum.empty?(response)
1685 test "does not return users who have reblogged the status but are blocked", %{
1686 conn: %{assigns: %{user: user}} = conn,
1689 other_user = insert(:user)
1690 {:ok, _user_relationship} = User.block(user, other_user)
1692 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1696 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1697 |> json_response_and_validate_schema(:ok)
1699 assert Enum.empty?(response)
1702 test "does not return users who have reblogged the status privately", %{
1705 other_user = insert(:user)
1706 {:ok, activity} = CommonAPI.post(other_user, %{status: "my secret post"})
1708 {:ok, _} = CommonAPI.repeat(activity.id, other_user, %{visibility: "private"})
1712 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1713 |> json_response_and_validate_schema(:ok)
1715 assert Enum.empty?(response)
1718 test "does not fail on an unauthenticated request", %{activity: activity} do
1719 other_user = insert(:user)
1720 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1724 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1725 |> json_response_and_validate_schema(:ok)
1727 [%{"id" => id}] = response
1728 assert id == other_user.id
1731 test "requires authentication for private posts", %{user: user} do
1732 other_user = insert(:user)
1735 CommonAPI.post(user, %{
1736 status: "@#{other_user.nickname} wanna get some #cofe together?",
1737 visibility: "direct"
1741 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1742 |> json_response_and_validate_schema(404)
1746 |> assign(:user, other_user)
1747 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1748 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1749 |> json_response_and_validate_schema(200)
1751 assert [] == response
1756 user = insert(:user)
1758 {:ok, %{id: id1}} = CommonAPI.post(user, %{status: "1"})
1759 {:ok, %{id: id2}} = CommonAPI.post(user, %{status: "2", in_reply_to_status_id: id1})
1760 {:ok, %{id: id3}} = CommonAPI.post(user, %{status: "3", in_reply_to_status_id: id2})
1761 {:ok, %{id: id4}} = CommonAPI.post(user, %{status: "4", in_reply_to_status_id: id3})
1762 {:ok, %{id: id5}} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
1766 |> get("/api/v1/statuses/#{id3}/context")
1767 |> json_response_and_validate_schema(:ok)
1770 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1771 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1775 test "favorites paginate correctly" do
1776 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1777 other_user = insert(:user)
1778 {:ok, first_post} = CommonAPI.post(other_user, %{status: "bla"})
1779 {:ok, second_post} = CommonAPI.post(other_user, %{status: "bla"})
1780 {:ok, third_post} = CommonAPI.post(other_user, %{status: "bla"})
1782 {:ok, _first_favorite} = CommonAPI.favorite(user, third_post.id)
1783 {:ok, _second_favorite} = CommonAPI.favorite(user, first_post.id)
1784 {:ok, third_favorite} = CommonAPI.favorite(user, second_post.id)
1788 |> get("/api/v1/favourites?limit=1")
1790 assert [%{"id" => post_id}] = json_response_and_validate_schema(result, 200)
1791 assert post_id == second_post.id
1793 # Using the header for pagination works correctly
1794 [next, _] = get_resp_header(result, "link") |> hd() |> String.split(", ")
1795 [_, max_id] = Regex.run(~r/max_id=([^&]+)/, next)
1797 assert max_id == third_favorite.id
1801 |> get("/api/v1/favourites?max_id=#{max_id}")
1803 assert [%{"id" => first_post_id}, %{"id" => third_post_id}] =
1804 json_response_and_validate_schema(result, 200)
1806 assert first_post_id == first_post.id
1807 assert third_post_id == third_post.id
1810 test "returns the favorites of a user" do
1811 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1812 other_user = insert(:user)
1814 {:ok, _} = CommonAPI.post(other_user, %{status: "bla"})
1815 {:ok, activity} = CommonAPI.post(other_user, %{status: "trees are happy"})
1817 {:ok, last_like} = CommonAPI.favorite(user, activity.id)
1819 first_conn = get(conn, "/api/v1/favourites")
1821 assert [status] = json_response_and_validate_schema(first_conn, 200)
1822 assert status["id"] == to_string(activity.id)
1824 assert [{"link", _link_header}] =
1825 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1827 # Honours query params
1828 {:ok, second_activity} =
1829 CommonAPI.post(other_user, %{
1830 status: "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1833 {:ok, _} = CommonAPI.favorite(user, second_activity.id)
1835 second_conn = get(conn, "/api/v1/favourites?since_id=#{last_like.id}")
1837 assert [second_status] = json_response_and_validate_schema(second_conn, 200)
1838 assert second_status["id"] == to_string(second_activity.id)
1840 third_conn = get(conn, "/api/v1/favourites?limit=0")
1842 assert [] = json_response_and_validate_schema(third_conn, 200)
1845 test "expires_at is nil for another user" do
1846 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1847 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
1848 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", expires_in: 1_000_000})
1850 assert %{"pleroma" => %{"expires_at" => a_expires_at}} =
1852 |> get("/api/v1/statuses/#{activity.id}")
1853 |> json_response_and_validate_schema(:ok)
1855 {:ok, a_expires_at, 0} = DateTime.from_iso8601(a_expires_at)
1856 assert DateTime.diff(expires_at, a_expires_at) == 0
1858 %{conn: conn} = oauth_access(["read:statuses"])
1860 assert %{"pleroma" => %{"expires_at" => nil}} =
1862 |> get("/api/v1/statuses/#{activity.id}")
1863 |> json_response_and_validate_schema(:ok)
1866 test "posting a local only status" do
1867 %{user: _user, conn: conn} = oauth_access(["write:statuses"])
1871 |> put_req_header("content-type", "application/json")
1872 |> post("/api/v1/statuses", %{
1874 "visibility" => "local"
1877 local = Pleroma.Constants.as_local_public()
1879 assert %{"content" => "cofe", "id" => id, "visibility" => "local"} =
1880 json_response(conn_one, 200)
1882 assert %Activity{id: ^id, data: %{"to" => [^local]}} = Activity.get_by_id(id)
1885 describe "muted reactions" do
1887 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1889 other_user = insert(:user)
1890 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1892 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1893 User.mute(user, other_user)
1897 |> get("/api/v1/statuses/?ids[]=#{activity.id}")
1898 |> json_response_and_validate_schema(200)
1903 "emoji_reactions" => []
1910 |> get("/api/v1/statuses/?ids[]=#{activity.id}&with_muted=true")
1911 |> json_response_and_validate_schema(200)
1916 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
1923 # %{conn: conn, user: user, token: token} = oauth_access(["read:statuses"])
1924 %{conn: conn, user: user, token: _token} = oauth_access(["read:statuses"])
1926 other_user = insert(:user)
1927 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1929 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1930 User.mute(user, other_user)
1934 |> get("/api/v1/statuses/#{activity.id}")
1935 |> json_response_and_validate_schema(200)
1939 "emoji_reactions" => []
1945 |> get("/api/v1/statuses/#{activity.id}?with_muted=true")
1946 |> json_response_and_validate_schema(200)
1950 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]