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.MastodonAPIControllerTest do
6 use Pleroma.Web.ConnCase
10 alias Pleroma.Notification
13 alias Pleroma.ScheduledActivity
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.MastodonAPI.FilterView
18 alias Pleroma.Web.OAuth.App
19 alias Pleroma.Web.OAuth.Token
20 alias Pleroma.Web.OStatus
21 alias Pleroma.Web.Push
22 alias Pleroma.Web.TwitterAPI.TwitterAPI
23 import Pleroma.Factory
24 import ExUnit.CaptureLog
28 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
32 test "the home timeline", %{conn: conn} do
34 following = insert(:user)
36 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
40 |> assign(:user, user)
41 |> get("/api/v1/timelines/home")
43 assert Enum.empty?(json_response(conn, 200))
45 {:ok, user} = User.follow(user, following)
49 |> assign(:user, user)
50 |> get("/api/v1/timelines/home")
52 assert [%{"content" => "test"}] = json_response(conn, 200)
55 test "the public timeline", %{conn: conn} do
56 following = insert(:user)
59 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
62 OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
66 |> get("/api/v1/timelines/public", %{"local" => "False"})
68 assert length(json_response(conn, 200)) == 2
72 |> get("/api/v1/timelines/public", %{"local" => "True"})
74 assert [%{"content" => "test"}] = json_response(conn, 200)
78 |> get("/api/v1/timelines/public", %{"local" => "1"})
80 assert [%{"content" => "test"}] = json_response(conn, 200)
84 test "the public timeline when public is set to false", %{conn: conn} do
85 public = Pleroma.Config.get([:instance, :public])
86 Pleroma.Config.put([:instance, :public], false)
89 Pleroma.Config.put([:instance, :public], public)
93 |> get("/api/v1/timelines/public", %{"local" => "False"})
94 |> json_response(403) == %{"error" => "This resource requires authentication."}
97 test "posting a status", %{conn: conn} do
100 idempotency_key = "Pikachu rocks!"
104 |> assign(:user, user)
105 |> put_req_header("idempotency-key", idempotency_key)
106 |> post("/api/v1/statuses", %{
108 "spoiler_text" => "2hu",
109 "sensitive" => "false"
112 {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
114 assert ttl > :timer.seconds(6 * 60 * 60 - 1)
116 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
117 json_response(conn_one, 200)
119 assert Activity.get_by_id(id)
123 |> assign(:user, user)
124 |> put_req_header("idempotency-key", idempotency_key)
125 |> post("/api/v1/statuses", %{
127 "spoiler_text" => "2hu",
128 "sensitive" => "false"
131 assert %{"id" => second_id} = json_response(conn_two, 200)
133 assert id == second_id
137 |> assign(:user, user)
138 |> post("/api/v1/statuses", %{
140 "spoiler_text" => "2hu",
141 "sensitive" => "false"
144 assert %{"id" => third_id} = json_response(conn_three, 200)
146 refute id == third_id
149 describe "posting polls" do
150 test "posting a poll", %{conn: conn} do
152 time = NaiveDateTime.utc_now()
156 |> assign(:user, user)
157 |> post("/api/v1/statuses", %{
158 "status" => "Who is the #bestgrill?",
159 "poll" => %{"options" => ["Rei", "Asuka", "Misato"], "expires_in" => 420}
162 response = json_response(conn, 200)
164 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
165 title in ["Rei", "Asuka", "Misato"]
168 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
169 refute response["poll"]["expred"]
172 test "option limit is enforced", %{conn: conn} do
174 limit = Pleroma.Config.get([:instance, :poll_limits, :max_options])
178 |> assign(:user, user)
179 |> post("/api/v1/statuses", %{
181 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
184 %{"error" => error} = json_response(conn, 422)
185 assert error == "Poll can't contain more than #{limit} options"
188 test "option character limit is enforced", %{conn: conn} do
190 limit = Pleroma.Config.get([:instance, :poll_limits, :max_option_chars])
194 |> assign(:user, user)
195 |> post("/api/v1/statuses", %{
198 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
203 %{"error" => error} = json_response(conn, 422)
204 assert error == "Poll options cannot be longer than #{limit} characters each"
207 test "minimal date limit is enforced", %{conn: conn} do
209 limit = Pleroma.Config.get([:instance, :poll_limits, :min_expiration])
213 |> assign(:user, user)
214 |> post("/api/v1/statuses", %{
215 "status" => "imagine arbitrary limits",
217 "options" => ["this post was made by pleroma gang"],
218 "expires_in" => limit - 1
222 %{"error" => error} = json_response(conn, 422)
223 assert error == "Expiration date is too soon"
226 test "maximum date limit is enforced", %{conn: conn} do
228 limit = Pleroma.Config.get([:instance, :poll_limits, :max_expiration])
232 |> assign(:user, user)
233 |> post("/api/v1/statuses", %{
234 "status" => "imagine arbitrary limits",
236 "options" => ["this post was made by pleroma gang"],
237 "expires_in" => limit + 1
241 %{"error" => error} = json_response(conn, 422)
242 assert error == "Expiration date is too far in the future"
246 test "posting a sensitive status", %{conn: conn} do
251 |> assign(:user, user)
252 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
254 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
255 assert Activity.get_by_id(id)
258 test "posting a fake status", %{conn: conn} do
263 |> assign(:user, user)
264 |> post("/api/v1/statuses", %{
266 "\"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"
269 real_status = json_response(real_conn, 200)
272 assert Object.get_by_ap_id(real_status["uri"])
276 |> Map.put("id", nil)
277 |> Map.put("url", nil)
278 |> Map.put("uri", nil)
279 |> Map.put("created_at", nil)
280 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
284 |> assign(:user, user)
285 |> post("/api/v1/statuses", %{
287 "\"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",
291 fake_status = json_response(fake_conn, 200)
294 refute Object.get_by_ap_id(fake_status["uri"])
298 |> Map.put("id", nil)
299 |> Map.put("url", nil)
300 |> Map.put("uri", nil)
301 |> Map.put("created_at", nil)
302 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
304 assert real_status == fake_status
307 test "posting a status with OGP link preview", %{conn: conn} do
308 Pleroma.Config.put([:rich_media, :enabled], true)
313 |> assign(:user, user)
314 |> post("/api/v1/statuses", %{
315 "status" => "http://example.com/ogp"
318 assert %{"id" => id, "card" => %{"title" => "The Rock"}} = json_response(conn, 200)
319 assert Activity.get_by_id(id)
320 Pleroma.Config.put([:rich_media, :enabled], false)
323 test "posting a direct status", %{conn: conn} do
324 user1 = insert(:user)
325 user2 = insert(:user)
326 content = "direct cofe @#{user2.nickname}"
330 |> assign(:user, user1)
331 |> post("api/v1/statuses", %{"status" => content, "visibility" => "direct"})
333 assert %{"id" => id, "visibility" => "direct"} = json_response(conn, 200)
334 assert activity = Activity.get_by_id(id)
335 assert activity.recipients == [user2.ap_id, user1.ap_id]
336 assert activity.data["to"] == [user2.ap_id]
337 assert activity.data["cc"] == []
340 test "direct timeline", %{conn: conn} do
341 user_one = insert(:user)
342 user_two = insert(:user)
344 {:ok, user_two} = User.follow(user_two, user_one)
347 CommonAPI.post(user_one, %{
348 "status" => "Hi @#{user_two.nickname}!",
349 "visibility" => "direct"
352 {:ok, _follower_only} =
353 CommonAPI.post(user_one, %{
354 "status" => "Hi @#{user_two.nickname}!",
355 "visibility" => "private"
358 # Only direct should be visible here
361 |> assign(:user, user_two)
362 |> get("api/v1/timelines/direct")
364 [status] = json_response(res_conn, 200)
366 assert %{"visibility" => "direct"} = status
367 assert status["url"] != direct.data["id"]
369 # User should be able to see his own direct message
372 |> assign(:user, user_one)
373 |> get("api/v1/timelines/direct")
375 [status] = json_response(res_conn, 200)
377 assert %{"visibility" => "direct"} = status
379 # Both should be visible here
382 |> assign(:user, user_two)
383 |> get("api/v1/timelines/home")
385 [_s1, _s2] = json_response(res_conn, 200)
388 Enum.each(1..20, fn _ ->
390 CommonAPI.post(user_one, %{
391 "status" => "Hi @#{user_two.nickname}!",
392 "visibility" => "direct"
398 |> assign(:user, user_two)
399 |> get("api/v1/timelines/direct")
401 statuses = json_response(res_conn, 200)
402 assert length(statuses) == 20
406 |> assign(:user, user_two)
407 |> get("api/v1/timelines/direct", %{max_id: List.last(statuses)["id"]})
409 [status] = json_response(res_conn, 200)
411 assert status["url"] != direct.data["id"]
414 test "Conversations", %{conn: conn} do
415 user_one = insert(:user)
416 user_two = insert(:user)
417 user_three = insert(:user)
419 {:ok, user_two} = User.follow(user_two, user_one)
422 CommonAPI.post(user_one, %{
423 "status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
424 "visibility" => "direct"
427 {:ok, _follower_only} =
428 CommonAPI.post(user_one, %{
429 "status" => "Hi @#{user_two.nickname}!",
430 "visibility" => "private"
435 |> assign(:user, user_one)
436 |> get("/api/v1/conversations")
438 assert response = json_response(res_conn, 200)
443 "accounts" => res_accounts,
444 "last_status" => res_last_status,
449 account_ids = Enum.map(res_accounts, & &1["id"])
450 assert length(res_accounts) == 2
451 assert user_two.id in account_ids
452 assert user_three.id in account_ids
453 assert is_binary(res_id)
454 assert unread == true
455 assert res_last_status["id"] == direct.id
457 # Apparently undocumented API endpoint
460 |> assign(:user, user_one)
461 |> post("/api/v1/conversations/#{res_id}/read")
463 assert response = json_response(res_conn, 200)
464 assert length(response["accounts"]) == 2
465 assert response["last_status"]["id"] == direct.id
466 assert response["unread"] == false
468 # (vanilla) Mastodon frontend behaviour
471 |> assign(:user, user_one)
472 |> get("/api/v1/statuses/#{res_last_status["id"]}/context")
474 assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
477 test "doesn't include DMs from blocked users", %{conn: conn} do
478 blocker = insert(:user)
479 blocked = insert(:user)
481 {:ok, blocker} = User.block(blocker, blocked)
483 {:ok, _blocked_direct} =
484 CommonAPI.post(blocked, %{
485 "status" => "Hi @#{blocker.nickname}!",
486 "visibility" => "direct"
490 CommonAPI.post(user, %{
491 "status" => "Hi @#{blocker.nickname}!",
492 "visibility" => "direct"
497 |> assign(:user, user)
498 |> get("api/v1/timelines/direct")
500 [status] = json_response(res_conn, 200)
501 assert status["id"] == direct.id
504 test "replying to a status", %{conn: conn} do
507 {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"})
511 |> assign(:user, user)
512 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
514 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
516 activity = Activity.get_by_id(id)
518 assert activity.data["context"] == replied_to.data["context"]
519 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
522 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
527 |> assign(:user, user)
528 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
530 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
532 activity = Activity.get_by_id(id)
537 test "verify_credentials", %{conn: conn} do
542 |> assign(:user, user)
543 |> get("/api/v1/accounts/verify_credentials")
545 assert %{"id" => id, "source" => %{"privacy" => "public"}} = json_response(conn, 200)
546 assert id == to_string(user.id)
549 test "verify_credentials default scope unlisted", %{conn: conn} do
550 user = insert(:user, %{info: %User.Info{default_scope: "unlisted"}})
554 |> assign(:user, user)
555 |> get("/api/v1/accounts/verify_credentials")
557 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} = json_response(conn, 200)
558 assert id == to_string(user.id)
561 test "apps/verify_credentials", %{conn: conn} do
562 token = insert(:oauth_token)
566 |> assign(:user, token.user)
567 |> assign(:token, token)
568 |> get("/api/v1/apps/verify_credentials")
570 app = Repo.preload(token, :app).app
573 "name" => app.client_name,
574 "website" => app.website,
575 "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
578 assert expected == json_response(conn, 200)
581 test "creates an oauth app", %{conn: conn} do
583 app_attrs = build(:oauth_app)
587 |> assign(:user, user)
588 |> post("/api/v1/apps", %{
589 client_name: app_attrs.client_name,
590 redirect_uris: app_attrs.redirect_uris
593 [app] = Repo.all(App)
596 "name" => app.client_name,
597 "website" => app.website,
598 "client_id" => app.client_id,
599 "client_secret" => app.client_secret,
600 "id" => app.id |> to_string(),
601 "redirect_uri" => app.redirect_uris,
602 "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
605 assert expected == json_response(conn, 200)
608 test "get a status", %{conn: conn} do
609 activity = insert(:note_activity)
613 |> get("/api/v1/statuses/#{activity.id}")
615 assert %{"id" => id} = json_response(conn, 200)
616 assert id == to_string(activity.id)
619 describe "deleting a status" do
620 test "when you created it", %{conn: conn} do
621 activity = insert(:note_activity)
622 author = User.get_cached_by_ap_id(activity.data["actor"])
626 |> assign(:user, author)
627 |> delete("/api/v1/statuses/#{activity.id}")
629 assert %{} = json_response(conn, 200)
631 refute Activity.get_by_id(activity.id)
634 test "when you didn't create it", %{conn: conn} do
635 activity = insert(:note_activity)
640 |> assign(:user, user)
641 |> delete("/api/v1/statuses/#{activity.id}")
643 assert %{"error" => _} = json_response(conn, 403)
645 assert Activity.get_by_id(activity.id) == activity
648 test "when you're an admin or moderator", %{conn: conn} do
649 activity1 = insert(:note_activity)
650 activity2 = insert(:note_activity)
651 admin = insert(:user, info: %{is_admin: true})
652 moderator = insert(:user, info: %{is_moderator: true})
656 |> assign(:user, admin)
657 |> delete("/api/v1/statuses/#{activity1.id}")
659 assert %{} = json_response(res_conn, 200)
663 |> assign(:user, moderator)
664 |> delete("/api/v1/statuses/#{activity2.id}")
666 assert %{} = json_response(res_conn, 200)
668 refute Activity.get_by_id(activity1.id)
669 refute Activity.get_by_id(activity2.id)
673 describe "filters" do
674 test "creating a filter", %{conn: conn} do
677 filter = %Pleroma.Filter{
684 |> assign(:user, user)
685 |> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context})
687 assert response = json_response(conn, 200)
688 assert response["phrase"] == filter.phrase
689 assert response["context"] == filter.context
690 assert response["irreversible"] == false
691 assert response["id"] != nil
692 assert response["id"] != ""
695 test "fetching a list of filters", %{conn: conn} do
698 query_one = %Pleroma.Filter{
705 query_two = %Pleroma.Filter{
712 {:ok, filter_one} = Pleroma.Filter.create(query_one)
713 {:ok, filter_two} = Pleroma.Filter.create(query_two)
717 |> assign(:user, user)
718 |> get("/api/v1/filters")
719 |> json_response(200)
725 filters: [filter_two, filter_one]
729 test "get a filter", %{conn: conn} do
732 query = %Pleroma.Filter{
739 {:ok, filter} = Pleroma.Filter.create(query)
743 |> assign(:user, user)
744 |> get("/api/v1/filters/#{filter.filter_id}")
746 assert _response = json_response(conn, 200)
749 test "update a filter", %{conn: conn} do
752 query = %Pleroma.Filter{
759 {:ok, _filter} = Pleroma.Filter.create(query)
761 new = %Pleroma.Filter{
768 |> assign(:user, user)
769 |> put("/api/v1/filters/#{query.filter_id}", %{
774 assert response = json_response(conn, 200)
775 assert response["phrase"] == new.phrase
776 assert response["context"] == new.context
779 test "delete a filter", %{conn: conn} do
782 query = %Pleroma.Filter{
789 {:ok, filter} = Pleroma.Filter.create(query)
793 |> assign(:user, user)
794 |> delete("/api/v1/filters/#{filter.filter_id}")
796 assert response = json_response(conn, 200)
797 assert response == %{}
802 test "creating a list", %{conn: conn} do
807 |> assign(:user, user)
808 |> post("/api/v1/lists", %{"title" => "cuties"})
810 assert %{"title" => title} = json_response(conn, 200)
811 assert title == "cuties"
814 test "adding users to a list", %{conn: conn} do
816 other_user = insert(:user)
817 {:ok, list} = Pleroma.List.create("name", user)
821 |> assign(:user, user)
822 |> post("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
824 assert %{} == json_response(conn, 200)
825 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
826 assert following == [other_user.follower_address]
829 test "removing users from a list", %{conn: conn} do
831 other_user = insert(:user)
832 third_user = insert(:user)
833 {:ok, list} = Pleroma.List.create("name", user)
834 {:ok, list} = Pleroma.List.follow(list, other_user)
835 {:ok, list} = Pleroma.List.follow(list, third_user)
839 |> assign(:user, user)
840 |> delete("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
842 assert %{} == json_response(conn, 200)
843 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
844 assert following == [third_user.follower_address]
847 test "listing users in a list", %{conn: conn} do
849 other_user = insert(:user)
850 {:ok, list} = Pleroma.List.create("name", user)
851 {:ok, list} = Pleroma.List.follow(list, other_user)
855 |> assign(:user, user)
856 |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
858 assert [%{"id" => id}] = json_response(conn, 200)
859 assert id == to_string(other_user.id)
862 test "retrieving a list", %{conn: conn} do
864 {:ok, list} = Pleroma.List.create("name", user)
868 |> assign(:user, user)
869 |> get("/api/v1/lists/#{list.id}")
871 assert %{"id" => id} = json_response(conn, 200)
872 assert id == to_string(list.id)
875 test "renaming a list", %{conn: conn} do
877 {:ok, list} = Pleroma.List.create("name", user)
881 |> assign(:user, user)
882 |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
884 assert %{"title" => name} = json_response(conn, 200)
885 assert name == "newname"
888 test "deleting a list", %{conn: conn} do
890 {:ok, list} = Pleroma.List.create("name", user)
894 |> assign(:user, user)
895 |> delete("/api/v1/lists/#{list.id}")
897 assert %{} = json_response(conn, 200)
898 assert is_nil(Repo.get(Pleroma.List, list.id))
901 test "list timeline", %{conn: conn} do
903 other_user = insert(:user)
904 {:ok, _activity_one} = TwitterAPI.create_status(user, %{"status" => "Marisa is cute."})
905 {:ok, activity_two} = TwitterAPI.create_status(other_user, %{"status" => "Marisa is cute."})
906 {:ok, list} = Pleroma.List.create("name", user)
907 {:ok, list} = Pleroma.List.follow(list, other_user)
911 |> assign(:user, user)
912 |> get("/api/v1/timelines/list/#{list.id}")
914 assert [%{"id" => id}] = json_response(conn, 200)
916 assert id == to_string(activity_two.id)
919 test "list timeline does not leak non-public statuses for unfollowed users", %{conn: conn} do
921 other_user = insert(:user)
922 {:ok, activity_one} = TwitterAPI.create_status(other_user, %{"status" => "Marisa is cute."})
924 {:ok, _activity_two} =
925 TwitterAPI.create_status(other_user, %{
926 "status" => "Marisa is cute.",
927 "visibility" => "private"
930 {:ok, list} = Pleroma.List.create("name", user)
931 {:ok, list} = Pleroma.List.follow(list, other_user)
935 |> assign(:user, user)
936 |> get("/api/v1/timelines/list/#{list.id}")
938 assert [%{"id" => id}] = json_response(conn, 200)
940 assert id == to_string(activity_one.id)
944 describe "notifications" do
945 test "list of notifications", %{conn: conn} do
947 other_user = insert(:user)
950 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
952 {:ok, [_notification]} = Notification.create_notifications(activity)
956 |> assign(:user, user)
957 |> get("/api/v1/notifications")
960 "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
962 }\">@<span>#{user.nickname}</span></a></span>"
964 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
965 assert response == expected_response
968 test "getting a single notification", %{conn: conn} do
970 other_user = insert(:user)
973 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
975 {:ok, [notification]} = Notification.create_notifications(activity)
979 |> assign(:user, user)
980 |> get("/api/v1/notifications/#{notification.id}")
983 "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
985 }\">@<span>#{user.nickname}</span></a></span>"
987 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
988 assert response == expected_response
991 test "dismissing a single notification", %{conn: conn} do
993 other_user = insert(:user)
996 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
998 {:ok, [notification]} = Notification.create_notifications(activity)
1002 |> assign(:user, user)
1003 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
1005 assert %{} = json_response(conn, 200)
1008 test "clearing all notifications", %{conn: conn} do
1009 user = insert(:user)
1010 other_user = insert(:user)
1013 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
1015 {:ok, [_notification]} = Notification.create_notifications(activity)
1019 |> assign(:user, user)
1020 |> post("/api/v1/notifications/clear")
1022 assert %{} = json_response(conn, 200)
1026 |> assign(:user, user)
1027 |> get("/api/v1/notifications")
1029 assert all = json_response(conn, 200)
1033 test "paginates notifications using min_id, since_id, max_id, and limit", %{conn: conn} do
1034 user = insert(:user)
1035 other_user = insert(:user)
1037 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
1038 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
1039 {:ok, activity3} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
1040 {:ok, activity4} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
1042 notification1_id = Repo.get_by(Notification, activity_id: activity1.id).id |> to_string()
1043 notification2_id = Repo.get_by(Notification, activity_id: activity2.id).id |> to_string()
1044 notification3_id = Repo.get_by(Notification, activity_id: activity3.id).id |> to_string()
1045 notification4_id = Repo.get_by(Notification, activity_id: activity4.id).id |> to_string()
1049 |> assign(:user, user)
1054 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
1056 result = json_response(conn_res, 200)
1057 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
1062 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
1064 result = json_response(conn_res, 200)
1065 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
1070 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
1072 result = json_response(conn_res, 200)
1073 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
1076 test "filters notifications using exclude_types", %{conn: conn} do
1077 user = insert(:user)
1078 other_user = insert(:user)
1080 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
1081 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
1082 {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user)
1083 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
1084 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
1086 mention_notification_id =
1087 Repo.get_by(Notification, activity_id: mention_activity.id).id |> to_string()
1089 favorite_notification_id =
1090 Repo.get_by(Notification, activity_id: favorite_activity.id).id |> to_string()
1092 reblog_notification_id =
1093 Repo.get_by(Notification, activity_id: reblog_activity.id).id |> to_string()
1095 follow_notification_id =
1096 Repo.get_by(Notification, activity_id: follow_activity.id).id |> to_string()
1100 |> assign(:user, user)
1103 get(conn, "/api/v1/notifications", %{exclude_types: ["mention", "favourite", "reblog"]})
1105 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
1108 get(conn, "/api/v1/notifications", %{exclude_types: ["favourite", "reblog", "follow"]})
1110 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
1113 get(conn, "/api/v1/notifications", %{exclude_types: ["reblog", "follow", "mention"]})
1115 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
1118 get(conn, "/api/v1/notifications", %{exclude_types: ["follow", "mention", "favourite"]})
1120 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
1123 test "destroy multiple", %{conn: conn} do
1124 user = insert(:user)
1125 other_user = insert(:user)
1127 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
1128 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
1129 {:ok, activity3} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
1130 {:ok, activity4} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
1132 notification1_id = Repo.get_by(Notification, activity_id: activity1.id).id |> to_string()
1133 notification2_id = Repo.get_by(Notification, activity_id: activity2.id).id |> to_string()
1134 notification3_id = Repo.get_by(Notification, activity_id: activity3.id).id |> to_string()
1135 notification4_id = Repo.get_by(Notification, activity_id: activity4.id).id |> to_string()
1139 |> assign(:user, user)
1143 |> get("/api/v1/notifications")
1145 result = json_response(conn_res, 200)
1146 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
1150 |> assign(:user, other_user)
1154 |> get("/api/v1/notifications")
1156 result = json_response(conn_res, 200)
1157 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
1161 |> delete("/api/v1/notifications/destroy_multiple", %{
1162 "ids" => [notification1_id, notification2_id]
1165 assert json_response(conn_destroy, 200) == %{}
1169 |> get("/api/v1/notifications")
1171 result = json_response(conn_res, 200)
1172 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
1176 describe "reblogging" do
1177 test "reblogs and returns the reblogged status", %{conn: conn} do
1178 activity = insert(:note_activity)
1179 user = insert(:user)
1183 |> assign(:user, user)
1184 |> post("/api/v1/statuses/#{activity.id}/reblog")
1187 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1189 } = json_response(conn, 200)
1191 assert to_string(activity.id) == id
1194 test "reblogged status for another user", %{conn: conn} do
1195 activity = insert(:note_activity)
1196 user1 = insert(:user)
1197 user2 = insert(:user)
1198 user3 = insert(:user)
1199 CommonAPI.favorite(activity.id, user2)
1200 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
1201 {:ok, reblog_activity1, _object} = CommonAPI.repeat(activity.id, user1)
1202 {:ok, _, _object} = CommonAPI.repeat(activity.id, user2)
1206 |> assign(:user, user3)
1207 |> get("/api/v1/statuses/#{reblog_activity1.id}")
1210 "reblog" => %{"id" => id, "reblogged" => false, "reblogs_count" => 2},
1211 "reblogged" => false,
1212 "favourited" => false,
1213 "bookmarked" => false
1214 } = json_response(conn_res, 200)
1218 |> assign(:user, user2)
1219 |> get("/api/v1/statuses/#{reblog_activity1.id}")
1222 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
1223 "reblogged" => true,
1224 "favourited" => true,
1225 "bookmarked" => true
1226 } = json_response(conn_res, 200)
1228 assert to_string(activity.id) == id
1232 describe "unreblogging" do
1233 test "unreblogs and returns the unreblogged status", %{conn: conn} do
1234 activity = insert(:note_activity)
1235 user = insert(:user)
1237 {:ok, _, _} = CommonAPI.repeat(activity.id, user)
1241 |> assign(:user, user)
1242 |> post("/api/v1/statuses/#{activity.id}/unreblog")
1244 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
1246 assert to_string(activity.id) == id
1250 describe "favoriting" do
1251 test "favs a status and returns it", %{conn: conn} do
1252 activity = insert(:note_activity)
1253 user = insert(:user)
1257 |> assign(:user, user)
1258 |> post("/api/v1/statuses/#{activity.id}/favourite")
1260 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
1261 json_response(conn, 200)
1263 assert to_string(activity.id) == id
1266 test "returns 500 for a wrong id", %{conn: conn} do
1267 user = insert(:user)
1271 |> assign(:user, user)
1272 |> post("/api/v1/statuses/1/favourite")
1273 |> json_response(500)
1275 assert resp == "Something went wrong"
1279 describe "unfavoriting" do
1280 test "unfavorites a status and returns it", %{conn: conn} do
1281 activity = insert(:note_activity)
1282 user = insert(:user)
1284 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
1288 |> assign(:user, user)
1289 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
1291 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
1292 json_response(conn, 200)
1294 assert to_string(activity.id) == id
1298 describe "user timelines" do
1299 test "gets a users statuses", %{conn: conn} do
1300 user_one = insert(:user)
1301 user_two = insert(:user)
1302 user_three = insert(:user)
1304 {:ok, user_three} = User.follow(user_three, user_one)
1306 {:ok, activity} = CommonAPI.post(user_one, %{"status" => "HI!!!"})
1308 {:ok, direct_activity} =
1309 CommonAPI.post(user_one, %{
1310 "status" => "Hi, @#{user_two.nickname}.",
1311 "visibility" => "direct"
1314 {:ok, private_activity} =
1315 CommonAPI.post(user_one, %{"status" => "private", "visibility" => "private"})
1319 |> get("/api/v1/accounts/#{user_one.id}/statuses")
1321 assert [%{"id" => id}] = json_response(resp, 200)
1322 assert id == to_string(activity.id)
1326 |> assign(:user, user_two)
1327 |> get("/api/v1/accounts/#{user_one.id}/statuses")
1329 assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
1330 assert id_one == to_string(direct_activity.id)
1331 assert id_two == to_string(activity.id)
1335 |> assign(:user, user_three)
1336 |> get("/api/v1/accounts/#{user_one.id}/statuses")
1338 assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
1339 assert id_one == to_string(private_activity.id)
1340 assert id_two == to_string(activity.id)
1343 test "unimplemented pinned statuses feature", %{conn: conn} do
1344 note = insert(:note_activity)
1345 user = User.get_cached_by_ap_id(note.data["actor"])
1349 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1351 assert json_response(conn, 200) == []
1354 test "gets an users media", %{conn: conn} do
1355 note = insert(:note_activity)
1356 user = User.get_cached_by_ap_id(note.data["actor"])
1358 file = %Plug.Upload{
1359 content_type: "image/jpg",
1360 path: Path.absname("test/fixtures/image.jpg"),
1361 filename: "an_image.jpg"
1365 TwitterAPI.upload(file, user, "json")
1369 TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})
1373 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
1375 assert [%{"id" => id}] = json_response(conn, 200)
1376 assert id == to_string(image_post.id)
1380 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
1382 assert [%{"id" => id}] = json_response(conn, 200)
1383 assert id == to_string(image_post.id)
1386 test "gets a user's statuses without reblogs", %{conn: conn} do
1387 user = insert(:user)
1388 {:ok, post} = CommonAPI.post(user, %{"status" => "HI!!!"})
1389 {:ok, _, _} = CommonAPI.repeat(post.id, user)
1393 |> get("/api/v1/accounts/#{user.id}/statuses", %{"exclude_reblogs" => "true"})
1395 assert [%{"id" => id}] = json_response(conn, 200)
1396 assert id == to_string(post.id)
1400 |> get("/api/v1/accounts/#{user.id}/statuses", %{"exclude_reblogs" => "1"})
1402 assert [%{"id" => id}] = json_response(conn, 200)
1403 assert id == to_string(post.id)
1407 describe "user relationships" do
1408 test "returns the relationships for the current user", %{conn: conn} do
1409 user = insert(:user)
1410 other_user = insert(:user)
1411 {:ok, user} = User.follow(user, other_user)
1415 |> assign(:user, user)
1416 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
1418 assert [relationship] = json_response(conn, 200)
1420 assert to_string(other_user.id) == relationship["id"]
1424 describe "media upload" do
1426 upload_config = Pleroma.Config.get([Pleroma.Upload])
1427 proxy_config = Pleroma.Config.get([:media_proxy])
1430 Pleroma.Config.put([Pleroma.Upload], upload_config)
1431 Pleroma.Config.put([:media_proxy], proxy_config)
1434 user = insert(:user)
1438 |> assign(:user, user)
1440 image = %Plug.Upload{
1441 content_type: "image/jpg",
1442 path: Path.absname("test/fixtures/image.jpg"),
1443 filename: "an_image.jpg"
1446 [conn: conn, image: image]
1449 test "returns uploaded image", %{conn: conn, image: image} do
1450 desc = "Description of the image"
1454 |> post("/api/v1/media", %{"file" => image, "description" => desc})
1455 |> json_response(:ok)
1457 assert media["type"] == "image"
1458 assert media["description"] == desc
1461 object = Repo.get(Object, media["id"])
1462 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
1465 test "returns proxied url when media proxy is enabled", %{conn: conn, image: image} do
1466 Pleroma.Config.put([Pleroma.Upload, :base_url], "https://media.pleroma.social")
1468 proxy_url = "https://cache.pleroma.social"
1469 Pleroma.Config.put([:media_proxy, :enabled], true)
1470 Pleroma.Config.put([:media_proxy, :base_url], proxy_url)
1474 |> post("/api/v1/media", %{"file" => image})
1475 |> json_response(:ok)
1477 assert String.starts_with?(media["url"], proxy_url)
1480 test "returns media url when proxy is enabled but media url is whitelisted", %{
1484 media_url = "https://media.pleroma.social"
1485 Pleroma.Config.put([Pleroma.Upload, :base_url], media_url)
1487 Pleroma.Config.put([:media_proxy, :enabled], true)
1488 Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")
1489 Pleroma.Config.put([:media_proxy, :whitelist], ["media.pleroma.social"])
1493 |> post("/api/v1/media", %{"file" => image})
1494 |> json_response(:ok)
1496 assert String.starts_with?(media["url"], media_url)
1500 describe "locked accounts" do
1501 test "/api/v1/follow_requests works" do
1502 user = insert(:user, %{info: %User.Info{locked: true}})
1503 other_user = insert(:user)
1505 {:ok, _activity} = ActivityPub.follow(other_user, user)
1507 user = User.get_cached_by_id(user.id)
1508 other_user = User.get_cached_by_id(other_user.id)
1510 assert User.following?(other_user, user) == false
1514 |> assign(:user, user)
1515 |> get("/api/v1/follow_requests")
1517 assert [relationship] = json_response(conn, 200)
1518 assert to_string(other_user.id) == relationship["id"]
1521 test "/api/v1/follow_requests/:id/authorize works" do
1522 user = insert(:user, %{info: %User.Info{locked: true}})
1523 other_user = insert(:user)
1525 {:ok, _activity} = ActivityPub.follow(other_user, user)
1527 user = User.get_cached_by_id(user.id)
1528 other_user = User.get_cached_by_id(other_user.id)
1530 assert User.following?(other_user, user) == false
1534 |> assign(:user, user)
1535 |> post("/api/v1/follow_requests/#{other_user.id}/authorize")
1537 assert relationship = json_response(conn, 200)
1538 assert to_string(other_user.id) == relationship["id"]
1540 user = User.get_cached_by_id(user.id)
1541 other_user = User.get_cached_by_id(other_user.id)
1543 assert User.following?(other_user, user) == true
1546 test "verify_credentials", %{conn: conn} do
1547 user = insert(:user, %{info: %User.Info{default_scope: "private"}})
1551 |> assign(:user, user)
1552 |> get("/api/v1/accounts/verify_credentials")
1554 assert %{"id" => id, "source" => %{"privacy" => "private"}} = json_response(conn, 200)
1555 assert id == to_string(user.id)
1558 test "/api/v1/follow_requests/:id/reject works" do
1559 user = insert(:user, %{info: %User.Info{locked: true}})
1560 other_user = insert(:user)
1562 {:ok, _activity} = ActivityPub.follow(other_user, user)
1564 user = User.get_cached_by_id(user.id)
1568 |> assign(:user, user)
1569 |> post("/api/v1/follow_requests/#{other_user.id}/reject")
1571 assert relationship = json_response(conn, 200)
1572 assert to_string(other_user.id) == relationship["id"]
1574 user = User.get_cached_by_id(user.id)
1575 other_user = User.get_cached_by_id(other_user.id)
1577 assert User.following?(other_user, user) == false
1581 test "account fetching", %{conn: conn} do
1582 user = insert(:user)
1586 |> get("/api/v1/accounts/#{user.id}")
1588 assert %{"id" => id} = json_response(conn, 200)
1589 assert id == to_string(user.id)
1593 |> get("/api/v1/accounts/-1")
1595 assert %{"error" => "Can't find user"} = json_response(conn, 404)
1598 test "account fetching also works nickname", %{conn: conn} do
1599 user = insert(:user)
1603 |> get("/api/v1/accounts/#{user.nickname}")
1605 assert %{"id" => id} = json_response(conn, 200)
1606 assert id == user.id
1609 test "mascot upload", %{conn: conn} do
1610 user = insert(:user)
1612 non_image_file = %Plug.Upload{
1613 content_type: "audio/mpeg",
1614 path: Path.absname("test/fixtures/sound.mp3"),
1615 filename: "sound.mp3"
1620 |> assign(:user, user)
1621 |> put("/api/v1/pleroma/mascot", %{"file" => non_image_file})
1623 assert json_response(conn, 415)
1625 file = %Plug.Upload{
1626 content_type: "image/jpg",
1627 path: Path.absname("test/fixtures/image.jpg"),
1628 filename: "an_image.jpg"
1633 |> assign(:user, user)
1634 |> put("/api/v1/pleroma/mascot", %{"file" => file})
1636 assert %{"id" => _, "type" => image} = json_response(conn, 200)
1639 test "mascot retrieving", %{conn: conn} do
1640 user = insert(:user)
1641 # When user hasn't set a mascot, we should just get pleroma tan back
1644 |> assign(:user, user)
1645 |> get("/api/v1/pleroma/mascot")
1647 assert %{"url" => url} = json_response(conn, 200)
1648 assert url =~ "pleroma-fox-tan-smol"
1650 # When a user sets their mascot, we should get that back
1651 file = %Plug.Upload{
1652 content_type: "image/jpg",
1653 path: Path.absname("test/fixtures/image.jpg"),
1654 filename: "an_image.jpg"
1659 |> assign(:user, user)
1660 |> put("/api/v1/pleroma/mascot", %{"file" => file})
1662 assert json_response(conn, 200)
1664 user = User.get_cached_by_id(user.id)
1668 |> assign(:user, user)
1669 |> get("/api/v1/pleroma/mascot")
1671 assert %{"url" => url, "type" => "image"} = json_response(conn, 200)
1672 assert url =~ "an_image"
1675 test "hashtag timeline", %{conn: conn} do
1676 following = insert(:user)
1679 {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
1681 {:ok, [_activity]} =
1682 OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
1686 |> get("/api/v1/timelines/tag/2hu")
1688 assert [%{"id" => id}] = json_response(nconn, 200)
1690 assert id == to_string(activity.id)
1692 # works for different capitalization too
1695 |> get("/api/v1/timelines/tag/2HU")
1697 assert [%{"id" => id}] = json_response(nconn, 200)
1699 assert id == to_string(activity.id)
1703 test "multi-hashtag timeline", %{conn: conn} do
1704 user = insert(:user)
1706 {:ok, activity_test} = CommonAPI.post(user, %{"status" => "#test"})
1707 {:ok, activity_test1} = CommonAPI.post(user, %{"status" => "#test #test1"})
1708 {:ok, activity_none} = CommonAPI.post(user, %{"status" => "#test #none"})
1712 |> get("/api/v1/timelines/tag/test", %{"any" => ["test1"]})
1714 [status_none, status_test1, status_test] = json_response(any_test, 200)
1716 assert to_string(activity_test.id) == status_test["id"]
1717 assert to_string(activity_test1.id) == status_test1["id"]
1718 assert to_string(activity_none.id) == status_none["id"]
1722 |> get("/api/v1/timelines/tag/test", %{"all" => ["test1"], "none" => ["none"]})
1724 assert [status_test1] == json_response(restricted_test, 200)
1726 all_test = conn |> get("/api/v1/timelines/tag/test", %{"all" => ["none"]})
1728 assert [status_none] == json_response(all_test, 200)
1731 test "getting followers", %{conn: conn} do
1732 user = insert(:user)
1733 other_user = insert(:user)
1734 {:ok, user} = User.follow(user, other_user)
1738 |> get("/api/v1/accounts/#{other_user.id}/followers")
1740 assert [%{"id" => id}] = json_response(conn, 200)
1741 assert id == to_string(user.id)
1744 test "getting followers, hide_followers", %{conn: conn} do
1745 user = insert(:user)
1746 other_user = insert(:user, %{info: %{hide_followers: true}})
1747 {:ok, _user} = User.follow(user, other_user)
1751 |> get("/api/v1/accounts/#{other_user.id}/followers")
1753 assert [] == json_response(conn, 200)
1756 test "getting followers, hide_followers, same user requesting", %{conn: conn} do
1757 user = insert(:user)
1758 other_user = insert(:user, %{info: %{hide_followers: true}})
1759 {:ok, _user} = User.follow(user, other_user)
1763 |> assign(:user, other_user)
1764 |> get("/api/v1/accounts/#{other_user.id}/followers")
1766 refute [] == json_response(conn, 200)
1769 test "getting followers, pagination", %{conn: conn} do
1770 user = insert(:user)
1771 follower1 = insert(:user)
1772 follower2 = insert(:user)
1773 follower3 = insert(:user)
1774 {:ok, _} = User.follow(follower1, user)
1775 {:ok, _} = User.follow(follower2, user)
1776 {:ok, _} = User.follow(follower3, user)
1780 |> assign(:user, user)
1784 |> get("/api/v1/accounts/#{user.id}/followers?since_id=#{follower1.id}")
1786 assert [%{"id" => id3}, %{"id" => id2}] = json_response(res_conn, 200)
1787 assert id3 == follower3.id
1788 assert id2 == follower2.id
1792 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3.id}")
1794 assert [%{"id" => id2}, %{"id" => id1}] = json_response(res_conn, 200)
1795 assert id2 == follower2.id
1796 assert id1 == follower1.id
1800 |> get("/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3.id}")
1802 assert [%{"id" => id2}] = json_response(res_conn, 200)
1803 assert id2 == follower2.id
1805 assert [link_header] = get_resp_header(res_conn, "link")
1806 assert link_header =~ ~r/min_id=#{follower2.id}/
1807 assert link_header =~ ~r/max_id=#{follower2.id}/
1810 test "getting following", %{conn: conn} do
1811 user = insert(:user)
1812 other_user = insert(:user)
1813 {:ok, user} = User.follow(user, other_user)
1817 |> get("/api/v1/accounts/#{user.id}/following")
1819 assert [%{"id" => id}] = json_response(conn, 200)
1820 assert id == to_string(other_user.id)
1823 test "getting following, hide_follows", %{conn: conn} do
1824 user = insert(:user, %{info: %{hide_follows: true}})
1825 other_user = insert(:user)
1826 {:ok, user} = User.follow(user, other_user)
1830 |> get("/api/v1/accounts/#{user.id}/following")
1832 assert [] == json_response(conn, 200)
1835 test "getting following, hide_follows, same user requesting", %{conn: conn} do
1836 user = insert(:user, %{info: %{hide_follows: true}})
1837 other_user = insert(:user)
1838 {:ok, user} = User.follow(user, other_user)
1842 |> assign(:user, user)
1843 |> get("/api/v1/accounts/#{user.id}/following")
1845 refute [] == json_response(conn, 200)
1848 test "getting following, pagination", %{conn: conn} do
1849 user = insert(:user)
1850 following1 = insert(:user)
1851 following2 = insert(:user)
1852 following3 = insert(:user)
1853 {:ok, _} = User.follow(user, following1)
1854 {:ok, _} = User.follow(user, following2)
1855 {:ok, _} = User.follow(user, following3)
1859 |> assign(:user, user)
1863 |> get("/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
1865 assert [%{"id" => id3}, %{"id" => id2}] = json_response(res_conn, 200)
1866 assert id3 == following3.id
1867 assert id2 == following2.id
1871 |> get("/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
1873 assert [%{"id" => id2}, %{"id" => id1}] = json_response(res_conn, 200)
1874 assert id2 == following2.id
1875 assert id1 == following1.id
1879 |> get("/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
1881 assert [%{"id" => id2}] = json_response(res_conn, 200)
1882 assert id2 == following2.id
1884 assert [link_header] = get_resp_header(res_conn, "link")
1885 assert link_header =~ ~r/min_id=#{following2.id}/
1886 assert link_header =~ ~r/max_id=#{following2.id}/
1889 test "following / unfollowing a user", %{conn: conn} do
1890 user = insert(:user)
1891 other_user = insert(:user)
1895 |> assign(:user, user)
1896 |> post("/api/v1/accounts/#{other_user.id}/follow")
1898 assert %{"id" => _id, "following" => true} = json_response(conn, 200)
1900 user = User.get_cached_by_id(user.id)
1904 |> assign(:user, user)
1905 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
1907 assert %{"id" => _id, "following" => false} = json_response(conn, 200)
1909 user = User.get_cached_by_id(user.id)
1913 |> assign(:user, user)
1914 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
1916 assert %{"id" => id} = json_response(conn, 200)
1917 assert id == to_string(other_user.id)
1920 test "following without reblogs" do
1921 follower = insert(:user)
1922 followed = insert(:user)
1923 other_user = insert(:user)
1927 |> assign(:user, follower)
1928 |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=false")
1930 assert %{"showing_reblogs" => false} = json_response(conn, 200)
1932 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey"})
1933 {:ok, reblog, _} = CommonAPI.repeat(activity.id, followed)
1937 |> assign(:user, User.get_cached_by_id(follower.id))
1938 |> get("/api/v1/timelines/home")
1940 assert [] == json_response(conn, 200)
1944 |> assign(:user, follower)
1945 |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=true")
1947 assert %{"showing_reblogs" => true} = json_response(conn, 200)
1951 |> assign(:user, User.get_cached_by_id(follower.id))
1952 |> get("/api/v1/timelines/home")
1954 expected_activity_id = reblog.id
1955 assert [%{"id" => ^expected_activity_id}] = json_response(conn, 200)
1958 test "following / unfollowing errors" do
1959 user = insert(:user)
1963 |> assign(:user, user)
1966 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
1967 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
1970 user = User.get_cached_by_id(user.id)
1971 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
1972 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
1974 # self follow via uri
1975 user = User.get_cached_by_id(user.id)
1976 conn_res = post(conn, "/api/v1/follows", %{"uri" => user.nickname})
1977 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
1979 # follow non existing user
1980 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
1981 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
1983 # follow non existing user via uri
1984 conn_res = post(conn, "/api/v1/follows", %{"uri" => "doesntexist"})
1985 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
1987 # unfollow non existing user
1988 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
1989 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
1992 test "muting / unmuting a user", %{conn: conn} do
1993 user = insert(:user)
1994 other_user = insert(:user)
1998 |> assign(:user, user)
1999 |> post("/api/v1/accounts/#{other_user.id}/mute")
2001 assert %{"id" => _id, "muting" => true} = json_response(conn, 200)
2003 user = User.get_cached_by_id(user.id)
2007 |> assign(:user, user)
2008 |> post("/api/v1/accounts/#{other_user.id}/unmute")
2010 assert %{"id" => _id, "muting" => false} = json_response(conn, 200)
2013 test "subscribing / unsubscribing to a user", %{conn: conn} do
2014 user = insert(:user)
2015 subscription_target = insert(:user)
2019 |> assign(:user, user)
2020 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
2022 assert %{"id" => _id, "subscribing" => true} = json_response(conn, 200)
2026 |> assign(:user, user)
2027 |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
2029 assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
2032 test "getting a list of mutes", %{conn: conn} do
2033 user = insert(:user)
2034 other_user = insert(:user)
2036 {:ok, user} = User.mute(user, other_user)
2040 |> assign(:user, user)
2041 |> get("/api/v1/mutes")
2043 other_user_id = to_string(other_user.id)
2044 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
2047 test "blocking / unblocking a user", %{conn: conn} do
2048 user = insert(:user)
2049 other_user = insert(:user)
2053 |> assign(:user, user)
2054 |> post("/api/v1/accounts/#{other_user.id}/block")
2056 assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
2058 user = User.get_cached_by_id(user.id)
2062 |> assign(:user, user)
2063 |> post("/api/v1/accounts/#{other_user.id}/unblock")
2065 assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
2068 test "getting a list of blocks", %{conn: conn} do
2069 user = insert(:user)
2070 other_user = insert(:user)
2072 {:ok, user} = User.block(user, other_user)
2076 |> assign(:user, user)
2077 |> get("/api/v1/blocks")
2079 other_user_id = to_string(other_user.id)
2080 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
2083 test "blocking / unblocking a domain", %{conn: conn} do
2084 user = insert(:user)
2085 other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})
2089 |> assign(:user, user)
2090 |> post("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})
2092 assert %{} = json_response(conn, 200)
2093 user = User.get_cached_by_ap_id(user.ap_id)
2094 assert User.blocks?(user, other_user)
2098 |> assign(:user, user)
2099 |> delete("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})
2101 assert %{} = json_response(conn, 200)
2102 user = User.get_cached_by_ap_id(user.ap_id)
2103 refute User.blocks?(user, other_user)
2106 test "getting a list of domain blocks", %{conn: conn} do
2107 user = insert(:user)
2109 {:ok, user} = User.block_domain(user, "bad.site")
2110 {:ok, user} = User.block_domain(user, "even.worse.site")
2114 |> assign(:user, user)
2115 |> get("/api/v1/domain_blocks")
2117 domain_blocks = json_response(conn, 200)
2119 assert "bad.site" in domain_blocks
2120 assert "even.worse.site" in domain_blocks
2123 test "unimplemented follow_requests, blocks, domain blocks" do
2124 user = insert(:user)
2126 ["blocks", "domain_blocks", "follow_requests"]
2127 |> Enum.each(fn endpoint ->
2130 |> assign(:user, user)
2131 |> get("/api/v1/#{endpoint}")
2133 assert [] = json_response(conn, 200)
2137 test "returns the favorites of a user", %{conn: conn} do
2138 user = insert(:user)
2139 other_user = insert(:user)
2141 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
2142 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
2144 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
2148 |> assign(:user, user)
2149 |> get("/api/v1/favourites")
2151 assert [status] = json_response(first_conn, 200)
2152 assert status["id"] == to_string(activity.id)
2154 assert [{"link", _link_header}] =
2155 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
2157 # Honours query params
2158 {:ok, second_activity} =
2159 CommonAPI.post(other_user, %{
2161 "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
2164 {:ok, _, _} = CommonAPI.favorite(second_activity.id, user)
2166 last_like = status["id"]
2170 |> assign(:user, user)
2171 |> get("/api/v1/favourites?since_id=#{last_like}")
2173 assert [second_status] = json_response(second_conn, 200)
2174 assert second_status["id"] == to_string(second_activity.id)
2178 |> assign(:user, user)
2179 |> get("/api/v1/favourites?limit=0")
2181 assert [] = json_response(third_conn, 200)
2184 describe "getting favorites timeline of specified user" do
2186 [current_user, user] = insert_pair(:user, %{info: %{hide_favorites: false}})
2187 [current_user: current_user, user: user]
2190 test "returns list of statuses favorited by specified user", %{
2192 current_user: current_user,
2195 [activity | _] = insert_pair(:note_activity)
2196 CommonAPI.favorite(activity.id, user)
2200 |> assign(:user, current_user)
2201 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
2202 |> json_response(:ok)
2206 assert length(response) == 1
2207 assert like["id"] == activity.id
2210 test "returns favorites for specified user_id when user is not logged in", %{
2214 activity = insert(:note_activity)
2215 CommonAPI.favorite(activity.id, user)
2219 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
2220 |> json_response(:ok)
2222 assert length(response) == 1
2225 test "returns favorited DM only when user is logged in and he is one of recipients", %{
2227 current_user: current_user,
2231 CommonAPI.post(current_user, %{
2232 "status" => "Hi @#{user.nickname}!",
2233 "visibility" => "direct"
2236 CommonAPI.favorite(direct.id, user)
2240 |> assign(:user, current_user)
2241 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
2242 |> json_response(:ok)
2244 assert length(response) == 1
2246 anonymous_response =
2248 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
2249 |> json_response(:ok)
2251 assert Enum.empty?(anonymous_response)
2254 test "does not return others' favorited DM when user is not one of recipients", %{
2256 current_user: current_user,
2259 user_two = insert(:user)
2262 CommonAPI.post(user_two, %{
2263 "status" => "Hi @#{user.nickname}!",
2264 "visibility" => "direct"
2267 CommonAPI.favorite(direct.id, user)
2271 |> assign(:user, current_user)
2272 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
2273 |> json_response(:ok)
2275 assert Enum.empty?(response)
2278 test "paginates favorites using since_id and max_id", %{
2280 current_user: current_user,
2283 activities = insert_list(10, :note_activity)
2285 Enum.each(activities, fn activity ->
2286 CommonAPI.favorite(activity.id, user)
2289 third_activity = Enum.at(activities, 2)
2290 seventh_activity = Enum.at(activities, 6)
2294 |> assign(:user, current_user)
2295 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{
2296 since_id: third_activity.id,
2297 max_id: seventh_activity.id
2299 |> json_response(:ok)
2301 assert length(response) == 3
2302 refute third_activity in response
2303 refute seventh_activity in response
2306 test "limits favorites using limit parameter", %{
2308 current_user: current_user,
2312 |> insert_list(:note_activity)
2313 |> Enum.each(fn activity ->
2314 CommonAPI.favorite(activity.id, user)
2319 |> assign(:user, current_user)
2320 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{limit: "3"})
2321 |> json_response(:ok)
2323 assert length(response) == 3
2326 test "returns empty response when user does not have any favorited statuses", %{
2328 current_user: current_user,
2333 |> assign(:user, current_user)
2334 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
2335 |> json_response(:ok)
2337 assert Enum.empty?(response)
2340 test "returns 404 error when specified user is not exist", %{conn: conn} do
2341 conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
2343 assert json_response(conn, 404) == %{"error" => "Record not found"}
2346 test "returns 403 error when user has hidden own favorites", %{
2348 current_user: current_user
2350 user = insert(:user, %{info: %{hide_favorites: true}})
2351 activity = insert(:note_activity)
2352 CommonAPI.favorite(activity.id, user)
2356 |> assign(:user, current_user)
2357 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
2359 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
2362 test "hides favorites for new users by default", %{conn: conn, current_user: current_user} do
2363 user = insert(:user)
2364 activity = insert(:note_activity)
2365 CommonAPI.favorite(activity.id, user)
2369 |> assign(:user, current_user)
2370 |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
2372 assert user.info.hide_favorites
2373 assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
2377 describe "updating credentials" do
2378 test "sets user settings in a generic way", %{conn: conn} do
2379 user = insert(:user)
2383 |> assign(:user, user)
2384 |> patch("/api/v1/accounts/update_credentials", %{
2385 "pleroma_settings_store" => %{
2392 assert user = json_response(res_conn, 200)
2393 assert user["pleroma"]["settings_store"] == %{"pleroma_fe" => %{"theme" => "bla"}}
2395 user = Repo.get(User, user["id"])
2399 |> assign(:user, user)
2400 |> patch("/api/v1/accounts/update_credentials", %{
2401 "pleroma_settings_store" => %{
2408 assert user = json_response(res_conn, 200)
2410 assert user["pleroma"]["settings_store"] ==
2412 "pleroma_fe" => %{"theme" => "bla"},
2413 "masto_fe" => %{"theme" => "bla"}
2416 user = Repo.get(User, user["id"])
2420 |> assign(:user, user)
2421 |> patch("/api/v1/accounts/update_credentials", %{
2422 "pleroma_settings_store" => %{
2429 assert user = json_response(res_conn, 200)
2431 assert user["pleroma"]["settings_store"] ==
2433 "pleroma_fe" => %{"theme" => "bla"},
2434 "masto_fe" => %{"theme" => "blub"}
2438 test "updates the user's bio", %{conn: conn} do
2439 user = insert(:user)
2440 user2 = insert(:user)
2444 |> assign(:user, user)
2445 |> patch("/api/v1/accounts/update_credentials", %{
2446 "note" => "I drink #cofe with @#{user2.nickname}"
2449 assert user = json_response(conn, 200)
2451 assert user["note"] ==
2452 ~s(I drink <a class="hashtag" data-tag="cofe" href="http://localhost:4001/tag/cofe" rel="tag">#cofe</a> with <span class="h-card"><a data-user=") <>
2454 ~s(" class="u-url mention" href=") <>
2455 user2.ap_id <> ~s(">@<span>) <> user2.nickname <> ~s(</span></a></span>)
2458 test "updates the user's locking status", %{conn: conn} do
2459 user = insert(:user)
2463 |> assign(:user, user)
2464 |> patch("/api/v1/accounts/update_credentials", %{locked: "true"})
2466 assert user = json_response(conn, 200)
2467 assert user["locked"] == true
2470 test "updates the user's default scope", %{conn: conn} do
2471 user = insert(:user)
2475 |> assign(:user, user)
2476 |> patch("/api/v1/accounts/update_credentials", %{default_scope: "cofe"})
2478 assert user = json_response(conn, 200)
2479 assert user["source"]["privacy"] == "cofe"
2482 test "updates the user's hide_followers status", %{conn: conn} do
2483 user = insert(:user)
2487 |> assign(:user, user)
2488 |> patch("/api/v1/accounts/update_credentials", %{hide_followers: "true"})
2490 assert user = json_response(conn, 200)
2491 assert user["pleroma"]["hide_followers"] == true
2494 test "updates the user's skip_thread_containment option", %{conn: conn} do
2495 user = insert(:user)
2499 |> assign(:user, user)
2500 |> patch("/api/v1/accounts/update_credentials", %{skip_thread_containment: "true"})
2501 |> json_response(200)
2503 assert response["pleroma"]["skip_thread_containment"] == true
2504 assert refresh_record(user).info.skip_thread_containment
2507 test "updates the user's hide_follows status", %{conn: conn} do
2508 user = insert(:user)
2512 |> assign(:user, user)
2513 |> patch("/api/v1/accounts/update_credentials", %{hide_follows: "true"})
2515 assert user = json_response(conn, 200)
2516 assert user["pleroma"]["hide_follows"] == true
2519 test "updates the user's hide_favorites status", %{conn: conn} do
2520 user = insert(:user)
2524 |> assign(:user, user)
2525 |> patch("/api/v1/accounts/update_credentials", %{hide_favorites: "true"})
2527 assert user = json_response(conn, 200)
2528 assert user["pleroma"]["hide_favorites"] == true
2531 test "updates the user's show_role status", %{conn: conn} do
2532 user = insert(:user)
2536 |> assign(:user, user)
2537 |> patch("/api/v1/accounts/update_credentials", %{show_role: "false"})
2539 assert user = json_response(conn, 200)
2540 assert user["source"]["pleroma"]["show_role"] == false
2543 test "updates the user's no_rich_text status", %{conn: conn} do
2544 user = insert(:user)
2548 |> assign(:user, user)
2549 |> patch("/api/v1/accounts/update_credentials", %{no_rich_text: "true"})
2551 assert user = json_response(conn, 200)
2552 assert user["source"]["pleroma"]["no_rich_text"] == true
2555 test "updates the user's name", %{conn: conn} do
2556 user = insert(:user)
2560 |> assign(:user, user)
2561 |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
2563 assert user = json_response(conn, 200)
2564 assert user["display_name"] == "markorepairs"
2567 test "updates the user's avatar", %{conn: conn} do
2568 user = insert(:user)
2570 new_avatar = %Plug.Upload{
2571 content_type: "image/jpg",
2572 path: Path.absname("test/fixtures/image.jpg"),
2573 filename: "an_image.jpg"
2578 |> assign(:user, user)
2579 |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
2581 assert user_response = json_response(conn, 200)
2582 assert user_response["avatar"] != User.avatar_url(user)
2585 test "updates the user's banner", %{conn: conn} do
2586 user = insert(:user)
2588 new_header = %Plug.Upload{
2589 content_type: "image/jpg",
2590 path: Path.absname("test/fixtures/image.jpg"),
2591 filename: "an_image.jpg"
2596 |> assign(:user, user)
2597 |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
2599 assert user_response = json_response(conn, 200)
2600 assert user_response["header"] != User.banner_url(user)
2603 test "requires 'write' permission", %{conn: conn} do
2604 token1 = insert(:oauth_token, scopes: ["read"])
2605 token2 = insert(:oauth_token, scopes: ["write", "follow"])
2607 for token <- [token1, token2] do
2610 |> put_req_header("authorization", "Bearer #{token.token}")
2611 |> patch("/api/v1/accounts/update_credentials", %{})
2613 if token == token1 do
2614 assert %{"error" => "Insufficient permissions: write."} == json_response(conn, 403)
2616 assert json_response(conn, 200)
2621 test "updates profile emojos", %{conn: conn} do
2622 user = insert(:user)
2624 note = "*sips :blank:*"
2625 name = "I am :firefox:"
2629 |> assign(:user, user)
2630 |> patch("/api/v1/accounts/update_credentials", %{
2632 "display_name" => name
2635 assert json_response(conn, 200)
2639 |> get("/api/v1/accounts/#{user.id}")
2641 assert user = json_response(conn, 200)
2643 assert user["note"] == note
2644 assert user["display_name"] == name
2645 assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user["emojis"]
2649 test "get instance information", %{conn: conn} do
2650 conn = get(conn, "/api/v1/instance")
2651 assert result = json_response(conn, 200)
2653 email = Pleroma.Config.get([:instance, :email])
2654 # Note: not checking for "max_toot_chars" since it's optional
2660 "email" => from_config_email,
2662 "streaming_api" => _
2667 "registrations" => _,
2671 assert email == from_config_email
2674 test "get instance stats", %{conn: conn} do
2675 user = insert(:user, %{local: true})
2677 user2 = insert(:user, %{local: true})
2678 {:ok, _user2} = User.deactivate(user2, !user2.info.deactivated)
2680 insert(:user, %{local: false, nickname: "u@peer1.com"})
2681 insert(:user, %{local: false, nickname: "u@peer2.com"})
2683 {:ok, _} = TwitterAPI.create_status(user, %{"status" => "cofe"})
2685 # Stats should count users with missing or nil `info.deactivated` value
2686 user = User.get_cached_by_id(user.id)
2687 info_change = Changeset.change(user.info, %{deactivated: nil})
2691 |> Changeset.change()
2692 |> Changeset.put_embed(:info, info_change)
2693 |> User.update_and_set_cache()
2695 Pleroma.Stats.update_stats()
2697 conn = get(conn, "/api/v1/instance")
2699 assert result = json_response(conn, 200)
2701 stats = result["stats"]
2704 assert stats["user_count"] == 1
2705 assert stats["status_count"] == 1
2706 assert stats["domain_count"] == 2
2709 test "get peers", %{conn: conn} do
2710 insert(:user, %{local: false, nickname: "u@peer1.com"})
2711 insert(:user, %{local: false, nickname: "u@peer2.com"})
2713 Pleroma.Stats.update_stats()
2715 conn = get(conn, "/api/v1/instance/peers")
2717 assert result = json_response(conn, 200)
2719 assert ["peer1.com", "peer2.com"] == Enum.sort(result)
2722 test "put settings", %{conn: conn} do
2723 user = insert(:user)
2727 |> assign(:user, user)
2728 |> put("/api/web/settings", %{"data" => %{"programming" => "socks"}})
2730 assert _result = json_response(conn, 200)
2732 user = User.get_cached_by_ap_id(user.ap_id)
2733 assert user.info.settings == %{"programming" => "socks"}
2736 describe "pinned statuses" do
2738 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
2740 user = insert(:user)
2741 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
2743 [user: user, activity: activity]
2746 test "returns pinned statuses", %{conn: conn, user: user, activity: activity} do
2747 {:ok, _} = CommonAPI.pin(activity.id, user)
2751 |> assign(:user, user)
2752 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
2753 |> json_response(200)
2755 id_str = to_string(activity.id)
2757 assert [%{"id" => ^id_str, "pinned" => true}] = result
2760 test "pin status", %{conn: conn, user: user, activity: activity} do
2761 id_str = to_string(activity.id)
2763 assert %{"id" => ^id_str, "pinned" => true} =
2765 |> assign(:user, user)
2766 |> post("/api/v1/statuses/#{activity.id}/pin")
2767 |> json_response(200)
2769 assert [%{"id" => ^id_str, "pinned" => true}] =
2771 |> assign(:user, user)
2772 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
2773 |> json_response(200)
2776 test "unpin status", %{conn: conn, user: user, activity: activity} do
2777 {:ok, _} = CommonAPI.pin(activity.id, user)
2779 id_str = to_string(activity.id)
2780 user = refresh_record(user)
2782 assert %{"id" => ^id_str, "pinned" => false} =
2784 |> assign(:user, user)
2785 |> post("/api/v1/statuses/#{activity.id}/unpin")
2786 |> json_response(200)
2790 |> assign(:user, user)
2791 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
2792 |> json_response(200)
2795 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
2796 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
2798 id_str_one = to_string(activity_one.id)
2800 assert %{"id" => ^id_str_one, "pinned" => true} =
2802 |> assign(:user, user)
2803 |> post("/api/v1/statuses/#{id_str_one}/pin")
2804 |> json_response(200)
2806 user = refresh_record(user)
2808 assert %{"error" => "You have already pinned the maximum number of statuses"} =
2810 |> assign(:user, user)
2811 |> post("/api/v1/statuses/#{activity_two.id}/pin")
2812 |> json_response(400)
2818 Pleroma.Config.put([:rich_media, :enabled], true)
2821 Pleroma.Config.put([:rich_media, :enabled], false)
2824 user = insert(:user)
2828 test "returns rich-media card", %{conn: conn, user: user} do
2829 {:ok, activity} = CommonAPI.post(user, %{"status" => "http://example.com/ogp"})
2832 "image" => "http://ia.media-imdb.com/images/rock.jpg",
2833 "provider_name" => "www.imdb.com",
2834 "provider_url" => "http://www.imdb.com",
2835 "title" => "The Rock",
2837 "url" => "http://www.imdb.com/title/tt0117500/",
2839 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
2842 "image" => "http://ia.media-imdb.com/images/rock.jpg",
2843 "title" => "The Rock",
2844 "type" => "video.movie",
2845 "url" => "http://www.imdb.com/title/tt0117500/",
2847 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
2854 |> get("/api/v1/statuses/#{activity.id}/card")
2855 |> json_response(200)
2857 assert response == card_data
2859 # works with private posts
2861 CommonAPI.post(user, %{"status" => "http://example.com/ogp", "visibility" => "direct"})
2865 |> assign(:user, user)
2866 |> get("/api/v1/statuses/#{activity.id}/card")
2867 |> json_response(200)
2869 assert response_two == card_data
2872 test "replaces missing description with an empty string", %{conn: conn, user: user} do
2873 {:ok, activity} = CommonAPI.post(user, %{"status" => "http://example.com/ogp-missing-data"})
2877 |> get("/api/v1/statuses/#{activity.id}/card")
2878 |> json_response(:ok)
2880 assert response == %{
2882 "title" => "Pleroma",
2883 "description" => "",
2885 "provider_name" => "pleroma.social",
2886 "provider_url" => "https://pleroma.social",
2887 "url" => "https://pleroma.social/",
2890 "title" => "Pleroma",
2891 "type" => "website",
2892 "url" => "https://pleroma.social/"
2900 user = insert(:user)
2901 for_user = insert(:user)
2904 CommonAPI.post(user, %{
2905 "status" => "heweoo?"
2909 CommonAPI.post(user, %{
2910 "status" => "heweoo!"
2915 |> assign(:user, for_user)
2916 |> post("/api/v1/statuses/#{activity1.id}/bookmark")
2918 assert json_response(response1, 200)["bookmarked"] == true
2922 |> assign(:user, for_user)
2923 |> post("/api/v1/statuses/#{activity2.id}/bookmark")
2925 assert json_response(response2, 200)["bookmarked"] == true
2929 |> assign(:user, for_user)
2930 |> get("/api/v1/bookmarks")
2932 assert [json_response(response2, 200), json_response(response1, 200)] ==
2933 json_response(bookmarks, 200)
2937 |> assign(:user, for_user)
2938 |> post("/api/v1/statuses/#{activity1.id}/unbookmark")
2940 assert json_response(response1, 200)["bookmarked"] == false
2944 |> assign(:user, for_user)
2945 |> get("/api/v1/bookmarks")
2947 assert [json_response(response2, 200)] == json_response(bookmarks, 200)
2950 describe "conversation muting" do
2952 user = insert(:user)
2953 {:ok, activity} = CommonAPI.post(user, %{"status" => "HIE"})
2955 [user: user, activity: activity]
2958 test "mute conversation", %{conn: conn, user: user, activity: activity} do
2959 id_str = to_string(activity.id)
2961 assert %{"id" => ^id_str, "muted" => true} =
2963 |> assign(:user, user)
2964 |> post("/api/v1/statuses/#{activity.id}/mute")
2965 |> json_response(200)
2968 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
2969 {:ok, _} = CommonAPI.add_mute(user, activity)
2971 id_str = to_string(activity.id)
2972 user = refresh_record(user)
2974 assert %{"id" => ^id_str, "muted" => false} =
2976 |> assign(:user, user)
2977 |> post("/api/v1/statuses/#{activity.id}/unmute")
2978 |> json_response(200)
2982 describe "reports" do
2984 reporter = insert(:user)
2985 target_user = insert(:user)
2987 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
2989 [reporter: reporter, target_user: target_user, activity: activity]
2992 test "submit a basic report", %{conn: conn, reporter: reporter, target_user: target_user} do
2993 assert %{"action_taken" => false, "id" => _} =
2995 |> assign(:user, reporter)
2996 |> post("/api/v1/reports", %{"account_id" => target_user.id})
2997 |> json_response(200)
3000 test "submit a report with statuses and comment", %{
3003 target_user: target_user,
3006 assert %{"action_taken" => false, "id" => _} =
3008 |> assign(:user, reporter)
3009 |> post("/api/v1/reports", %{
3010 "account_id" => target_user.id,
3011 "status_ids" => [activity.id],
3012 "comment" => "bad status!"
3014 |> json_response(200)
3017 test "account_id is required", %{
3022 assert %{"error" => "Valid `account_id` required"} =
3024 |> assign(:user, reporter)
3025 |> post("/api/v1/reports", %{"status_ids" => [activity.id]})
3026 |> json_response(400)
3029 test "comment must be up to the size specified in the config", %{
3032 target_user: target_user
3034 max_size = Pleroma.Config.get([:instance, :max_report_comment_size], 1000)
3035 comment = String.pad_trailing("a", max_size + 1, "a")
3037 error = %{"error" => "Comment must be up to #{max_size} characters"}
3041 |> assign(:user, reporter)
3042 |> post("/api/v1/reports", %{"account_id" => target_user.id, "comment" => comment})
3043 |> json_response(400)
3047 describe "link headers" do
3048 test "preserves parameters in link headers", %{conn: conn} do
3049 user = insert(:user)
3050 other_user = insert(:user)
3053 CommonAPI.post(other_user, %{
3054 "status" => "hi @#{user.nickname}",
3055 "visibility" => "public"
3059 CommonAPI.post(other_user, %{
3060 "status" => "hi @#{user.nickname}",
3061 "visibility" => "public"
3064 notification1 = Repo.get_by(Notification, activity_id: activity1.id)
3065 notification2 = Repo.get_by(Notification, activity_id: activity2.id)
3069 |> assign(:user, user)
3070 |> get("/api/v1/notifications", %{media_only: true})
3072 assert [link_header] = get_resp_header(conn, "link")
3073 assert link_header =~ ~r/media_only=true/
3074 assert link_header =~ ~r/min_id=#{notification2.id}/
3075 assert link_header =~ ~r/max_id=#{notification1.id}/
3079 test "accounts fetches correct account for nicknames beginning with numbers", %{conn: conn} do
3080 # Need to set an old-style integer ID to reproduce the problem
3081 # (these are no longer assigned to new accounts but were preserved
3082 # for existing accounts during the migration to flakeIDs)
3083 user_one = insert(:user, %{id: 1212})
3084 user_two = insert(:user, %{nickname: "#{user_one.id}garbage"})
3088 |> get("/api/v1/accounts/#{user_one.id}")
3092 |> get("/api/v1/accounts/#{user_two.nickname}")
3096 |> get("/api/v1/accounts/#{user_two.id}")
3098 acc_one = json_response(resp_one, 200)
3099 acc_two = json_response(resp_two, 200)
3100 acc_three = json_response(resp_three, 200)
3101 refute acc_one == acc_two
3102 assert acc_two == acc_three
3105 describe "custom emoji" do
3106 test "with tags", %{conn: conn} do
3109 |> get("/api/v1/custom_emojis")
3110 |> json_response(200)
3112 assert Map.has_key?(emoji, "shortcode")
3113 assert Map.has_key?(emoji, "static_url")
3114 assert Map.has_key?(emoji, "tags")
3115 assert is_list(emoji["tags"])
3116 assert Map.has_key?(emoji, "url")
3117 assert Map.has_key?(emoji, "visible_in_picker")
3121 describe "index/2 redirections" do
3122 setup %{conn: conn} do
3126 signing_salt: "cooldude"
3131 |> Plug.Session.call(Plug.Session.init(session_opts))
3134 test_path = "/web/statuses/test"
3135 %{conn: conn, path: test_path}
3138 test "redirects not logged-in users to the login page", %{conn: conn, path: path} do
3139 conn = get(conn, path)
3141 assert conn.status == 302
3142 assert redirected_to(conn) == "/web/login"
3145 test "does not redirect logged in users to the login page", %{conn: conn, path: path} do
3146 token = insert(:oauth_token)
3150 |> assign(:user, token.user)
3151 |> put_session(:oauth_token, token.token)
3154 assert conn.status == 200
3157 test "saves referer path to session", %{conn: conn, path: path} do
3158 conn = get(conn, path)
3159 return_to = Plug.Conn.get_session(conn, :return_to)
3161 assert return_to == path
3164 test "redirects to the saved path after log in", %{conn: conn, path: path} do
3165 app = insert(:oauth_app, client_name: "Mastodon-Local", redirect_uris: ".")
3166 auth = insert(:oauth_authorization, app: app)
3170 |> put_session(:return_to, path)
3171 |> get("/web/login", %{code: auth.token})
3173 assert conn.status == 302
3174 assert redirected_to(conn) == path
3177 test "redirects to the getting-started page when referer is not present", %{conn: conn} do
3178 app = insert(:oauth_app, client_name: "Mastodon-Local", redirect_uris: ".")
3179 auth = insert(:oauth_authorization, app: app)
3181 conn = get(conn, "/web/login", %{code: auth.token})
3183 assert conn.status == 302
3184 assert redirected_to(conn) == "/web/getting-started"
3188 describe "scheduled activities" do
3189 test "creates a scheduled activity", %{conn: conn} do
3190 user = insert(:user)
3191 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
3195 |> assign(:user, user)
3196 |> post("/api/v1/statuses", %{
3197 "status" => "scheduled",
3198 "scheduled_at" => scheduled_at
3201 assert %{"scheduled_at" => expected_scheduled_at} = json_response(conn, 200)
3202 assert expected_scheduled_at == Pleroma.Web.CommonAPI.Utils.to_masto_date(scheduled_at)
3203 assert [] == Repo.all(Activity)
3206 test "creates a scheduled activity with a media attachment", %{conn: conn} do
3207 user = insert(:user)
3208 scheduled_at = NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
3210 file = %Plug.Upload{
3211 content_type: "image/jpg",
3212 path: Path.absname("test/fixtures/image.jpg"),
3213 filename: "an_image.jpg"
3216 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
3220 |> assign(:user, user)
3221 |> post("/api/v1/statuses", %{
3222 "media_ids" => [to_string(upload.id)],
3223 "status" => "scheduled",
3224 "scheduled_at" => scheduled_at
3227 assert %{"media_attachments" => [media_attachment]} = json_response(conn, 200)
3228 assert %{"type" => "image"} = media_attachment
3231 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
3233 user = insert(:user)
3236 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
3240 |> assign(:user, user)
3241 |> post("/api/v1/statuses", %{
3242 "status" => "not scheduled",
3243 "scheduled_at" => scheduled_at
3246 assert %{"content" => "not scheduled"} = json_response(conn, 200)
3247 assert [] == Repo.all(ScheduledActivity)
3250 test "returns error when daily user limit is exceeded", %{conn: conn} do
3251 user = insert(:user)
3254 NaiveDateTime.utc_now()
3255 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
3256 |> NaiveDateTime.to_iso8601()
3258 attrs = %{params: %{}, scheduled_at: today}
3259 {:ok, _} = ScheduledActivity.create(user, attrs)
3260 {:ok, _} = ScheduledActivity.create(user, attrs)
3264 |> assign(:user, user)
3265 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
3267 assert %{"error" => "daily limit exceeded"} == json_response(conn, 422)
3270 test "returns error when total user limit is exceeded", %{conn: conn} do
3271 user = insert(:user)
3274 NaiveDateTime.utc_now()
3275 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
3276 |> NaiveDateTime.to_iso8601()
3279 NaiveDateTime.utc_now()
3280 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
3281 |> NaiveDateTime.to_iso8601()
3283 attrs = %{params: %{}, scheduled_at: today}
3284 {:ok, _} = ScheduledActivity.create(user, attrs)
3285 {:ok, _} = ScheduledActivity.create(user, attrs)
3286 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
3290 |> assign(:user, user)
3291 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
3293 assert %{"error" => "total limit exceeded"} == json_response(conn, 422)
3296 test "shows scheduled activities", %{conn: conn} do
3297 user = insert(:user)
3298 scheduled_activity_id1 = insert(:scheduled_activity, user: user).id |> to_string()
3299 scheduled_activity_id2 = insert(:scheduled_activity, user: user).id |> to_string()
3300 scheduled_activity_id3 = insert(:scheduled_activity, user: user).id |> to_string()
3301 scheduled_activity_id4 = insert(:scheduled_activity, user: user).id |> to_string()
3305 |> assign(:user, user)
3310 |> get("/api/v1/scheduled_statuses?limit=2&min_id=#{scheduled_activity_id1}")
3312 result = json_response(conn_res, 200)
3313 assert [%{"id" => ^scheduled_activity_id3}, %{"id" => ^scheduled_activity_id2}] = result
3318 |> get("/api/v1/scheduled_statuses?limit=2&since_id=#{scheduled_activity_id1}")
3320 result = json_response(conn_res, 200)
3321 assert [%{"id" => ^scheduled_activity_id4}, %{"id" => ^scheduled_activity_id3}] = result
3326 |> get("/api/v1/scheduled_statuses?limit=2&max_id=#{scheduled_activity_id4}")
3328 result = json_response(conn_res, 200)
3329 assert [%{"id" => ^scheduled_activity_id3}, %{"id" => ^scheduled_activity_id2}] = result
3332 test "shows a scheduled activity", %{conn: conn} do
3333 user = insert(:user)
3334 scheduled_activity = insert(:scheduled_activity, user: user)
3338 |> assign(:user, user)
3339 |> get("/api/v1/scheduled_statuses/#{scheduled_activity.id}")
3341 assert %{"id" => scheduled_activity_id} = json_response(res_conn, 200)
3342 assert scheduled_activity_id == scheduled_activity.id |> to_string()
3346 |> assign(:user, user)
3347 |> get("/api/v1/scheduled_statuses/404")
3349 assert %{"error" => "Record not found"} = json_response(res_conn, 404)
3352 test "updates a scheduled activity", %{conn: conn} do
3353 user = insert(:user)
3354 scheduled_activity = insert(:scheduled_activity, user: user)
3357 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
3361 |> assign(:user, user)
3362 |> put("/api/v1/scheduled_statuses/#{scheduled_activity.id}", %{
3363 scheduled_at: new_scheduled_at
3366 assert %{"scheduled_at" => expected_scheduled_at} = json_response(res_conn, 200)
3367 assert expected_scheduled_at == Pleroma.Web.CommonAPI.Utils.to_masto_date(new_scheduled_at)
3371 |> assign(:user, user)
3372 |> put("/api/v1/scheduled_statuses/404", %{scheduled_at: new_scheduled_at})
3374 assert %{"error" => "Record not found"} = json_response(res_conn, 404)
3377 test "deletes a scheduled activity", %{conn: conn} do
3378 user = insert(:user)
3379 scheduled_activity = insert(:scheduled_activity, user: user)
3383 |> assign(:user, user)
3384 |> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}")
3386 assert %{} = json_response(res_conn, 200)
3387 assert nil == Repo.get(ScheduledActivity, scheduled_activity.id)
3391 |> assign(:user, user)
3392 |> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}")
3394 assert %{"error" => "Record not found"} = json_response(res_conn, 404)
3398 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
3399 user1 = insert(:user)
3400 user2 = insert(:user)
3401 user3 = insert(:user)
3403 {:ok, replied_to} = TwitterAPI.create_status(user1, %{"status" => "cofe"})
3405 # Reply to status from another user
3408 |> assign(:user, user2)
3409 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
3411 assert %{"content" => "xD", "id" => id} = json_response(conn1, 200)
3413 activity = Activity.get_by_id_with_object(id)
3415 assert Object.normalize(activity).data["inReplyTo"] == Object.normalize(replied_to).data["id"]
3416 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
3418 # Reblog from the third user
3421 |> assign(:user, user3)
3422 |> post("/api/v1/statuses/#{activity.id}/reblog")
3424 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
3425 json_response(conn2, 200)
3427 assert to_string(activity.id) == id
3429 # Getting third user status
3432 |> assign(:user, user3)
3433 |> get("api/v1/timelines/home")
3435 [reblogged_activity] = json_response(conn3, 200)
3437 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
3439 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
3440 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
3443 describe "create account by app" do
3444 test "Account registration via Application", %{conn: conn} do
3447 |> post("/api/v1/apps", %{
3448 client_name: "client_name",
3449 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
3450 scopes: "read, write, follow"
3454 "client_id" => client_id,
3455 "client_secret" => client_secret,
3457 "name" => "client_name",
3458 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
3461 } = json_response(conn, 200)
3465 |> post("/oauth/token", %{
3466 grant_type: "client_credentials",
3467 client_id: client_id,
3468 client_secret: client_secret
3471 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
3472 json_response(conn, 200)
3475 token_from_db = Repo.get_by(Token, token: token)
3476 assert token_from_db
3478 assert scope == "read write follow"
3482 |> put_req_header("authorization", "Bearer " <> token)
3483 |> post("/api/v1/accounts", %{
3485 email: "lain@example.org",
3486 password: "PlzDontHackLain",
3491 "access_token" => token,
3492 "created_at" => _created_at,
3494 "token_type" => "Bearer"
3495 } = json_response(conn, 200)
3497 token_from_db = Repo.get_by(Token, token: token)
3498 assert token_from_db
3499 token_from_db = Repo.preload(token_from_db, :user)
3500 assert token_from_db.user
3502 assert token_from_db.user.info.confirmation_pending
3505 test "rate limit", %{conn: conn} do
3506 app_token = insert(:oauth_token, user: nil)
3509 put_req_header(conn, "authorization", "Bearer " <> app_token.token)
3510 |> Map.put(:remote_ip, {15, 15, 15, 15})
3515 |> post("/api/v1/accounts", %{
3516 username: "#{i}lain",
3517 email: "#{i}lain@example.org",
3518 password: "PlzDontHackLain",
3523 "access_token" => token,
3524 "created_at" => _created_at,
3526 "token_type" => "Bearer"
3527 } = json_response(conn, 200)
3529 token_from_db = Repo.get_by(Token, token: token)
3530 assert token_from_db
3531 token_from_db = Repo.preload(token_from_db, :user)
3532 assert token_from_db.user
3534 assert token_from_db.user.info.confirmation_pending
3539 |> post("/api/v1/accounts", %{
3541 email: "6lain@example.org",
3542 password: "PlzDontHackLain",
3546 assert json_response(conn, :too_many_requests) == %{"error" => "Throttled"}
3550 describe "GET /api/v1/polls/:id" do
3551 test "returns poll entity for object id", %{conn: conn} do
3552 user = insert(:user)
3555 CommonAPI.post(user, %{
3556 "status" => "Pleroma does",
3557 "poll" => %{"options" => ["what Mastodon't", "n't what Mastodoes"], "expires_in" => 20}
3560 object = Object.normalize(activity)
3564 |> assign(:user, user)
3565 |> get("/api/v1/polls/#{object.id}")
3567 response = json_response(conn, 200)
3569 assert %{"id" => ^id, "expired" => false, "multiple" => false} = response
3572 test "does not expose polls for private statuses", %{conn: conn} do
3573 user = insert(:user)
3574 other_user = insert(:user)
3577 CommonAPI.post(user, %{
3578 "status" => "Pleroma does",
3579 "poll" => %{"options" => ["what Mastodon't", "n't what Mastodoes"], "expires_in" => 20},
3580 "visibility" => "private"
3583 object = Object.normalize(activity)
3587 |> assign(:user, other_user)
3588 |> get("/api/v1/polls/#{object.id}")
3590 assert json_response(conn, 404)
3594 describe "POST /api/v1/polls/:id/votes" do
3595 test "votes are added to the poll", %{conn: conn} do
3596 user = insert(:user)
3597 other_user = insert(:user)
3600 CommonAPI.post(user, %{
3601 "status" => "A very delicious sandwich",
3603 "options" => ["Lettuce", "Grilled Bacon", "Tomato"],
3609 object = Object.normalize(activity)
3613 |> assign(:user, other_user)
3614 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
3616 assert json_response(conn, 200)
3617 object = Object.get_by_id(object.id)
3619 assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
3624 test "author can't vote", %{conn: conn} do
3625 user = insert(:user)
3628 CommonAPI.post(user, %{
3629 "status" => "Am I cute?",
3630 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
3633 object = Object.normalize(activity)
3636 |> assign(:user, user)
3637 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [1]})
3638 |> json_response(422) == %{"error" => "Poll's author can't vote"}
3640 object = Object.get_by_id(object.id)
3642 refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
3645 test "does not allow multiple choices on a single-choice question", %{conn: conn} do
3646 user = insert(:user)
3647 other_user = insert(:user)
3650 CommonAPI.post(user, %{
3651 "status" => "The glass is",
3652 "poll" => %{"options" => ["half empty", "half full"], "expires_in" => 20}
3655 object = Object.normalize(activity)
3658 |> assign(:user, other_user)
3659 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
3660 |> json_response(422) == %{"error" => "Too many choices"}
3662 object = Object.get_by_id(object.id)
3664 refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->