1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.ActivityPub.InternalFetchActor
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.OAuth.Token
15 import Pleroma.Factory
17 describe "account fetching" do
19 %User{id: user_id} = insert(:user)
21 assert %{"id" => ^user_id} =
23 |> get("/api/v1/accounts/#{user_id}")
24 |> json_response_and_validate_schema(200)
26 assert %{"error" => "Can't find user"} =
28 |> get("/api/v1/accounts/-1")
29 |> json_response_and_validate_schema(404)
32 test "relationship field" do
33 %{conn: conn, user: user} = oauth_access(["read"])
35 other_user = insert(:user)
39 |> get("/api/v1/accounts/#{other_user.id}")
40 |> json_response_and_validate_schema(200)
42 assert response["id"] == other_user.id
43 assert response["pleroma"]["relationship"] == %{}
45 assert %{"pleroma" => %{"relationship" => %{"following" => false, "followed_by" => false}}} =
47 |> get("/api/v1/accounts/#{other_user.id}?with_relationships=true")
48 |> json_response_and_validate_schema(200)
50 {:ok, _, %{id: other_id}} = User.follow(user, other_user)
54 "pleroma" => %{"relationship" => %{"following" => true, "followed_by" => false}}
57 |> get("/api/v1/accounts/#{other_id}?with_relationships=true")
58 |> json_response_and_validate_schema(200)
60 {:ok, _, _} = User.follow(other_user, user)
64 "pleroma" => %{"relationship" => %{"following" => true, "followed_by" => true}}
67 |> get("/api/v1/accounts/#{other_id}?with_relationships=true")
68 |> json_response_and_validate_schema(200)
71 test "works by nickname" do
74 assert %{"id" => _user_id} =
76 |> get("/api/v1/accounts/#{user.nickname}")
77 |> json_response_and_validate_schema(200)
80 test "works by nickname for remote users" do
81 clear_config([:instance, :limit_to_local_content], false)
83 user = insert(:user, nickname: "user@example.com", local: false)
85 assert %{"id" => _user_id} =
87 |> get("/api/v1/accounts/#{user.nickname}")
88 |> json_response_and_validate_schema(200)
91 test "respects limit_to_local_content == :all for remote user nicknames" do
92 clear_config([:instance, :limit_to_local_content], :all)
94 user = insert(:user, nickname: "user@example.com", local: false)
97 |> get("/api/v1/accounts/#{user.nickname}")
98 |> json_response_and_validate_schema(404)
101 test "respects limit_to_local_content == :unauthenticated for remote user nicknames" do
102 clear_config([:instance, :limit_to_local_content], :unauthenticated)
104 user = insert(:user, nickname: "user@example.com", local: false)
105 reading_user = insert(:user)
109 |> get("/api/v1/accounts/#{user.nickname}")
111 assert json_response_and_validate_schema(conn, 404)
115 |> assign(:user, reading_user)
116 |> assign(:token, insert(:oauth_token, user: reading_user, scopes: ["read:accounts"]))
117 |> get("/api/v1/accounts/#{user.nickname}")
119 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
123 test "accounts fetches correct account for nicknames beginning with numbers", %{conn: conn} do
124 # Need to set an old-style integer ID to reproduce the problem
125 # (these are no longer assigned to new accounts but were preserved
126 # for existing accounts during the migration to flakeIDs)
127 user_one = insert(:user, %{id: 1212})
128 user_two = insert(:user, %{nickname: "#{user_one.id}garbage"})
132 |> get("/api/v1/accounts/#{user_one.id}")
133 |> json_response_and_validate_schema(:ok)
137 |> get("/api/v1/accounts/#{user_two.nickname}")
138 |> json_response_and_validate_schema(:ok)
142 |> get("/api/v1/accounts/#{user_two.id}")
143 |> json_response_and_validate_schema(:ok)
145 refute acc_one == acc_two
146 assert acc_two == acc_three
149 test "returns 404 when user is invisible", %{conn: conn} do
150 user = insert(:user, %{invisible: true})
152 assert %{"error" => "Can't find user"} =
154 |> get("/api/v1/accounts/#{user.nickname}")
155 |> json_response_and_validate_schema(404)
158 test "returns 404 for internal.fetch actor", %{conn: conn} do
159 %User{nickname: "internal.fetch"} = InternalFetchActor.get_actor()
161 assert %{"error" => "Can't find user"} =
163 |> get("/api/v1/accounts/internal.fetch")
164 |> json_response_and_validate_schema(404)
167 test "returns 404 for deactivated user", %{conn: conn} do
168 user = insert(:user, is_active: false)
170 assert %{"error" => "Can't find user"} =
172 |> get("/api/v1/accounts/#{user.id}")
173 |> json_response_and_validate_schema(:not_found)
177 defp local_and_remote_users do
178 local = insert(:user)
179 remote = insert(:user, local: false)
180 {:ok, local: local, remote: remote}
183 describe "user fetching with restrict unauthenticated profiles for local and remote" do
184 setup do: local_and_remote_users()
186 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
188 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
190 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
191 assert %{"error" => "This API requires an authenticated user"} ==
193 |> get("/api/v1/accounts/#{local.id}")
194 |> json_response_and_validate_schema(:unauthorized)
196 assert %{"error" => "This API requires an authenticated user"} ==
198 |> get("/api/v1/accounts/#{remote.id}")
199 |> json_response_and_validate_schema(:unauthorized)
202 test "if user is authenticated", %{local: local, remote: remote} do
203 %{conn: conn} = oauth_access(["read"])
205 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
206 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
208 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
209 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
213 describe "user fetching with restrict unauthenticated profiles for local" do
214 setup do: local_and_remote_users()
216 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
218 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
219 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
221 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
222 "error" => "This API requires an authenticated user"
225 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
226 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
229 test "if user is authenticated", %{local: local, remote: remote} do
230 %{conn: conn} = oauth_access(["read"])
232 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
233 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
235 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
236 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
240 describe "user fetching with restrict unauthenticated profiles for remote" do
241 setup do: local_and_remote_users()
243 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
245 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
246 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
247 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
249 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
251 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
252 "error" => "This API requires an authenticated user"
256 test "if user is authenticated", %{local: local, remote: remote} do
257 %{conn: conn} = oauth_access(["read"])
259 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
260 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
262 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
263 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
267 describe "user timelines" do
268 setup do: oauth_access(["read:statuses"])
270 test "works with announces that are just addressed to public", %{conn: conn} do
271 user = insert(:user, ap_id: "https://honktest/u/test", local: false)
272 other_user = insert(:user)
274 {:ok, post} = CommonAPI.post(other_user, %{status: "bonkeronk"})
278 "@context" => "https://www.w3.org/ns/activitystreams",
279 "actor" => "https://honktest/u/test",
280 "id" => "https://honktest/u/test/bonk/1793M7B9MQ48847vdx",
281 "object" => post.data["object"],
282 "published" => "2019-06-25T19:33:58Z",
283 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
286 |> ActivityPub.persist(local: false)
290 |> get("/api/v1/accounts/#{user.id}/statuses")
291 |> json_response_and_validate_schema(200)
293 assert [%{"id" => id}] = resp
294 assert id == announce.id
297 test "deactivated user", %{conn: conn} do
298 user = insert(:user, is_active: false)
300 assert %{"error" => "Can't find user"} ==
302 |> get("/api/v1/accounts/#{user.id}/statuses")
303 |> json_response_and_validate_schema(:not_found)
306 test "returns 404 when user is invisible", %{conn: conn} do
307 user = insert(:user, %{invisible: true})
309 assert %{"error" => "Can't find user"} =
311 |> get("/api/v1/accounts/#{user.id}")
312 |> json_response_and_validate_schema(404)
315 test "respects blocks", %{user: user_one, conn: conn} do
316 user_two = insert(:user)
317 user_three = insert(:user)
319 User.block(user_one, user_two)
321 {:ok, activity} = CommonAPI.post(user_two, %{status: "User one sux0rz"})
322 {:ok, repeat} = CommonAPI.repeat(activity.id, user_three)
326 |> get("/api/v1/accounts/#{user_two.id}/statuses")
327 |> json_response_and_validate_schema(200)
329 assert [%{"id" => id}] = resp
330 assert id == activity.id
332 # Even a blocked user will deliver the full user timeline, there would be
333 # no point in looking at a blocked users timeline otherwise
336 |> get("/api/v1/accounts/#{user_two.id}/statuses")
337 |> json_response_and_validate_schema(200)
339 assert [%{"id" => id}] = resp
340 assert id == activity.id
342 # Third user's timeline includes the repeat when viewed by unauthenticated user
345 |> get("/api/v1/accounts/#{user_three.id}/statuses")
346 |> json_response_and_validate_schema(200)
348 assert [%{"id" => id}] = resp
349 assert id == repeat.id
351 # When viewing a third user's timeline, the blocked users' statuses will NOT be shown
352 resp = get(conn, "/api/v1/accounts/#{user_three.id}/statuses")
354 assert [] == json_response_and_validate_schema(resp, 200)
357 test "gets users statuses", %{conn: conn} do
358 user_one = insert(:user)
359 user_two = insert(:user)
360 user_three = insert(:user)
362 {:ok, _user_three, _user_one} = User.follow(user_three, user_one)
364 {:ok, activity} = CommonAPI.post(user_one, %{status: "HI!!!"})
366 {:ok, direct_activity} =
367 CommonAPI.post(user_one, %{
368 status: "Hi, @#{user_two.nickname}.",
372 {:ok, private_activity} =
373 CommonAPI.post(user_one, %{status: "private", visibility: "private"})
378 |> get("/api/v1/accounts/#{user_one.id}/statuses")
379 |> json_response_and_validate_schema(200)
381 assert [%{"id" => id}] = resp
382 assert id == to_string(activity.id)
386 |> assign(:user, user_two)
387 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
388 |> get("/api/v1/accounts/#{user_one.id}/statuses")
389 |> json_response_and_validate_schema(200)
391 assert [%{"id" => id_one}, %{"id" => id_two}] = resp
392 assert id_one == to_string(direct_activity.id)
393 assert id_two == to_string(activity.id)
397 |> assign(:user, user_three)
398 |> assign(:token, insert(:oauth_token, user: user_three, scopes: ["read:statuses"]))
399 |> get("/api/v1/accounts/#{user_one.id}/statuses")
400 |> json_response_and_validate_schema(200)
402 assert [%{"id" => id_one}, %{"id" => id_two}] = resp
403 assert id_one == to_string(private_activity.id)
404 assert id_two == to_string(activity.id)
407 test "unimplemented pinned statuses feature", %{conn: conn} do
408 note = insert(:note_activity)
409 user = User.get_cached_by_ap_id(note.data["actor"])
411 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?pinned=true")
413 assert json_response_and_validate_schema(conn, 200) == []
416 test "gets an users media, excludes reblogs", %{conn: conn} do
417 note = insert(:note_activity)
418 user = User.get_cached_by_ap_id(note.data["actor"])
419 other_user = insert(:user)
422 content_type: "image/jpeg",
423 path: Path.absname("test/fixtures/image.jpg"),
424 filename: "an_image.jpg"
427 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: user.ap_id)
429 {:ok, %{id: image_post_id}} = CommonAPI.post(user, %{status: "cofe", media_ids: [media_id]})
431 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: other_user.ap_id)
433 {:ok, %{id: other_image_post_id}} =
434 CommonAPI.post(other_user, %{status: "cofe2", media_ids: [media_id]})
436 {:ok, _announce} = CommonAPI.repeat(other_image_post_id, user)
438 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?only_media=true")
440 assert [%{"id" => ^image_post_id}] = json_response_and_validate_schema(conn, 200)
442 conn = get(build_conn(), "/api/v1/accounts/#{user.id}/statuses?only_media=1")
444 assert [%{"id" => ^image_post_id}] = json_response_and_validate_schema(conn, 200)
447 test "gets a user's statuses without reblogs", %{user: user, conn: conn} do
448 {:ok, %{id: post_id}} = CommonAPI.post(user, %{status: "HI!!!"})
449 {:ok, _} = CommonAPI.repeat(post_id, user)
451 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_reblogs=true")
452 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
454 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_reblogs=1")
455 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
458 test "filters user's statuses by a hashtag", %{user: user, conn: conn} do
459 {:ok, %{id: post_id}} = CommonAPI.post(user, %{status: "#hashtag"})
460 {:ok, _post} = CommonAPI.post(user, %{status: "hashtag"})
462 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?tagged=hashtag")
463 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
466 test "the user views their own timelines and excludes direct messages", %{
470 {:ok, %{id: public_activity_id}} =
471 CommonAPI.post(user, %{status: ".", visibility: "public"})
473 {:ok, _direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
475 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_visibilities[]=direct")
476 assert [%{"id" => ^public_activity_id}] = json_response_and_validate_schema(conn, 200)
479 test "muted reactions", %{user: user, conn: conn} do
480 user2 = insert(:user)
481 User.mute(user, user2)
482 {:ok, activity} = CommonAPI.post(user, %{status: "."})
483 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
487 |> get("/api/v1/accounts/#{user.id}/statuses")
488 |> json_response_and_validate_schema(200)
493 "emoji_reactions" => []
500 |> get("/api/v1/accounts/#{user.id}/statuses?with_muted=true")
501 |> json_response_and_validate_schema(200)
506 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
512 test "paginates a user's statuses", %{user: user, conn: conn} do
513 {:ok, post_1} = CommonAPI.post(user, %{status: "first post"})
514 {:ok, post_2} = CommonAPI.post(user, %{status: "second post"})
516 response_1 = get(conn, "/api/v1/accounts/#{user.id}/statuses?limit=1")
517 assert [res] = json_response_and_validate_schema(response_1, 200)
518 assert res["id"] == post_2.id
520 response_2 = get(conn, "/api/v1/accounts/#{user.id}/statuses?limit=1&max_id=#{res["id"]}")
521 assert [res] = json_response_and_validate_schema(response_2, 200)
522 assert res["id"] == post_1.id
524 refute response_1 == response_2
528 defp local_and_remote_activities(%{local: local, remote: remote}) do
529 insert(:note_activity, user: local)
530 insert(:note_activity, user: remote, local: false)
535 describe "statuses with restrict unauthenticated profiles for local and remote" do
536 setup do: local_and_remote_users()
537 setup :local_and_remote_activities
539 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
541 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
543 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
544 assert %{"error" => "This API requires an authenticated user"} ==
546 |> get("/api/v1/accounts/#{local.id}/statuses")
547 |> json_response_and_validate_schema(:unauthorized)
549 assert %{"error" => "This API requires an authenticated user"} ==
551 |> get("/api/v1/accounts/#{remote.id}/statuses")
552 |> json_response_and_validate_schema(:unauthorized)
555 test "if user is authenticated", %{local: local, remote: remote} do
556 %{conn: conn} = oauth_access(["read"])
558 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
559 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
561 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
562 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
566 describe "statuses with restrict unauthenticated profiles for local" do
567 setup do: local_and_remote_users()
568 setup :local_and_remote_activities
570 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
572 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
573 assert %{"error" => "This API requires an authenticated user"} ==
575 |> get("/api/v1/accounts/#{local.id}/statuses")
576 |> json_response_and_validate_schema(:unauthorized)
578 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
579 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
582 test "if user is authenticated", %{local: local, remote: remote} do
583 %{conn: conn} = oauth_access(["read"])
585 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
586 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
588 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
589 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
593 describe "statuses with restrict unauthenticated profiles for remote" do
594 setup do: local_and_remote_users()
595 setup :local_and_remote_activities
597 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
599 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
600 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
601 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
603 assert %{"error" => "This API requires an authenticated user"} ==
605 |> get("/api/v1/accounts/#{remote.id}/statuses")
606 |> json_response_and_validate_schema(:unauthorized)
609 test "if user is authenticated", %{local: local, remote: remote} do
610 %{conn: conn} = oauth_access(["read"])
612 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
613 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
615 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
616 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
620 describe "followers" do
621 setup do: oauth_access(["read:accounts"])
623 test "getting followers", %{user: user, conn: conn} do
624 other_user = insert(:user)
625 {:ok, %{id: user_id}, other_user} = User.follow(user, other_user)
627 conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers")
629 assert [%{"id" => ^user_id}] = json_response_and_validate_schema(conn, 200)
632 test "following with relationship", %{conn: conn, user: user} do
633 other_user = insert(:user)
634 {:ok, %{id: id}, _} = User.follow(other_user, user)
642 "following" => false,
643 "followed_by" => true
649 |> get("/api/v1/accounts/#{user.id}/followers?with_relationships=true")
650 |> json_response_and_validate_schema(200)
652 {:ok, _, _} = User.follow(user, other_user)
661 "followed_by" => true
667 |> get("/api/v1/accounts/#{user.id}/followers?with_relationships=true")
668 |> json_response_and_validate_schema(200)
671 test "getting followers, hide_followers", %{user: user, conn: conn} do
672 other_user = insert(:user, hide_followers: true)
673 {:ok, _user, _other_user} = User.follow(user, other_user)
675 conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers")
677 assert [] == json_response_and_validate_schema(conn, 200)
680 test "getting followers, hide_followers, same user requesting" do
682 other_user = insert(:user, hide_followers: true)
683 {:ok, _user, _other_user} = User.follow(user, other_user)
687 |> assign(:user, other_user)
688 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
689 |> get("/api/v1/accounts/#{other_user.id}/followers")
691 refute [] == json_response_and_validate_schema(conn, 200)
694 test "getting followers, pagination", %{user: user, conn: conn} do
695 {:ok, %User{id: follower1_id}, _user} = :user |> insert() |> User.follow(user)
696 {:ok, %User{id: follower2_id}, _user} = :user |> insert() |> User.follow(user)
697 {:ok, %User{id: follower3_id}, _user} = :user |> insert() |> User.follow(user)
699 assert [%{"id" => ^follower3_id}, %{"id" => ^follower2_id}] =
701 |> get("/api/v1/accounts/#{user.id}/followers?since_id=#{follower1_id}")
702 |> json_response_and_validate_schema(200)
704 assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] =
706 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3_id}")
707 |> json_response_and_validate_schema(200)
709 assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] =
712 "/api/v1/accounts/#{user.id}/followers?id=#{user.id}&limit=20&max_id=#{follower3_id}"
714 |> json_response_and_validate_schema(200)
716 res_conn = get(conn, "/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3_id}")
718 assert [%{"id" => ^follower2_id}] = json_response_and_validate_schema(res_conn, 200)
720 assert [link_header] = get_resp_header(res_conn, "link")
721 assert link_header =~ ~r/min_id=#{follower2_id}/
722 assert link_header =~ ~r/max_id=#{follower2_id}/
726 describe "following" do
727 setup do: oauth_access(["read:accounts"])
729 test "getting following", %{user: user, conn: conn} do
730 other_user = insert(:user)
731 {:ok, user, other_user} = User.follow(user, other_user)
733 conn = get(conn, "/api/v1/accounts/#{user.id}/following")
735 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
736 assert id == to_string(other_user.id)
739 test "following with relationship", %{conn: conn, user: user} do
740 other_user = insert(:user)
741 {:ok, user, other_user} = User.follow(user, other_user)
743 conn = get(conn, "/api/v1/accounts/#{user.id}/following?with_relationships=true")
751 "relationship" => %{"id" => ^id, "following" => true, "followed_by" => false}
754 ] = json_response_and_validate_schema(conn, 200)
757 test "getting following, hide_follows, other user requesting" do
758 user = insert(:user, hide_follows: true)
759 other_user = insert(:user)
760 {:ok, user, other_user} = User.follow(user, other_user)
764 |> assign(:user, other_user)
765 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
766 |> get("/api/v1/accounts/#{user.id}/following")
768 assert [] == json_response_and_validate_schema(conn, 200)
771 test "getting following, hide_follows, same user requesting" do
772 user = insert(:user, hide_follows: true)
773 other_user = insert(:user)
774 {:ok, user, _other_user} = User.follow(user, other_user)
778 |> assign(:user, user)
779 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read:accounts"]))
780 |> get("/api/v1/accounts/#{user.id}/following")
782 refute [] == json_response_and_validate_schema(conn, 200)
785 test "getting following, pagination", %{user: user, conn: conn} do
786 following1 = insert(:user)
787 following2 = insert(:user)
788 following3 = insert(:user)
789 {:ok, _, _} = User.follow(user, following1)
790 {:ok, _, _} = User.follow(user, following2)
791 {:ok, _, _} = User.follow(user, following3)
793 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
795 assert [%{"id" => id3}, %{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
796 assert id3 == following3.id
797 assert id2 == following2.id
799 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
801 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
802 assert id2 == following2.id
803 assert id1 == following1.id
808 "/api/v1/accounts/#{user.id}/following?id=#{user.id}&limit=20&max_id=#{following3.id}"
811 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
812 assert id2 == following2.id
813 assert id1 == following1.id
816 get(conn, "/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
818 assert [%{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
819 assert id2 == following2.id
821 assert [link_header] = get_resp_header(res_conn, "link")
822 assert link_header =~ ~r/min_id=#{following2.id}/
823 assert link_header =~ ~r/max_id=#{following2.id}/
827 describe "follow/unfollow" do
828 setup do: oauth_access(["follow"])
830 test "following / unfollowing a user", %{conn: conn} do
831 %{id: other_user_id, nickname: other_user_nickname} = insert(:user)
833 assert %{"id" => _id, "following" => true} =
835 |> post("/api/v1/accounts/#{other_user_id}/follow")
836 |> json_response_and_validate_schema(200)
838 assert %{"id" => _id, "following" => false} =
840 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
841 |> json_response_and_validate_schema(200)
843 assert %{"id" => ^other_user_id} =
845 |> put_req_header("content-type", "application/json")
846 |> post("/api/v1/follows", %{"uri" => other_user_nickname})
847 |> json_response_and_validate_schema(200)
850 test "cancelling follow request", %{conn: conn} do
851 %{id: other_user_id} = insert(:user, %{is_locked: true})
853 assert %{"id" => ^other_user_id, "following" => false, "requested" => true} =
855 |> post("/api/v1/accounts/#{other_user_id}/follow")
856 |> json_response_and_validate_schema(:ok)
858 assert %{"id" => ^other_user_id, "following" => false, "requested" => false} =
860 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
861 |> json_response_and_validate_schema(:ok)
864 test "following without reblogs" do
865 %{conn: conn} = oauth_access(["follow", "read:statuses"])
866 followed = insert(:user)
867 other_user = insert(:user)
871 |> put_req_header("content-type", "application/json")
872 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
874 assert %{"showing_reblogs" => false} = json_response_and_validate_schema(ret_conn, 200)
876 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
877 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
881 |> get("/api/v1/timelines/home")
882 |> json_response_and_validate_schema(200)
884 assert %{"showing_reblogs" => true} =
886 |> put_req_header("content-type", "application/json")
887 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: true})
888 |> json_response_and_validate_schema(200)
890 assert [%{"id" => ^reblog_id}] =
892 |> get("/api/v1/timelines/home")
893 |> json_response_and_validate_schema(200)
896 test "following with reblogs" do
897 %{conn: conn} = oauth_access(["follow", "read:statuses"])
898 followed = insert(:user)
899 other_user = insert(:user)
901 ret_conn = post(conn, "/api/v1/accounts/#{followed.id}/follow")
903 assert %{"showing_reblogs" => true} = json_response_and_validate_schema(ret_conn, 200)
905 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
906 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
908 assert [%{"id" => ^reblog_id}] =
910 |> get("/api/v1/timelines/home")
911 |> json_response_and_validate_schema(200)
913 assert %{"showing_reblogs" => false} =
915 |> put_req_header("content-type", "application/json")
916 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
917 |> json_response_and_validate_schema(200)
921 |> get("/api/v1/timelines/home")
922 |> json_response_and_validate_schema(200)
925 test "following with subscription and unsubscribing" do
926 %{conn: conn} = oauth_access(["follow"])
927 followed = insert(:user)
931 |> put_req_header("content-type", "application/json")
932 |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: true})
934 assert %{"id" => _id, "subscribing" => true} =
935 json_response_and_validate_schema(ret_conn, 200)
939 |> put_req_header("content-type", "application/json")
940 |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: false})
942 assert %{"id" => _id, "subscribing" => false} =
943 json_response_and_validate_schema(ret_conn, 200)
946 test "following / unfollowing errors", %{user: user, conn: conn} do
948 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
950 assert %{"error" => "Can not follow yourself"} =
951 json_response_and_validate_schema(conn_res, 400)
954 user = User.get_cached_by_id(user.id)
955 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
957 assert %{"error" => "Can not unfollow yourself"} =
958 json_response_and_validate_schema(conn_res, 400)
960 # self follow via uri
961 user = User.get_cached_by_id(user.id)
963 assert %{"error" => "Can not follow yourself"} =
965 |> put_req_header("content-type", "multipart/form-data")
966 |> post("/api/v1/follows", %{"uri" => user.nickname})
967 |> json_response_and_validate_schema(400)
969 # follow non existing user
970 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
971 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
973 # follow non existing user via uri
976 |> put_req_header("content-type", "multipart/form-data")
977 |> post("/api/v1/follows", %{"uri" => "doesntexist"})
979 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
981 # unfollow non existing user
982 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
983 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
987 describe "mute/unmute" do
988 setup do: oauth_access(["write:mutes"])
990 test "with notifications", %{conn: conn} do
991 other_user = insert(:user)
993 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} =
995 |> post("/api/v1/accounts/#{other_user.id}/mute")
996 |> json_response_and_validate_schema(200)
998 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
1000 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
1001 json_response_and_validate_schema(conn, 200)
1004 test "without notifications", %{conn: conn} do
1005 other_user = insert(:user)
1009 |> put_req_header("content-type", "multipart/form-data")
1010 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
1012 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} =
1013 json_response_and_validate_schema(ret_conn, 200)
1015 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
1017 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
1018 json_response_and_validate_schema(conn, 200)
1022 describe "pinned statuses" do
1024 user = insert(:user)
1025 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1026 %{conn: conn} = oauth_access(["read:statuses"], user: user)
1028 [conn: conn, user: user, activity: activity]
1031 test "returns pinned statuses", %{conn: conn, user: user, activity: %{id: activity_id}} do
1032 {:ok, _} = CommonAPI.pin(activity_id, user)
1034 assert [%{"id" => ^activity_id, "pinned" => true}] =
1036 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1037 |> json_response_and_validate_schema(200)
1041 test "blocking / unblocking a user" do
1042 %{conn: conn} = oauth_access(["follow"])
1043 other_user = insert(:user)
1045 ret_conn = post(conn, "/api/v1/accounts/#{other_user.id}/block")
1047 assert %{"id" => _id, "blocking" => true} = json_response_and_validate_schema(ret_conn, 200)
1049 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unblock")
1051 assert %{"id" => _id, "blocking" => false} = json_response_and_validate_schema(conn, 200)
1054 describe "create account by app" do
1058 email: "lain@example.org",
1059 password: "PlzDontHackLain",
1063 [valid_params: valid_params]
1066 test "registers and logs in without :account_activation_required / :account_approval_required",
1068 clear_config([:instance, :account_activation_required], false)
1069 clear_config([:instance, :account_approval_required], false)
1073 |> put_req_header("content-type", "application/json")
1074 |> post("/api/v1/apps", %{
1075 client_name: "client_name",
1076 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1077 scopes: "read, write, follow"
1081 "client_id" => client_id,
1082 "client_secret" => client_secret,
1084 "name" => "client_name",
1085 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1088 } = json_response_and_validate_schema(conn, 200)
1091 post(conn, "/oauth/token", %{
1092 grant_type: "client_credentials",
1093 client_id: client_id,
1094 client_secret: client_secret
1097 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1098 json_response(conn, 200)
1101 token_from_db = Repo.get_by(Token, token: token)
1102 assert token_from_db
1104 assert scope == "read write follow"
1106 clear_config([User, :email_blacklist], ["example.org"])
1110 email: "lain@example.org",
1111 password: "PlzDontHackLain",
1118 |> put_req_header("content-type", "multipart/form-data")
1119 |> put_req_header("authorization", "Bearer " <> token)
1120 |> post("/api/v1/accounts", params)
1122 assert %{"error" => "{\"email\":[\"Invalid email\"]}"} =
1123 json_response_and_validate_schema(conn, 400)
1125 clear_config([User, :email_blacklist], [])
1129 |> put_req_header("content-type", "multipart/form-data")
1130 |> put_req_header("authorization", "Bearer " <> token)
1131 |> post("/api/v1/accounts", params)
1134 "access_token" => token,
1135 "created_at" => _created_at,
1137 "token_type" => "Bearer"
1138 } = json_response_and_validate_schema(conn, 200)
1140 token_from_db = Repo.get_by(Token, token: token)
1141 assert token_from_db
1142 user = Repo.preload(token_from_db, :user).user
1145 assert user.is_confirmed
1146 assert user.is_approved
1149 test "registers but does not log in with :account_activation_required", %{conn: conn} do
1150 clear_config([:instance, :account_activation_required], true)
1151 clear_config([:instance, :account_approval_required], false)
1155 |> put_req_header("content-type", "application/json")
1156 |> post("/api/v1/apps", %{
1157 client_name: "client_name",
1158 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1159 scopes: "read, write, follow"
1163 "client_id" => client_id,
1164 "client_secret" => client_secret,
1166 "name" => "client_name",
1167 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1170 } = json_response_and_validate_schema(conn, 200)
1173 post(conn, "/oauth/token", %{
1174 grant_type: "client_credentials",
1175 client_id: client_id,
1176 client_secret: client_secret
1179 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1180 json_response(conn, 200)
1183 token_from_db = Repo.get_by(Token, token: token)
1184 assert token_from_db
1186 assert scope == "read write follow"
1190 |> put_req_header("content-type", "multipart/form-data")
1191 |> put_req_header("authorization", "Bearer " <> token)
1192 |> post("/api/v1/accounts", %{
1194 email: "lain@example.org",
1195 password: "PlzDontHackLain",
1200 response = json_response_and_validate_schema(conn, 200)
1201 assert %{"identifier" => "missing_confirmed_email"} = response
1202 refute response["access_token"]
1203 refute response["token_type"]
1205 user = Repo.get_by(User, email: "lain@example.org")
1206 refute user.is_confirmed
1209 test "registers but does not log in with :account_approval_required", %{conn: conn} do
1210 clear_config([:instance, :account_approval_required], true)
1211 clear_config([:instance, :account_activation_required], false)
1215 |> put_req_header("content-type", "application/json")
1216 |> post("/api/v1/apps", %{
1217 client_name: "client_name",
1218 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1219 scopes: "read, write, follow"
1223 "client_id" => client_id,
1224 "client_secret" => client_secret,
1226 "name" => "client_name",
1227 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1230 } = json_response_and_validate_schema(conn, 200)
1233 post(conn, "/oauth/token", %{
1234 grant_type: "client_credentials",
1235 client_id: client_id,
1236 client_secret: client_secret
1239 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1240 json_response(conn, 200)
1243 token_from_db = Repo.get_by(Token, token: token)
1244 assert token_from_db
1246 assert scope == "read write follow"
1250 |> put_req_header("content-type", "multipart/form-data")
1251 |> put_req_header("authorization", "Bearer " <> token)
1252 |> post("/api/v1/accounts", %{
1254 email: "lain@example.org",
1255 password: "PlzDontHackLain",
1258 reason: "I'm a cool dude, bro"
1261 response = json_response_and_validate_schema(conn, 200)
1262 assert %{"identifier" => "awaiting_approval"} = response
1263 refute response["access_token"]
1264 refute response["token_type"]
1266 user = Repo.get_by(User, email: "lain@example.org")
1268 refute user.is_approved
1269 assert user.registration_reason == "I'm a cool dude, bro"
1272 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
1273 _user = insert(:user, email: "lain@example.org")
1274 app_token = insert(:oauth_token, user: nil)
1278 |> put_req_header("authorization", "Bearer " <> app_token.token)
1279 |> put_req_header("content-type", "application/json")
1280 |> post("/api/v1/accounts", valid_params)
1282 assert json_response_and_validate_schema(res, 400) == %{
1283 "error" => "{\"email\":[\"has already been taken\"]}"
1287 test "returns bad_request if missing required params", %{
1289 valid_params: valid_params
1291 app_token = insert(:oauth_token, user: nil)
1295 |> put_req_header("authorization", "Bearer " <> app_token.token)
1296 |> put_req_header("content-type", "application/json")
1298 res = post(conn, "/api/v1/accounts", valid_params)
1299 assert json_response_and_validate_schema(res, 200)
1301 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
1302 |> Stream.zip(Map.delete(valid_params, :email))
1303 |> Enum.each(fn {ip, {attr, _}} ->
1306 |> Map.put(:remote_ip, ip)
1307 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
1308 |> json_response_and_validate_schema(400)
1311 "error" => "Missing field: #{attr}.",
1314 "message" => "Missing field: #{attr}",
1315 "source" => %{"pointer" => "/#{attr}"},
1316 "title" => "Invalid value"
1323 test "returns bad_request if missing email params when :account_activation_required is enabled",
1324 %{conn: conn, valid_params: valid_params} do
1325 clear_config([:instance, :account_activation_required], true)
1327 app_token = insert(:oauth_token, user: nil)
1331 |> put_req_header("authorization", "Bearer " <> app_token.token)
1332 |> put_req_header("content-type", "application/json")
1336 |> Map.put(:remote_ip, {127, 0, 0, 5})
1337 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1339 assert json_response_and_validate_schema(res, 400) ==
1340 %{"error" => "Missing parameter: email"}
1344 |> Map.put(:remote_ip, {127, 0, 0, 6})
1345 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1347 assert json_response_and_validate_schema(res, 400) == %{
1348 "error" => "{\"email\":[\"can't be blank\"]}"
1352 test "allow registration without an email", %{conn: conn, valid_params: valid_params} do
1353 app_token = insert(:oauth_token, user: nil)
1354 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1358 |> put_req_header("content-type", "application/json")
1359 |> Map.put(:remote_ip, {127, 0, 0, 7})
1360 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1362 assert json_response_and_validate_schema(res, 200)
1365 test "allow registration with an empty email", %{conn: conn, valid_params: valid_params} do
1366 app_token = insert(:oauth_token, user: nil)
1367 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1371 |> put_req_header("content-type", "application/json")
1372 |> Map.put(:remote_ip, {127, 0, 0, 8})
1373 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1375 assert json_response_and_validate_schema(res, 200)
1378 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
1381 |> put_req_header("authorization", "Bearer " <> "invalid-token")
1382 |> put_req_header("content-type", "multipart/form-data")
1383 |> post("/api/v1/accounts", valid_params)
1385 assert json_response_and_validate_schema(res, 403) == %{"error" => "Invalid credentials"}
1388 test "registration from trusted app" do
1389 clear_config([Pleroma.Captcha, :enabled], true)
1390 app = insert(:oauth_app, trusted: true, scopes: ["read", "write", "follow", "push"])
1394 |> post("/oauth/token", %{
1395 "grant_type" => "client_credentials",
1396 "client_id" => app.client_id,
1397 "client_secret" => app.client_secret
1400 assert %{"access_token" => token, "token_type" => "Bearer"} = json_response(conn, 200)
1404 |> Plug.Conn.put_req_header("authorization", "Bearer " <> token)
1405 |> put_req_header("content-type", "multipart/form-data")
1406 |> post("/api/v1/accounts", %{
1407 nickname: "nickanme",
1409 email: "email@example.com",
1412 password: "some_password",
1413 confirm: "some_password"
1415 |> json_response_and_validate_schema(200)
1418 "access_token" => access_token,
1420 "scope" => "read write follow push",
1421 "token_type" => "Bearer"
1426 |> Plug.Conn.put_req_header("authorization", "Bearer " <> access_token)
1427 |> get("/api/v1/accounts/verify_credentials")
1428 |> json_response_and_validate_schema(200)
1433 "display_name" => "Lain",
1434 "follow_requests_count" => 0,
1435 "followers_count" => 0,
1436 "following_count" => 0,
1443 "actor_type" => "Person",
1444 "discoverable" => false,
1445 "no_rich_text" => false,
1448 "privacy" => "public",
1449 "sensitive" => false
1451 "statuses_count" => 0,
1452 "username" => "Lain"
1457 describe "create account by app / rate limit" do
1458 setup do: clear_config([:rate_limit, :app_account_creation], {10_000, 2})
1460 test "respects rate limit setting", %{conn: conn} do
1461 app_token = insert(:oauth_token, user: nil)
1465 |> put_req_header("authorization", "Bearer " <> app_token.token)
1466 |> Map.put(:remote_ip, {15, 15, 15, 15})
1467 |> put_req_header("content-type", "multipart/form-data")
1472 |> post("/api/v1/accounts", %{
1473 username: "#{i}lain",
1474 email: "#{i}lain@example.org",
1475 password: "PlzDontHackLain",
1480 "access_token" => token,
1481 "created_at" => _created_at,
1483 "token_type" => "Bearer"
1484 } = json_response_and_validate_schema(conn, 200)
1486 token_from_db = Repo.get_by(Token, token: token)
1487 assert token_from_db
1488 token_from_db = Repo.preload(token_from_db, :user)
1489 assert token_from_db.user
1493 post(conn, "/api/v1/accounts", %{
1495 email: "6lain@example.org",
1496 password: "PlzDontHackLain",
1500 assert json_response_and_validate_schema(conn, :too_many_requests) == %{
1501 "error" => "Throttled"
1506 describe "create account with enabled captcha" do
1507 setup %{conn: conn} do
1508 app_token = insert(:oauth_token, user: nil)
1512 |> put_req_header("authorization", "Bearer " <> app_token.token)
1513 |> put_req_header("content-type", "multipart/form-data")
1518 setup do: clear_config([Pleroma.Captcha, :enabled], true)
1520 test "creates an account and returns 200 if captcha is valid", %{conn: conn} do
1521 %{token: token, answer_data: answer_data} = Pleroma.Captcha.new()
1525 email: "lain@example.org",
1526 password: "PlzDontHackLain",
1528 captcha_solution: Pleroma.Captcha.Mock.solution(),
1529 captcha_token: token,
1530 captcha_answer_data: answer_data
1534 "access_token" => access_token,
1537 "token_type" => "Bearer"
1540 |> post("/api/v1/accounts", params)
1541 |> json_response_and_validate_schema(:ok)
1543 assert Token |> Repo.get_by(token: access_token) |> Repo.preload(:user) |> Map.get(:user)
1546 test "returns 400 if any captcha field is not provided", %{conn: conn} do
1547 captcha_fields = [:captcha_solution, :captcha_token, :captcha_answer_data]
1551 email: "lain@example.org",
1552 password: "PlzDontHackLain",
1554 captcha_solution: "xx",
1555 captcha_token: "xx",
1556 captcha_answer_data: "xx"
1559 for field <- captcha_fields do
1561 "error" => "{\"captcha\":[\"Invalid CAPTCHA (Missing parameter: #{field})\"]}"
1566 |> post("/api/v1/accounts", Map.delete(valid_params, field))
1567 |> json_response_and_validate_schema(:bad_request)
1571 test "returns an error if captcha is invalid", %{conn: conn} do
1574 email: "lain@example.org",
1575 password: "PlzDontHackLain",
1577 captcha_solution: "cofe",
1578 captcha_token: "cofe",
1579 captcha_answer_data: "cofe"
1582 assert %{"error" => "{\"captcha\":[\"Invalid answer data\"]}"} ==
1584 |> post("/api/v1/accounts", params)
1585 |> json_response_and_validate_schema(:bad_request)
1589 describe "GET /api/v1/accounts/:id/lists - account_lists" do
1590 test "returns lists to which the account belongs" do
1591 %{user: user, conn: conn} = oauth_access(["read:lists"])
1592 other_user = insert(:user)
1593 assert {:ok, %Pleroma.List{id: _list_id} = list} = Pleroma.List.create("Test List", user)
1594 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
1596 assert [%{"id" => _list_id, "title" => "Test List"}] =
1598 |> get("/api/v1/accounts/#{other_user.id}/lists")
1599 |> json_response_and_validate_schema(200)
1603 describe "verify_credentials" do
1604 test "verify_credentials" do
1605 %{user: user, conn: conn} = oauth_access(["read:accounts"])
1607 [notification | _] =
1608 insert_list(7, :notification, user: user, activity: insert(:note_activity))
1610 Pleroma.Notification.set_read_up_to(user, notification.id)
1611 conn = get(conn, "/api/v1/accounts/verify_credentials")
1613 response = json_response_and_validate_schema(conn, 200)
1615 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
1616 assert response["pleroma"]["chat_token"]
1617 assert response["pleroma"]["unread_notifications_count"] == 6
1618 assert id == to_string(user.id)
1621 test "verify_credentials default scope unlisted" do
1622 user = insert(:user, default_scope: "unlisted")
1623 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1625 conn = get(conn, "/api/v1/accounts/verify_credentials")
1627 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} =
1628 json_response_and_validate_schema(conn, 200)
1630 assert id == to_string(user.id)
1633 test "locked accounts" do
1634 user = insert(:user, default_scope: "private")
1635 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1637 conn = get(conn, "/api/v1/accounts/verify_credentials")
1639 assert %{"id" => id, "source" => %{"privacy" => "private"}} =
1640 json_response_and_validate_schema(conn, 200)
1642 assert id == to_string(user.id)
1646 describe "user relationships" do
1647 setup do: oauth_access(["read:follows"])
1649 test "returns the relationships for the current user", %{user: user, conn: conn} do
1650 %{id: other_user_id} = other_user = insert(:user)
1651 {:ok, _user, _other_user} = User.follow(user, other_user)
1653 assert [%{"id" => ^other_user_id}] =
1655 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
1656 |> json_response_and_validate_schema(200)
1658 assert [%{"id" => ^other_user_id}] =
1660 |> get("/api/v1/accounts/relationships?id[]=#{other_user.id}")
1661 |> json_response_and_validate_schema(200)
1664 test "returns an empty list on a bad request", %{conn: conn} do
1665 conn = get(conn, "/api/v1/accounts/relationships", %{})
1667 assert [] = json_response_and_validate_schema(conn, 200)
1671 test "getting a list of mutes" do
1672 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1673 %{id: id1} = other_user1 = insert(:user)
1674 %{id: id2} = other_user2 = insert(:user)
1675 %{id: id3} = other_user3 = insert(:user)
1677 {:ok, _user_relationships} = User.mute(user, other_user1)
1678 {:ok, _user_relationships} = User.mute(user, other_user2)
1679 {:ok, _user_relationships} = User.mute(user, other_user3)
1683 |> get("/api/v1/mutes")
1684 |> json_response_and_validate_schema(200)
1686 assert [id1, id2, id3] == Enum.map(result, & &1["id"])
1690 |> get("/api/v1/mutes?limit=1")
1691 |> json_response_and_validate_schema(200)
1693 assert [%{"id" => ^id1}] = result
1697 |> get("/api/v1/mutes?since_id=#{id1}")
1698 |> json_response_and_validate_schema(200)
1700 assert [%{"id" => ^id2}, %{"id" => ^id3}] = result
1704 |> get("/api/v1/mutes?since_id=#{id1}&max_id=#{id3}")
1705 |> json_response_and_validate_schema(200)
1707 assert [%{"id" => ^id2}] = result
1711 |> get("/api/v1/mutes?since_id=#{id1}&limit=1")
1712 |> json_response_and_validate_schema(200)
1714 assert [%{"id" => ^id2}] = result
1717 test "list of mutes with with_relationships parameter" do
1718 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1719 %{id: id1} = other_user1 = insert(:user)
1720 %{id: id2} = other_user2 = insert(:user)
1721 %{id: id3} = other_user3 = insert(:user)
1723 {:ok, _, _} = User.follow(other_user1, user)
1724 {:ok, _, _} = User.follow(other_user2, user)
1725 {:ok, _, _} = User.follow(other_user3, user)
1727 {:ok, _} = User.mute(user, other_user1)
1728 {:ok, _} = User.mute(user, other_user2)
1729 {:ok, _} = User.mute(user, other_user3)
1734 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1738 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1742 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1746 |> get("/api/v1/mutes?with_relationships=true")
1747 |> json_response_and_validate_schema(200)
1750 test "getting a list of blocks" do
1751 %{user: user, conn: conn} = oauth_access(["read:blocks"])
1752 %{id: id1} = other_user1 = insert(:user)
1753 %{id: id2} = other_user2 = insert(:user)
1754 %{id: id3} = other_user3 = insert(:user)
1756 {:ok, _user_relationship} = User.block(user, other_user1)
1757 {:ok, _user_relationship} = User.block(user, other_user3)
1758 {:ok, _user_relationship} = User.block(user, other_user2)
1762 |> assign(:user, user)
1763 |> get("/api/v1/blocks")
1764 |> json_response_and_validate_schema(200)
1766 assert [id1, id2, id3] == Enum.map(result, & &1["id"])
1770 |> assign(:user, user)
1771 |> get("/api/v1/blocks?limit=1")
1772 |> json_response_and_validate_schema(200)
1774 assert [%{"id" => ^id1}] = result
1778 |> assign(:user, user)
1779 |> get("/api/v1/blocks?since_id=#{id1}")
1780 |> json_response_and_validate_schema(200)
1782 assert [%{"id" => ^id2}, %{"id" => ^id3}] = result
1786 |> assign(:user, user)
1787 |> get("/api/v1/blocks?since_id=#{id1}&max_id=#{id3}")
1788 |> json_response_and_validate_schema(200)
1790 assert [%{"id" => ^id2}] = result
1794 |> assign(:user, user)
1795 |> get("/api/v1/blocks?since_id=#{id1}&limit=1")
1796 |> json_response_and_validate_schema(200)
1798 assert [%{"id" => ^id2}] = result
1801 test "account lookup", %{conn: conn} do
1802 %{nickname: acct} = insert(:user, %{nickname: "nickname"})
1803 %{nickname: acct_two} = insert(:user, %{nickname: "nickname@notlocaldoma.in"})
1807 |> get("/api/v1/accounts/lookup?acct=#{acct}")
1808 |> json_response_and_validate_schema(200)
1810 assert %{"acct" => ^acct} = result
1814 |> get("/api/v1/accounts/lookup?acct=#{acct_two}")
1815 |> json_response_and_validate_schema(200)
1817 assert %{"acct" => ^acct_two} = result
1821 |> get("/api/v1/accounts/lookup?acct=unexisting_nickname")
1822 |> json_response_and_validate_schema(404)
1825 test "create a note on a user" do
1826 %{conn: conn} = oauth_access(["write:accounts", "read:follows"])
1827 other_user = insert(:user)
1830 |> put_req_header("content-type", "application/json")
1831 |> post("/api/v1/accounts/#{other_user.id}/note", %{
1832 "comment" => "Example note"
1835 assert [%{"note" => "Example note"}] =
1837 |> put_req_header("content-type", "application/json")
1838 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
1839 |> json_response_and_validate_schema(200)