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 "posting an undefined status with an attachment", %{user: user, conn: conn} do
126 content_type: "image/jpg",
127 path: Path.absname("test/fixtures/image.jpg"),
128 filename: "an_image.jpg"
131 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
134 post(conn, "/api/v1/statuses", %{
135 "media_ids" => [to_string(upload.id)]
138 assert json_response(conn, 200)
141 test "replying to a status", %{user: user, conn: conn} do
142 {:ok, replied_to} = CommonAPI.post(user, %{"status" => "cofe"})
146 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
148 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
150 activity = Activity.get_by_id(id)
152 assert activity.data["context"] == replied_to.data["context"]
153 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
156 test "replying to a direct message with visibility other than direct", %{
160 {:ok, replied_to} = CommonAPI.post(user, %{"status" => "suya..", "visibility" => "direct"})
162 Enum.each(["public", "private", "unlisted"], fn visibility ->
165 |> post("/api/v1/statuses", %{
166 "status" => "@#{user.nickname} hey",
167 "in_reply_to_id" => replied_to.id,
168 "visibility" => visibility
171 assert json_response(conn, 422) == %{"error" => "The message visibility must be direct"}
175 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
176 conn = post(conn, "/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
178 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
179 assert Activity.get_by_id(id)
182 test "posting a sensitive status", %{conn: conn} do
183 conn = post(conn, "/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
185 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
186 assert Activity.get_by_id(id)
189 test "posting a fake status", %{conn: conn} do
191 post(conn, "/api/v1/statuses", %{
193 "\"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"
196 real_status = json_response(real_conn, 200)
199 assert Object.get_by_ap_id(real_status["uri"])
203 |> Map.put("id", nil)
204 |> Map.put("url", nil)
205 |> Map.put("uri", nil)
206 |> Map.put("created_at", nil)
207 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
210 post(conn, "/api/v1/statuses", %{
212 "\"Tenshi Eating a Corndog\" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it",
216 fake_status = json_response(fake_conn, 200)
219 refute Object.get_by_ap_id(fake_status["uri"])
223 |> Map.put("id", nil)
224 |> Map.put("url", nil)
225 |> Map.put("uri", nil)
226 |> Map.put("created_at", nil)
227 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
229 assert real_status == fake_status
232 test "posting a status with OGP link preview", %{conn: conn} do
233 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
234 Config.put([:rich_media, :enabled], true)
237 post(conn, "/api/v1/statuses", %{
238 "status" => "https://example.com/ogp"
241 assert %{"id" => id, "card" => %{"title" => "The Rock"}} = json_response(conn, 200)
242 assert Activity.get_by_id(id)
245 test "posting a direct status", %{conn: conn} do
246 user2 = insert(:user)
247 content = "direct cofe @#{user2.nickname}"
249 conn = post(conn, "api/v1/statuses", %{"status" => content, "visibility" => "direct"})
251 assert %{"id" => id} = response = json_response(conn, 200)
252 assert response["visibility"] == "direct"
253 assert response["pleroma"]["direct_conversation_id"]
254 assert activity = Activity.get_by_id(id)
255 assert activity.recipients == [user2.ap_id, conn.assigns[:user].ap_id]
256 assert activity.data["to"] == [user2.ap_id]
257 assert activity.data["cc"] == []
261 describe "posting scheduled statuses" do
262 setup do: oauth_access(["write:statuses"])
264 test "creates a scheduled activity", %{conn: conn} do
265 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
268 post(conn, "/api/v1/statuses", %{
269 "status" => "scheduled",
270 "scheduled_at" => scheduled_at
273 assert %{"scheduled_at" => expected_scheduled_at} = json_response(conn, 200)
274 assert expected_scheduled_at == CommonAPI.Utils.to_masto_date(scheduled_at)
275 assert [] == Repo.all(Activity)
278 test "creates a scheduled activity with a media attachment", %{user: user, conn: conn} do
279 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
282 content_type: "image/jpg",
283 path: Path.absname("test/fixtures/image.jpg"),
284 filename: "an_image.jpg"
287 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
290 post(conn, "/api/v1/statuses", %{
291 "media_ids" => [to_string(upload.id)],
292 "status" => "scheduled",
293 "scheduled_at" => scheduled_at
296 assert %{"media_attachments" => [media_attachment]} = json_response(conn, 200)
297 assert %{"type" => "image"} = media_attachment
300 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
303 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
306 post(conn, "/api/v1/statuses", %{
307 "status" => "not scheduled",
308 "scheduled_at" => scheduled_at
311 assert %{"content" => "not scheduled"} = json_response(conn, 200)
312 assert [] == Repo.all(ScheduledActivity)
315 test "returns error when daily user limit is exceeded", %{user: user, conn: conn} do
317 NaiveDateTime.utc_now()
318 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
319 |> NaiveDateTime.to_iso8601()
321 attrs = %{params: %{}, scheduled_at: today}
322 {:ok, _} = ScheduledActivity.create(user, attrs)
323 {:ok, _} = ScheduledActivity.create(user, attrs)
325 conn = post(conn, "/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
327 assert %{"error" => "daily limit exceeded"} == json_response(conn, 422)
330 test "returns error when total user limit is exceeded", %{user: user, conn: conn} do
332 NaiveDateTime.utc_now()
333 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
334 |> NaiveDateTime.to_iso8601()
337 NaiveDateTime.utc_now()
338 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
339 |> NaiveDateTime.to_iso8601()
341 attrs = %{params: %{}, scheduled_at: today}
342 {:ok, _} = ScheduledActivity.create(user, attrs)
343 {:ok, _} = ScheduledActivity.create(user, attrs)
344 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
347 post(conn, "/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
349 assert %{"error" => "total limit exceeded"} == json_response(conn, 422)
353 describe "posting polls" do
354 setup do: oauth_access(["write:statuses"])
356 test "posting a poll", %{conn: conn} do
357 time = NaiveDateTime.utc_now()
360 post(conn, "/api/v1/statuses", %{
361 "status" => "Who is the #bestgrill?",
362 "poll" => %{"options" => ["Rei", "Asuka", "Misato"], "expires_in" => 420}
365 response = json_response(conn, 200)
367 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
368 title in ["Rei", "Asuka", "Misato"]
371 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
372 refute response["poll"]["expred"]
375 test "option limit is enforced", %{conn: conn} do
376 limit = Config.get([:instance, :poll_limits, :max_options])
379 post(conn, "/api/v1/statuses", %{
381 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
384 %{"error" => error} = json_response(conn, 422)
385 assert error == "Poll can't contain more than #{limit} options"
388 test "option character limit is enforced", %{conn: conn} do
389 limit = Config.get([:instance, :poll_limits, :max_option_chars])
392 post(conn, "/api/v1/statuses", %{
395 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
400 %{"error" => error} = json_response(conn, 422)
401 assert error == "Poll options cannot be longer than #{limit} characters each"
404 test "minimal date limit is enforced", %{conn: conn} do
405 limit = Config.get([:instance, :poll_limits, :min_expiration])
408 post(conn, "/api/v1/statuses", %{
409 "status" => "imagine arbitrary limits",
411 "options" => ["this post was made by pleroma gang"],
412 "expires_in" => limit - 1
416 %{"error" => error} = json_response(conn, 422)
417 assert error == "Expiration date is too soon"
420 test "maximum date limit is enforced", %{conn: conn} do
421 limit = Config.get([:instance, :poll_limits, :max_expiration])
424 post(conn, "/api/v1/statuses", %{
425 "status" => "imagine arbitrary limits",
427 "options" => ["this post was made by pleroma gang"],
428 "expires_in" => limit + 1
432 %{"error" => error} = json_response(conn, 422)
433 assert error == "Expiration date is too far in the future"
437 test "get a status" do
438 %{conn: conn} = oauth_access(["read:statuses"])
439 activity = insert(:note_activity)
441 conn = get(conn, "/api/v1/statuses/#{activity.id}")
443 assert %{"id" => id} = json_response(conn, 200)
444 assert id == to_string(activity.id)
447 test "get a direct status" do
448 %{user: user, conn: conn} = oauth_access(["read:statuses"])
449 other_user = insert(:user)
452 CommonAPI.post(user, %{"status" => "@#{other_user.nickname}", "visibility" => "direct"})
456 |> assign(:user, user)
457 |> get("/api/v1/statuses/#{activity.id}")
459 [participation] = Participation.for_user(user)
461 res = json_response(conn, 200)
462 assert res["pleroma"]["direct_conversation_id"] == participation.id
465 test "get statuses by IDs" do
466 %{conn: conn} = oauth_access(["read:statuses"])
467 %{id: id1} = insert(:note_activity)
468 %{id: id2} = insert(:note_activity)
470 query_string = "ids[]=#{id1}&ids[]=#{id2}"
471 conn = get(conn, "/api/v1/statuses/?#{query_string}")
473 assert [%{"id" => ^id1}, %{"id" => ^id2}] = Enum.sort_by(json_response(conn, :ok), & &1["id"])
476 describe "deleting a status" do
477 test "when you created it" do
478 %{user: author, conn: conn} = oauth_access(["write:statuses"])
479 activity = insert(:note_activity, user: author)
483 |> assign(:user, author)
484 |> delete("/api/v1/statuses/#{activity.id}")
486 assert %{} = json_response(conn, 200)
488 refute Activity.get_by_id(activity.id)
491 test "when you didn't create it" do
492 %{conn: conn} = oauth_access(["write:statuses"])
493 activity = insert(:note_activity)
495 conn = delete(conn, "/api/v1/statuses/#{activity.id}")
497 assert %{"error" => _} = json_response(conn, 403)
499 assert Activity.get_by_id(activity.id) == activity
502 test "when you're an admin or moderator", %{conn: conn} do
503 activity1 = insert(:note_activity)
504 activity2 = insert(:note_activity)
505 admin = insert(:user, is_admin: true)
506 moderator = insert(:user, is_moderator: true)
510 |> assign(:user, admin)
511 |> assign(:token, insert(:oauth_token, user: admin, scopes: ["write:statuses"]))
512 |> delete("/api/v1/statuses/#{activity1.id}")
514 assert %{} = json_response(res_conn, 200)
518 |> assign(:user, moderator)
519 |> assign(:token, insert(:oauth_token, user: moderator, scopes: ["write:statuses"]))
520 |> delete("/api/v1/statuses/#{activity2.id}")
522 assert %{} = json_response(res_conn, 200)
524 refute Activity.get_by_id(activity1.id)
525 refute Activity.get_by_id(activity2.id)
529 describe "reblogging" do
530 setup do: oauth_access(["write:statuses"])
532 test "reblogs and returns the reblogged status", %{conn: conn} do
533 activity = insert(:note_activity)
535 conn = post(conn, "/api/v1/statuses/#{activity.id}/reblog")
538 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
540 } = json_response(conn, 200)
542 assert to_string(activity.id) == id
545 test "reblogs privately and returns the reblogged status", %{conn: conn} do
546 activity = insert(:note_activity)
548 conn = post(conn, "/api/v1/statuses/#{activity.id}/reblog", %{"visibility" => "private"})
551 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
553 "visibility" => "private"
554 } = json_response(conn, 200)
556 assert to_string(activity.id) == id
559 test "reblogged status for another user" do
560 activity = insert(:note_activity)
561 user1 = insert(:user)
562 user2 = insert(:user)
563 user3 = insert(:user)
564 CommonAPI.favorite(activity.id, user2)
565 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
566 {:ok, reblog_activity1, _object} = CommonAPI.repeat(activity.id, user1)
567 {:ok, _, _object} = CommonAPI.repeat(activity.id, user2)
571 |> assign(:user, user3)
572 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
573 |> get("/api/v1/statuses/#{reblog_activity1.id}")
576 "reblog" => %{"id" => id, "reblogged" => false, "reblogs_count" => 2},
577 "reblogged" => false,
578 "favourited" => false,
579 "bookmarked" => false
580 } = json_response(conn_res, 200)
584 |> assign(:user, user2)
585 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["read:statuses"]))
586 |> get("/api/v1/statuses/#{reblog_activity1.id}")
589 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
591 "favourited" => true,
593 } = json_response(conn_res, 200)
595 assert to_string(activity.id) == id
598 test "returns 400 error when activity is not exist", %{conn: conn} do
599 conn = post(conn, "/api/v1/statuses/foo/reblog")
601 assert json_response(conn, 400) == %{"error" => "Could not repeat"}
605 describe "unreblogging" do
606 setup do: oauth_access(["write:statuses"])
608 test "unreblogs and returns the unreblogged status", %{user: user, conn: conn} do
609 activity = insert(:note_activity)
611 {:ok, _, _} = CommonAPI.repeat(activity.id, user)
613 conn = post(conn, "/api/v1/statuses/#{activity.id}/unreblog")
615 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
617 assert to_string(activity.id) == id
620 test "returns 400 error when activity is not exist", %{conn: conn} do
621 conn = post(conn, "/api/v1/statuses/foo/unreblog")
623 assert json_response(conn, 400) == %{"error" => "Could not unrepeat"}
627 describe "favoriting" do
628 setup do: oauth_access(["write:favourites"])
630 test "favs a status and returns it", %{conn: conn} do
631 activity = insert(:note_activity)
633 conn = post(conn, "/api/v1/statuses/#{activity.id}/favourite")
635 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
636 json_response(conn, 200)
638 assert to_string(activity.id) == id
641 test "returns 400 error for a wrong id", %{conn: conn} do
642 conn = post(conn, "/api/v1/statuses/1/favourite")
644 assert json_response(conn, 400) == %{"error" => "Could not favorite"}
648 describe "unfavoriting" do
649 setup do: oauth_access(["write:favourites"])
651 test "unfavorites a status and returns it", %{user: user, conn: conn} do
652 activity = insert(:note_activity)
654 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
656 conn = post(conn, "/api/v1/statuses/#{activity.id}/unfavourite")
658 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
659 json_response(conn, 200)
661 assert to_string(activity.id) == id
664 test "returns 400 error for a wrong id", %{conn: conn} do
665 conn = post(conn, "/api/v1/statuses/1/unfavourite")
667 assert json_response(conn, 400) == %{"error" => "Could not unfavorite"}
671 describe "pinned statuses" do
672 setup do: oauth_access(["write:accounts"])
674 setup %{user: user} do
675 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
677 %{activity: activity}
680 clear_config([:instance, :max_pinned_statuses]) do
681 Config.put([:instance, :max_pinned_statuses], 1)
684 test "pin status", %{conn: conn, user: user, activity: activity} do
685 id_str = to_string(activity.id)
687 assert %{"id" => ^id_str, "pinned" => true} =
689 |> post("/api/v1/statuses/#{activity.id}/pin")
690 |> json_response(200)
692 assert [%{"id" => ^id_str, "pinned" => true}] =
694 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
695 |> json_response(200)
698 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
699 {:ok, dm} = CommonAPI.post(user, %{"status" => "test", "visibility" => "direct"})
701 conn = post(conn, "/api/v1/statuses/#{dm.id}/pin")
703 assert json_response(conn, 400) == %{"error" => "Could not pin"}
706 test "unpin status", %{conn: conn, user: user, activity: activity} do
707 {:ok, _} = CommonAPI.pin(activity.id, user)
708 user = refresh_record(user)
710 id_str = to_string(activity.id)
712 assert %{"id" => ^id_str, "pinned" => false} =
714 |> assign(:user, user)
715 |> post("/api/v1/statuses/#{activity.id}/unpin")
716 |> json_response(200)
720 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
721 |> json_response(200)
724 test "/unpin: returns 400 error when activity is not exist", %{conn: conn} do
725 conn = post(conn, "/api/v1/statuses/1/unpin")
727 assert json_response(conn, 400) == %{"error" => "Could not unpin"}
730 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
731 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
733 id_str_one = to_string(activity_one.id)
735 assert %{"id" => ^id_str_one, "pinned" => true} =
737 |> post("/api/v1/statuses/#{id_str_one}/pin")
738 |> json_response(200)
740 user = refresh_record(user)
742 assert %{"error" => "You have already pinned the maximum number of statuses"} =
744 |> assign(:user, user)
745 |> post("/api/v1/statuses/#{activity_two.id}/pin")
746 |> json_response(400)
752 Config.put([:rich_media, :enabled], true)
754 oauth_access(["read:statuses"])
757 test "returns rich-media card", %{conn: conn, user: user} do
758 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
760 {:ok, activity} = CommonAPI.post(user, %{"status" => "https://example.com/ogp"})
763 "image" => "http://ia.media-imdb.com/images/rock.jpg",
764 "provider_name" => "example.com",
765 "provider_url" => "https://example.com",
766 "title" => "The Rock",
768 "url" => "https://example.com/ogp",
770 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
773 "image" => "http://ia.media-imdb.com/images/rock.jpg",
774 "title" => "The Rock",
775 "type" => "video.movie",
776 "url" => "https://example.com/ogp",
778 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
785 |> get("/api/v1/statuses/#{activity.id}/card")
786 |> json_response(200)
788 assert response == card_data
790 # works with private posts
792 CommonAPI.post(user, %{"status" => "https://example.com/ogp", "visibility" => "direct"})
796 |> get("/api/v1/statuses/#{activity.id}/card")
797 |> json_response(200)
799 assert response_two == card_data
802 test "replaces missing description with an empty string", %{conn: conn, user: user} do
803 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
806 CommonAPI.post(user, %{"status" => "https://example.com/ogp-missing-data"})
810 |> get("/api/v1/statuses/#{activity.id}/card")
811 |> json_response(:ok)
813 assert response == %{
815 "title" => "Pleroma",
818 "provider_name" => "example.com",
819 "provider_url" => "https://example.com",
820 "url" => "https://example.com/ogp-missing-data",
823 "title" => "Pleroma",
825 "url" => "https://example.com/ogp-missing-data"
833 %{conn: conn} = oauth_access(["write:bookmarks", "read:bookmarks"])
834 author = insert(:user)
837 CommonAPI.post(author, %{
838 "status" => "heweoo?"
842 CommonAPI.post(author, %{
843 "status" => "heweoo!"
846 response1 = post(conn, "/api/v1/statuses/#{activity1.id}/bookmark")
848 assert json_response(response1, 200)["bookmarked"] == true
850 response2 = post(conn, "/api/v1/statuses/#{activity2.id}/bookmark")
852 assert json_response(response2, 200)["bookmarked"] == true
854 bookmarks = get(conn, "/api/v1/bookmarks")
856 assert [json_response(response2, 200), json_response(response1, 200)] ==
857 json_response(bookmarks, 200)
859 response1 = post(conn, "/api/v1/statuses/#{activity1.id}/unbookmark")
861 assert json_response(response1, 200)["bookmarked"] == false
863 bookmarks = get(conn, "/api/v1/bookmarks")
865 assert [json_response(response2, 200)] == json_response(bookmarks, 200)
868 describe "conversation muting" do
869 setup do: oauth_access(["write:mutes"])
872 post_user = insert(:user)
873 {:ok, activity} = CommonAPI.post(post_user, %{"status" => "HIE"})
874 %{activity: activity}
877 test "mute conversation", %{conn: conn, activity: activity} do
878 id_str = to_string(activity.id)
880 assert %{"id" => ^id_str, "muted" => true} =
882 |> post("/api/v1/statuses/#{activity.id}/mute")
883 |> json_response(200)
886 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
887 {:ok, _} = CommonAPI.add_mute(user, activity)
889 conn = post(conn, "/api/v1/statuses/#{activity.id}/mute")
891 assert json_response(conn, 400) == %{"error" => "conversation is already muted"}
894 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
895 {:ok, _} = CommonAPI.add_mute(user, activity)
897 id_str = to_string(activity.id)
899 assert %{"id" => ^id_str, "muted" => false} =
901 # |> assign(:user, user)
902 |> post("/api/v1/statuses/#{activity.id}/unmute")
903 |> json_response(200)
907 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
908 user1 = insert(:user)
909 user2 = insert(:user)
910 user3 = insert(:user)
912 {:ok, replied_to} = CommonAPI.post(user1, %{"status" => "cofe"})
914 # Reply to status from another user
917 |> assign(:user, user2)
918 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["write:statuses"]))
919 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
921 assert %{"content" => "xD", "id" => id} = json_response(conn1, 200)
923 activity = Activity.get_by_id_with_object(id)
925 assert Object.normalize(activity).data["inReplyTo"] == Object.normalize(replied_to).data["id"]
926 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
928 # Reblog from the third user
931 |> assign(:user, user3)
932 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["write:statuses"]))
933 |> post("/api/v1/statuses/#{activity.id}/reblog")
935 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
936 json_response(conn2, 200)
938 assert to_string(activity.id) == id
940 # Getting third user status
943 |> assign(:user, user3)
944 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
945 |> get("api/v1/timelines/home")
947 [reblogged_activity] = json_response(conn3, 200)
949 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
951 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
952 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
955 describe "GET /api/v1/statuses/:id/favourited_by" do
956 setup do: oauth_access(["read:accounts"])
958 setup %{user: user} do
959 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
961 %{activity: activity}
964 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
965 other_user = insert(:user)
966 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
970 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
971 |> json_response(:ok)
973 [%{"id" => id}] = response
975 assert id == other_user.id
978 test "returns empty array when status has not been favorited yet", %{
984 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
985 |> json_response(:ok)
987 assert Enum.empty?(response)
990 test "does not return users who have favorited the status but are blocked", %{
991 conn: %{assigns: %{user: user}} = conn,
994 other_user = insert(:user)
995 {:ok, _user_relationship} = User.block(user, other_user)
997 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1001 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1002 |> json_response(:ok)
1004 assert Enum.empty?(response)
1007 test "does not fail on an unauthenticated request", %{activity: activity} do
1008 other_user = insert(:user)
1009 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1013 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1014 |> json_response(:ok)
1016 [%{"id" => id}] = response
1017 assert id == other_user.id
1020 test "requires authentication for private posts", %{user: user} do
1021 other_user = insert(:user)
1024 CommonAPI.post(user, %{
1025 "status" => "@#{other_user.nickname} wanna get some #cofe together?",
1026 "visibility" => "direct"
1029 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
1031 favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by"
1034 |> get(favourited_by_url)
1035 |> json_response(404)
1039 |> assign(:user, other_user)
1040 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1043 |> assign(:token, nil)
1044 |> get(favourited_by_url)
1045 |> json_response(404)
1049 |> get(favourited_by_url)
1050 |> json_response(200)
1052 [%{"id" => id}] = response
1053 assert id == other_user.id
1057 describe "GET /api/v1/statuses/:id/reblogged_by" do
1058 setup do: oauth_access(["read:accounts"])
1060 setup %{user: user} do
1061 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
1063 %{activity: activity}
1066 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1067 other_user = insert(:user)
1068 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1072 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1073 |> json_response(:ok)
1075 [%{"id" => id}] = response
1077 assert id == other_user.id
1080 test "returns empty array when status has not been reblogged yet", %{
1086 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1087 |> json_response(:ok)
1089 assert Enum.empty?(response)
1092 test "does not return users who have reblogged the status but are blocked", %{
1093 conn: %{assigns: %{user: user}} = conn,
1096 other_user = insert(:user)
1097 {:ok, _user_relationship} = User.block(user, other_user)
1099 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1103 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1104 |> json_response(:ok)
1106 assert Enum.empty?(response)
1109 test "does not return users who have reblogged the status privately", %{
1113 other_user = insert(:user)
1115 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user, %{"visibility" => "private"})
1119 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1120 |> json_response(:ok)
1122 assert Enum.empty?(response)
1125 test "does not fail on an unauthenticated request", %{activity: activity} do
1126 other_user = insert(:user)
1127 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
1131 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1132 |> json_response(:ok)
1134 [%{"id" => id}] = response
1135 assert id == other_user.id
1138 test "requires authentication for private posts", %{user: user} do
1139 other_user = insert(:user)
1142 CommonAPI.post(user, %{
1143 "status" => "@#{other_user.nickname} wanna get some #cofe together?",
1144 "visibility" => "direct"
1148 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1149 |> json_response(404)
1153 |> assign(:user, other_user)
1154 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1155 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1156 |> json_response(200)
1158 assert [] == response
1163 user = insert(:user)
1165 {:ok, %{id: id1}} = CommonAPI.post(user, %{"status" => "1"})
1166 {:ok, %{id: id2}} = CommonAPI.post(user, %{"status" => "2", "in_reply_to_status_id" => id1})
1167 {:ok, %{id: id3}} = CommonAPI.post(user, %{"status" => "3", "in_reply_to_status_id" => id2})
1168 {:ok, %{id: id4}} = CommonAPI.post(user, %{"status" => "4", "in_reply_to_status_id" => id3})
1169 {:ok, %{id: id5}} = CommonAPI.post(user, %{"status" => "5", "in_reply_to_status_id" => id4})
1173 |> get("/api/v1/statuses/#{id3}/context")
1174 |> json_response(:ok)
1177 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1178 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1182 test "returns the favorites of a user" do
1183 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1184 other_user = insert(:user)
1186 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
1187 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
1189 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
1191 first_conn = get(conn, "/api/v1/favourites")
1193 assert [status] = json_response(first_conn, 200)
1194 assert status["id"] == to_string(activity.id)
1196 assert [{"link", _link_header}] =
1197 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1199 # Honours query params
1200 {:ok, second_activity} =
1201 CommonAPI.post(other_user, %{
1203 "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1206 {:ok, _, _} = CommonAPI.favorite(second_activity.id, user)
1208 last_like = status["id"]
1210 second_conn = get(conn, "/api/v1/favourites?since_id=#{last_like}")
1212 assert [second_status] = json_response(second_conn, 200)
1213 assert second_status["id"] == to_string(second_activity.id)
1215 third_conn = get(conn, "/api/v1/favourites?limit=0")
1217 assert [] = json_response(third_conn, 200)