1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.ActivityExpiration
11 alias Pleroma.Conversation.Participation
14 alias Pleroma.ScheduledActivity
15 alias Pleroma.Tests.ObanHelpers
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
22 clear_config([:instance, :federating])
23 clear_config([:instance, :allow_relay])
25 describe "posting statuses" do
26 setup do: oauth_access(["write:statuses"])
28 test "posting a status does not increment reblog_count when relaying", %{conn: conn} do
29 Pleroma.Config.put([:instance, :federating], true)
30 Pleroma.Config.get([:instance, :allow_relay], true)
34 |> post("api/v1/statuses", %{
35 "content_type" => "text/plain",
36 "source" => "Pleroma FE",
37 "status" => "Hello world",
38 "visibility" => "public"
42 assert response["reblogs_count"] == 0
43 ObanHelpers.perform_all()
47 |> get("api/v1/statuses/#{response["id"]}", %{})
50 assert response["reblogs_count"] == 0
53 test "posting a status", %{conn: conn} do
54 idempotency_key = "Pikachu rocks!"
58 |> put_req_header("idempotency-key", idempotency_key)
59 |> post("/api/v1/statuses", %{
61 "spoiler_text" => "2hu",
62 "sensitive" => "false"
65 {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
67 assert ttl > :timer.seconds(6 * 60 * 60 - 1)
69 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
70 json_response(conn_one, 200)
72 assert Activity.get_by_id(id)
76 |> put_req_header("idempotency-key", idempotency_key)
77 |> post("/api/v1/statuses", %{
79 "spoiler_text" => "2hu",
80 "sensitive" => "false"
83 assert %{"id" => second_id} = json_response(conn_two, 200)
84 assert id == second_id
88 |> post("/api/v1/statuses", %{
90 "spoiler_text" => "2hu",
91 "sensitive" => "false"
94 assert %{"id" => third_id} = json_response(conn_three, 200)
97 # An activity that will expire:
103 |> post("api/v1/statuses", %{
104 "status" => "oolong",
105 "expires_in" => expires_in
108 assert fourth_response = %{"id" => fourth_id} = json_response(conn_four, 200)
109 assert activity = Activity.get_by_id(fourth_id)
110 assert expiration = ActivityExpiration.get_by_activity_id(fourth_id)
112 estimated_expires_at =
113 NaiveDateTime.utc_now()
114 |> NaiveDateTime.add(expires_in)
115 |> NaiveDateTime.truncate(:second)
117 # This assert will fail if the test takes longer than a minute. I sure hope it never does:
118 assert abs(NaiveDateTime.diff(expiration.scheduled_at, estimated_expires_at, :second)) < 60
120 assert fourth_response["pleroma"]["expires_at"] ==
121 NaiveDateTime.to_iso8601(expiration.scheduled_at)
124 test "it fails to create a status if `expires_in` is less or equal than an hour", %{
130 assert %{"error" => "Expiry date is too soon"} =
132 |> post("api/v1/statuses", %{
133 "status" => "oolong",
134 "expires_in" => expires_in
136 |> json_response(422)
141 assert %{"error" => "Expiry date is too soon"} =
143 |> post("api/v1/statuses", %{
144 "status" => "oolong",
145 "expires_in" => expires_in
147 |> json_response(422)
150 test "posting an undefined status with an attachment", %{user: user, conn: conn} do
152 content_type: "image/jpg",
153 path: Path.absname("test/fixtures/image.jpg"),
154 filename: "an_image.jpg"
157 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
160 post(conn, "/api/v1/statuses", %{
161 "media_ids" => [to_string(upload.id)]
164 assert json_response(conn, 200)
167 test "replying to a status", %{user: user, conn: conn} do
168 {:ok, replied_to} = CommonAPI.post(user, %{"status" => "cofe"})
172 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
174 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
176 activity = Activity.get_by_id(id)
178 assert activity.data["context"] == replied_to.data["context"]
179 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
182 test "replying to a direct message with visibility other than direct", %{
186 {:ok, replied_to} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
188 Enum.each(["public", "private", "unlisted"], fn visibility ->
191 |> post("/api/v1/statuses", %{
192 "status" => "@#{user.nickname} hey",
193 "in_reply_to_id" => replied_to.id,
194 "visibility" => visibility
197 assert json_response(conn, 422) == %{"error" => "The message visibility must be direct"}
201 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
202 conn = post(conn, "/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
204 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
205 assert Activity.get_by_id(id)
208 test "posting a sensitive status", %{conn: conn} do
209 conn = post(conn, "/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
211 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
212 assert Activity.get_by_id(id)
215 test "posting a fake status", %{conn: conn} do
217 post(conn, "/api/v1/statuses", %{
219 "\"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"
222 real_status = json_response(real_conn, 200)
225 assert Object.get_by_ap_id(real_status["uri"])
229 |> Map.put("id", nil)
230 |> Map.put("url", nil)
231 |> Map.put("uri", nil)
232 |> Map.put("created_at", nil)
233 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
236 post(conn, "/api/v1/statuses", %{
238 "\"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",
242 fake_status = json_response(fake_conn, 200)
245 refute Object.get_by_ap_id(fake_status["uri"])
249 |> Map.put("id", nil)
250 |> Map.put("url", nil)
251 |> Map.put("uri", nil)
252 |> Map.put("created_at", nil)
253 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
255 assert real_status == fake_status
258 test "posting a status with OGP link preview", %{conn: conn} do
259 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
260 Config.put([:rich_media, :enabled], true)
263 post(conn, "/api/v1/statuses", %{
264 "status" => "https://example.com/ogp"
267 assert %{"id" => id, "card" => %{"title" => "The Rock"}} = json_response(conn, 200)
268 assert Activity.get_by_id(id)
271 test "posting a direct status", %{conn: conn} do
272 user2 = insert(:user)
273 content = "direct cofe @#{user2.nickname}"
275 conn = post(conn, "api/v1/statuses", %{"status" => content, "visibility" => "direct"})
277 assert %{"id" => id} = response = json_response(conn, 200)
278 assert response["visibility"] == "direct"
279 assert response["pleroma"]["direct_conversation_id"]
280 assert activity = Activity.get_by_id(id)
281 assert activity.recipients == [user2.ap_id, conn.assigns[:user].ap_id]
282 assert activity.data["to"] == [user2.ap_id]
283 assert activity.data["cc"] == []
287 describe "posting scheduled statuses" do
288 setup do: oauth_access(["write:statuses"])
290 test "creates a scheduled activity", %{conn: conn} do
291 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
294 post(conn, "/api/v1/statuses", %{
295 "status" => "scheduled",
296 "scheduled_at" => scheduled_at
299 assert %{"scheduled_at" => expected_scheduled_at} = json_response(conn, 200)
300 assert expected_scheduled_at == CommonAPI.Utils.to_masto_date(scheduled_at)
301 assert [] == Repo.all(Activity)
304 test "creates a scheduled activity with a media attachment", %{user: user, conn: conn} do
305 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
308 content_type: "image/jpg",
309 path: Path.absname("test/fixtures/image.jpg"),
310 filename: "an_image.jpg"
313 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
316 post(conn, "/api/v1/statuses", %{
317 "media_ids" => [to_string(upload.id)],
318 "status" => "scheduled",
319 "scheduled_at" => scheduled_at
322 assert %{"media_attachments" => [media_attachment]} = json_response(conn, 200)
323 assert %{"type" => "image"} = media_attachment
326 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
329 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
332 post(conn, "/api/v1/statuses", %{
333 "status" => "not scheduled",
334 "scheduled_at" => scheduled_at
337 assert %{"content" => "not scheduled"} = json_response(conn, 200)
338 assert [] == Repo.all(ScheduledActivity)
341 test "returns error when daily user limit is exceeded", %{user: user, conn: conn} do
343 NaiveDateTime.utc_now()
344 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
345 |> NaiveDateTime.to_iso8601()
347 attrs = %{params: %{}, scheduled_at: today}
348 {:ok, _} = ScheduledActivity.create(user, attrs)
349 {:ok, _} = ScheduledActivity.create(user, attrs)
351 conn = post(conn, "/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
353 assert %{"error" => "daily limit exceeded"} == json_response(conn, 422)
356 test "returns error when total user limit is exceeded", %{user: user, conn: conn} do
358 NaiveDateTime.utc_now()
359 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
360 |> NaiveDateTime.to_iso8601()
363 NaiveDateTime.utc_now()
364 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
365 |> NaiveDateTime.to_iso8601()
367 attrs = %{params: %{}, scheduled_at: today}
368 {:ok, _} = ScheduledActivity.create(user, attrs)
369 {:ok, _} = ScheduledActivity.create(user, attrs)
370 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
373 post(conn, "/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
375 assert %{"error" => "total limit exceeded"} == json_response(conn, 422)
379 describe "posting polls" do
380 setup do: oauth_access(["write:statuses"])
382 test "posting a poll", %{conn: conn} do
383 time = NaiveDateTime.utc_now()
386 post(conn, "/api/v1/statuses", %{
387 "status" => "Who is the #bestgrill?",
388 "poll" => %{"options" => ["Rei", "Asuka", "Misato"], "expires_in" => 420}
391 response = json_response(conn, 200)
393 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
394 title in ["Rei", "Asuka", "Misato"]
397 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
398 refute response["poll"]["expred"]
400 question = Object.get_by_id(response["poll"]["id"])
402 # closed contains utc timezone
403 assert question.data["closed"] =~ "Z"
406 test "option limit is enforced", %{conn: conn} do
407 limit = Config.get([:instance, :poll_limits, :max_options])
410 post(conn, "/api/v1/statuses", %{
412 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
415 %{"error" => error} = json_response(conn, 422)
416 assert error == "Poll can't contain more than #{limit} options"
419 test "option character limit is enforced", %{conn: conn} do
420 limit = Config.get([:instance, :poll_limits, :max_option_chars])
423 post(conn, "/api/v1/statuses", %{
426 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
431 %{"error" => error} = json_response(conn, 422)
432 assert error == "Poll options cannot be longer than #{limit} characters each"
435 test "minimal date limit is enforced", %{conn: conn} do
436 limit = Config.get([:instance, :poll_limits, :min_expiration])
439 post(conn, "/api/v1/statuses", %{
440 "status" => "imagine arbitrary limits",
442 "options" => ["this post was made by pleroma gang"],
443 "expires_in" => limit - 1
447 %{"error" => error} = json_response(conn, 422)
448 assert error == "Expiration date is too soon"
451 test "maximum date limit is enforced", %{conn: conn} do
452 limit = Config.get([:instance, :poll_limits, :max_expiration])
455 post(conn, "/api/v1/statuses", %{
456 "status" => "imagine arbitrary limits",
458 "options" => ["this post was made by pleroma gang"],
459 "expires_in" => limit + 1
463 %{"error" => error} = json_response(conn, 422)
464 assert error == "Expiration date is too far in the future"
468 test "get a status" do
469 %{conn: conn} = oauth_access(["read:statuses"])
470 activity = insert(:note_activity)
472 conn = get(conn, "/api/v1/statuses/#{activity.id}")
474 assert %{"id" => id} = json_response(conn, 200)
475 assert id == to_string(activity.id)
478 test "get a direct status" do
479 %{user: user, conn: conn} = oauth_access(["read:statuses"])
480 other_user = insert(:user)
483 CommonAPI.post(user, %{"status" => "@#{other_user.nickname}", "visibility" => "direct"})
487 |> assign(:user, user)
488 |> get("/api/v1/statuses/#{activity.id}")
490 [participation] = Participation.for_user(user)
492 res = json_response(conn, 200)
493 assert res["pleroma"]["direct_conversation_id"] == participation.id
496 test "get statuses by IDs" do
497 %{conn: conn} = oauth_access(["read:statuses"])
498 %{id: id1} = insert(:note_activity)
499 %{id: id2} = insert(:note_activity)
501 query_string = "ids[]=#{id1}&ids[]=#{id2}"
502 conn = get(conn, "/api/v1/statuses/?#{query_string}")
504 assert [%{"id" => ^id1}, %{"id" => ^id2}] = Enum.sort_by(json_response(conn, :ok), & &1["id"])
507 describe "deleting a status" do
508 test "when you created it" do
509 %{user: author, conn: conn} = oauth_access(["write:statuses"])
510 activity = insert(:note_activity, user: author)
514 |> assign(:user, author)
515 |> delete("/api/v1/statuses/#{activity.id}")
517 assert %{} = json_response(conn, 200)
519 refute Activity.get_by_id(activity.id)
522 test "when you didn't create it" do
523 %{conn: conn} = oauth_access(["write:statuses"])
524 activity = insert(:note_activity)
526 conn = delete(conn, "/api/v1/statuses/#{activity.id}")
528 assert %{"error" => _} = json_response(conn, 403)
530 assert Activity.get_by_id(activity.id) == activity
533 test "when you're an admin or moderator", %{conn: conn} do
534 activity1 = insert(:note_activity)
535 activity2 = insert(:note_activity)
536 admin = insert(:user, is_admin: true)
537 moderator = insert(:user, is_moderator: true)
541 |> assign(:user, admin)
542 |> assign(:token, insert(:oauth_token, user: admin, scopes: ["write:statuses"]))
543 |> delete("/api/v1/statuses/#{activity1.id}")
545 assert %{} = json_response(res_conn, 200)
549 |> assign(:user, moderator)
550 |> assign(:token, insert(:oauth_token, user: moderator, scopes: ["write:statuses"]))
551 |> delete("/api/v1/statuses/#{activity2.id}")
553 assert %{} = json_response(res_conn, 200)
555 refute Activity.get_by_id(activity1.id)
556 refute Activity.get_by_id(activity2.id)
560 describe "reblogging" do
561 setup do: oauth_access(["write:statuses"])
563 test "reblogs and returns the reblogged status", %{conn: conn} do
564 activity = insert(:note_activity)
566 conn = post(conn, "/api/v1/statuses/#{activity.id}/reblog")
569 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
571 } = json_response(conn, 200)
573 assert to_string(activity.id) == id
576 test "reblogs privately and returns the reblogged status", %{conn: conn} do
577 activity = insert(:note_activity)
579 conn = post(conn, "/api/v1/statuses/#{activity.id}/reblog", %{"visibility" => "private"})
582 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
584 "visibility" => "private"
585 } = json_response(conn, 200)
587 assert to_string(activity.id) == id
590 test "reblogged status for another user" do
591 activity = insert(:note_activity)
592 user1 = insert(:user)
593 user2 = insert(:user)
594 user3 = insert(:user)
595 CommonAPI.favorite(activity.id, user2)
596 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
597 {:ok, reblog_activity1, _object} = CommonAPI.repeat(activity.id, user1)
598 {:ok, _, _object} = CommonAPI.repeat(activity.id, user2)
602 |> assign(:user, user3)
603 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
604 |> get("/api/v1/statuses/#{reblog_activity1.id}")
607 "reblog" => %{"id" => id, "reblogged" => false, "reblogs_count" => 2},
608 "reblogged" => false,
609 "favourited" => false,
610 "bookmarked" => false
611 } = json_response(conn_res, 200)
615 |> assign(:user, user2)
616 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["read:statuses"]))
617 |> get("/api/v1/statuses/#{reblog_activity1.id}")
620 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
622 "favourited" => true,
624 } = json_response(conn_res, 200)
626 assert to_string(activity.id) == id
629 test "returns 400 error when activity is not exist", %{conn: conn} do
630 conn = post(conn, "/api/v1/statuses/foo/reblog")
632 assert json_response(conn, 400) == %{"error" => "Could not repeat"}
636 describe "unreblogging" do
637 setup do: oauth_access(["write:statuses"])
639 test "unreblogs and returns the unreblogged status", %{user: user, conn: conn} do
640 activity = insert(:note_activity)
642 {:ok, _, _} = CommonAPI.repeat(activity.id, user)
644 conn = post(conn, "/api/v1/statuses/#{activity.id}/unreblog")
646 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
648 assert to_string(activity.id) == id
651 test "returns 400 error when activity is not exist", %{conn: conn} do
652 conn = post(conn, "/api/v1/statuses/foo/unreblog")
654 assert json_response(conn, 400) == %{"error" => "Could not unrepeat"}
658 describe "favoriting" do
659 setup do: oauth_access(["write:favourites"])
661 test "favs a status and returns it", %{conn: conn} do
662 activity = insert(:note_activity)
664 conn = post(conn, "/api/v1/statuses/#{activity.id}/favourite")
666 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
667 json_response(conn, 200)
669 assert to_string(activity.id) == id
672 test "favoriting twice will just return 200", %{conn: conn} do
673 activity = insert(:note_activity)
675 post(conn, "/api/v1/statuses/#{activity.id}/favourite")
676 assert post(conn, "/api/v1/statuses/#{activity.id}/favourite") |> json_response(200)
679 test "returns 400 error for a wrong id", %{conn: conn} do
680 conn = post(conn, "/api/v1/statuses/1/favourite")
682 assert json_response(conn, 400) == %{"error" => "Could not favorite"}
686 describe "unfavoriting" do
687 setup do: oauth_access(["write:favourites"])
689 test "unfavorites a status and returns it", %{user: user, conn: conn} do
690 activity = insert(:note_activity)
692 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
694 conn = post(conn, "/api/v1/statuses/#{activity.id}/unfavourite")
696 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
697 json_response(conn, 200)
699 assert to_string(activity.id) == id
702 test "returns 400 error for a wrong id", %{conn: conn} do
703 conn = post(conn, "/api/v1/statuses/1/unfavourite")
705 assert json_response(conn, 400) == %{"error" => "Could not unfavorite"}
709 describe "pinned statuses" do
710 setup do: oauth_access(["write:accounts"])
712 setup %{user: user} do
713 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
715 %{activity: activity}
718 clear_config([:instance, :max_pinned_statuses]) do
719 Config.put([:instance, :max_pinned_statuses], 1)
722 test "pin status", %{conn: conn, user: user, activity: activity} do
723 id_str = to_string(activity.id)
725 assert %{"id" => ^id_str, "pinned" => true} =
727 |> post("/api/v1/statuses/#{activity.id}/pin")
728 |> json_response(200)
730 assert [%{"id" => ^id_str, "pinned" => true}] =
732 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
733 |> json_response(200)
736 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
737 {:ok, dm} = CommonAPI.post(user, %{"status" => "test", "visibility" => "direct"})
739 conn = post(conn, "/api/v1/statuses/#{dm.id}/pin")
741 assert json_response(conn, 400) == %{"error" => "Could not pin"}
744 test "unpin status", %{conn: conn, user: user, activity: activity} do
745 {:ok, _} = CommonAPI.pin(activity.id, user)
746 user = refresh_record(user)
748 id_str = to_string(activity.id)
750 assert %{"id" => ^id_str, "pinned" => false} =
752 |> assign(:user, user)
753 |> post("/api/v1/statuses/#{activity.id}/unpin")
754 |> json_response(200)
758 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
759 |> json_response(200)
762 test "/unpin: returns 400 error when activity is not exist", %{conn: conn} do
763 conn = post(conn, "/api/v1/statuses/1/unpin")
765 assert json_response(conn, 400) == %{"error" => "Could not unpin"}
768 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
769 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
771 id_str_one = to_string(activity_one.id)
773 assert %{"id" => ^id_str_one, "pinned" => true} =
775 |> post("/api/v1/statuses/#{id_str_one}/pin")
776 |> json_response(200)
778 user = refresh_record(user)
780 assert %{"error" => "You have already pinned the maximum number of statuses"} =
782 |> assign(:user, user)
783 |> post("/api/v1/statuses/#{activity_two.id}/pin")
784 |> json_response(400)
790 Config.put([:rich_media, :enabled], true)
792 oauth_access(["read:statuses"])
795 test "returns rich-media card", %{conn: conn, user: user} do
796 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
798 {:ok, activity} = CommonAPI.post(user, %{"status" => "https://example.com/ogp"})
801 "image" => "http://ia.media-imdb.com/images/rock.jpg",
802 "provider_name" => "example.com",
803 "provider_url" => "https://example.com",
804 "title" => "The Rock",
806 "url" => "https://example.com/ogp",
808 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
811 "image" => "http://ia.media-imdb.com/images/rock.jpg",
812 "title" => "The Rock",
813 "type" => "video.movie",
814 "url" => "https://example.com/ogp",
816 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
823 |> get("/api/v1/statuses/#{activity.id}/card")
824 |> json_response(200)
826 assert response == card_data
828 # works with private posts
830 CommonAPI.post(user, %{"status" => "https://example.com/ogp", "visibility" => "direct"})
834 |> get("/api/v1/statuses/#{activity.id}/card")
835 |> json_response(200)
837 assert response_two == card_data
840 test "replaces missing description with an empty string", %{conn: conn, user: user} do
841 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
844 CommonAPI.post(user, %{"status" => "https://example.com/ogp-missing-data"})
848 |> get("/api/v1/statuses/#{activity.id}/card")
849 |> json_response(:ok)
851 assert response == %{
853 "title" => "Pleroma",
856 "provider_name" => "example.com",
857 "provider_url" => "https://example.com",
858 "url" => "https://example.com/ogp-missing-data",
861 "title" => "Pleroma",
863 "url" => "https://example.com/ogp-missing-data"
871 %{conn: conn} = oauth_access(["write:bookmarks", "read:bookmarks"])
872 author = insert(:user)
875 CommonAPI.post(author, %{
876 "status" => "heweoo?"
880 CommonAPI.post(author, %{
881 "status" => "heweoo!"
884 response1 = post(conn, "/api/v1/statuses/#{activity1.id}/bookmark")
886 assert json_response(response1, 200)["bookmarked"] == true
888 response2 = post(conn, "/api/v1/statuses/#{activity2.id}/bookmark")
890 assert json_response(response2, 200)["bookmarked"] == true
892 bookmarks = get(conn, "/api/v1/bookmarks")
894 assert [json_response(response2, 200), json_response(response1, 200)] ==
895 json_response(bookmarks, 200)
897 response1 = post(conn, "/api/v1/statuses/#{activity1.id}/unbookmark")
899 assert json_response(response1, 200)["bookmarked"] == false
901 bookmarks = get(conn, "/api/v1/bookmarks")
903 assert [json_response(response2, 200)] == json_response(bookmarks, 200)
906 describe "conversation muting" do
907 setup do: oauth_access(["write:mutes"])
910 post_user = insert(:user)
911 {:ok, activity} = CommonAPI.post(post_user, %{"status" => "HIE"})
912 %{activity: activity}
915 test "mute conversation", %{conn: conn, activity: activity} do
916 id_str = to_string(activity.id)
918 assert %{"id" => ^id_str, "muted" => true} =
920 |> post("/api/v1/statuses/#{activity.id}/mute")
921 |> json_response(200)
924 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
925 {:ok, _} = CommonAPI.add_mute(user, activity)
927 conn = post(conn, "/api/v1/statuses/#{activity.id}/mute")
929 assert json_response(conn, 400) == %{"error" => "conversation is already muted"}
932 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
933 {:ok, _} = CommonAPI.add_mute(user, activity)
935 id_str = to_string(activity.id)
937 assert %{"id" => ^id_str, "muted" => false} =
939 # |> assign(:user, user)
940 |> post("/api/v1/statuses/#{activity.id}/unmute")
941 |> json_response(200)
945 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
946 user1 = insert(:user)
947 user2 = insert(:user)
948 user3 = insert(:user)
950 {:ok, replied_to} = CommonAPI.post(user1, %{"status" => "cofe"})
952 # Reply to status from another user
955 |> assign(:user, user2)
956 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["write:statuses"]))
957 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
959 assert %{"content" => "xD", "id" => id} = json_response(conn1, 200)
961 activity = Activity.get_by_id_with_object(id)
963 assert Object.normalize(activity).data["inReplyTo"] == Object.normalize(replied_to).data["id"]
964 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
966 # Reblog from the third user
969 |> assign(:user, user3)
970 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["write:statuses"]))
971 |> post("/api/v1/statuses/#{activity.id}/reblog")
973 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
974 json_response(conn2, 200)
976 assert to_string(activity.id) == id
978 # Getting third user status
981 |> assign(:user, user3)
982 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
983 |> get("api/v1/timelines/home")
985 [reblogged_activity] = json_response(conn3, 200)
987 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
989 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
990 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
993 describe "GET /api/v1/statuses/:id/favourited_by" do
994 setup do: oauth_access(["read:accounts"])
996 setup %{user: user} do
997 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
999 %{activity: activity}
1002 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
1003 other_user = insert(:user)
1004 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1008 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1009 |> json_response(:ok)
1011 [%{"id" => id}] = response
1013 assert id == other_user.id
1016 test "returns empty array when status has not been favorited yet", %{
1022 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1023 |> json_response(:ok)
1025 assert Enum.empty?(response)
1028 test "does not return users who have favorited the status but are blocked", %{
1029 conn: %{assigns: %{user: user}} = conn,
1032 other_user = insert(:user)
1033 {:ok, _user_relationship} = User.block(user, other_user)
1035 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1039 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1040 |> json_response(:ok)
1042 assert Enum.empty?(response)
1045 test "does not fail on an unauthenticated request", %{activity: activity} do
1046 other_user = insert(:user)
1047 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1051 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1052 |> json_response(:ok)
1054 [%{"id" => id}] = response
1055 assert id == other_user.id
1058 test "requires authentication for private posts", %{user: user} do
1059 other_user = insert(:user)
1062 CommonAPI.post(user, %{
1063 "status" => "@#{other_user.nickname} wanna get some #cofe together?",
1064 "visibility" => "direct"
1067 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1069 favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by"
1072 |> get(favourited_by_url)
1073 |> json_response(404)
1077 |> assign(:user, other_user)
1078 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1081 |> assign(:token, nil)
1082 |> get(favourited_by_url)
1083 |> json_response(404)
1087 |> get(favourited_by_url)
1088 |> json_response(200)
1090 [%{"id" => id}] = response
1091 assert id == other_user.id
1095 describe "GET /api/v1/statuses/:id/reblogged_by" do
1096 setup do: oauth_access(["read:accounts"])
1098 setup %{user: user} do
1099 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1101 %{activity: activity}
1104 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1105 other_user = insert(:user)
1106 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1110 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1111 |> json_response(:ok)
1113 [%{"id" => id}] = response
1115 assert id == other_user.id
1118 test "returns empty array when status has not been reblogged yet", %{
1124 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1125 |> json_response(:ok)
1127 assert Enum.empty?(response)
1130 test "does not return users who have reblogged the status but are blocked", %{
1131 conn: %{assigns: %{user: user}} = conn,
1134 other_user = insert(:user)
1135 {:ok, _user_relationship} = User.block(user, other_user)
1137 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1141 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1142 |> json_response(:ok)
1144 assert Enum.empty?(response)
1147 test "does not return users who have reblogged the status privately", %{
1151 other_user = insert(:user)
1153 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user, %{"visibility" => "private"})
1157 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1158 |> json_response(:ok)
1160 assert Enum.empty?(response)
1163 test "does not fail on an unauthenticated request", %{activity: activity} do
1164 other_user = insert(:user)
1165 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1169 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1170 |> json_response(:ok)
1172 [%{"id" => id}] = response
1173 assert id == other_user.id
1176 test "requires authentication for private posts", %{user: user} do
1177 other_user = insert(:user)
1180 CommonAPI.post(user, %{
1181 "status" => "@#{other_user.nickname} wanna get some #cofe together?",
1182 "visibility" => "direct"
1186 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1187 |> json_response(404)
1191 |> assign(:user, other_user)
1192 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1193 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1194 |> json_response(200)
1196 assert [] == response
1201 user = insert(:user)
1203 {:ok, %{id: id1}} = CommonAPI.post(user, %{"status" => "1"})
1204 {:ok, %{id: id2}} = CommonAPI.post(user, %{"status" => "2", "in_reply_to_status_id" => id1})
1205 {:ok, %{id: id3}} = CommonAPI.post(user, %{"status" => "3", "in_reply_to_status_id" => id2})
1206 {:ok, %{id: id4}} = CommonAPI.post(user, %{"status" => "4", "in_reply_to_status_id" => id3})
1207 {:ok, %{id: id5}} = CommonAPI.post(user, %{"status" => "5", "in_reply_to_status_id" => id4})
1211 |> get("/api/v1/statuses/#{id3}/context")
1212 |> json_response(:ok)
1215 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1216 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1220 test "returns the favorites of a user" do
1221 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1222 other_user = insert(:user)
1224 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
1225 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
1227 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
1229 first_conn = get(conn, "/api/v1/favourites")
1231 assert [status] = json_response(first_conn, 200)
1232 assert status["id"] == to_string(activity.id)
1234 assert [{"link", _link_header}] =
1235 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1237 # Honours query params
1238 {:ok, second_activity} =
1239 CommonAPI.post(other_user, %{
1241 "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1244 {:ok, _, _} = CommonAPI.favorite(second_activity.id, user)
1246 last_like = status["id"]
1248 second_conn = get(conn, "/api/v1/favourites?since_id=#{last_like}")
1250 assert [second_status] = json_response(second_conn, 200)
1251 assert second_status["id"] == to_string(second_activity.id)
1253 third_conn = get(conn, "/api/v1/favourites?limit=0")
1255 assert [] = json_response(third_conn, 200)
1258 test "expires_at is nil for another user" do
1259 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1260 {:ok, activity} = CommonAPI.post(user, %{"status" => "foobar", "expires_in" => 1_000_000})
1264 |> ActivityExpiration.get_by_activity_id()
1265 |> Map.get(:scheduled_at)
1266 |> NaiveDateTime.to_iso8601()
1268 assert %{"pleroma" => %{"expires_at" => ^expires_at}} =
1269 conn |> get("/api/v1/statuses/#{activity.id}") |> json_response(:ok)
1271 %{conn: conn} = oauth_access(["read:statuses"])
1273 assert %{"pleroma" => %{"expires_at" => nil}} =
1274 conn |> get("/api/v1/statuses/#{activity.id}") |> json_response(:ok)