1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.ActivityExpiration
11 alias Pleroma.Conversation.Participation
14 alias Pleroma.ScheduledActivity
15 alias Pleroma.Tests.ObanHelpers
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
22 setup do: clear_config([:instance, :federating])
23 setup do: clear_config([:instance, :allow_relay])
24 setup do: clear_config([:rich_media, :enabled])
26 describe "posting statuses" do
27 setup do: oauth_access(["write:statuses"])
29 test "posting a status does not increment reblog_count when relaying", %{conn: conn} do
30 Pleroma.Config.put([:instance, :federating], true)
31 Pleroma.Config.get([:instance, :allow_relay], true)
35 |> post("api/v1/statuses", %{
36 "content_type" => "text/plain",
37 "source" => "Pleroma FE",
38 "status" => "Hello world",
39 "visibility" => "public"
43 assert response["reblogs_count"] == 0
44 ObanHelpers.perform_all()
48 |> get("api/v1/statuses/#{response["id"]}", %{})
51 assert response["reblogs_count"] == 0
54 test "posting a status", %{conn: conn} do
55 idempotency_key = "Pikachu rocks!"
59 |> put_req_header("idempotency-key", idempotency_key)
60 |> post("/api/v1/statuses", %{
62 "spoiler_text" => "2hu",
63 "sensitive" => "false"
66 {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
68 assert ttl > :timer.seconds(6 * 60 * 60 - 1)
70 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
71 json_response(conn_one, 200)
73 assert Activity.get_by_id(id)
77 |> put_req_header("idempotency-key", idempotency_key)
78 |> post("/api/v1/statuses", %{
80 "spoiler_text" => "2hu",
81 "sensitive" => "false"
84 assert %{"id" => second_id} = json_response(conn_two, 200)
85 assert id == second_id
89 |> post("/api/v1/statuses", %{
91 "spoiler_text" => "2hu",
92 "sensitive" => "false"
95 assert %{"id" => third_id} = json_response(conn_three, 200)
98 # An activity that will expire:
100 expires_in = 120 * 60
104 |> post("api/v1/statuses", %{
105 "status" => "oolong",
106 "expires_in" => expires_in
109 assert fourth_response = %{"id" => fourth_id} = json_response(conn_four, 200)
110 assert activity = Activity.get_by_id(fourth_id)
111 assert expiration = ActivityExpiration.get_by_activity_id(fourth_id)
113 estimated_expires_at =
114 NaiveDateTime.utc_now()
115 |> NaiveDateTime.add(expires_in)
116 |> NaiveDateTime.truncate(:second)
118 # This assert will fail if the test takes longer than a minute. I sure hope it never does:
119 assert abs(NaiveDateTime.diff(expiration.scheduled_at, estimated_expires_at, :second)) < 60
121 assert fourth_response["pleroma"]["expires_at"] ==
122 NaiveDateTime.to_iso8601(expiration.scheduled_at)
125 test "it fails to create a status if `expires_in` is less or equal than an hour", %{
131 assert %{"error" => "Expiry date is too soon"} =
133 |> post("api/v1/statuses", %{
134 "status" => "oolong",
135 "expires_in" => expires_in
137 |> json_response(422)
142 assert %{"error" => "Expiry date is too soon"} =
144 |> post("api/v1/statuses", %{
145 "status" => "oolong",
146 "expires_in" => expires_in
148 |> json_response(422)
151 test "posting an undefined status with an attachment", %{user: user, conn: conn} do
153 content_type: "image/jpg",
154 path: Path.absname("test/fixtures/image.jpg"),
155 filename: "an_image.jpg"
158 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
161 post(conn, "/api/v1/statuses", %{
162 "media_ids" => [to_string(upload.id)]
165 assert json_response(conn, 200)
168 test "replying to a status", %{user: user, conn: conn} do
169 {:ok, replied_to} = CommonAPI.post(user, %{"status" => "cofe"})
173 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
175 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
177 activity = Activity.get_by_id(id)
179 assert activity.data["context"] == replied_to.data["context"]
180 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
183 test "replying to a direct message with visibility other than direct", %{
187 {:ok, replied_to} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
189 Enum.each(["public", "private", "unlisted"], fn visibility ->
192 |> post("/api/v1/statuses", %{
193 "status" => "@#{user.nickname} hey",
194 "in_reply_to_id" => replied_to.id,
195 "visibility" => visibility
198 assert json_response(conn, 422) == %{"error" => "The message visibility must be direct"}
202 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
203 conn = post(conn, "/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
205 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
206 assert Activity.get_by_id(id)
209 test "posting a sensitive status", %{conn: conn} do
210 conn = post(conn, "/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
212 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
213 assert Activity.get_by_id(id)
216 test "posting a fake status", %{conn: conn} do
218 post(conn, "/api/v1/statuses", %{
220 "\"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"
223 real_status = json_response(real_conn, 200)
226 assert Object.get_by_ap_id(real_status["uri"])
230 |> Map.put("id", nil)
231 |> Map.put("url", nil)
232 |> Map.put("uri", nil)
233 |> Map.put("created_at", nil)
234 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
237 post(conn, "/api/v1/statuses", %{
239 "\"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",
243 fake_status = json_response(fake_conn, 200)
246 refute Object.get_by_ap_id(fake_status["uri"])
250 |> Map.put("id", nil)
251 |> Map.put("url", nil)
252 |> Map.put("uri", nil)
253 |> Map.put("created_at", nil)
254 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
256 assert real_status == fake_status
259 test "posting a status with OGP link preview", %{conn: conn} do
260 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
261 Config.put([:rich_media, :enabled], true)
264 post(conn, "/api/v1/statuses", %{
265 "status" => "https://example.com/ogp"
268 assert %{"id" => id, "card" => %{"title" => "The Rock"}} = json_response(conn, 200)
269 assert Activity.get_by_id(id)
272 test "posting a direct status", %{conn: conn} do
273 user2 = insert(:user)
274 content = "direct cofe @#{user2.nickname}"
276 conn = post(conn, "api/v1/statuses", %{"status" => content, "visibility" => "direct"})
278 assert %{"id" => id} = response = json_response(conn, 200)
279 assert response["visibility"] == "direct"
280 assert response["pleroma"]["direct_conversation_id"]
281 assert activity = Activity.get_by_id(id)
282 assert activity.recipients == [user2.ap_id, conn.assigns[:user].ap_id]
283 assert activity.data["to"] == [user2.ap_id]
284 assert activity.data["cc"] == []
288 describe "posting scheduled statuses" do
289 setup do: oauth_access(["write:statuses"])
291 test "creates a scheduled activity", %{conn: conn} do
292 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
295 post(conn, "/api/v1/statuses", %{
296 "status" => "scheduled",
297 "scheduled_at" => scheduled_at
300 assert %{"scheduled_at" => expected_scheduled_at} = json_response(conn, 200)
301 assert expected_scheduled_at == CommonAPI.Utils.to_masto_date(scheduled_at)
302 assert [] == Repo.all(Activity)
305 test "creates a scheduled activity with a media attachment", %{user: user, conn: conn} do
306 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
309 content_type: "image/jpg",
310 path: Path.absname("test/fixtures/image.jpg"),
311 filename: "an_image.jpg"
314 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
317 post(conn, "/api/v1/statuses", %{
318 "media_ids" => [to_string(upload.id)],
319 "status" => "scheduled",
320 "scheduled_at" => scheduled_at
323 assert %{"media_attachments" => [media_attachment]} = json_response(conn, 200)
324 assert %{"type" => "image"} = media_attachment
327 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
330 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
333 post(conn, "/api/v1/statuses", %{
334 "status" => "not scheduled",
335 "scheduled_at" => scheduled_at
338 assert %{"content" => "not scheduled"} = json_response(conn, 200)
339 assert [] == Repo.all(ScheduledActivity)
342 test "returns error when daily user limit is exceeded", %{user: user, conn: conn} do
344 NaiveDateTime.utc_now()
345 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
346 |> NaiveDateTime.to_iso8601()
348 attrs = %{params: %{}, scheduled_at: today}
349 {:ok, _} = ScheduledActivity.create(user, attrs)
350 {:ok, _} = ScheduledActivity.create(user, attrs)
352 conn = post(conn, "/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
354 assert %{"error" => "daily limit exceeded"} == json_response(conn, 422)
357 test "returns error when total user limit is exceeded", %{user: user, conn: conn} do
359 NaiveDateTime.utc_now()
360 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
361 |> NaiveDateTime.to_iso8601()
364 NaiveDateTime.utc_now()
365 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
366 |> NaiveDateTime.to_iso8601()
368 attrs = %{params: %{}, scheduled_at: today}
369 {:ok, _} = ScheduledActivity.create(user, attrs)
370 {:ok, _} = ScheduledActivity.create(user, attrs)
371 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
374 post(conn, "/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
376 assert %{"error" => "total limit exceeded"} == json_response(conn, 422)
380 describe "posting polls" do
381 setup do: oauth_access(["write:statuses"])
383 test "posting a poll", %{conn: conn} do
384 time = NaiveDateTime.utc_now()
387 post(conn, "/api/v1/statuses", %{
388 "status" => "Who is the #bestgrill?",
389 "poll" => %{"options" => ["Rei", "Asuka", "Misato"], "expires_in" => 420}
392 response = json_response(conn, 200)
394 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
395 title in ["Rei", "Asuka", "Misato"]
398 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
399 refute response["poll"]["expred"]
401 question = Object.get_by_id(response["poll"]["id"])
403 # closed contains utc timezone
404 assert question.data["closed"] =~ "Z"
407 test "option limit is enforced", %{conn: conn} do
408 limit = Config.get([:instance, :poll_limits, :max_options])
411 post(conn, "/api/v1/statuses", %{
413 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
416 %{"error" => error} = json_response(conn, 422)
417 assert error == "Poll can't contain more than #{limit} options"
420 test "option character limit is enforced", %{conn: conn} do
421 limit = Config.get([:instance, :poll_limits, :max_option_chars])
424 post(conn, "/api/v1/statuses", %{
427 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
432 %{"error" => error} = json_response(conn, 422)
433 assert error == "Poll options cannot be longer than #{limit} characters each"
436 test "minimal date limit is enforced", %{conn: conn} do
437 limit = Config.get([:instance, :poll_limits, :min_expiration])
440 post(conn, "/api/v1/statuses", %{
441 "status" => "imagine arbitrary limits",
443 "options" => ["this post was made by pleroma gang"],
444 "expires_in" => limit - 1
448 %{"error" => error} = json_response(conn, 422)
449 assert error == "Expiration date is too soon"
452 test "maximum date limit is enforced", %{conn: conn} do
453 limit = Config.get([:instance, :poll_limits, :max_expiration])
456 post(conn, "/api/v1/statuses", %{
457 "status" => "imagine arbitrary limits",
459 "options" => ["this post was made by pleroma gang"],
460 "expires_in" => limit + 1
464 %{"error" => error} = json_response(conn, 422)
465 assert error == "Expiration date is too far in the future"
469 test "get a status" do
470 %{conn: conn} = oauth_access(["read:statuses"])
471 activity = insert(:note_activity)
473 conn = get(conn, "/api/v1/statuses/#{activity.id}")
475 assert %{"id" => id} = json_response(conn, 200)
476 assert id == to_string(activity.id)
479 defp local_and_remote_activities do
480 local = insert(:note_activity)
481 remote = insert(:note_activity, local: false)
482 {:ok, local: local, remote: remote}
485 describe "status with restrict unauthenticated activities for local and remote" do
486 setup do: local_and_remote_activities()
488 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
490 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
492 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
493 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
495 assert json_response(res_conn, :not_found) == %{
496 "error" => "Record not found"
499 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
501 assert json_response(res_conn, :not_found) == %{
502 "error" => "Record not found"
506 test "if user is authenticated", %{local: local, remote: remote} do
507 %{conn: conn} = oauth_access(["read"])
508 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
509 assert %{"id" => _} = json_response(res_conn, 200)
511 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
512 assert %{"id" => _} = json_response(res_conn, 200)
516 describe "status with restrict unauthenticated activities for local" do
517 setup do: local_and_remote_activities()
519 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
521 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
522 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
524 assert json_response(res_conn, :not_found) == %{
525 "error" => "Record not found"
528 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
529 assert %{"id" => _} = json_response(res_conn, 200)
532 test "if user is authenticated", %{local: local, remote: remote} do
533 %{conn: conn} = oauth_access(["read"])
534 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
535 assert %{"id" => _} = json_response(res_conn, 200)
537 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
538 assert %{"id" => _} = json_response(res_conn, 200)
542 describe "status with restrict unauthenticated activities for remote" do
543 setup do: local_and_remote_activities()
545 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
547 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
548 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
549 assert %{"id" => _} = json_response(res_conn, 200)
551 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
553 assert json_response(res_conn, :not_found) == %{
554 "error" => "Record not found"
558 test "if user is authenticated", %{local: local, remote: remote} do
559 %{conn: conn} = oauth_access(["read"])
560 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
561 assert %{"id" => _} = json_response(res_conn, 200)
563 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
564 assert %{"id" => _} = json_response(res_conn, 200)
568 test "getting a status that doesn't exist returns 404" do
569 %{conn: conn} = oauth_access(["read:statuses"])
570 activity = insert(:note_activity)
572 conn = get(conn, "/api/v1/statuses/#{String.downcase(activity.id)}")
574 assert json_response(conn, 404) == %{"error" => "Record not found"}
577 test "get a direct status" do
578 %{user: user, conn: conn} = oauth_access(["read:statuses"])
579 other_user = insert(:user)
582 CommonAPI.post(user, %{"status" => "@#{other_user.nickname}", "visibility" => "direct"})
586 |> assign(:user, user)
587 |> get("/api/v1/statuses/#{activity.id}")
589 [participation] = Participation.for_user(user)
591 res = json_response(conn, 200)
592 assert res["pleroma"]["direct_conversation_id"] == participation.id
595 test "get statuses by IDs" do
596 %{conn: conn} = oauth_access(["read:statuses"])
597 %{id: id1} = insert(:note_activity)
598 %{id: id2} = insert(:note_activity)
600 query_string = "ids[]=#{id1}&ids[]=#{id2}"
601 conn = get(conn, "/api/v1/statuses/?#{query_string}")
603 assert [%{"id" => ^id1}, %{"id" => ^id2}] = Enum.sort_by(json_response(conn, :ok), & &1["id"])
606 describe "getting statuses by ids with restricted unauthenticated for local and remote" do
607 setup do: local_and_remote_activities()
609 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
611 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
613 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
614 res_conn = get(conn, "/api/v1/statuses", %{ids: [local.id, remote.id]})
616 assert json_response(res_conn, 200) == []
619 test "if user is authenticated", %{local: local, remote: remote} do
620 %{conn: conn} = oauth_access(["read"])
622 res_conn = get(conn, "/api/v1/statuses", %{ids: [local.id, remote.id]})
624 assert length(json_response(res_conn, 200)) == 2
628 describe "getting statuses by ids with restricted unauthenticated for local" do
629 setup do: local_and_remote_activities()
631 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
633 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
634 res_conn = get(conn, "/api/v1/statuses", %{ids: [local.id, remote.id]})
636 remote_id = remote.id
637 assert [%{"id" => ^remote_id}] = json_response(res_conn, 200)
640 test "if user is authenticated", %{local: local, remote: remote} do
641 %{conn: conn} = oauth_access(["read"])
643 res_conn = get(conn, "/api/v1/statuses", %{ids: [local.id, remote.id]})
645 assert length(json_response(res_conn, 200)) == 2
649 describe "getting statuses by ids with restricted unauthenticated for remote" do
650 setup do: local_and_remote_activities()
652 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
654 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
655 res_conn = get(conn, "/api/v1/statuses", %{ids: [local.id, remote.id]})
658 assert [%{"id" => ^local_id}] = json_response(res_conn, 200)
661 test "if user is authenticated", %{local: local, remote: remote} do
662 %{conn: conn} = oauth_access(["read"])
664 res_conn = get(conn, "/api/v1/statuses", %{ids: [local.id, remote.id]})
666 assert length(json_response(res_conn, 200)) == 2
670 describe "deleting a status" do
671 test "when you created it" do
672 %{user: author, conn: conn} = oauth_access(["write:statuses"])
673 activity = insert(:note_activity, user: author)
677 |> assign(:user, author)
678 |> delete("/api/v1/statuses/#{activity.id}")
680 assert %{} = json_response(conn, 200)
682 refute Activity.get_by_id(activity.id)
685 test "when it doesn't exist" do
686 %{user: author, conn: conn} = oauth_access(["write:statuses"])
687 activity = insert(:note_activity, user: author)
691 |> assign(:user, author)
692 |> delete("/api/v1/statuses/#{String.downcase(activity.id)}")
694 assert %{"error" => "Record not found"} == json_response(conn, 404)
697 test "when you didn't create it" do
698 %{conn: conn} = oauth_access(["write:statuses"])
699 activity = insert(:note_activity)
701 conn = delete(conn, "/api/v1/statuses/#{activity.id}")
703 assert %{"error" => _} = json_response(conn, 403)
705 assert Activity.get_by_id(activity.id) == activity
708 test "when you're an admin or moderator", %{conn: conn} do
709 activity1 = insert(:note_activity)
710 activity2 = insert(:note_activity)
711 admin = insert(:user, is_admin: true)
712 moderator = insert(:user, is_moderator: true)
716 |> assign(:user, admin)
717 |> assign(:token, insert(:oauth_token, user: admin, scopes: ["write:statuses"]))
718 |> delete("/api/v1/statuses/#{activity1.id}")
720 assert %{} = json_response(res_conn, 200)
724 |> assign(:user, moderator)
725 |> assign(:token, insert(:oauth_token, user: moderator, scopes: ["write:statuses"]))
726 |> delete("/api/v1/statuses/#{activity2.id}")
728 assert %{} = json_response(res_conn, 200)
730 refute Activity.get_by_id(activity1.id)
731 refute Activity.get_by_id(activity2.id)
735 describe "reblogging" do
736 setup do: oauth_access(["write:statuses"])
738 test "reblogs and returns the reblogged status", %{conn: conn} do
739 activity = insert(:note_activity)
741 conn = post(conn, "/api/v1/statuses/#{activity.id}/reblog")
744 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
746 } = json_response(conn, 200)
748 assert to_string(activity.id) == id
751 test "returns 404 if the reblogged status doesn't exist", %{conn: conn} do
752 activity = insert(:note_activity)
754 conn = post(conn, "/api/v1/statuses/#{String.downcase(activity.id)}/reblog")
756 assert %{"error" => "Record not found"} = json_response(conn, 404)
759 test "reblogs privately and returns the reblogged status", %{conn: conn} do
760 activity = insert(:note_activity)
762 conn = post(conn, "/api/v1/statuses/#{activity.id}/reblog", %{"visibility" => "private"})
765 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
767 "visibility" => "private"
768 } = json_response(conn, 200)
770 assert to_string(activity.id) == id
773 test "reblogged status for another user" do
774 activity = insert(:note_activity)
775 user1 = insert(:user)
776 user2 = insert(:user)
777 user3 = insert(:user)
778 {:ok, _} = CommonAPI.favorite(user2, activity.id)
779 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
780 {:ok, reblog_activity1, _object} = CommonAPI.repeat(activity.id, user1)
781 {:ok, _, _object} = CommonAPI.repeat(activity.id, user2)
785 |> assign(:user, user3)
786 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
787 |> get("/api/v1/statuses/#{reblog_activity1.id}")
790 "reblog" => %{"id" => id, "reblogged" => false, "reblogs_count" => 2},
791 "reblogged" => false,
792 "favourited" => false,
793 "bookmarked" => false
794 } = json_response(conn_res, 200)
798 |> assign(:user, user2)
799 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["read:statuses"]))
800 |> get("/api/v1/statuses/#{reblog_activity1.id}")
803 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
805 "favourited" => true,
807 } = json_response(conn_res, 200)
809 assert to_string(activity.id) == id
813 describe "unreblogging" do
814 setup do: oauth_access(["write:statuses"])
816 test "unreblogs and returns the unreblogged status", %{user: user, conn: conn} do
817 activity = insert(:note_activity)
819 {:ok, _, _} = CommonAPI.repeat(activity.id, user)
821 conn = post(conn, "/api/v1/statuses/#{activity.id}/unreblog")
823 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
825 assert to_string(activity.id) == id
828 test "returns 404 error when activity does not exist", %{conn: conn} do
829 conn = post(conn, "/api/v1/statuses/foo/unreblog")
831 assert json_response(conn, 404) == %{"error" => "Record not found"}
835 describe "favoriting" do
836 setup do: oauth_access(["write:favourites"])
838 test "favs a status and returns it", %{conn: conn} do
839 activity = insert(:note_activity)
841 conn = post(conn, "/api/v1/statuses/#{activity.id}/favourite")
843 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
844 json_response(conn, 200)
846 assert to_string(activity.id) == id
849 test "favoriting twice will just return 200", %{conn: conn} do
850 activity = insert(:note_activity)
852 post(conn, "/api/v1/statuses/#{activity.id}/favourite")
854 assert post(conn, "/api/v1/statuses/#{activity.id}/favourite")
855 |> json_response(200)
858 test "returns 404 error for a wrong id", %{conn: conn} do
861 |> post("/api/v1/statuses/1/favourite")
863 assert json_response(conn, 404) == %{"error" => "Record not found"}
867 describe "unfavoriting" do
868 setup do: oauth_access(["write:favourites"])
870 test "unfavorites a status and returns it", %{user: user, conn: conn} do
871 activity = insert(:note_activity)
873 {:ok, _} = CommonAPI.favorite(user, activity.id)
875 conn = post(conn, "/api/v1/statuses/#{activity.id}/unfavourite")
877 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
878 json_response(conn, 200)
880 assert to_string(activity.id) == id
883 test "returns 404 error for a wrong id", %{conn: conn} do
884 conn = post(conn, "/api/v1/statuses/1/unfavourite")
886 assert json_response(conn, 404) == %{"error" => "Record not found"}
890 describe "pinned statuses" do
891 setup do: oauth_access(["write:accounts"])
893 setup %{user: user} do
894 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
896 %{activity: activity}
899 setup do: clear_config([:instance, :max_pinned_statuses], 1)
901 test "pin status", %{conn: conn, user: user, activity: activity} do
902 id_str = to_string(activity.id)
904 assert %{"id" => ^id_str, "pinned" => true} =
906 |> post("/api/v1/statuses/#{activity.id}/pin")
907 |> json_response(200)
909 assert [%{"id" => ^id_str, "pinned" => true}] =
911 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
912 |> json_response(200)
915 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
916 {:ok, dm} = CommonAPI.post(user, %{"status" => "test", "visibility" => "direct"})
918 conn = post(conn, "/api/v1/statuses/#{dm.id}/pin")
920 assert json_response(conn, 400) == %{"error" => "Could not pin"}
923 test "unpin status", %{conn: conn, user: user, activity: activity} do
924 {:ok, _} = CommonAPI.pin(activity.id, user)
925 user = refresh_record(user)
927 id_str = to_string(activity.id)
929 assert %{"id" => ^id_str, "pinned" => false} =
931 |> assign(:user, user)
932 |> post("/api/v1/statuses/#{activity.id}/unpin")
933 |> json_response(200)
937 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
938 |> json_response(200)
941 test "/unpin: returns 400 error when activity is not exist", %{conn: conn} do
942 conn = post(conn, "/api/v1/statuses/1/unpin")
944 assert json_response(conn, 400) == %{"error" => "Could not unpin"}
947 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
948 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
950 id_str_one = to_string(activity_one.id)
952 assert %{"id" => ^id_str_one, "pinned" => true} =
954 |> post("/api/v1/statuses/#{id_str_one}/pin")
955 |> json_response(200)
957 user = refresh_record(user)
959 assert %{"error" => "You have already pinned the maximum number of statuses"} =
961 |> assign(:user, user)
962 |> post("/api/v1/statuses/#{activity_two.id}/pin")
963 |> json_response(400)
969 Config.put([:rich_media, :enabled], true)
971 oauth_access(["read:statuses"])
974 test "returns rich-media card", %{conn: conn, user: user} do
975 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
977 {:ok, activity} = CommonAPI.post(user, %{"status" => "https://example.com/ogp"})
980 "image" => "http://ia.media-imdb.com/images/rock.jpg",
981 "provider_name" => "example.com",
982 "provider_url" => "https://example.com",
983 "title" => "The Rock",
985 "url" => "https://example.com/ogp",
987 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
990 "image" => "http://ia.media-imdb.com/images/rock.jpg",
991 "title" => "The Rock",
992 "type" => "video.movie",
993 "url" => "https://example.com/ogp",
995 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
1002 |> get("/api/v1/statuses/#{activity.id}/card")
1003 |> json_response(200)
1005 assert response == card_data
1007 # works with private posts
1009 CommonAPI.post(user, %{"status" => "https://example.com/ogp", "visibility" => "direct"})
1013 |> get("/api/v1/statuses/#{activity.id}/card")
1014 |> json_response(200)
1016 assert response_two == card_data
1019 test "replaces missing description with an empty string", %{conn: conn, user: user} do
1020 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
1023 CommonAPI.post(user, %{"status" => "https://example.com/ogp-missing-data"})
1027 |> get("/api/v1/statuses/#{activity.id}/card")
1028 |> json_response(:ok)
1030 assert response == %{
1032 "title" => "Pleroma",
1033 "description" => "",
1035 "provider_name" => "example.com",
1036 "provider_url" => "https://example.com",
1037 "url" => "https://example.com/ogp-missing-data",
1040 "title" => "Pleroma",
1041 "type" => "website",
1042 "url" => "https://example.com/ogp-missing-data"
1050 %{conn: conn} = oauth_access(["write:bookmarks", "read:bookmarks"])
1051 author = insert(:user)
1054 CommonAPI.post(author, %{
1055 "status" => "heweoo?"
1059 CommonAPI.post(author, %{
1060 "status" => "heweoo!"
1063 response1 = post(conn, "/api/v1/statuses/#{activity1.id}/bookmark")
1065 assert json_response(response1, 200)["bookmarked"] == true
1067 response2 = post(conn, "/api/v1/statuses/#{activity2.id}/bookmark")
1069 assert json_response(response2, 200)["bookmarked"] == true
1071 bookmarks = get(conn, "/api/v1/bookmarks")
1073 assert [json_response(response2, 200), json_response(response1, 200)] ==
1074 json_response(bookmarks, 200)
1076 response1 = post(conn, "/api/v1/statuses/#{activity1.id}/unbookmark")
1078 assert json_response(response1, 200)["bookmarked"] == false
1080 bookmarks = get(conn, "/api/v1/bookmarks")
1082 assert [json_response(response2, 200)] == json_response(bookmarks, 200)
1085 describe "conversation muting" do
1086 setup do: oauth_access(["write:mutes"])
1089 post_user = insert(:user)
1090 {:ok, activity} = CommonAPI.post(post_user, %{"status" => "HIE"})
1091 %{activity: activity}
1094 test "mute conversation", %{conn: conn, activity: activity} do
1095 id_str = to_string(activity.id)
1097 assert %{"id" => ^id_str, "muted" => true} =
1099 |> post("/api/v1/statuses/#{activity.id}/mute")
1100 |> json_response(200)
1103 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
1104 {:ok, _} = CommonAPI.add_mute(user, activity)
1106 conn = post(conn, "/api/v1/statuses/#{activity.id}/mute")
1108 assert json_response(conn, 400) == %{"error" => "conversation is already muted"}
1111 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
1112 {:ok, _} = CommonAPI.add_mute(user, activity)
1114 id_str = to_string(activity.id)
1116 assert %{"id" => ^id_str, "muted" => false} =
1118 # |> assign(:user, user)
1119 |> post("/api/v1/statuses/#{activity.id}/unmute")
1120 |> json_response(200)
1124 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
1125 user1 = insert(:user)
1126 user2 = insert(:user)
1127 user3 = insert(:user)
1129 {:ok, replied_to} = CommonAPI.post(user1, %{"status" => "cofe"})
1131 # Reply to status from another user
1134 |> assign(:user, user2)
1135 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["write:statuses"]))
1136 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
1138 assert %{"content" => "xD", "id" => id} = json_response(conn1, 200)
1140 activity = Activity.get_by_id_with_object(id)
1142 assert Object.normalize(activity).data["inReplyTo"] == Object.normalize(replied_to).data["id"]
1143 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
1145 # Reblog from the third user
1148 |> assign(:user, user3)
1149 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["write:statuses"]))
1150 |> post("/api/v1/statuses/#{activity.id}/reblog")
1152 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
1153 json_response(conn2, 200)
1155 assert to_string(activity.id) == id
1157 # Getting third user status
1160 |> assign(:user, user3)
1161 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1162 |> get("api/v1/timelines/home")
1164 [reblogged_activity] = json_response(conn3, 200)
1166 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
1168 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
1169 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
1172 describe "GET /api/v1/statuses/:id/favourited_by" do
1173 setup do: oauth_access(["read:accounts"])
1175 setup %{user: user} do
1176 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1178 %{activity: activity}
1181 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
1182 other_user = insert(:user)
1183 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1187 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1188 |> json_response(:ok)
1190 [%{"id" => id}] = response
1192 assert id == other_user.id
1195 test "returns empty array when status has not been favorited yet", %{
1201 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1202 |> json_response(:ok)
1204 assert Enum.empty?(response)
1207 test "does not return users who have favorited the status but are blocked", %{
1208 conn: %{assigns: %{user: user}} = conn,
1211 other_user = insert(:user)
1212 {:ok, _user_relationship} = User.block(user, other_user)
1214 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1218 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1219 |> json_response(:ok)
1221 assert Enum.empty?(response)
1224 test "does not fail on an unauthenticated request", %{activity: activity} do
1225 other_user = insert(:user)
1226 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1230 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1231 |> json_response(:ok)
1233 [%{"id" => id}] = response
1234 assert id == other_user.id
1237 test "requires authentication for private posts", %{user: user} do
1238 other_user = insert(:user)
1241 CommonAPI.post(user, %{
1242 "status" => "@#{other_user.nickname} wanna get some #cofe together?",
1243 "visibility" => "direct"
1246 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1248 favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by"
1251 |> get(favourited_by_url)
1252 |> json_response(404)
1256 |> assign(:user, other_user)
1257 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1260 |> assign(:token, nil)
1261 |> get(favourited_by_url)
1262 |> json_response(404)
1266 |> get(favourited_by_url)
1267 |> json_response(200)
1269 [%{"id" => id}] = response
1270 assert id == other_user.id
1274 describe "GET /api/v1/statuses/:id/reblogged_by" do
1275 setup do: oauth_access(["read:accounts"])
1277 setup %{user: user} do
1278 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1280 %{activity: activity}
1283 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1284 other_user = insert(:user)
1285 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1289 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1290 |> json_response(:ok)
1292 [%{"id" => id}] = response
1294 assert id == other_user.id
1297 test "returns empty array when status has not been reblogged yet", %{
1303 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1304 |> json_response(:ok)
1306 assert Enum.empty?(response)
1309 test "does not return users who have reblogged the status but are blocked", %{
1310 conn: %{assigns: %{user: user}} = conn,
1313 other_user = insert(:user)
1314 {:ok, _user_relationship} = User.block(user, other_user)
1316 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1320 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1321 |> json_response(:ok)
1323 assert Enum.empty?(response)
1326 test "does not return users who have reblogged the status privately", %{
1330 other_user = insert(:user)
1332 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user, %{"visibility" => "private"})
1336 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1337 |> json_response(:ok)
1339 assert Enum.empty?(response)
1342 test "does not fail on an unauthenticated request", %{activity: activity} do
1343 other_user = insert(:user)
1344 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1348 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1349 |> json_response(:ok)
1351 [%{"id" => id}] = response
1352 assert id == other_user.id
1355 test "requires authentication for private posts", %{user: user} do
1356 other_user = insert(:user)
1359 CommonAPI.post(user, %{
1360 "status" => "@#{other_user.nickname} wanna get some #cofe together?",
1361 "visibility" => "direct"
1365 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1366 |> json_response(404)
1370 |> assign(:user, other_user)
1371 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1372 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1373 |> json_response(200)
1375 assert [] == response
1380 user = insert(:user)
1382 {:ok, %{id: id1}} = CommonAPI.post(user, %{"status" => "1"})
1383 {:ok, %{id: id2}} = CommonAPI.post(user, %{"status" => "2", "in_reply_to_status_id" => id1})
1384 {:ok, %{id: id3}} = CommonAPI.post(user, %{"status" => "3", "in_reply_to_status_id" => id2})
1385 {:ok, %{id: id4}} = CommonAPI.post(user, %{"status" => "4", "in_reply_to_status_id" => id3})
1386 {:ok, %{id: id5}} = CommonAPI.post(user, %{"status" => "5", "in_reply_to_status_id" => id4})
1390 |> get("/api/v1/statuses/#{id3}/context")
1391 |> json_response(:ok)
1394 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1395 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1399 test "returns the favorites of a user" do
1400 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1401 other_user = insert(:user)
1403 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
1404 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
1406 {:ok, _} = CommonAPI.favorite(user, activity.id)
1408 first_conn = get(conn, "/api/v1/favourites")
1410 assert [status] = json_response(first_conn, 200)
1411 assert status["id"] == to_string(activity.id)
1413 assert [{"link", _link_header}] =
1414 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1416 # Honours query params
1417 {:ok, second_activity} =
1418 CommonAPI.post(other_user, %{
1420 "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1423 {:ok, _} = CommonAPI.favorite(user, second_activity.id)
1425 last_like = status["id"]
1427 second_conn = get(conn, "/api/v1/favourites?since_id=#{last_like}")
1429 assert [second_status] = json_response(second_conn, 200)
1430 assert second_status["id"] == to_string(second_activity.id)
1432 third_conn = get(conn, "/api/v1/favourites?limit=0")
1434 assert [] = json_response(third_conn, 200)
1437 test "expires_at is nil for another user" do
1438 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1439 {:ok, activity} = CommonAPI.post(user, %{"status" => "foobar", "expires_in" => 1_000_000})
1443 |> ActivityExpiration.get_by_activity_id()
1444 |> Map.get(:scheduled_at)
1445 |> NaiveDateTime.to_iso8601()
1447 assert %{"pleroma" => %{"expires_at" => ^expires_at}} =
1448 conn |> get("/api/v1/statuses/#{activity.id}") |> json_response(:ok)
1450 %{conn: conn} = oauth_access(["read:statuses"])
1452 assert %{"pleroma" => %{"expires_at" => nil}} =
1453 conn |> get("/api/v1/statuses/#{activity.id}") |> json_response(:ok)