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=#{
716 |> json_response_and_validate_schema(200)
718 res_conn = get(conn, "/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3_id}")
720 assert [%{"id" => ^follower2_id}] = json_response_and_validate_schema(res_conn, 200)
722 assert [link_header] = get_resp_header(res_conn, "link")
723 assert link_header =~ ~r/min_id=#{follower2_id}/
724 assert link_header =~ ~r/max_id=#{follower2_id}/
728 describe "following" do
729 setup do: oauth_access(["read:accounts"])
731 test "getting following", %{user: user, conn: conn} do
732 other_user = insert(:user)
733 {:ok, user, other_user} = User.follow(user, other_user)
735 conn = get(conn, "/api/v1/accounts/#{user.id}/following")
737 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
738 assert id == to_string(other_user.id)
741 test "following with relationship", %{conn: conn, user: user} do
742 other_user = insert(:user)
743 {:ok, user, other_user} = User.follow(user, other_user)
745 conn = get(conn, "/api/v1/accounts/#{user.id}/following?with_relationships=true")
753 "relationship" => %{"id" => ^id, "following" => true, "followed_by" => false}
756 ] = json_response_and_validate_schema(conn, 200)
759 test "getting following, hide_follows, other user requesting" do
760 user = insert(:user, hide_follows: true)
761 other_user = insert(:user)
762 {:ok, user, other_user} = User.follow(user, other_user)
766 |> assign(:user, other_user)
767 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
768 |> get("/api/v1/accounts/#{user.id}/following")
770 assert [] == json_response_and_validate_schema(conn, 200)
773 test "getting following, hide_follows, same user requesting" do
774 user = insert(:user, hide_follows: true)
775 other_user = insert(:user)
776 {:ok, user, _other_user} = User.follow(user, other_user)
780 |> assign(:user, user)
781 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read:accounts"]))
782 |> get("/api/v1/accounts/#{user.id}/following")
784 refute [] == json_response_and_validate_schema(conn, 200)
787 test "getting following, pagination", %{user: user, conn: conn} do
788 following1 = insert(:user)
789 following2 = insert(:user)
790 following3 = insert(:user)
791 {:ok, _, _} = User.follow(user, following1)
792 {:ok, _, _} = User.follow(user, following2)
793 {:ok, _, _} = User.follow(user, following3)
795 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
797 assert [%{"id" => id3}, %{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
798 assert id3 == following3.id
799 assert id2 == following2.id
801 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
803 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
804 assert id2 == following2.id
805 assert id1 == following1.id
810 "/api/v1/accounts/#{user.id}/following?id=#{user.id}&limit=20&max_id=#{following3.id}"
813 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
814 assert id2 == following2.id
815 assert id1 == following1.id
818 get(conn, "/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
820 assert [%{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
821 assert id2 == following2.id
823 assert [link_header] = get_resp_header(res_conn, "link")
824 assert link_header =~ ~r/min_id=#{following2.id}/
825 assert link_header =~ ~r/max_id=#{following2.id}/
829 describe "follow/unfollow" do
830 setup do: oauth_access(["follow"])
832 test "following / unfollowing a user", %{conn: conn} do
833 %{id: other_user_id, nickname: other_user_nickname} = insert(:user)
835 assert %{"id" => _id, "following" => true} =
837 |> post("/api/v1/accounts/#{other_user_id}/follow")
838 |> json_response_and_validate_schema(200)
840 assert %{"id" => _id, "following" => false} =
842 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
843 |> json_response_and_validate_schema(200)
845 assert %{"id" => ^other_user_id} =
847 |> put_req_header("content-type", "application/json")
848 |> post("/api/v1/follows", %{"uri" => other_user_nickname})
849 |> json_response_and_validate_schema(200)
852 test "cancelling follow request", %{conn: conn} do
853 %{id: other_user_id} = insert(:user, %{is_locked: true})
855 assert %{"id" => ^other_user_id, "following" => false, "requested" => true} =
857 |> post("/api/v1/accounts/#{other_user_id}/follow")
858 |> json_response_and_validate_schema(:ok)
860 assert %{"id" => ^other_user_id, "following" => false, "requested" => false} =
862 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
863 |> json_response_and_validate_schema(:ok)
866 test "following without reblogs" do
867 %{conn: conn} = oauth_access(["follow", "read:statuses"])
868 followed = insert(:user)
869 other_user = insert(:user)
873 |> put_req_header("content-type", "application/json")
874 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
876 assert %{"showing_reblogs" => false} = json_response_and_validate_schema(ret_conn, 200)
878 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
879 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
883 |> get("/api/v1/timelines/home")
884 |> json_response_and_validate_schema(200)
886 assert %{"showing_reblogs" => true} =
888 |> put_req_header("content-type", "application/json")
889 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: true})
890 |> json_response_and_validate_schema(200)
892 assert [%{"id" => ^reblog_id}] =
894 |> get("/api/v1/timelines/home")
895 |> json_response_and_validate_schema(200)
898 test "following with reblogs" do
899 %{conn: conn} = oauth_access(["follow", "read:statuses"])
900 followed = insert(:user)
901 other_user = insert(:user)
903 ret_conn = post(conn, "/api/v1/accounts/#{followed.id}/follow")
905 assert %{"showing_reblogs" => true} = json_response_and_validate_schema(ret_conn, 200)
907 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
908 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
910 assert [%{"id" => ^reblog_id}] =
912 |> get("/api/v1/timelines/home")
913 |> json_response_and_validate_schema(200)
915 assert %{"showing_reblogs" => false} =
917 |> put_req_header("content-type", "application/json")
918 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
919 |> json_response_and_validate_schema(200)
923 |> get("/api/v1/timelines/home")
924 |> json_response_and_validate_schema(200)
927 test "following / unfollowing errors", %{user: user, conn: conn} do
929 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
931 assert %{"error" => "Can not follow yourself"} =
932 json_response_and_validate_schema(conn_res, 400)
935 user = User.get_cached_by_id(user.id)
936 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
938 assert %{"error" => "Can not unfollow yourself"} =
939 json_response_and_validate_schema(conn_res, 400)
941 # self follow via uri
942 user = User.get_cached_by_id(user.id)
944 assert %{"error" => "Can not follow yourself"} =
946 |> put_req_header("content-type", "multipart/form-data")
947 |> post("/api/v1/follows", %{"uri" => user.nickname})
948 |> json_response_and_validate_schema(400)
950 # follow non existing user
951 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
952 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
954 # follow non existing user via uri
957 |> put_req_header("content-type", "multipart/form-data")
958 |> post("/api/v1/follows", %{"uri" => "doesntexist"})
960 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
962 # unfollow non existing user
963 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
964 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
968 describe "mute/unmute" do
969 setup do: oauth_access(["write:mutes"])
971 test "with notifications", %{conn: conn} do
972 other_user = insert(:user)
974 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} =
976 |> post("/api/v1/accounts/#{other_user.id}/mute")
977 |> json_response_and_validate_schema(200)
979 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
981 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
982 json_response_and_validate_schema(conn, 200)
985 test "without notifications", %{conn: conn} do
986 other_user = insert(:user)
990 |> put_req_header("content-type", "multipart/form-data")
991 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
993 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} =
994 json_response_and_validate_schema(ret_conn, 200)
996 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
998 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
999 json_response_and_validate_schema(conn, 200)
1003 describe "pinned statuses" do
1005 user = insert(:user)
1006 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1007 %{conn: conn} = oauth_access(["read:statuses"], user: user)
1009 [conn: conn, user: user, activity: activity]
1012 test "returns pinned statuses", %{conn: conn, user: user, activity: %{id: activity_id}} do
1013 {:ok, _} = CommonAPI.pin(activity_id, user)
1015 assert [%{"id" => ^activity_id, "pinned" => true}] =
1017 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1018 |> json_response_and_validate_schema(200)
1022 test "blocking / unblocking a user" do
1023 %{conn: conn} = oauth_access(["follow"])
1024 other_user = insert(:user)
1026 ret_conn = post(conn, "/api/v1/accounts/#{other_user.id}/block")
1028 assert %{"id" => _id, "blocking" => true} = json_response_and_validate_schema(ret_conn, 200)
1030 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unblock")
1032 assert %{"id" => _id, "blocking" => false} = json_response_and_validate_schema(conn, 200)
1035 describe "create account by app" do
1039 email: "lain@example.org",
1040 password: "PlzDontHackLain",
1044 [valid_params: valid_params]
1047 test "registers and logs in without :account_activation_required / :account_approval_required",
1049 clear_config([:instance, :account_activation_required], false)
1050 clear_config([:instance, :account_approval_required], false)
1054 |> put_req_header("content-type", "application/json")
1055 |> post("/api/v1/apps", %{
1056 client_name: "client_name",
1057 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1058 scopes: "read, write, follow"
1062 "client_id" => client_id,
1063 "client_secret" => client_secret,
1065 "name" => "client_name",
1066 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1069 } = json_response_and_validate_schema(conn, 200)
1072 post(conn, "/oauth/token", %{
1073 grant_type: "client_credentials",
1074 client_id: client_id,
1075 client_secret: client_secret
1078 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1079 json_response(conn, 200)
1082 token_from_db = Repo.get_by(Token, token: token)
1083 assert token_from_db
1085 assert scope == "read write follow"
1087 clear_config([User, :email_blacklist], ["example.org"])
1091 email: "lain@example.org",
1092 password: "PlzDontHackLain",
1099 |> put_req_header("content-type", "multipart/form-data")
1100 |> put_req_header("authorization", "Bearer " <> token)
1101 |> post("/api/v1/accounts", params)
1103 assert %{"error" => "{\"email\":[\"Invalid email\"]}"} =
1104 json_response_and_validate_schema(conn, 400)
1106 clear_config([User, :email_blacklist], [])
1110 |> put_req_header("content-type", "multipart/form-data")
1111 |> put_req_header("authorization", "Bearer " <> token)
1112 |> post("/api/v1/accounts", params)
1115 "access_token" => token,
1116 "created_at" => _created_at,
1118 "token_type" => "Bearer"
1119 } = json_response_and_validate_schema(conn, 200)
1121 token_from_db = Repo.get_by(Token, token: token)
1122 assert token_from_db
1123 user = Repo.preload(token_from_db, :user).user
1126 assert user.is_confirmed
1127 assert user.is_approved
1130 test "registers but does not log in with :account_activation_required", %{conn: conn} do
1131 clear_config([:instance, :account_activation_required], true)
1132 clear_config([:instance, :account_approval_required], false)
1136 |> put_req_header("content-type", "application/json")
1137 |> post("/api/v1/apps", %{
1138 client_name: "client_name",
1139 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1140 scopes: "read, write, follow"
1144 "client_id" => client_id,
1145 "client_secret" => client_secret,
1147 "name" => "client_name",
1148 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1151 } = json_response_and_validate_schema(conn, 200)
1154 post(conn, "/oauth/token", %{
1155 grant_type: "client_credentials",
1156 client_id: client_id,
1157 client_secret: client_secret
1160 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1161 json_response(conn, 200)
1164 token_from_db = Repo.get_by(Token, token: token)
1165 assert token_from_db
1167 assert scope == "read write follow"
1171 |> put_req_header("content-type", "multipart/form-data")
1172 |> put_req_header("authorization", "Bearer " <> token)
1173 |> post("/api/v1/accounts", %{
1175 email: "lain@example.org",
1176 password: "PlzDontHackLain",
1181 response = json_response_and_validate_schema(conn, 200)
1182 assert %{"identifier" => "missing_confirmed_email"} = response
1183 refute response["access_token"]
1184 refute response["token_type"]
1186 user = Repo.get_by(User, email: "lain@example.org")
1187 refute user.is_confirmed
1190 test "registers but does not log in with :account_approval_required", %{conn: conn} do
1191 clear_config([:instance, :account_approval_required], true)
1192 clear_config([:instance, :account_activation_required], false)
1196 |> put_req_header("content-type", "application/json")
1197 |> post("/api/v1/apps", %{
1198 client_name: "client_name",
1199 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1200 scopes: "read, write, follow"
1204 "client_id" => client_id,
1205 "client_secret" => client_secret,
1207 "name" => "client_name",
1208 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1211 } = json_response_and_validate_schema(conn, 200)
1214 post(conn, "/oauth/token", %{
1215 grant_type: "client_credentials",
1216 client_id: client_id,
1217 client_secret: client_secret
1220 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1221 json_response(conn, 200)
1224 token_from_db = Repo.get_by(Token, token: token)
1225 assert token_from_db
1227 assert scope == "read write follow"
1231 |> put_req_header("content-type", "multipart/form-data")
1232 |> put_req_header("authorization", "Bearer " <> token)
1233 |> post("/api/v1/accounts", %{
1235 email: "lain@example.org",
1236 password: "PlzDontHackLain",
1239 reason: "I'm a cool dude, bro"
1242 response = json_response_and_validate_schema(conn, 200)
1243 assert %{"identifier" => "awaiting_approval"} = response
1244 refute response["access_token"]
1245 refute response["token_type"]
1247 user = Repo.get_by(User, email: "lain@example.org")
1249 refute user.is_approved
1250 assert user.registration_reason == "I'm a cool dude, bro"
1253 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
1254 _user = insert(:user, email: "lain@example.org")
1255 app_token = insert(:oauth_token, user: nil)
1259 |> put_req_header("authorization", "Bearer " <> app_token.token)
1260 |> put_req_header("content-type", "application/json")
1261 |> post("/api/v1/accounts", valid_params)
1263 assert json_response_and_validate_schema(res, 400) == %{
1264 "error" => "{\"email\":[\"has already been taken\"]}"
1268 test "returns bad_request if missing required params", %{
1270 valid_params: valid_params
1272 app_token = insert(:oauth_token, user: nil)
1276 |> put_req_header("authorization", "Bearer " <> app_token.token)
1277 |> put_req_header("content-type", "application/json")
1279 res = post(conn, "/api/v1/accounts", valid_params)
1280 assert json_response_and_validate_schema(res, 200)
1282 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
1283 |> Stream.zip(Map.delete(valid_params, :email))
1284 |> Enum.each(fn {ip, {attr, _}} ->
1287 |> Map.put(:remote_ip, ip)
1288 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
1289 |> json_response_and_validate_schema(400)
1292 "error" => "Missing field: #{attr}.",
1295 "message" => "Missing field: #{attr}",
1296 "source" => %{"pointer" => "/#{attr}"},
1297 "title" => "Invalid value"
1304 test "returns bad_request if missing email params when :account_activation_required is enabled",
1305 %{conn: conn, valid_params: valid_params} do
1306 clear_config([:instance, :account_activation_required], true)
1308 app_token = insert(:oauth_token, user: nil)
1312 |> put_req_header("authorization", "Bearer " <> app_token.token)
1313 |> put_req_header("content-type", "application/json")
1317 |> Map.put(:remote_ip, {127, 0, 0, 5})
1318 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1320 assert json_response_and_validate_schema(res, 400) ==
1321 %{"error" => "Missing parameter: email"}
1325 |> Map.put(:remote_ip, {127, 0, 0, 6})
1326 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1328 assert json_response_and_validate_schema(res, 400) == %{
1329 "error" => "{\"email\":[\"can't be blank\"]}"
1333 test "allow registration without an email", %{conn: conn, valid_params: valid_params} do
1334 app_token = insert(:oauth_token, user: nil)
1335 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1339 |> put_req_header("content-type", "application/json")
1340 |> Map.put(:remote_ip, {127, 0, 0, 7})
1341 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1343 assert json_response_and_validate_schema(res, 200)
1346 test "allow registration with an empty email", %{conn: conn, valid_params: valid_params} do
1347 app_token = insert(:oauth_token, user: nil)
1348 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1352 |> put_req_header("content-type", "application/json")
1353 |> Map.put(:remote_ip, {127, 0, 0, 8})
1354 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1356 assert json_response_and_validate_schema(res, 200)
1359 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
1362 |> put_req_header("authorization", "Bearer " <> "invalid-token")
1363 |> put_req_header("content-type", "multipart/form-data")
1364 |> post("/api/v1/accounts", valid_params)
1366 assert json_response_and_validate_schema(res, 403) == %{"error" => "Invalid credentials"}
1369 test "registration from trusted app" do
1370 clear_config([Pleroma.Captcha, :enabled], true)
1371 app = insert(:oauth_app, trusted: true, scopes: ["read", "write", "follow", "push"])
1375 |> post("/oauth/token", %{
1376 "grant_type" => "client_credentials",
1377 "client_id" => app.client_id,
1378 "client_secret" => app.client_secret
1381 assert %{"access_token" => token, "token_type" => "Bearer"} = json_response(conn, 200)
1385 |> Plug.Conn.put_req_header("authorization", "Bearer " <> token)
1386 |> put_req_header("content-type", "multipart/form-data")
1387 |> post("/api/v1/accounts", %{
1388 nickname: "nickanme",
1390 email: "email@example.com",
1393 password: "some_password",
1394 confirm: "some_password"
1396 |> json_response_and_validate_schema(200)
1399 "access_token" => access_token,
1401 "scope" => "read write follow push",
1402 "token_type" => "Bearer"
1407 |> Plug.Conn.put_req_header("authorization", "Bearer " <> access_token)
1408 |> get("/api/v1/accounts/verify_credentials")
1409 |> json_response_and_validate_schema(200)
1414 "display_name" => "Lain",
1415 "follow_requests_count" => 0,
1416 "followers_count" => 0,
1417 "following_count" => 0,
1424 "actor_type" => "Person",
1425 "discoverable" => false,
1426 "no_rich_text" => false,
1429 "privacy" => "public",
1430 "sensitive" => false
1432 "statuses_count" => 0,
1433 "username" => "Lain"
1438 describe "create account by app / rate limit" do
1439 setup do: clear_config([:rate_limit, :app_account_creation], {10_000, 2})
1441 test "respects rate limit setting", %{conn: conn} do
1442 app_token = insert(:oauth_token, user: nil)
1446 |> put_req_header("authorization", "Bearer " <> app_token.token)
1447 |> Map.put(:remote_ip, {15, 15, 15, 15})
1448 |> put_req_header("content-type", "multipart/form-data")
1453 |> post("/api/v1/accounts", %{
1454 username: "#{i}lain",
1455 email: "#{i}lain@example.org",
1456 password: "PlzDontHackLain",
1461 "access_token" => token,
1462 "created_at" => _created_at,
1464 "token_type" => "Bearer"
1465 } = json_response_and_validate_schema(conn, 200)
1467 token_from_db = Repo.get_by(Token, token: token)
1468 assert token_from_db
1469 token_from_db = Repo.preload(token_from_db, :user)
1470 assert token_from_db.user
1474 post(conn, "/api/v1/accounts", %{
1476 email: "6lain@example.org",
1477 password: "PlzDontHackLain",
1481 assert json_response_and_validate_schema(conn, :too_many_requests) == %{
1482 "error" => "Throttled"
1487 describe "create account with enabled captcha" do
1488 setup %{conn: conn} do
1489 app_token = insert(:oauth_token, user: nil)
1493 |> put_req_header("authorization", "Bearer " <> app_token.token)
1494 |> put_req_header("content-type", "multipart/form-data")
1499 setup do: clear_config([Pleroma.Captcha, :enabled], true)
1501 test "creates an account and returns 200 if captcha is valid", %{conn: conn} do
1502 %{token: token, answer_data: answer_data} = Pleroma.Captcha.new()
1506 email: "lain@example.org",
1507 password: "PlzDontHackLain",
1509 captcha_solution: Pleroma.Captcha.Mock.solution(),
1510 captcha_token: token,
1511 captcha_answer_data: answer_data
1515 "access_token" => access_token,
1518 "token_type" => "Bearer"
1521 |> post("/api/v1/accounts", params)
1522 |> json_response_and_validate_schema(:ok)
1524 assert Token |> Repo.get_by(token: access_token) |> Repo.preload(:user) |> Map.get(:user)
1527 test "returns 400 if any captcha field is not provided", %{conn: conn} do
1528 captcha_fields = [:captcha_solution, :captcha_token, :captcha_answer_data]
1532 email: "lain@example.org",
1533 password: "PlzDontHackLain",
1535 captcha_solution: "xx",
1536 captcha_token: "xx",
1537 captcha_answer_data: "xx"
1540 for field <- captcha_fields do
1542 "error" => "{\"captcha\":[\"Invalid CAPTCHA (Missing parameter: #{field})\"]}"
1547 |> post("/api/v1/accounts", Map.delete(valid_params, field))
1548 |> json_response_and_validate_schema(:bad_request)
1552 test "returns an error if captcha is invalid", %{conn: conn} do
1555 email: "lain@example.org",
1556 password: "PlzDontHackLain",
1558 captcha_solution: "cofe",
1559 captcha_token: "cofe",
1560 captcha_answer_data: "cofe"
1563 assert %{"error" => "{\"captcha\":[\"Invalid answer data\"]}"} ==
1565 |> post("/api/v1/accounts", params)
1566 |> json_response_and_validate_schema(:bad_request)
1570 describe "GET /api/v1/accounts/:id/lists - account_lists" do
1571 test "returns lists to which the account belongs" do
1572 %{user: user, conn: conn} = oauth_access(["read:lists"])
1573 other_user = insert(:user)
1574 assert {:ok, %Pleroma.List{id: _list_id} = list} = Pleroma.List.create("Test List", user)
1575 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
1577 assert [%{"id" => _list_id, "title" => "Test List"}] =
1579 |> get("/api/v1/accounts/#{other_user.id}/lists")
1580 |> json_response_and_validate_schema(200)
1584 describe "verify_credentials" do
1585 test "verify_credentials" do
1586 %{user: user, conn: conn} = oauth_access(["read:accounts"])
1588 [notification | _] =
1589 insert_list(7, :notification, user: user, activity: insert(:note_activity))
1591 Pleroma.Notification.set_read_up_to(user, notification.id)
1592 conn = get(conn, "/api/v1/accounts/verify_credentials")
1594 response = json_response_and_validate_schema(conn, 200)
1596 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
1597 assert response["pleroma"]["chat_token"]
1598 assert response["pleroma"]["unread_notifications_count"] == 6
1599 assert id == to_string(user.id)
1602 test "verify_credentials default scope unlisted" do
1603 user = insert(:user, default_scope: "unlisted")
1604 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1606 conn = get(conn, "/api/v1/accounts/verify_credentials")
1608 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} =
1609 json_response_and_validate_schema(conn, 200)
1611 assert id == to_string(user.id)
1614 test "locked accounts" do
1615 user = insert(:user, default_scope: "private")
1616 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1618 conn = get(conn, "/api/v1/accounts/verify_credentials")
1620 assert %{"id" => id, "source" => %{"privacy" => "private"}} =
1621 json_response_and_validate_schema(conn, 200)
1623 assert id == to_string(user.id)
1627 describe "user relationships" do
1628 setup do: oauth_access(["read:follows"])
1630 test "returns the relationships for the current user", %{user: user, conn: conn} do
1631 %{id: other_user_id} = other_user = insert(:user)
1632 {:ok, _user, _other_user} = User.follow(user, other_user)
1634 assert [%{"id" => ^other_user_id}] =
1636 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
1637 |> json_response_and_validate_schema(200)
1639 assert [%{"id" => ^other_user_id}] =
1641 |> get("/api/v1/accounts/relationships?id[]=#{other_user.id}")
1642 |> json_response_and_validate_schema(200)
1645 test "returns an empty list on a bad request", %{conn: conn} do
1646 conn = get(conn, "/api/v1/accounts/relationships", %{})
1648 assert [] = json_response_and_validate_schema(conn, 200)
1652 test "getting a list of mutes" do
1653 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1654 %{id: id1} = other_user1 = insert(:user)
1655 %{id: id2} = other_user2 = insert(:user)
1656 %{id: id3} = other_user3 = insert(:user)
1658 {:ok, _user_relationships} = User.mute(user, other_user1)
1659 {:ok, _user_relationships} = User.mute(user, other_user2)
1660 {:ok, _user_relationships} = User.mute(user, other_user3)
1664 |> get("/api/v1/mutes")
1665 |> json_response_and_validate_schema(200)
1667 assert [id1, id2, id3] == Enum.map(result, & &1["id"])
1671 |> get("/api/v1/mutes?limit=1")
1672 |> json_response_and_validate_schema(200)
1674 assert [%{"id" => ^id1}] = result
1678 |> get("/api/v1/mutes?since_id=#{id1}")
1679 |> json_response_and_validate_schema(200)
1681 assert [%{"id" => ^id2}, %{"id" => ^id3}] = result
1685 |> get("/api/v1/mutes?since_id=#{id1}&max_id=#{id3}")
1686 |> json_response_and_validate_schema(200)
1688 assert [%{"id" => ^id2}] = result
1692 |> get("/api/v1/mutes?since_id=#{id1}&limit=1")
1693 |> json_response_and_validate_schema(200)
1695 assert [%{"id" => ^id2}] = result
1698 test "list of mutes with with_relationships parameter" do
1699 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1700 %{id: id1} = other_user1 = insert(:user)
1701 %{id: id2} = other_user2 = insert(:user)
1702 %{id: id3} = other_user3 = insert(:user)
1704 {:ok, _, _} = User.follow(other_user1, user)
1705 {:ok, _, _} = User.follow(other_user2, user)
1706 {:ok, _, _} = User.follow(other_user3, user)
1708 {:ok, _} = User.mute(user, other_user1)
1709 {:ok, _} = User.mute(user, other_user2)
1710 {:ok, _} = User.mute(user, other_user3)
1715 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1719 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1723 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1727 |> get("/api/v1/mutes?with_relationships=true")
1728 |> json_response_and_validate_schema(200)
1731 test "getting a list of blocks" do
1732 %{user: user, conn: conn} = oauth_access(["read:blocks"])
1733 %{id: id1} = other_user1 = insert(:user)
1734 %{id: id2} = other_user2 = insert(:user)
1735 %{id: id3} = other_user3 = insert(:user)
1737 {:ok, _user_relationship} = User.block(user, other_user1)
1738 {:ok, _user_relationship} = User.block(user, other_user3)
1739 {:ok, _user_relationship} = User.block(user, other_user2)
1743 |> assign(:user, user)
1744 |> get("/api/v1/blocks")
1745 |> json_response_and_validate_schema(200)
1747 assert [id1, id2, id3] == Enum.map(result, & &1["id"])
1751 |> assign(:user, user)
1752 |> get("/api/v1/blocks?limit=1")
1753 |> json_response_and_validate_schema(200)
1755 assert [%{"id" => ^id1}] = result
1759 |> assign(:user, user)
1760 |> get("/api/v1/blocks?since_id=#{id1}")
1761 |> json_response_and_validate_schema(200)
1763 assert [%{"id" => ^id2}, %{"id" => ^id3}] = result
1767 |> assign(:user, user)
1768 |> get("/api/v1/blocks?since_id=#{id1}&max_id=#{id3}")
1769 |> json_response_and_validate_schema(200)
1771 assert [%{"id" => ^id2}] = result
1775 |> assign(:user, user)
1776 |> get("/api/v1/blocks?since_id=#{id1}&limit=1")
1777 |> json_response_and_validate_schema(200)
1779 assert [%{"id" => ^id2}] = result