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.AccountControllerTest do
6 use Pleroma.Web.ConnCase
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.OAuth.Token
14 import Pleroma.Factory
16 describe "account fetching" do
22 |> get("/api/v1/accounts/#{user.id}")
24 assert %{"id" => id} = json_response(conn, 200)
25 assert id == to_string(user.id)
29 |> get("/api/v1/accounts/-1")
31 assert %{"error" => "Can't find user"} = json_response(conn, 404)
34 test "works by nickname" do
39 |> get("/api/v1/accounts/#{user.nickname}")
41 assert %{"id" => id} = json_response(conn, 200)
45 test "works by nickname for remote users" do
46 limit_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
47 Pleroma.Config.put([:instance, :limit_to_local_content], false)
48 user = insert(:user, nickname: "user@example.com", local: false)
52 |> get("/api/v1/accounts/#{user.nickname}")
54 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local)
55 assert %{"id" => id} = json_response(conn, 200)
59 test "respects limit_to_local_content == :all for remote user nicknames" do
60 limit_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
61 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
63 user = insert(:user, nickname: "user@example.com", local: false)
67 |> get("/api/v1/accounts/#{user.nickname}")
69 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local)
70 assert json_response(conn, 404)
73 test "respects limit_to_local_content == :unauthenticated for remote user nicknames" do
74 limit_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
75 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
77 user = insert(:user, nickname: "user@example.com", local: false)
78 reading_user = insert(:user)
82 |> get("/api/v1/accounts/#{user.nickname}")
84 assert json_response(conn, 404)
88 |> assign(:user, reading_user)
89 |> get("/api/v1/accounts/#{user.nickname}")
91 Pleroma.Config.put([:instance, :limit_to_local_content], limit_to_local)
92 assert %{"id" => id} = json_response(conn, 200)
96 test "accounts fetches correct account for nicknames beginning with numbers", %{conn: conn} do
97 # Need to set an old-style integer ID to reproduce the problem
98 # (these are no longer assigned to new accounts but were preserved
99 # for existing accounts during the migration to flakeIDs)
100 user_one = insert(:user, %{id: 1212})
101 user_two = insert(:user, %{nickname: "#{user_one.id}garbage"})
105 |> get("/api/v1/accounts/#{user_one.id}")
109 |> get("/api/v1/accounts/#{user_two.nickname}")
113 |> get("/api/v1/accounts/#{user_two.id}")
115 acc_one = json_response(resp_one, 200)
116 acc_two = json_response(resp_two, 200)
117 acc_three = json_response(resp_three, 200)
118 refute acc_one == acc_two
119 assert acc_two == acc_three
123 describe "user timelines" do
124 test "gets a users statuses", %{conn: conn} do
125 user_one = insert(:user)
126 user_two = insert(:user)
127 user_three = insert(:user)
129 {:ok, user_three} = User.follow(user_three, user_one)
131 {:ok, activity} = CommonAPI.post(user_one, %{"status" => "HI!!!"})
133 {:ok, direct_activity} =
134 CommonAPI.post(user_one, %{
135 "status" => "Hi, @#{user_two.nickname}.",
136 "visibility" => "direct"
139 {:ok, private_activity} =
140 CommonAPI.post(user_one, %{"status" => "private", "visibility" => "private"})
144 |> get("/api/v1/accounts/#{user_one.id}/statuses")
146 assert [%{"id" => id}] = json_response(resp, 200)
147 assert id == to_string(activity.id)
151 |> assign(:user, user_two)
152 |> get("/api/v1/accounts/#{user_one.id}/statuses")
154 assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
155 assert id_one == to_string(direct_activity.id)
156 assert id_two == to_string(activity.id)
160 |> assign(:user, user_three)
161 |> get("/api/v1/accounts/#{user_one.id}/statuses")
163 assert [%{"id" => id_one}, %{"id" => id_two}] = json_response(resp, 200)
164 assert id_one == to_string(private_activity.id)
165 assert id_two == to_string(activity.id)
168 test "unimplemented pinned statuses feature", %{conn: conn} do
169 note = insert(:note_activity)
170 user = User.get_cached_by_ap_id(note.data["actor"])
174 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
176 assert json_response(conn, 200) == []
179 test "gets an users media", %{conn: conn} do
180 note = insert(:note_activity)
181 user = User.get_cached_by_ap_id(note.data["actor"])
184 content_type: "image/jpg",
185 path: Path.absname("test/fixtures/image.jpg"),
186 filename: "an_image.jpg"
189 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: user.ap_id)
191 {:ok, image_post} = CommonAPI.post(user, %{"status" => "cofe", "media_ids" => [media_id]})
195 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"})
197 assert [%{"id" => id}] = json_response(conn, 200)
198 assert id == to_string(image_post.id)
202 |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"})
204 assert [%{"id" => id}] = json_response(conn, 200)
205 assert id == to_string(image_post.id)
208 test "gets a user's statuses without reblogs", %{conn: conn} do
210 {:ok, post} = CommonAPI.post(user, %{"status" => "HI!!!"})
211 {:ok, _, _} = CommonAPI.repeat(post.id, user)
215 |> get("/api/v1/accounts/#{user.id}/statuses", %{"exclude_reblogs" => "true"})
217 assert [%{"id" => id}] = json_response(conn, 200)
218 assert id == to_string(post.id)
222 |> get("/api/v1/accounts/#{user.id}/statuses", %{"exclude_reblogs" => "1"})
224 assert [%{"id" => id}] = json_response(conn, 200)
225 assert id == to_string(post.id)
228 test "filters user's statuses by a hashtag", %{conn: conn} do
230 {:ok, post} = CommonAPI.post(user, %{"status" => "#hashtag"})
231 {:ok, _post} = CommonAPI.post(user, %{"status" => "hashtag"})
235 |> get("/api/v1/accounts/#{user.id}/statuses", %{"tagged" => "hashtag"})
237 assert [%{"id" => id}] = json_response(conn, 200)
238 assert id == to_string(post.id)
242 describe "followers" do
243 test "getting followers", %{conn: conn} do
245 other_user = insert(:user)
246 {:ok, user} = User.follow(user, other_user)
250 |> get("/api/v1/accounts/#{other_user.id}/followers")
252 assert [%{"id" => id}] = json_response(conn, 200)
253 assert id == to_string(user.id)
256 test "getting followers, hide_followers", %{conn: conn} do
258 other_user = insert(:user, %{info: %{hide_followers: true}})
259 {:ok, _user} = User.follow(user, other_user)
263 |> get("/api/v1/accounts/#{other_user.id}/followers")
265 assert [] == json_response(conn, 200)
268 test "getting followers, hide_followers, same user requesting", %{conn: conn} do
270 other_user = insert(:user, %{info: %{hide_followers: true}})
271 {:ok, _user} = User.follow(user, other_user)
275 |> assign(:user, other_user)
276 |> get("/api/v1/accounts/#{other_user.id}/followers")
278 refute [] == json_response(conn, 200)
281 test "getting followers, pagination", %{conn: conn} do
283 follower1 = insert(:user)
284 follower2 = insert(:user)
285 follower3 = insert(:user)
286 {:ok, _} = User.follow(follower1, user)
287 {:ok, _} = User.follow(follower2, user)
288 {:ok, _} = User.follow(follower3, user)
292 |> assign(:user, user)
296 |> get("/api/v1/accounts/#{user.id}/followers?since_id=#{follower1.id}")
298 assert [%{"id" => id3}, %{"id" => id2}] = json_response(res_conn, 200)
299 assert id3 == follower3.id
300 assert id2 == follower2.id
304 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3.id}")
306 assert [%{"id" => id2}, %{"id" => id1}] = json_response(res_conn, 200)
307 assert id2 == follower2.id
308 assert id1 == follower1.id
312 |> get("/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3.id}")
314 assert [%{"id" => id2}] = json_response(res_conn, 200)
315 assert id2 == follower2.id
317 assert [link_header] = get_resp_header(res_conn, "link")
318 assert link_header =~ ~r/min_id=#{follower2.id}/
319 assert link_header =~ ~r/max_id=#{follower2.id}/
323 describe "following" do
324 test "getting following", %{conn: conn} do
326 other_user = insert(:user)
327 {:ok, user} = User.follow(user, other_user)
331 |> get("/api/v1/accounts/#{user.id}/following")
333 assert [%{"id" => id}] = json_response(conn, 200)
334 assert id == to_string(other_user.id)
337 test "getting following, hide_follows", %{conn: conn} do
338 user = insert(:user, %{info: %{hide_follows: true}})
339 other_user = insert(:user)
340 {:ok, user} = User.follow(user, other_user)
344 |> get("/api/v1/accounts/#{user.id}/following")
346 assert [] == json_response(conn, 200)
349 test "getting following, hide_follows, same user requesting", %{conn: conn} do
350 user = insert(:user, %{info: %{hide_follows: true}})
351 other_user = insert(:user)
352 {:ok, user} = User.follow(user, other_user)
356 |> assign(:user, user)
357 |> get("/api/v1/accounts/#{user.id}/following")
359 refute [] == json_response(conn, 200)
362 test "getting following, pagination", %{conn: conn} do
364 following1 = insert(:user)
365 following2 = insert(:user)
366 following3 = insert(:user)
367 {:ok, _} = User.follow(user, following1)
368 {:ok, _} = User.follow(user, following2)
369 {:ok, _} = User.follow(user, following3)
373 |> assign(:user, user)
377 |> get("/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
379 assert [%{"id" => id3}, %{"id" => id2}] = json_response(res_conn, 200)
380 assert id3 == following3.id
381 assert id2 == following2.id
385 |> get("/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
387 assert [%{"id" => id2}, %{"id" => id1}] = json_response(res_conn, 200)
388 assert id2 == following2.id
389 assert id1 == following1.id
393 |> get("/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
395 assert [%{"id" => id2}] = json_response(res_conn, 200)
396 assert id2 == following2.id
398 assert [link_header] = get_resp_header(res_conn, "link")
399 assert link_header =~ ~r/min_id=#{following2.id}/
400 assert link_header =~ ~r/max_id=#{following2.id}/
404 describe "follow/unfollow" do
405 test "following / unfollowing a user", %{conn: conn} do
407 other_user = insert(:user)
411 |> assign(:user, user)
412 |> post("/api/v1/accounts/#{other_user.id}/follow")
414 assert %{"id" => _id, "following" => true} = json_response(conn, 200)
416 user = User.get_cached_by_id(user.id)
420 |> assign(:user, user)
421 |> post("/api/v1/accounts/#{other_user.id}/unfollow")
423 assert %{"id" => _id, "following" => false} = json_response(conn, 200)
425 user = User.get_cached_by_id(user.id)
429 |> assign(:user, user)
430 |> post("/api/v1/follows", %{"uri" => other_user.nickname})
432 assert %{"id" => id} = json_response(conn, 200)
433 assert id == to_string(other_user.id)
436 test "following without reblogs" do
437 follower = insert(:user)
438 followed = insert(:user)
439 other_user = insert(:user)
443 |> assign(:user, follower)
444 |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=false")
446 assert %{"showing_reblogs" => false} = json_response(conn, 200)
448 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey"})
449 {:ok, reblog, _} = CommonAPI.repeat(activity.id, followed)
453 |> assign(:user, User.get_cached_by_id(follower.id))
454 |> get("/api/v1/timelines/home")
456 assert [] == json_response(conn, 200)
460 |> assign(:user, follower)
461 |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=true")
463 assert %{"showing_reblogs" => true} = json_response(conn, 200)
467 |> assign(:user, User.get_cached_by_id(follower.id))
468 |> get("/api/v1/timelines/home")
470 expected_activity_id = reblog.id
471 assert [%{"id" => ^expected_activity_id}] = json_response(conn, 200)
474 test "following / unfollowing errors" do
479 |> assign(:user, user)
482 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
483 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
486 user = User.get_cached_by_id(user.id)
487 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
488 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
490 # self follow via uri
491 user = User.get_cached_by_id(user.id)
492 conn_res = post(conn, "/api/v1/follows", %{"uri" => user.nickname})
493 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
495 # follow non existing user
496 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
497 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
499 # follow non existing user via uri
500 conn_res = post(conn, "/api/v1/follows", %{"uri" => "doesntexist"})
501 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
503 # unfollow non existing user
504 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
505 assert %{"error" => "Record not found"} = json_response(conn_res, 404)
509 describe "mute/unmute" do
510 test "with notifications", %{conn: conn} do
512 other_user = insert(:user)
516 |> assign(:user, user)
517 |> post("/api/v1/accounts/#{other_user.id}/mute")
519 response = json_response(conn, 200)
521 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} = response
522 user = User.get_cached_by_id(user.id)
526 |> assign(:user, user)
527 |> post("/api/v1/accounts/#{other_user.id}/unmute")
529 response = json_response(conn, 200)
530 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = response
533 test "without notifications", %{conn: conn} do
535 other_user = insert(:user)
539 |> assign(:user, user)
540 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
542 response = json_response(conn, 200)
544 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} = response
545 user = User.get_cached_by_id(user.id)
549 |> assign(:user, user)
550 |> post("/api/v1/accounts/#{other_user.id}/unmute")
552 response = json_response(conn, 200)
553 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = response
557 describe "pinned statuses" do
560 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
562 [user: user, activity: activity]
565 test "returns pinned statuses", %{conn: conn, user: user, activity: activity} do
566 {:ok, _} = CommonAPI.pin(activity.id, user)
570 |> assign(:user, user)
571 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
572 |> json_response(200)
574 id_str = to_string(activity.id)
576 assert [%{"id" => ^id_str, "pinned" => true}] = result
580 test "blocking / unblocking a user", %{conn: conn} do
582 other_user = insert(:user)
586 |> assign(:user, user)
587 |> post("/api/v1/accounts/#{other_user.id}/block")
589 assert %{"id" => _id, "blocking" => true} = json_response(conn, 200)
591 user = User.get_cached_by_id(user.id)
595 |> assign(:user, user)
596 |> post("/api/v1/accounts/#{other_user.id}/unblock")
598 assert %{"id" => _id, "blocking" => false} = json_response(conn, 200)
601 describe "create account by app" do
605 email: "lain@example.org",
606 password: "PlzDontHackLain",
610 [valid_params: valid_params]
613 test "Account registration via Application", %{conn: conn} do
616 |> post("/api/v1/apps", %{
617 client_name: "client_name",
618 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
619 scopes: "read, write, follow"
623 "client_id" => client_id,
624 "client_secret" => client_secret,
626 "name" => "client_name",
627 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
630 } = json_response(conn, 200)
634 |> post("/oauth/token", %{
635 grant_type: "client_credentials",
636 client_id: client_id,
637 client_secret: client_secret
640 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
641 json_response(conn, 200)
644 token_from_db = Repo.get_by(Token, token: token)
647 assert scope == "read write follow"
651 |> put_req_header("authorization", "Bearer " <> token)
652 |> post("/api/v1/accounts", %{
654 email: "lain@example.org",
655 password: "PlzDontHackLain",
661 "access_token" => token,
662 "created_at" => _created_at,
664 "token_type" => "Bearer"
665 } = json_response(conn, 200)
667 token_from_db = Repo.get_by(Token, token: token)
669 token_from_db = Repo.preload(token_from_db, :user)
670 assert token_from_db.user
672 assert token_from_db.user.info.confirmation_pending
675 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
676 _user = insert(:user, email: "lain@example.org")
677 app_token = insert(:oauth_token, user: nil)
681 |> put_req_header("authorization", "Bearer " <> app_token.token)
683 res = post(conn, "/api/v1/accounts", valid_params)
684 assert json_response(res, 400) == %{"error" => "{\"email\":[\"has already been taken\"]}"}
687 test "rate limit", %{conn: conn} do
688 app_token = insert(:oauth_token, user: nil)
691 put_req_header(conn, "authorization", "Bearer " <> app_token.token)
692 |> Map.put(:remote_ip, {15, 15, 15, 15})
697 |> post("/api/v1/accounts", %{
698 username: "#{i}lain",
699 email: "#{i}lain@example.org",
700 password: "PlzDontHackLain",
705 "access_token" => token,
706 "created_at" => _created_at,
708 "token_type" => "Bearer"
709 } = json_response(conn, 200)
711 token_from_db = Repo.get_by(Token, token: token)
713 token_from_db = Repo.preload(token_from_db, :user)
714 assert token_from_db.user
716 assert token_from_db.user.info.confirmation_pending
721 |> post("/api/v1/accounts", %{
723 email: "6lain@example.org",
724 password: "PlzDontHackLain",
728 assert json_response(conn, :too_many_requests) == %{"error" => "Throttled"}
731 test "returns bad_request if missing required params", %{
733 valid_params: valid_params
735 app_token = insert(:oauth_token, user: nil)
739 |> put_req_header("authorization", "Bearer " <> app_token.token)
741 res = post(conn, "/api/v1/accounts", valid_params)
742 assert json_response(res, 200)
744 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
745 |> Stream.zip(valid_params)
746 |> Enum.each(fn {ip, {attr, _}} ->
749 |> Map.put(:remote_ip, ip)
750 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
751 |> json_response(400)
753 assert res == %{"error" => "Missing parameters"}
757 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
760 |> put_req_header("authorization", "Bearer " <> "invalid-token")
762 res = post(conn, "/api/v1/accounts", valid_params)
763 assert json_response(res, 403) == %{"error" => "Invalid credentials"}
767 describe "GET /api/v1/accounts/:id/lists - account_lists" do
768 test "returns lists to which the account belongs", %{conn: conn} do
770 other_user = insert(:user)
771 assert {:ok, %Pleroma.List{} = list} = Pleroma.List.create("Test List", user)
772 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
776 |> assign(:user, user)
777 |> get("/api/v1/accounts/#{other_user.id}/lists")
778 |> json_response(200)
780 assert res == [%{"id" => to_string(list.id), "title" => "Test List"}]
784 describe "verify_credentials" do
785 test "verify_credentials", %{conn: conn} do
790 |> assign(:user, user)
791 |> get("/api/v1/accounts/verify_credentials")
793 response = json_response(conn, 200)
795 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
796 assert response["pleroma"]["chat_token"]
797 assert id == to_string(user.id)
800 test "verify_credentials default scope unlisted", %{conn: conn} do
801 user = insert(:user, %{info: %User.Info{default_scope: "unlisted"}})
805 |> assign(:user, user)
806 |> get("/api/v1/accounts/verify_credentials")
808 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} = json_response(conn, 200)
809 assert id == to_string(user.id)
812 test "locked accounts", %{conn: conn} do
813 user = insert(:user, %{info: %User.Info{default_scope: "private"}})
817 |> assign(:user, user)
818 |> get("/api/v1/accounts/verify_credentials")
820 assert %{"id" => id, "source" => %{"privacy" => "private"}} = json_response(conn, 200)
821 assert id == to_string(user.id)
825 describe "user relationships" do
826 test "returns the relationships for the current user", %{conn: conn} do
828 other_user = insert(:user)
829 {:ok, user} = User.follow(user, other_user)
833 |> assign(:user, user)
834 |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
836 assert [relationship] = json_response(conn, 200)
838 assert to_string(other_user.id) == relationship["id"]
841 test "returns an empty list on a bad request", %{conn: conn} do
846 |> assign(:user, user)
847 |> get("/api/v1/accounts/relationships", %{})
849 assert [] = json_response(conn, 200)
853 test "getting a list of mutes", %{conn: conn} do
855 other_user = insert(:user)
857 {:ok, user} = User.mute(user, other_user)
861 |> assign(:user, user)
862 |> get("/api/v1/mutes")
864 other_user_id = to_string(other_user.id)
865 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
868 test "getting a list of blocks", %{conn: conn} do
870 other_user = insert(:user)
872 {:ok, user} = User.block(user, other_user)
876 |> assign(:user, user)
877 |> get("/api/v1/blocks")
879 other_user_id = to_string(other_user.id)
880 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)