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.ActivityPub.Utils
18 alias Pleroma.Web.CommonAPI
19 alias Pleroma.Workers.ScheduledActivityWorker
21 import Pleroma.Factory
23 setup do: clear_config([:instance, :federating])
24 setup do: clear_config([:instance, :allow_relay])
25 setup do: clear_config([:rich_media, :enabled])
26 setup do: clear_config([:mrf, :policies])
27 setup do: clear_config([:mrf_keyword, :reject])
28 setup do: clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
29 setup do: clear_config([Pleroma.Uploaders.Local, :uploads], "uploads")
31 describe "posting statuses" do
32 setup do: oauth_access(["write:statuses"])
34 test "posting a status does not increment reblog_count when relaying", %{conn: conn} do
35 clear_config([:instance, :federating], true)
36 Config.get([:instance, :allow_relay], true)
40 |> put_req_header("content-type", "application/json")
41 |> post("api/v1/statuses", %{
42 "content_type" => "text/plain",
43 "source" => "Pleroma FE",
44 "status" => "Hello world",
45 "visibility" => "public"
47 |> json_response_and_validate_schema(200)
49 assert response["reblogs_count"] == 0
50 ObanHelpers.perform_all()
54 |> get("api/v1/statuses/#{response["id"]}", %{})
55 |> json_response_and_validate_schema(200)
57 assert response["reblogs_count"] == 0
60 test "posting a status", %{conn: conn} do
61 idempotency_key = "Pikachu rocks!"
65 |> put_req_header("content-type", "application/json")
66 |> put_req_header("idempotency-key", idempotency_key)
67 |> post("/api/v1/statuses", %{
69 "spoiler_text" => "2hu",
73 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
74 json_response_and_validate_schema(conn_one, 200)
76 assert Activity.get_by_id(id)
80 |> put_req_header("content-type", "application/json")
81 |> put_req_header("idempotency-key", idempotency_key)
82 |> post("/api/v1/statuses", %{
84 "spoiler_text" => "2hu",
88 # Idempotency plug response means detection fail
89 assert %{"id" => second_id} = json_response(conn_two, 200)
90 assert id == second_id
94 |> put_req_header("content-type", "application/json")
95 |> post("/api/v1/statuses", %{
97 "spoiler_text" => "2hu",
98 "sensitive" => "False"
101 assert %{"id" => third_id} = json_response_and_validate_schema(conn_three, 200)
102 refute id == third_id
104 # An activity that will expire:
106 expires_in = 2 * 60 * 60
108 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
112 |> put_req_header("content-type", "application/json")
113 |> post("api/v1/statuses", %{
114 "status" => "oolong",
115 "expires_in" => expires_in
118 assert %{"id" => fourth_id} = json_response_and_validate_schema(conn_four, 200)
120 assert Activity.get_by_id(fourth_id)
123 worker: Pleroma.Workers.PurgeExpiredActivity,
124 args: %{activity_id: fourth_id},
125 scheduled_at: expires_at
129 test "it fails to create a status if `expires_in` is less or equal than an hour", %{
135 assert %{"error" => "Expiry date is too soon"} =
137 |> put_req_header("content-type", "application/json")
138 |> post("api/v1/statuses", %{
139 "status" => "oolong",
140 "expires_in" => expires_in
142 |> json_response_and_validate_schema(422)
147 assert %{"error" => "Expiry date is too soon"} =
149 |> put_req_header("content-type", "application/json")
150 |> post("api/v1/statuses", %{
151 "status" => "oolong",
152 "expires_in" => expires_in
154 |> json_response_and_validate_schema(422)
157 test "Get MRF reason when posting a status is rejected by one", %{conn: conn} do
158 clear_config([:mrf_keyword, :reject], ["GNO"])
159 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
161 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} =
163 |> put_req_header("content-type", "application/json")
164 |> post("api/v1/statuses", %{"status" => "GNO/Linux"})
165 |> json_response_and_validate_schema(422)
168 test "posting an undefined status with an attachment", %{user: user, conn: conn} do
170 content_type: "image/jpeg",
171 path: Path.absname("test/fixtures/image.jpg"),
172 filename: "an_image.jpg"
175 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
179 |> put_req_header("content-type", "application/json")
180 |> post("/api/v1/statuses", %{
181 "media_ids" => [to_string(upload.id)]
184 assert json_response_and_validate_schema(conn, 200)
187 test "replying to a status", %{user: user, conn: conn} do
188 {:ok, replied_to} = CommonAPI.post(user, %{status: "cofe"})
192 |> put_req_header("content-type", "application/json")
193 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
195 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn, 200)
197 activity = Activity.get_by_id(id)
199 assert activity.data["context"] == replied_to.data["context"]
200 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
203 test "replying to a direct message with visibility other than direct", %{
207 {:ok, replied_to} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
209 Enum.each(["public", "private", "unlisted"], fn visibility ->
212 |> put_req_header("content-type", "application/json")
213 |> post("/api/v1/statuses", %{
214 "status" => "@#{user.nickname} hey",
215 "in_reply_to_id" => replied_to.id,
216 "visibility" => visibility
219 assert json_response_and_validate_schema(conn, 422) == %{
220 "error" => "The message visibility must be direct"
225 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
228 |> put_req_header("content-type", "application/json")
229 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
231 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn, 200)
232 assert Activity.get_by_id(id)
235 test "posting a sensitive status", %{conn: conn} do
238 |> put_req_header("content-type", "application/json")
239 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
241 assert %{"content" => "cofe", "id" => id, "sensitive" => true} =
242 json_response_and_validate_schema(conn, 200)
244 assert Activity.get_by_id(id)
247 test "posting a fake status", %{conn: conn} do
250 |> put_req_header("content-type", "application/json")
251 |> post("/api/v1/statuses", %{
253 "\"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"
256 real_status = json_response_and_validate_schema(real_conn, 200)
259 assert Object.get_by_ap_id(real_status["uri"])
263 |> Map.put("id", nil)
264 |> Map.put("url", nil)
265 |> Map.put("uri", nil)
266 |> Map.put("created_at", nil)
267 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
271 |> assign(:user, refresh_record(conn.assigns.user))
272 |> put_req_header("content-type", "application/json")
273 |> post("/api/v1/statuses", %{
275 "\"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",
279 fake_status = json_response_and_validate_schema(fake_conn, 200)
282 refute Object.get_by_ap_id(fake_status["uri"])
286 |> Map.put("id", nil)
287 |> Map.put("url", nil)
288 |> Map.put("uri", nil)
289 |> Map.put("created_at", nil)
290 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
292 assert real_status == fake_status
295 test "fake statuses' preview card is not cached", %{conn: conn} do
296 clear_config([:rich_media, :enabled], true)
301 url: "https://example.com/twitter-card"
303 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
306 apply(HttpRequestMock, :request, [env])
311 |> put_req_header("content-type", "application/json")
312 |> post("/api/v1/statuses", %{
313 "status" => "https://example.com/ogp",
319 |> put_req_header("content-type", "application/json")
320 |> post("/api/v1/statuses", %{
321 "status" => "https://example.com/twitter-card",
325 assert %{"card" => %{"title" => "The Rock"}} = json_response_and_validate_schema(conn1, 200)
327 assert %{"card" => %{"title" => "Small Island Developing States Photo Submission"}} =
328 json_response_and_validate_schema(conn2, 200)
331 test "posting a status with OGP link preview", %{conn: conn} do
332 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
333 clear_config([:rich_media, :enabled], true)
337 |> put_req_header("content-type", "application/json")
338 |> post("/api/v1/statuses", %{
339 "status" => "https://example.com/ogp"
342 assert %{"id" => id, "card" => %{"title" => "The Rock"}} =
343 json_response_and_validate_schema(conn, 200)
345 assert Activity.get_by_id(id)
348 test "posting a direct status", %{conn: conn} do
349 user2 = insert(:user)
350 content = "direct cofe @#{user2.nickname}"
354 |> put_req_header("content-type", "application/json")
355 |> post("api/v1/statuses", %{"status" => content, "visibility" => "direct"})
357 assert %{"id" => id} = response = json_response_and_validate_schema(conn, 200)
358 assert response["visibility"] == "direct"
359 assert response["pleroma"]["direct_conversation_id"]
360 assert activity = Activity.get_by_id(id)
361 assert activity.recipients == [user2.ap_id, conn.assigns[:user].ap_id]
362 assert activity.data["to"] == [user2.ap_id]
363 assert activity.data["cc"] == []
366 test "discloses application metadata when enabled" do
367 user = insert(:user, disclose_client: true)
368 %{user: _user, token: token, conn: conn} = oauth_access(["write:statuses"], user: user)
370 %Pleroma.Web.OAuth.Token{
371 app: %Pleroma.Web.OAuth.App{
372 client_name: app_name,
379 |> put_req_header("content-type", "application/json")
380 |> post("/api/v1/statuses", %{
381 "status" => "cofe is my copilot"
385 "content" => "cofe is my copilot"
386 } = json_response_and_validate_schema(result, 200)
388 activity = result.assigns.activity.id
392 |> get("api/v1/statuses/#{activity}")
395 "content" => "cofe is my copilot",
398 "website" => ^app_website
400 } = json_response_and_validate_schema(result, 200)
403 test "hides application metadata when disabled" do
404 user = insert(:user, disclose_client: false)
405 %{user: _user, token: _token, conn: conn} = oauth_access(["write:statuses"], user: user)
409 |> put_req_header("content-type", "application/json")
410 |> post("/api/v1/statuses", %{
411 "status" => "club mate is my wingman"
414 assert %{"content" => "club mate is my wingman"} =
415 json_response_and_validate_schema(result, 200)
417 activity = result.assigns.activity.id
421 |> get("api/v1/statuses/#{activity}")
424 "content" => "club mate is my wingman",
426 } = json_response_and_validate_schema(result, 200)
430 describe "posting scheduled statuses" do
431 setup do: oauth_access(["write:statuses"])
433 test "creates a scheduled activity", %{conn: conn} do
435 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
436 |> NaiveDateTime.to_iso8601()
441 |> put_req_header("content-type", "application/json")
442 |> post("/api/v1/statuses", %{
443 "status" => "scheduled",
444 "scheduled_at" => scheduled_at
447 assert %{"scheduled_at" => expected_scheduled_at} =
448 json_response_and_validate_schema(conn, 200)
450 assert expected_scheduled_at == CommonAPI.Utils.to_masto_date(scheduled_at)
451 assert [] == Repo.all(Activity)
454 test "with expiration" do
455 %{conn: conn} = oauth_access(["write:statuses", "read:statuses"])
458 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(6), :millisecond)
459 |> NaiveDateTime.to_iso8601()
462 assert %{"id" => status_id, "params" => %{"expires_in" => 300}} =
464 |> put_req_header("content-type", "application/json")
465 |> post("/api/v1/statuses", %{
466 "status" => "scheduled",
467 "scheduled_at" => scheduled_at,
470 |> json_response_and_validate_schema(200)
472 assert %{"id" => ^status_id, "params" => %{"expires_in" => 300}} =
474 |> put_req_header("content-type", "application/json")
475 |> get("/api/v1/scheduled_statuses/#{status_id}")
476 |> json_response_and_validate_schema(200)
479 test "ignores nil values", %{conn: conn} do
482 |> put_req_header("content-type", "application/json")
483 |> post("/api/v1/statuses", %{
484 "status" => "not scheduled",
485 "scheduled_at" => nil
488 assert result = json_response_and_validate_schema(conn, 200)
489 assert Activity.get_by_id(result["id"])
492 test "creates a scheduled activity with a media attachment", %{user: user, conn: conn} do
494 NaiveDateTime.utc_now()
495 |> NaiveDateTime.add(:timer.minutes(120), :millisecond)
496 |> NaiveDateTime.to_iso8601()
500 content_type: "image/jpeg",
501 path: Path.absname("test/fixtures/image.jpg"),
502 filename: "an_image.jpg"
505 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
509 |> put_req_header("content-type", "application/json")
510 |> post("/api/v1/statuses", %{
511 "media_ids" => [to_string(upload.id)],
512 "status" => "scheduled",
513 "scheduled_at" => scheduled_at
516 assert %{"media_attachments" => [media_attachment]} =
517 json_response_and_validate_schema(conn, 200)
519 assert %{"type" => "image"} = media_attachment
522 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
525 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
526 |> NaiveDateTime.to_iso8601()
531 |> put_req_header("content-type", "application/json")
532 |> post("/api/v1/statuses", %{
533 "status" => "not scheduled",
534 "scheduled_at" => scheduled_at
537 assert %{"content" => "not scheduled"} = json_response_and_validate_schema(conn, 200)
538 assert [] == Repo.all(ScheduledActivity)
541 test "returns error when daily user limit is exceeded", %{user: user, conn: conn} do
543 NaiveDateTime.utc_now()
544 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
545 |> NaiveDateTime.to_iso8601()
549 attrs = %{params: %{}, scheduled_at: today}
550 {:ok, _} = ScheduledActivity.create(user, attrs)
551 {:ok, _} = ScheduledActivity.create(user, attrs)
555 |> put_req_header("content-type", "application/json")
556 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
558 assert %{"error" => "daily limit exceeded"} == json_response_and_validate_schema(conn, 422)
561 test "returns error when total user limit is exceeded", %{user: user, conn: conn} do
563 NaiveDateTime.utc_now()
564 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
565 |> NaiveDateTime.to_iso8601()
569 NaiveDateTime.utc_now()
570 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
571 |> NaiveDateTime.to_iso8601()
574 attrs = %{params: %{}, scheduled_at: today}
575 {:ok, _} = ScheduledActivity.create(user, attrs)
576 {:ok, _} = ScheduledActivity.create(user, attrs)
577 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
581 |> put_req_header("content-type", "application/json")
582 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
584 assert %{"error" => "total limit exceeded"} == json_response_and_validate_schema(conn, 422)
588 describe "posting polls" do
589 setup do: oauth_access(["write:statuses"])
591 test "posting a poll", %{conn: conn} do
592 time = NaiveDateTime.utc_now()
596 |> put_req_header("content-type", "application/json")
597 |> post("/api/v1/statuses", %{
598 "status" => "Who is the #bestgrill?",
600 "options" => ["Rei", "Asuka", "Misato"],
605 response = json_response_and_validate_schema(conn, 200)
607 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
608 title in ["Rei", "Asuka", "Misato"]
611 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
612 assert response["poll"]["expired"] == false
614 question = Object.get_by_id(response["poll"]["id"])
616 # closed contains utc timezone
617 assert question.data["closed"] =~ "Z"
620 test "option limit is enforced", %{conn: conn} do
621 limit = Config.get([:instance, :poll_limits, :max_options])
625 |> put_req_header("content-type", "application/json")
626 |> post("/api/v1/statuses", %{
628 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
631 %{"error" => error} = json_response_and_validate_schema(conn, 422)
632 assert error == "Poll can't contain more than #{limit} options"
635 test "option character limit is enforced", %{conn: conn} do
636 limit = Config.get([:instance, :poll_limits, :max_option_chars])
640 |> put_req_header("content-type", "application/json")
641 |> post("/api/v1/statuses", %{
644 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
649 %{"error" => error} = json_response_and_validate_schema(conn, 422)
650 assert error == "Poll options cannot be longer than #{limit} characters each"
653 test "minimal date limit is enforced", %{conn: conn} do
654 limit = Config.get([:instance, :poll_limits, :min_expiration])
658 |> put_req_header("content-type", "application/json")
659 |> post("/api/v1/statuses", %{
660 "status" => "imagine arbitrary limits",
662 "options" => ["this post was made by pleroma gang"],
663 "expires_in" => limit - 1
667 %{"error" => error} = json_response_and_validate_schema(conn, 422)
668 assert error == "Expiration date is too soon"
671 test "maximum date limit is enforced", %{conn: conn} do
672 limit = Config.get([:instance, :poll_limits, :max_expiration])
676 |> put_req_header("content-type", "application/json")
677 |> post("/api/v1/statuses", %{
678 "status" => "imagine arbitrary limits",
680 "options" => ["this post was made by pleroma gang"],
681 "expires_in" => limit + 1
685 %{"error" => error} = json_response_and_validate_schema(conn, 422)
686 assert error == "Expiration date is too far in the future"
689 test "scheduled poll", %{conn: conn} do
690 clear_config([ScheduledActivity, :enabled], true)
693 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(6), :millisecond)
694 |> NaiveDateTime.to_iso8601()
697 %{"id" => scheduled_id} =
699 |> put_req_header("content-type", "application/json")
700 |> post("/api/v1/statuses", %{
701 "status" => "very cool poll",
703 "options" => ~w(a b c),
706 "scheduled_at" => scheduled_at
708 |> json_response_and_validate_schema(200)
710 assert {:ok, %{id: activity_id}} =
711 perform_job(ScheduledActivityWorker, %{
712 activity_id: scheduled_id
715 refute_enqueued(worker: ScheduledActivityWorker)
719 |> Repo.get(activity_id)
720 |> Object.normalize()
722 assert object.data["content"] == "very cool poll"
723 assert object.data["type"] == "Question"
724 assert length(object.data["oneOf"]) == 3
728 test "get a status" do
729 %{conn: conn} = oauth_access(["read:statuses"])
730 activity = insert(:note_activity)
732 conn = get(conn, "/api/v1/statuses/#{activity.id}")
734 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
735 assert id == to_string(activity.id)
738 defp local_and_remote_activities do
739 local = insert(:note_activity)
740 remote = insert(:note_activity, local: false)
741 {:ok, local: local, remote: remote}
744 describe "status with restrict unauthenticated activities for local and remote" do
745 setup do: local_and_remote_activities()
747 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
749 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
751 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
752 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
754 assert json_response_and_validate_schema(res_conn, :not_found) == %{
755 "error" => "Record not found"
758 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
760 assert json_response_and_validate_schema(res_conn, :not_found) == %{
761 "error" => "Record not found"
765 test "if user is authenticated", %{local: local, remote: remote} do
766 %{conn: conn} = oauth_access(["read"])
767 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
768 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
770 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
771 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
775 describe "status with restrict unauthenticated activities for local" do
776 setup do: local_and_remote_activities()
778 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
780 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
781 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
783 assert json_response_and_validate_schema(res_conn, :not_found) == %{
784 "error" => "Record not found"
787 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
788 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
791 test "if user is authenticated", %{local: local, remote: remote} do
792 %{conn: conn} = oauth_access(["read"])
793 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
794 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
796 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
797 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
801 describe "status with restrict unauthenticated activities for remote" do
802 setup do: local_and_remote_activities()
804 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
806 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
807 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
808 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
810 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
812 assert json_response_and_validate_schema(res_conn, :not_found) == %{
813 "error" => "Record not found"
817 test "if user is authenticated", %{local: local, remote: remote} do
818 %{conn: conn} = oauth_access(["read"])
819 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
820 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
822 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
823 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
827 test "getting a status that doesn't exist returns 404" do
828 %{conn: conn} = oauth_access(["read:statuses"])
829 activity = insert(:note_activity)
831 conn = get(conn, "/api/v1/statuses/#{String.downcase(activity.id)}")
833 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
836 test "get a direct status" do
837 %{user: user, conn: conn} = oauth_access(["read:statuses"])
838 other_user = insert(:user)
841 CommonAPI.post(user, %{status: "@#{other_user.nickname}", visibility: "direct"})
845 |> assign(:user, user)
846 |> get("/api/v1/statuses/#{activity.id}")
848 [participation] = Participation.for_user(user)
850 res = json_response_and_validate_schema(conn, 200)
851 assert res["pleroma"]["direct_conversation_id"] == participation.id
854 test "get statuses by IDs" do
855 %{conn: conn} = oauth_access(["read:statuses"])
856 %{id: id1} = insert(:note_activity)
857 %{id: id2} = insert(:note_activity)
859 query_string = "ids[]=#{id1}&ids[]=#{id2}"
860 conn = get(conn, "/api/v1/statuses/?#{query_string}")
862 assert [%{"id" => ^id1}, %{"id" => ^id2}] =
863 Enum.sort_by(json_response_and_validate_schema(conn, :ok), & &1["id"])
866 describe "getting statuses by ids with restricted unauthenticated for local and remote" do
867 setup do: local_and_remote_activities()
869 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
871 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
873 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
874 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
876 assert json_response_and_validate_schema(res_conn, 200) == []
879 test "if user is authenticated", %{local: local, remote: remote} do
880 %{conn: conn} = oauth_access(["read"])
882 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
884 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
888 describe "getting statuses by ids with restricted unauthenticated for local" do
889 setup do: local_and_remote_activities()
891 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
893 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
894 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
896 remote_id = remote.id
897 assert [%{"id" => ^remote_id}] = json_response_and_validate_schema(res_conn, 200)
900 test "if user is authenticated", %{local: local, remote: remote} do
901 %{conn: conn} = oauth_access(["read"])
903 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
905 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
909 describe "getting statuses by ids with restricted unauthenticated for remote" do
910 setup do: local_and_remote_activities()
912 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
914 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
915 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
918 assert [%{"id" => ^local_id}] = json_response_and_validate_schema(res_conn, 200)
921 test "if user is authenticated", %{local: local, remote: remote} do
922 %{conn: conn} = oauth_access(["read"])
924 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
926 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
930 describe "deleting a status" do
931 test "when you created it" do
932 %{user: author, conn: conn} = oauth_access(["write:statuses"])
933 activity = insert(:note_activity, user: author)
934 object = Object.normalize(activity, fetch: false)
936 content = object.data["content"]
937 source = object.data["source"]
941 |> assign(:user, author)
942 |> delete("/api/v1/statuses/#{activity.id}")
943 |> json_response_and_validate_schema(200)
945 assert match?(%{"content" => ^content, "text" => ^source}, result)
947 refute Activity.get_by_id(activity.id)
950 test "when it doesn't exist" do
951 %{user: author, conn: conn} = oauth_access(["write:statuses"])
952 activity = insert(:note_activity, user: author)
956 |> assign(:user, author)
957 |> delete("/api/v1/statuses/#{String.downcase(activity.id)}")
959 assert %{"error" => "Record not found"} == json_response_and_validate_schema(conn, 404)
962 test "when you didn't create it" do
963 %{conn: conn} = oauth_access(["write:statuses"])
964 activity = insert(:note_activity)
966 conn = delete(conn, "/api/v1/statuses/#{activity.id}")
968 assert %{"error" => "Record not found"} == json_response_and_validate_schema(conn, 404)
970 assert Activity.get_by_id(activity.id) == activity
973 test "when you're an admin or moderator", %{conn: conn} do
974 activity1 = insert(:note_activity)
975 activity2 = insert(:note_activity)
976 admin = insert(:user, is_admin: true)
977 moderator = insert(:user, is_moderator: true)
981 |> assign(:user, admin)
982 |> assign(:token, insert(:oauth_token, user: admin, scopes: ["write:statuses"]))
983 |> delete("/api/v1/statuses/#{activity1.id}")
985 assert %{} = json_response_and_validate_schema(res_conn, 200)
989 |> assign(:user, moderator)
990 |> assign(:token, insert(:oauth_token, user: moderator, scopes: ["write:statuses"]))
991 |> delete("/api/v1/statuses/#{activity2.id}")
993 assert %{} = json_response_and_validate_schema(res_conn, 200)
995 refute Activity.get_by_id(activity1.id)
996 refute Activity.get_by_id(activity2.id)
1000 describe "reblogging" do
1001 setup do: oauth_access(["write:statuses"])
1003 test "reblogs and returns the reblogged status", %{conn: conn} do
1004 activity = insert(:note_activity)
1008 |> put_req_header("content-type", "application/json")
1009 |> post("/api/v1/statuses/#{activity.id}/reblog")
1012 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1014 } = json_response_and_validate_schema(conn, 200)
1016 assert to_string(activity.id) == id
1019 test "returns 404 if the reblogged status doesn't exist", %{conn: conn} do
1020 activity = insert(:note_activity)
1024 |> put_req_header("content-type", "application/json")
1025 |> post("/api/v1/statuses/#{String.downcase(activity.id)}/reblog")
1027 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
1030 test "reblogs privately and returns the reblogged status", %{conn: conn} do
1031 activity = insert(:note_activity)
1035 |> put_req_header("content-type", "application/json")
1037 "/api/v1/statuses/#{activity.id}/reblog",
1038 %{"visibility" => "private"}
1042 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1043 "reblogged" => true,
1044 "visibility" => "private"
1045 } = json_response_and_validate_schema(conn, 200)
1047 assert to_string(activity.id) == id
1050 test "reblogged status for another user" do
1051 activity = insert(:note_activity)
1052 user1 = insert(:user)
1053 user2 = insert(:user)
1054 user3 = insert(:user)
1055 {:ok, _} = CommonAPI.favorite(user2, activity.id)
1056 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
1057 {:ok, reblog_activity1} = CommonAPI.repeat(activity.id, user1)
1058 {:ok, _} = CommonAPI.repeat(activity.id, user2)
1062 |> assign(:user, user3)
1063 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1064 |> get("/api/v1/statuses/#{reblog_activity1.id}")
1067 "reblog" => %{"id" => _id, "reblogged" => false, "reblogs_count" => 2},
1068 "reblogged" => false,
1069 "favourited" => false,
1070 "bookmarked" => false
1071 } = json_response_and_validate_schema(conn_res, 200)
1075 |> assign(:user, user2)
1076 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["read:statuses"]))
1077 |> get("/api/v1/statuses/#{reblog_activity1.id}")
1080 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
1081 "reblogged" => true,
1082 "favourited" => true,
1083 "bookmarked" => true
1084 } = json_response_and_validate_schema(conn_res, 200)
1086 assert to_string(activity.id) == id
1089 test "author can reblog own private status", %{conn: conn, user: user} do
1090 {:ok, activity} = CommonAPI.post(user, %{status: "cofe", visibility: "private"})
1094 |> put_req_header("content-type", "application/json")
1095 |> post("/api/v1/statuses/#{activity.id}/reblog")
1098 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1099 "reblogged" => true,
1100 "visibility" => "private"
1101 } = json_response_and_validate_schema(conn, 200)
1103 assert to_string(activity.id) == id
1107 describe "unreblogging" do
1108 setup do: oauth_access(["write:statuses"])
1110 test "unreblogs and returns the unreblogged status", %{user: user, conn: conn} do
1111 activity = insert(:note_activity)
1113 {:ok, _} = CommonAPI.repeat(activity.id, user)
1117 |> put_req_header("content-type", "application/json")
1118 |> post("/api/v1/statuses/#{activity.id}/unreblog")
1120 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} =
1121 json_response_and_validate_schema(conn, 200)
1123 assert to_string(activity.id) == id
1126 test "returns 404 error when activity does not exist", %{conn: conn} do
1129 |> put_req_header("content-type", "application/json")
1130 |> post("/api/v1/statuses/foo/unreblog")
1132 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1136 describe "favoriting" do
1137 setup do: oauth_access(["write:favourites"])
1139 test "favs a status and returns it", %{conn: conn} do
1140 activity = insert(:note_activity)
1144 |> put_req_header("content-type", "application/json")
1145 |> post("/api/v1/statuses/#{activity.id}/favourite")
1147 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
1148 json_response_and_validate_schema(conn, 200)
1150 assert to_string(activity.id) == id
1153 test "favoriting twice will just return 200", %{conn: conn} do
1154 activity = insert(:note_activity)
1157 |> put_req_header("content-type", "application/json")
1158 |> post("/api/v1/statuses/#{activity.id}/favourite")
1161 |> put_req_header("content-type", "application/json")
1162 |> post("/api/v1/statuses/#{activity.id}/favourite")
1163 |> json_response_and_validate_schema(200)
1166 test "returns 404 error for a wrong id", %{conn: conn} do
1169 |> put_req_header("content-type", "application/json")
1170 |> post("/api/v1/statuses/1/favourite")
1172 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1176 describe "unfavoriting" do
1177 setup do: oauth_access(["write:favourites"])
1179 test "unfavorites a status and returns it", %{user: user, conn: conn} do
1180 activity = insert(:note_activity)
1182 {:ok, _} = CommonAPI.favorite(user, activity.id)
1186 |> put_req_header("content-type", "application/json")
1187 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
1189 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
1190 json_response_and_validate_schema(conn, 200)
1192 assert to_string(activity.id) == id
1195 test "returns 404 error for a wrong id", %{conn: conn} do
1198 |> put_req_header("content-type", "application/json")
1199 |> post("/api/v1/statuses/1/unfavourite")
1201 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1205 describe "pinned statuses" do
1206 setup do: oauth_access(["write:accounts"])
1208 setup %{user: user} do
1209 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1211 %{activity: activity}
1214 setup do: clear_config([:instance, :max_pinned_statuses], 1)
1216 test "pin status", %{conn: conn, user: user, activity: activity} do
1219 assert %{"id" => ^id, "pinned" => true} =
1221 |> put_req_header("content-type", "application/json")
1222 |> post("/api/v1/statuses/#{activity.id}/pin")
1223 |> json_response_and_validate_schema(200)
1225 assert [%{"id" => ^id, "pinned" => true}] =
1227 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1228 |> json_response_and_validate_schema(200)
1231 test "non authenticated user", %{activity: activity} do
1233 |> put_req_header("content-type", "application/json")
1234 |> post("/api/v1/statuses/#{activity.id}/pin")
1235 |> json_response(403) == %{"error" => "Invalid credentials."}
1238 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
1239 {:ok, dm} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
1243 |> put_req_header("content-type", "application/json")
1244 |> post("/api/v1/statuses/#{dm.id}/pin")
1246 assert json_response_and_validate_schema(conn, 422) == %{
1247 "error" => "Non-public status cannot be pinned"
1251 test "pin by another user", %{activity: activity} do
1252 %{conn: conn} = oauth_access(["write:accounts"])
1255 |> put_req_header("content-type", "application/json")
1256 |> post("/api/v1/statuses/#{activity.id}/pin")
1257 |> json_response(422) == %{"error" => "Someone else's status cannot be pinned"}
1260 test "unpin status", %{conn: conn, user: user, activity: activity} do
1261 {:ok, _} = CommonAPI.pin(activity.id, user)
1262 user = refresh_record(user)
1264 id_str = to_string(activity.id)
1266 assert %{"id" => ^id_str, "pinned" => false} =
1268 |> assign(:user, user)
1269 |> post("/api/v1/statuses/#{activity.id}/unpin")
1270 |> json_response_and_validate_schema(200)
1274 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1275 |> json_response_and_validate_schema(200)
1278 test "/unpin: returns 404 error when activity doesn't exist", %{conn: conn} do
1280 |> put_req_header("content-type", "application/json")
1281 |> post("/api/v1/statuses/1/unpin")
1282 |> json_response_and_validate_schema(404) == %{"error" => "Record not found"}
1285 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
1286 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
1288 id_str_one = to_string(activity_one.id)
1290 assert %{"id" => ^id_str_one, "pinned" => true} =
1292 |> put_req_header("content-type", "application/json")
1293 |> post("/api/v1/statuses/#{id_str_one}/pin")
1294 |> json_response_and_validate_schema(200)
1296 user = refresh_record(user)
1298 assert %{"error" => "You have already pinned the maximum number of statuses"} =
1300 |> assign(:user, user)
1301 |> post("/api/v1/statuses/#{activity_two.id}/pin")
1302 |> json_response_and_validate_schema(400)
1305 test "on pin removes deletion job, on unpin reschedule deletion" do
1306 %{conn: conn} = oauth_access(["write:accounts", "write:statuses"])
1307 expires_in = 2 * 60 * 60
1309 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
1311 assert %{"id" => id} =
1313 |> put_req_header("content-type", "application/json")
1314 |> post("api/v1/statuses", %{
1315 "status" => "oolong",
1316 "expires_in" => expires_in
1318 |> json_response_and_validate_schema(200)
1321 worker: Pleroma.Workers.PurgeExpiredActivity,
1322 args: %{activity_id: id},
1323 scheduled_at: expires_at
1326 assert %{"id" => ^id, "pinned" => true} =
1328 |> put_req_header("content-type", "application/json")
1329 |> post("/api/v1/statuses/#{id}/pin")
1330 |> json_response_and_validate_schema(200)
1333 worker: Pleroma.Workers.PurgeExpiredActivity,
1334 args: %{activity_id: id},
1335 scheduled_at: expires_at
1338 assert %{"id" => ^id, "pinned" => false} =
1340 |> put_req_header("content-type", "application/json")
1341 |> post("/api/v1/statuses/#{id}/unpin")
1342 |> json_response_and_validate_schema(200)
1345 worker: Pleroma.Workers.PurgeExpiredActivity,
1346 args: %{activity_id: id},
1347 scheduled_at: expires_at
1353 bookmarks_uri = "/api/v1/bookmarks"
1355 %{conn: conn} = oauth_access(["write:bookmarks", "read:bookmarks"])
1356 author = insert(:user)
1358 {:ok, activity1} = CommonAPI.post(author, %{status: "heweoo?"})
1359 {:ok, activity2} = CommonAPI.post(author, %{status: "heweoo!"})
1363 |> put_req_header("content-type", "application/json")
1364 |> post("/api/v1/statuses/#{activity1.id}/bookmark")
1366 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == true
1370 |> put_req_header("content-type", "application/json")
1371 |> post("/api/v1/statuses/#{activity2.id}/bookmark")
1373 assert json_response_and_validate_schema(response2, 200)["bookmarked"] == true
1375 bookmarks = get(conn, bookmarks_uri)
1378 json_response_and_validate_schema(response2, 200),
1379 json_response_and_validate_schema(response1, 200)
1381 json_response_and_validate_schema(bookmarks, 200)
1385 |> put_req_header("content-type", "application/json")
1386 |> post("/api/v1/statuses/#{activity1.id}/unbookmark")
1388 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == false
1390 bookmarks = get(conn, bookmarks_uri)
1392 assert [json_response_and_validate_schema(response2, 200)] ==
1393 json_response_and_validate_schema(bookmarks, 200)
1396 describe "conversation muting" do
1397 setup do: oauth_access(["write:mutes"])
1400 post_user = insert(:user)
1401 {:ok, activity} = CommonAPI.post(post_user, %{status: "HIE"})
1402 %{activity: activity}
1405 test "mute conversation", %{conn: conn, activity: activity} do
1406 id_str = to_string(activity.id)
1408 assert %{"id" => ^id_str, "muted" => true} =
1410 |> put_req_header("content-type", "application/json")
1411 |> post("/api/v1/statuses/#{activity.id}/mute")
1412 |> json_response_and_validate_schema(200)
1415 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
1416 {:ok, _} = CommonAPI.add_mute(user, activity)
1420 |> put_req_header("content-type", "application/json")
1421 |> post("/api/v1/statuses/#{activity.id}/mute")
1423 assert json_response_and_validate_schema(conn, 400) == %{
1424 "error" => "conversation is already muted"
1428 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
1429 {:ok, _} = CommonAPI.add_mute(user, activity)
1431 id_str = to_string(activity.id)
1433 assert %{"id" => ^id_str, "muted" => false} =
1435 # |> assign(:user, user)
1436 |> post("/api/v1/statuses/#{activity.id}/unmute")
1437 |> json_response_and_validate_schema(200)
1441 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
1442 user1 = insert(:user)
1443 user2 = insert(:user)
1444 user3 = insert(:user)
1446 {:ok, replied_to} = CommonAPI.post(user1, %{status: "cofe"})
1448 # Reply to status from another user
1451 |> assign(:user, user2)
1452 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["write:statuses"]))
1453 |> put_req_header("content-type", "application/json")
1454 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
1456 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn1, 200)
1458 activity = Activity.get_by_id_with_object(id)
1460 assert Object.normalize(activity, fetch: false).data["inReplyTo"] ==
1461 Object.normalize(replied_to, fetch: false).data["id"]
1463 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
1465 # Reblog from the third user
1468 |> assign(:user, user3)
1469 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["write:statuses"]))
1470 |> put_req_header("content-type", "application/json")
1471 |> post("/api/v1/statuses/#{activity.id}/reblog")
1473 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
1474 json_response_and_validate_schema(conn2, 200)
1476 assert to_string(activity.id) == id
1478 # Getting third user status
1481 |> assign(:user, user3)
1482 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1483 |> get("api/v1/timelines/home")
1485 [reblogged_activity] = json_response_and_validate_schema(conn3, 200)
1487 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
1489 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
1490 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
1493 describe "GET /api/v1/statuses/:id/favourited_by" do
1494 setup do: oauth_access(["read:accounts"])
1496 setup %{user: user} do
1497 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1499 %{activity: activity}
1502 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
1503 other_user = insert(:user)
1504 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1508 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1509 |> json_response_and_validate_schema(:ok)
1511 [%{"id" => id}] = response
1513 assert id == other_user.id
1516 test "returns empty array when status has not been favorited yet", %{
1522 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1523 |> json_response_and_validate_schema(:ok)
1525 assert Enum.empty?(response)
1528 test "does not return users who have favorited the status but are blocked", %{
1529 conn: %{assigns: %{user: user}} = conn,
1532 other_user = insert(:user)
1533 {:ok, _user_relationship} = User.block(user, other_user)
1535 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1539 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1540 |> json_response_and_validate_schema(:ok)
1542 assert Enum.empty?(response)
1545 test "does not fail on an unauthenticated request", %{activity: activity} do
1546 other_user = insert(:user)
1547 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1551 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1552 |> json_response_and_validate_schema(:ok)
1554 [%{"id" => id}] = response
1555 assert id == other_user.id
1558 test "requires authentication for private posts", %{user: user} do
1559 other_user = insert(:user)
1562 CommonAPI.post(user, %{
1563 status: "@#{other_user.nickname} wanna get some #cofe together?",
1564 visibility: "direct"
1567 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1569 favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by"
1572 |> get(favourited_by_url)
1573 |> json_response_and_validate_schema(404)
1577 |> assign(:user, other_user)
1578 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1581 |> assign(:token, nil)
1582 |> get(favourited_by_url)
1583 |> json_response_and_validate_schema(404)
1587 |> get(favourited_by_url)
1588 |> json_response_and_validate_schema(200)
1590 [%{"id" => id}] = response
1591 assert id == other_user.id
1594 test "returns empty array when :show_reactions is disabled", %{conn: conn, activity: activity} do
1595 clear_config([:instance, :show_reactions], false)
1597 other_user = insert(:user)
1598 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1602 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1603 |> json_response_and_validate_schema(:ok)
1605 assert Enum.empty?(response)
1609 describe "GET /api/v1/statuses/:id/reblogged_by" do
1610 setup do: oauth_access(["read:accounts"])
1612 setup %{user: user} do
1613 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1615 %{activity: activity}
1618 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1619 other_user = insert(:user)
1620 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1624 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1625 |> json_response_and_validate_schema(:ok)
1627 [%{"id" => id}] = response
1629 assert id == other_user.id
1632 test "returns empty array when status has not been reblogged yet", %{
1638 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1639 |> json_response_and_validate_schema(:ok)
1641 assert Enum.empty?(response)
1644 test "does not return users who have reblogged the status but are blocked", %{
1645 conn: %{assigns: %{user: user}} = conn,
1648 other_user = insert(:user)
1649 {:ok, _user_relationship} = User.block(user, other_user)
1651 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1655 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1656 |> json_response_and_validate_schema(:ok)
1658 assert Enum.empty?(response)
1661 test "does not return users who have reblogged the status privately", %{
1664 other_user = insert(:user)
1665 {:ok, activity} = CommonAPI.post(other_user, %{status: "my secret post"})
1667 {:ok, _} = CommonAPI.repeat(activity.id, other_user, %{visibility: "private"})
1671 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1672 |> json_response_and_validate_schema(:ok)
1674 assert Enum.empty?(response)
1677 test "does not fail on an unauthenticated request", %{activity: activity} do
1678 other_user = insert(:user)
1679 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1683 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1684 |> json_response_and_validate_schema(:ok)
1686 [%{"id" => id}] = response
1687 assert id == other_user.id
1690 test "requires authentication for private posts", %{user: user} do
1691 other_user = insert(:user)
1694 CommonAPI.post(user, %{
1695 status: "@#{other_user.nickname} wanna get some #cofe together?",
1696 visibility: "direct"
1700 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1701 |> json_response_and_validate_schema(404)
1705 |> assign(:user, other_user)
1706 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1707 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1708 |> json_response_and_validate_schema(200)
1710 assert [] == response
1715 user = insert(:user)
1717 {:ok, %{id: id1}} = CommonAPI.post(user, %{status: "1"})
1718 {:ok, %{id: id2}} = CommonAPI.post(user, %{status: "2", in_reply_to_status_id: id1})
1719 {:ok, %{id: id3}} = CommonAPI.post(user, %{status: "3", in_reply_to_status_id: id2})
1720 {:ok, %{id: id4}} = CommonAPI.post(user, %{status: "4", in_reply_to_status_id: id3})
1721 {:ok, %{id: id5}} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
1725 |> get("/api/v1/statuses/#{id3}/context")
1726 |> json_response_and_validate_schema(:ok)
1729 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1730 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1734 test "context when restrict_unauthenticated is on" do
1735 user = insert(:user)
1736 remote_user = insert(:user, local: false)
1738 {:ok, %{id: id1}} = CommonAPI.post(user, %{status: "1"})
1739 {:ok, %{id: id2}} = CommonAPI.post(user, %{status: "2", in_reply_to_status_id: id1})
1742 CommonAPI.post(remote_user, %{status: "3", in_reply_to_status_id: id2, local: false})
1746 |> get("/api/v1/statuses/#{id2}/context")
1747 |> json_response_and_validate_schema(:ok)
1750 "ancestors" => [%{"id" => ^id1}],
1751 "descendants" => [%{"id" => ^id3}]
1754 clear_config([:restrict_unauthenticated, :activities, :local], true)
1758 |> get("/api/v1/statuses/#{id2}/context")
1759 |> json_response_and_validate_schema(:ok)
1767 test "favorites paginate correctly" do
1768 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1769 other_user = insert(:user)
1770 {:ok, first_post} = CommonAPI.post(other_user, %{status: "bla"})
1771 {:ok, second_post} = CommonAPI.post(other_user, %{status: "bla"})
1772 {:ok, third_post} = CommonAPI.post(other_user, %{status: "bla"})
1774 {:ok, _first_favorite} = CommonAPI.favorite(user, third_post.id)
1775 {:ok, _second_favorite} = CommonAPI.favorite(user, first_post.id)
1776 {:ok, third_favorite} = CommonAPI.favorite(user, second_post.id)
1780 |> get("/api/v1/favourites?limit=1")
1782 assert [%{"id" => post_id}] = json_response_and_validate_schema(result, 200)
1783 assert post_id == second_post.id
1785 # Using the header for pagination works correctly
1786 [next, _] = get_resp_header(result, "link") |> hd() |> String.split(", ")
1787 [_, max_id] = Regex.run(~r/max_id=([^&]+)/, next)
1789 assert max_id == third_favorite.id
1793 |> get("/api/v1/favourites?max_id=#{max_id}")
1795 assert [%{"id" => first_post_id}, %{"id" => third_post_id}] =
1796 json_response_and_validate_schema(result, 200)
1798 assert first_post_id == first_post.id
1799 assert third_post_id == third_post.id
1802 test "returns the favorites of a user" do
1803 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1804 other_user = insert(:user)
1806 {:ok, _} = CommonAPI.post(other_user, %{status: "bla"})
1807 {:ok, activity} = CommonAPI.post(other_user, %{status: "trees are happy"})
1809 {:ok, last_like} = CommonAPI.favorite(user, activity.id)
1811 first_conn = get(conn, "/api/v1/favourites")
1813 assert [status] = json_response_and_validate_schema(first_conn, 200)
1814 assert status["id"] == to_string(activity.id)
1816 assert [{"link", _link_header}] =
1817 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1819 # Honours query params
1820 {:ok, second_activity} =
1821 CommonAPI.post(other_user, %{
1822 status: "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1825 {:ok, _} = CommonAPI.favorite(user, second_activity.id)
1827 second_conn = get(conn, "/api/v1/favourites?since_id=#{last_like.id}")
1829 assert [second_status] = json_response_and_validate_schema(second_conn, 200)
1830 assert second_status["id"] == to_string(second_activity.id)
1832 third_conn = get(conn, "/api/v1/favourites?limit=0")
1834 assert [] = json_response_and_validate_schema(third_conn, 200)
1837 test "expires_at is nil for another user" do
1838 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1839 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
1840 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", expires_in: 1_000_000})
1842 assert %{"pleroma" => %{"expires_at" => a_expires_at}} =
1844 |> get("/api/v1/statuses/#{activity.id}")
1845 |> json_response_and_validate_schema(:ok)
1847 {:ok, a_expires_at, 0} = DateTime.from_iso8601(a_expires_at)
1848 assert DateTime.diff(expires_at, a_expires_at) == 0
1850 %{conn: conn} = oauth_access(["read:statuses"])
1852 assert %{"pleroma" => %{"expires_at" => nil}} =
1854 |> get("/api/v1/statuses/#{activity.id}")
1855 |> json_response_and_validate_schema(:ok)
1858 test "posting a local only status" do
1859 %{user: _user, conn: conn} = oauth_access(["write:statuses"])
1863 |> put_req_header("content-type", "application/json")
1864 |> post("/api/v1/statuses", %{
1866 "visibility" => "local"
1869 local = Utils.as_local_public()
1871 assert %{"content" => "cofe", "id" => id, "visibility" => "local"} =
1872 json_response_and_validate_schema(conn_one, 200)
1874 assert %Activity{id: ^id, data: %{"to" => [^local]}} = Activity.get_by_id(id)
1877 describe "muted reactions" do
1879 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1881 other_user = insert(:user)
1882 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1884 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1885 User.mute(user, other_user)
1889 |> get("/api/v1/statuses/?ids[]=#{activity.id}")
1890 |> json_response_and_validate_schema(200)
1895 "emoji_reactions" => []
1902 |> get("/api/v1/statuses/?ids[]=#{activity.id}&with_muted=true")
1903 |> json_response_and_validate_schema(200)
1908 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
1915 # %{conn: conn, user: user, token: token} = oauth_access(["read:statuses"])
1916 %{conn: conn, user: user, token: _token} = oauth_access(["read:statuses"])
1918 other_user = insert(:user)
1919 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1921 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1922 User.mute(user, other_user)
1926 |> get("/api/v1/statuses/#{activity.id}")
1927 |> json_response_and_validate_schema(200)
1931 "emoji_reactions" => []
1937 |> get("/api/v1/statuses/#{activity.id}?with_muted=true")
1938 |> json_response_and_validate_schema(200)
1942 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
1948 describe "posting quotes" do
1949 setup do: oauth_access(["write:statuses"])
1951 test "posting a quote", %{conn: conn} do
1952 user = insert(:user)
1953 {:ok, quoted_status} = CommonAPI.post(user, %{status: "tell me, for whom do you fight?"})
1957 |> put_req_header("content-type", "application/json")
1958 |> post("/api/v1/statuses", %{
1959 "status" => "Hmph, how very glib",
1960 "quote_id" => quoted_status.id
1963 response = json_response_and_validate_schema(conn, 200)
1965 assert response["quote_id"] == quoted_status.id
1966 assert response["quote"]["id"] == quoted_status.id
1967 assert response["quote"]["content"] == quoted_status.object.data["content"]
1970 test "posting a quote, quoting a status that isn't public", %{conn: conn} do
1971 user = insert(:user)
1973 Enum.each(["private", "local", "direct"], fn visibility ->
1974 {:ok, quoted_status} =
1975 CommonAPI.post(user, %{
1976 status: "tell me, for whom do you fight?",
1977 visibility: visibility
1980 assert %{"error" => "You can only quote public or unlisted statuses"} =
1982 |> put_req_header("content-type", "application/json")
1983 |> post("/api/v1/statuses", %{
1984 "status" => "Hmph, how very glib",
1985 "quote_id" => quoted_status.id
1987 |> json_response_and_validate_schema(422)
1991 test "posting a quote, after quote, the status gets deleted", %{conn: conn} do
1992 user = insert(:user)
1994 {:ok, quoted_status} =
1995 CommonAPI.post(user, %{status: "tell me, for whom do you fight?", visibility: "public"})
1999 |> put_req_header("content-type", "application/json")
2000 |> post("/api/v1/statuses", %{
2001 "status" => "I fight for eorzea!",
2002 "quote_id" => quoted_status.id
2004 |> json_response_and_validate_schema(200)
2006 {:ok, _} = CommonAPI.delete(quoted_status.id, user)
2010 |> get("/api/v1/statuses/#{resp["id"]}")
2011 |> json_response_and_validate_schema(200)
2013 assert is_nil(resp["quote"])
2016 test "posting a quote of a deleted status", %{conn: conn} do
2017 user = insert(:user)
2019 {:ok, quoted_status} =
2020 CommonAPI.post(user, %{status: "tell me, for whom do you fight?", visibility: "public"})
2022 {:ok, _} = CommonAPI.delete(quoted_status.id, user)
2024 assert %{"error" => _} =
2026 |> put_req_header("content-type", "application/json")
2027 |> post("/api/v1/statuses", %{
2028 "status" => "I fight for eorzea!",
2029 "quote_id" => quoted_status.id
2031 |> json_response_and_validate_schema(422)
2034 test "posting a quote of a status that doesn't exist", %{conn: conn} do
2035 assert %{"error" => "You can't quote a status that doesn't exist"} =
2037 |> put_req_header("content-type", "application/json")
2038 |> post("/api/v1/statuses", %{
2039 "status" => "I fight for eorzea!",
2040 "quote_id" => "oops"
2042 |> json_response_and_validate_schema(422)