1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
2 use Pleroma.Web.ConnCase
4 alias Pleroma.Web.TwitterAPI.TwitterAPI
5 alias Pleroma.{Repo, User, Object, Activity, Notification}
6 alias Pleroma.Web.{OStatus, CommonAPI}
7 alias Pleroma.Web.ActivityPub.ActivityPub
8 alias Pleroma.Web.MastodonAPI.FilterView
10 import ExUnit.CaptureLog
14 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 test "the home timeline", %{conn: conn} do
20 following = insert(:user)
22 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
26 |> assign(:user, user)
27 |> get("/api/v1/timelines/home")
29 assert length(json_response(conn, 200)) == 0
31 {:ok, user} = User.follow(user, following)
35 |> assign(:user, user)
36 |> get("/api/v1/timelines/home")
38 assert [%{"content" => "test"}] = json_response(conn, 200)
41 test "the public timeline", %{conn: conn} do
42 following = insert(:user)
45 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
48 OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
52 |> get("/api/v1/timelines/public", %{"local" => "False"})
54 assert length(json_response(conn, 200)) == 2
58 |> get("/api/v1/timelines/public", %{"local" => "True"})
60 assert [%{"content" => "test"}] = json_response(conn, 200)
64 |> get("/api/v1/timelines/public", %{"local" => "1"})
66 assert [%{"content" => "test"}] = json_response(conn, 200)
70 test "posting a status", %{conn: conn} do
73 idempotency_key = "Pikachu rocks!"
77 |> assign(:user, user)
78 |> put_req_header("idempotency-key", idempotency_key)
79 |> post("/api/v1/statuses", %{
81 "spoiler_text" => "2hu",
82 "sensitive" => "false"
85 {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
87 assert ttl > :timer.seconds(6 * 60 * 60 - 1)
89 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
90 json_response(conn_one, 200)
92 assert Repo.get(Activity, id)
96 |> assign(:user, user)
97 |> put_req_header("idempotency-key", idempotency_key)
98 |> post("/api/v1/statuses", %{
100 "spoiler_text" => "2hu",
101 "sensitive" => "false"
104 assert %{"id" => second_id} = json_response(conn_two, 200)
106 assert id == second_id
110 |> assign(:user, user)
111 |> post("/api/v1/statuses", %{
113 "spoiler_text" => "2hu",
114 "sensitive" => "false"
117 assert %{"id" => third_id} = json_response(conn_three, 200)
119 refute id == third_id
122 test "posting a sensitive status", %{conn: conn} do
127 |> assign(:user, user)
128 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
130 assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200)
131 assert Repo.get(Activity, id)
134 test "posting a direct status", %{conn: conn} do
135 user1 = insert(:user)
136 user2 = insert(:user)
137 content = "direct cofe @#{user2.nickname}"
141 |> assign(:user, user1)
142 |> post("api/v1/statuses", %{"status" => content, "visibility" => "direct"})
144 assert %{"id" => id, "visibility" => "direct"} = json_response(conn, 200)
145 assert activity = Repo.get(Activity, id)
146 assert activity.recipients == [user2.ap_id]
147 assert activity.data["to"] == [user2.ap_id]
148 assert activity.data["cc"] == []
151 test "direct timeline", %{conn: conn} do
152 user_one = insert(:user)
153 user_two = insert(:user)
155 {:ok, user_two} = User.follow(user_two, user_one)
158 CommonAPI.post(user_one, %{
159 "status" => "Hi @#{user_two.nickname}!",
160 "visibility" => "direct"
163 {:ok, _follower_only} =
164 CommonAPI.post(user_one, %{
165 "status" => "Hi @#{user_two.nickname}!",
166 "visibility" => "private"
169 # Only direct should be visible here
172 |> assign(:user, user_two)
173 |> get("api/v1/timelines/direct")
175 [status] = json_response(res_conn, 200)
177 assert %{"visibility" => "direct"} = status
178 assert status["url"] != direct.data["id"]
180 # Both should be visible here
183 |> assign(:user, user_two)
184 |> get("api/v1/timelines/home")
186 [_s1, _s2] = json_response(res_conn, 200)
189 Enum.each(1..20, fn _ ->
191 CommonAPI.post(user_one, %{
192 "status" => "Hi @#{user_two.nickname}!",
193 "visibility" => "direct"
199 |> assign(:user, user_two)
200 |> get("api/v1/timelines/direct")
202 statuses = json_response(res_conn, 200)
203 assert length(statuses) == 20
207 |> assign(:user, user_two)
208 |> get("api/v1/timelines/direct", %{max_id: List.last(statuses)["id"]})
210 [status] = json_response(res_conn, 200)
212 assert status["url"] != direct.data["id"]
215 test "replying to a status", %{conn: conn} do
218 {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"})
222 |> assign(:user, user)
223 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
225 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
227 activity = Repo.get(Activity, id)
229 assert activity.data["context"] == replied_to.data["context"]
230 assert activity.data["object"]["inReplyToStatusId"] == replied_to.id
233 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
238 |> assign(:user, user)
239 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
241 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
243 activity = Repo.get(Activity, id)
248 test "verify_credentials", %{conn: conn} do
253 |> assign(:user, user)
254 |> get("/api/v1/accounts/verify_credentials")
256 assert %{"id" => id, "source" => %{"privacy" => "public"}} = json_response(conn, 200)
257 assert id == to_string(user.id)
260 test "verify_credentials default scope unlisted", %{conn: conn} do
261 user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "unlisted"}})
265 |> assign(:user, user)
266 |> get("/api/v1/accounts/verify_credentials")
268 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} = json_response(conn, 200)
269 assert id == to_string(user.id)
272 test "get a status", %{conn: conn} do
273 activity = insert(:note_activity)
277 |> get("/api/v1/statuses/#{activity.id}")
279 assert %{"id" => id} = json_response(conn, 200)
280 assert id == to_string(activity.id)
283 describe "deleting a status" do
284 test "when you created it", %{conn: conn} do
285 activity = insert(:note_activity)
286 author = User.get_by_ap_id(activity.data["actor"])
290 |> assign(:user, author)
291 |> delete("/api/v1/statuses/#{activity.id}")
293 assert %{} = json_response(conn, 200)
295 assert Repo.get(Activity, activity.id) == nil
298 test "when you didn't create it", %{conn: conn} do
299 activity = insert(:note_activity)
304 |> assign(:user, user)
305 |> delete("/api/v1/statuses/#{activity.id}")
307 assert %{"error" => _} = json_response(conn, 403)
309 assert Repo.get(Activity, activity.id) == activity
313 describe "filters" do
314 test "creating a filter", %{conn: conn} do
317 filter = %Pleroma.Filter{
324 |> assign(:user, user)
325 |> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context})
327 assert response = json_response(conn, 200)
328 assert response["phrase"] == filter.phrase
329 assert response["context"] == filter.context
330 assert response["id"] != nil
331 assert response["id"] != ""
334 test "fetching a list of filters", %{conn: conn} do
337 query_one = %Pleroma.Filter{
344 query_two = %Pleroma.Filter{
351 {:ok, filter_one} = Pleroma.Filter.create(query_one)
352 {:ok, filter_two} = Pleroma.Filter.create(query_two)
356 |> assign(:user, user)
357 |> get("/api/v1/filters")
358 |> json_response(200)
364 filters: [filter_two, filter_one]
368 test "get a filter", %{conn: conn} do
371 query = %Pleroma.Filter{
378 {:ok, filter} = Pleroma.Filter.create(query)
382 |> assign(:user, user)
383 |> get("/api/v1/filters/#{filter.filter_id}")
385 assert response = json_response(conn, 200)
388 test "update a filter", %{conn: conn} do
391 query = %Pleroma.Filter{
398 {:ok, _filter} = Pleroma.Filter.create(query)
400 new = %Pleroma.Filter{
407 |> assign(:user, user)
408 |> put("/api/v1/filters/#{query.filter_id}", %{
413 assert response = json_response(conn, 200)
414 assert response["phrase"] == new.phrase
415 assert response["context"] == new.context
418 test "delete a filter", %{conn: conn} do
421 query = %Pleroma.Filter{
428 {:ok, filter} = Pleroma.Filter.create(query)
432 |> assign(:user, user)
433 |> delete("/api/v1/filters/#{filter.filter_id}")
435 assert response = json_response(conn, 200)
436 assert response == %{}
441 test "creating a list", %{conn: conn} do
446 |> assign(:user, user)
447 |> post("/api/v1/lists", %{"title" => "cuties"})
449 assert %{"title" => title} = json_response(conn, 200)
450 assert title == "cuties"
453 test "adding users to a list", %{conn: conn} do
455 other_user = insert(:user)
456 {:ok, list} = Pleroma.List.create("name", user)
460 |> assign(:user, user)
461 |> post("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
463 assert %{} == json_response(conn, 200)
464 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
465 assert following == [other_user.follower_address]
468 test "removing users from a list", %{conn: conn} do
470 other_user = insert(:user)
471 third_user = insert(:user)
472 {:ok, list} = Pleroma.List.create("name", user)
473 {:ok, list} = Pleroma.List.follow(list, other_user)
474 {:ok, list} = Pleroma.List.follow(list, third_user)
478 |> assign(:user, user)
479 |> delete("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
481 assert %{} == json_response(conn, 200)
482 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
483 assert following == [third_user.follower_address]
486 test "listing users in a list", %{conn: conn} do
488 other_user = insert(:user)
489 {:ok, list} = Pleroma.List.create("name", user)
490 {:ok, list} = Pleroma.List.follow(list, other_user)
494 |> assign(:user, user)
495 |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
497 assert [%{"id" => id}] = json_response(conn, 200)
498 assert id == to_string(other_user.id)
501 test "retrieving a list", %{conn: conn} do
503 {:ok, list} = Pleroma.List.create("name", user)
507 |> assign(:user, user)
508 |> get("/api/v1/lists/#{list.id}")
510 assert %{"id" => id} = json_response(conn, 200)
511 assert id == to_string(list.id)
514 test "renaming a list", %{conn: conn} do
516 {:ok, list} = Pleroma.List.create("name", user)
520 |> assign(:user, user)
521 |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
523 assert %{"title" => name} = json_response(conn, 200)
524 assert name == "newname"
527 test "deleting a list", %{conn: conn} do
529 {:ok, list} = Pleroma.List.create("name", user)
533 |> assign(:user, user)
534 |> delete("/api/v1/lists/#{list.id}")
536 assert %{} = json_response(conn, 200)
537 assert is_nil(Repo.get(Pleroma.List, list.id))
540 test "list timeline", %{conn: conn} do
542 other_user = insert(:user)
543 {:ok, _activity_one} = TwitterAPI.create_status(user, %{"status" => "Marisa is cute."})
544 {:ok, activity_two} = TwitterAPI.create_status(other_user, %{"status" => "Marisa is cute."})
545 {:ok, list} = Pleroma.List.create("name", user)
546 {:ok, list} = Pleroma.List.follow(list, other_user)
550 |> assign(:user, user)
551 |> get("/api/v1/timelines/list/#{list.id}")
553 assert [%{"id" => id}] = json_response(conn, 200)
555 assert id == to_string(activity_two.id)
558 test "list timeline does not leak non-public statuses for unfollowed users", %{conn: conn} do
560 other_user = insert(:user)
561 {:ok, activity_one} = TwitterAPI.create_status(other_user, %{"status" => "Marisa is cute."})
563 {:ok, _activity_two} =
564 TwitterAPI.create_status(other_user, %{
565 "status" => "Marisa is cute.",
566 "visibility" => "private"
569 {:ok, list} = Pleroma.List.create("name", user)
570 {:ok, list} = Pleroma.List.follow(list, other_user)
574 |> assign(:user, user)
575 |> get("/api/v1/timelines/list/#{list.id}")
577 assert [%{"id" => id}] = json_response(conn, 200)
579 assert id == to_string(activity_one.id)
583 describe "notifications" do
584 test "list of notifications", %{conn: conn} do
586 other_user = insert(:user)
589 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
591 {:ok, [_notification]} = Notification.create_notifications(activity)
595 |> assign(:user, user)
596 |> get("/api/v1/notifications")
599 "hi <span><a data-user=\"#{user.id}\" href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
601 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
602 assert response == expected_response
605 test "getting a single notification", %{conn: conn} do
607 other_user = insert(:user)
610 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
612 {:ok, [notification]} = Notification.create_notifications(activity)
616 |> assign(:user, user)
617 |> get("/api/v1/notifications/#{notification.id}")
620 "hi <span><a data-user=\"#{user.id}\" href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
622 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
623 assert response == expected_response
626 test "dismissing a single notification", %{conn: conn} do
628 other_user = insert(:user)
631 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
633 {:ok, [notification]} = Notification.create_notifications(activity)
637 |> assign(:user, user)
638 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
640 assert %{} = json_response(conn, 200)
643 test "clearing all notifications", %{conn: conn} do
645 other_user = insert(:user)
648 TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"})
650 {:ok, [_notification]} = Notification.create_notifications(activity)
654 |> assign(:user, user)
655 |> post("/api/v1/notifications/clear")
657 assert %{} = json_response(conn, 200)
661 |> assign(:user, user)
662 |> get("/api/v1/notifications")
664 assert all = json_response(conn, 200)
669 describe "reblogging" do
670 test "reblogs and returns the reblogged status", %{conn: conn} do
671 activity = insert(:note_activity)
676 |> assign(:user, user)
677 |> post("/api/v1/statuses/#{activity.id}/reblog")
679 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
680 json_response(conn, 200)
682 assert to_string(activity.id) == id
686 describe "unreblogging" do
687 test "unreblogs and returns the unreblogged status", %{conn: conn} do
688 activity = insert(:note_activity)
691 {:ok, _, _} = CommonAPI.repeat(activity.id, user)
695 |> assign(:user, user)
696 |> post("/api/v1/statuses/#{activity.id}/unreblog")
698 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
700 assert to_string(activity.id) == id
704 describe "favoriting" do
705 test "favs a status and returns it", %{conn: conn} do
706 activity = insert(:note_activity)
711 |> assign(:user, user)
712 |> post("/api/v1/statuses/#{activity.id}/favourite")
714 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
715 json_response(conn, 200)
717 assert to_string(activity.id) == id
720 test "returns 500 for a wrong id", %{conn: conn} do
725 |> assign(:user, user)
726 |> post("/api/v1/statuses/1/favourite")
727 |> json_response(500)
729 assert resp == "Something went wrong"
733 describe "unfavoriting" do
734 test "unfavorites a status and returns it", %{conn: conn} do
735 activity = insert(:note_activity)
738 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
742 |> assign(:user, user)
743 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
745 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
746 json_response(conn, 200)
748 assert to_string(activity.id) == id
752 describe "user timelines" do
753 test "gets a users statuses", %{conn: conn} do
754 user_one = insert(:user)
755 user_two = insert(:user)
756 user_three = insert(:user)
758 {:ok, user_three} = User.follow(user_three, user_one)
760 {:ok, activity} = CommonAPI.post(user_one, %{"status" => "HI!!!"})
762 {:ok, direct_activity} =
763 CommonAPI.post(user_one, %{
764 "status" => "Hi, @#{user_two.nickname}.",
765 "visibility" => "direct"
768 {:ok, private_activity} =
769 CommonAPI.post(user_one, %{"status" => "private", "visibility" => "private"})
773 |> get("/api/v1/accounts/#{user_one.id}/statuses")
775 assert [%{"id" => id}] = json_response(resp, 200)
776 assert id == to_string(activity.id)
780 |> assign(:user, user_two)
781 |> get("/api/v1/accounts/#{user_one.id}/statuses")
783 assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
784 assert id_one == to_string(direct_activity.id)
785 assert id_two == to_string(activity.id)
789 |> assign(:user, user_three)
790 |> get("/api/v1/accounts/#{user_one.id}/statuses")
792 assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
793 assert id_one == to_string(private_activity.id)
794 assert id_two == to_string(activity.id)
797 test "unimplemented pinned statuses feature", %{conn: conn} do
798 note = insert(:note_activity)
799 user = User.get_by_ap_id(note.data["actor"])
803 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
805 assert json_response(conn, 200) == []
808 test "gets an users media", %{conn: conn} do
809 note = insert(:note_activity)
810 user = User.get_by_ap_id(note.data["actor"])
813 content_type: "image/jpg",
814 path: Path.absname("test/fixtures/image.jpg"),
815 filename: "an_image.jpg"
819 TwitterAPI.upload(file, user, "json")
823 TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]})
827 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
829 assert [%{"id" => id}] = json_response(conn, 200)
830 assert id == to_string(image_post.id)
834 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
836 assert [%{"id" => id}] = json_response(conn, 200)
837 assert id == to_string(image_post.id)
841 describe "user relationships" do
842 test "returns the relationships for the current user", %{conn: conn} do
844 other_user = insert(:user)
845 {:ok, user} = User.follow(user, other_user)
849 |> assign(:user, user)
850 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
852 assert [relationship] = json_response(conn, 200)
854 assert to_string(other_user.id) == relationship["id"]
858 describe "locked accounts" do
859 test "/api/v1/follow_requests works" do
860 user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
861 other_user = insert(:user)
863 {:ok, _activity} = ActivityPub.follow(other_user, user)
865 user = Repo.get(User, user.id)
866 other_user = Repo.get(User, other_user.id)
868 assert User.following?(other_user, user) == false
872 |> assign(:user, user)
873 |> get("/api/v1/follow_requests")
875 assert [relationship] = json_response(conn, 200)
876 assert to_string(other_user.id) == relationship["id"]
879 test "/api/v1/follow_requests/:id/authorize works" do
880 user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
881 other_user = insert(:user)
883 {:ok, _activity} = ActivityPub.follow(other_user, user)
885 user = Repo.get(User, user.id)
886 other_user = Repo.get(User, other_user.id)
888 assert User.following?(other_user, user) == false
892 |> assign(:user, user)
893 |> post("/api/v1/follow_requests/#{other_user.id}/authorize")
895 assert relationship = json_response(conn, 200)
896 assert to_string(other_user.id) == relationship["id"]
898 user = Repo.get(User, user.id)
899 other_user = Repo.get(User, other_user.id)
901 assert User.following?(other_user, user) == true
904 test "verify_credentials", %{conn: conn} do
905 user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "private"}})
909 |> assign(:user, user)
910 |> get("/api/v1/accounts/verify_credentials")
912 assert %{"id" => id, "source" => %{"privacy" => "private"}} = json_response(conn, 200)
913 assert id == to_string(user.id)
916 test "/api/v1/follow_requests/:id/reject works" do
917 user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
918 other_user = insert(:user)
920 {:ok, _activity} = ActivityPub.follow(other_user, user)
924 |> assign(:user, user)
925 |> post("/api/v1/follow_requests/#{other_user.id}/reject")
927 assert relationship = json_response(conn, 200)
928 assert to_string(other_user.id) == relationship["id"]
930 user = Repo.get(User, user.id)
931 other_user = Repo.get(User, other_user.id)
933 assert User.following?(other_user, user) == false
937 test "account fetching", %{conn: conn} do
942 |> get("/api/v1/accounts/#{user.id}")
944 assert %{"id" => id} = json_response(conn, 200)
945 assert id == to_string(user.id)
949 |> get("/api/v1/accounts/-1")
951 assert %{"error" => "Can't find user"} = json_response(conn, 404)
954 test "media upload", %{conn: conn} do
956 content_type: "image/jpg",
957 path: Path.absname("test/fixtures/image.jpg"),
958 filename: "an_image.jpg"
961 desc = "Description of the image"
967 |> assign(:user, user)
968 |> post("/api/v1/media", %{"file" => file, "description" => desc})
970 assert media = json_response(conn, 200)
972 assert media["type"] == "image"
973 assert media["description"] == desc
976 object = Repo.get(Object, media["id"])
977 assert object.data["actor"] == User.ap_id(user)
980 test "hashtag timeline", %{conn: conn} do
981 following = insert(:user)
984 {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
987 OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
991 |> get("/api/v1/timelines/tag/2hu")
993 assert [%{"id" => id}] = json_response(nconn, 200)
995 assert id == to_string(activity.id)
997 # works for different capitalization too
1000 |> get("/api/v1/timelines/tag/2HU")
1002 assert [%{"id" => id}] = json_response(nconn, 200)
1004 assert id == to_string(activity.id)
1008 test "getting followers", %{conn: conn} do
1009 user = insert(:user)
1010 other_user = insert(:user)
1011 {:ok, user} = User.follow(user, other_user)
1015 |> get("/api/v1/accounts/#{other_user.id}/followers")
1017 assert [%{"id" => id}] = json_response(conn, 200)
1018 assert id == to_string(user.id)
1021 test "getting followers, hide_network", %{conn: conn} do
1022 user = insert(:user)
1023 other_user = insert(:user, %{info: %{hide_network: true}})
1024 {:ok, _user} = User.follow(user, other_user)
1028 |> get("/api/v1/accounts/#{other_user.id}/followers")
1030 assert [] == json_response(conn, 200)
1033 test "getting followers, hide_network, same user requesting", %{conn: conn} do
1034 user = insert(:user)
1035 other_user = insert(:user, %{info: %{hide_network: true}})
1036 {:ok, _user} = User.follow(user, other_user)
1040 |> assign(:user, other_user)
1041 |> get("/api/v1/accounts/#{other_user.id}/followers")
1043 refute [] == json_response(conn, 200)
1046 test "getting following", %{conn: conn} do
1047 user = insert(:user)
1048 other_user = insert(:user)
1049 {:ok, user} = User.follow(user, other_user)
1053 |> get("/api/v1/accounts/#{user.id}/following")
1055 assert [%{"id" => id}] = json_response(conn, 200)
1056 assert id == to_string(other_user.id)
1059 test "getting following, hide_network", %{conn: conn} do
1060 user = insert(:user, %{info: %{hide_network: true}})
1061 other_user = insert(:user)
1062 {:ok, user} = User.follow(user, other_user)
1066 |> get("/api/v1/accounts/#{user.id}/following")
1068 assert [] == json_response(conn, 200)
1071 test "getting following, hide_network, same user requesting", %{conn: conn} do
1072 user = insert(:user, %{info: %{hide_network: true}})
1073 other_user = insert(:user)
1074 {:ok, user} = User.follow(user, other_user)
1078 |> assign(:user, user)
1079 |> get("/api/v1/accounts/#{user.id}/following")
1081 refute [] == json_response(conn, 200)
1084 test "following / unfollowing a user", %{conn: conn} do
1085 user = insert(:user)
1086 other_user = insert(:user)
1090 |> assign(:user, user)
1091 |> post("/api/v1/accounts/#{other_user.id}/follow")
1093 assert %{"id" => _id, "following" => true} = json_response(conn, 200)
1095 user = Repo.get(User, user.id)
1099 |> assign(:user, user)
1100 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
1102 assert %{"id" => _id, "following" => false} = json_response(conn, 200)
1104 user = Repo.get(User, user.id)
1108 |> assign(:user, user)
1109 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
1111 assert %{"id" => id} = json_response(conn, 200)
1112 assert id == to_string(other_user.id)
1115 test "blocking / unblocking a user", %{conn: conn} do
1116 user = insert(:user)
1117 other_user = insert(:user)
1121 |> assign(:user, user)
1122 |> post("/api/v1/accounts/#{other_user.id}/block")
1124 assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
1126 user = Repo.get(User, user.id)
1130 |> assign(:user, user)
1131 |> post("/api/v1/accounts/#{other_user.id}/unblock")
1133 assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
1136 test "getting a list of blocks", %{conn: conn} do
1137 user = insert(:user)
1138 other_user = insert(:user)
1140 {:ok, user} = User.block(user, other_user)
1144 |> assign(:user, user)
1145 |> get("/api/v1/blocks")
1147 other_user_id = to_string(other_user.id)
1148 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
1151 test "blocking / unblocking a domain", %{conn: conn} do
1152 user = insert(:user)
1153 other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})
1157 |> assign(:user, user)
1158 |> post("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})
1160 assert %{} = json_response(conn, 200)
1161 user = User.get_cached_by_ap_id(user.ap_id)
1162 assert User.blocks?(user, other_user)
1166 |> assign(:user, user)
1167 |> delete("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})
1169 assert %{} = json_response(conn, 200)
1170 user = User.get_cached_by_ap_id(user.ap_id)
1171 refute User.blocks?(user, other_user)
1174 test "getting a list of domain blocks", %{conn: conn} do
1175 user = insert(:user)
1177 {:ok, user} = User.block_domain(user, "bad.site")
1178 {:ok, user} = User.block_domain(user, "even.worse.site")
1182 |> assign(:user, user)
1183 |> get("/api/v1/domain_blocks")
1185 domain_blocks = json_response(conn, 200)
1187 assert "bad.site" in domain_blocks
1188 assert "even.worse.site" in domain_blocks
1191 test "unimplemented mute endpoints" do
1192 user = insert(:user)
1193 other_user = insert(:user)
1196 |> Enum.each(fn endpoint ->
1199 |> assign(:user, user)
1200 |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
1202 assert %{"id" => id} = json_response(conn, 200)
1203 assert id == to_string(other_user.id)
1207 test "unimplemented mutes, follow_requests, blocks, domain blocks" do
1208 user = insert(:user)
1210 ["blocks", "domain_blocks", "mutes", "follow_requests"]
1211 |> Enum.each(fn endpoint ->
1214 |> assign(:user, user)
1215 |> get("/api/v1/#{endpoint}")
1217 assert [] = json_response(conn, 200)
1221 test "account search", %{conn: conn} do
1222 user = insert(:user)
1223 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
1224 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
1228 |> assign(:user, user)
1229 |> get("/api/v1/accounts/search", %{"q" => "shp"})
1230 |> json_response(200)
1232 result_ids = for result <- results, do: result["acct"]
1234 assert user_two.nickname in result_ids
1235 assert user_three.nickname in result_ids
1239 |> assign(:user, user)
1240 |> get("/api/v1/accounts/search", %{"q" => "2hu"})
1241 |> json_response(200)
1243 result_ids = for result <- results, do: result["acct"]
1245 assert user_three.nickname in result_ids
1248 test "search", %{conn: conn} do
1249 user = insert(:user)
1250 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
1251 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
1253 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
1256 CommonAPI.post(user, %{
1257 "status" => "This is about 2hu, but private",
1258 "visibility" => "private"
1261 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
1265 |> get("/api/v1/search", %{"q" => "2hu"})
1267 assert results = json_response(conn, 200)
1269 [account | _] = results["accounts"]
1270 assert account["id"] == to_string(user_three.id)
1272 assert results["hashtags"] == []
1274 [status] = results["statuses"]
1275 assert status["id"] == to_string(activity.id)
1278 test "search fetches remote statuses", %{conn: conn} do
1282 |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
1284 assert results = json_response(conn, 200)
1286 [status] = results["statuses"]
1287 assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
1291 test "search fetches remote accounts", %{conn: conn} do
1294 |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
1296 assert results = json_response(conn, 200)
1297 [account] = results["accounts"]
1298 assert account["acct"] == "shp@social.heldscal.la"
1301 test "returns the favorites of a user", %{conn: conn} do
1302 user = insert(:user)
1303 other_user = insert(:user)
1305 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
1306 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
1308 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
1312 |> assign(:user, user)
1313 |> get("/api/v1/favourites")
1315 assert [status] = json_response(conn, 200)
1316 assert status["id"] == to_string(activity.id)
1319 describe "updating credentials" do
1320 test "updates the user's bio", %{conn: conn} do
1321 user = insert(:user)
1322 user2 = insert(:user)
1326 |> assign(:user, user)
1327 |> patch("/api/v1/accounts/update_credentials", %{
1328 "note" => "I drink #cofe with @#{user2.nickname}"
1331 assert user = json_response(conn, 200)
1333 assert user["note"] ==
1334 "I drink <a data-tag=\"cofe\" href=\"http://localhost:4001/tag/cofe\">#cofe</a> with <span><a data-user=\"#{
1336 }\" href=\"#{user2.ap_id}\">@<span>#{user2.nickname}</span></a></span>"
1339 test "updates the user's locking status", %{conn: conn} do
1340 user = insert(:user)
1344 |> assign(:user, user)
1345 |> patch("/api/v1/accounts/update_credentials", %{locked: "true"})
1347 assert user = json_response(conn, 200)
1348 assert user["locked"] == true
1351 test "updates the user's name", %{conn: conn} do
1352 user = insert(:user)
1356 |> assign(:user, user)
1357 |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
1359 assert user = json_response(conn, 200)
1360 assert user["display_name"] == "markorepairs"
1363 test "updates the user's avatar", %{conn: conn} do
1364 user = insert(:user)
1366 new_avatar = %Plug.Upload{
1367 content_type: "image/jpg",
1368 path: Path.absname("test/fixtures/image.jpg"),
1369 filename: "an_image.jpg"
1374 |> assign(:user, user)
1375 |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
1377 assert user_response = json_response(conn, 200)
1378 assert user_response["avatar"] != User.avatar_url(user)
1381 test "updates the user's banner", %{conn: conn} do
1382 user = insert(:user)
1384 new_header = %Plug.Upload{
1385 content_type: "image/jpg",
1386 path: Path.absname("test/fixtures/image.jpg"),
1387 filename: "an_image.jpg"
1392 |> assign(:user, user)
1393 |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
1395 assert user_response = json_response(conn, 200)
1396 assert user_response["header"] != User.banner_url(user)
1400 test "get instance information", %{conn: conn} do
1401 insert(:user, %{local: true})
1402 user = insert(:user, %{local: true})
1403 insert(:user, %{local: false})
1405 {:ok, _} = TwitterAPI.create_status(user, %{"status" => "cofe"})
1407 Pleroma.Stats.update_stats()
1411 |> get("/api/v1/instance")
1413 assert result = json_response(conn, 200)
1415 assert result["stats"]["user_count"] == 2
1416 assert result["stats"]["status_count"] == 1
1419 test "put settings", %{conn: conn} do
1420 user = insert(:user)
1424 |> assign(:user, user)
1425 |> put("/api/web/settings", %{"data" => %{"programming" => "socks"}})
1427 assert result = json_response(conn, 200)
1429 user = User.get_cached_by_ap_id(user.ap_id)
1430 assert user.info.settings == %{"programming" => "socks"}