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 / unfollowing errors", %{user: user, conn: conn} do
927 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
929 assert %{"error" => "Can not follow yourself"} =
930 json_response_and_validate_schema(conn_res, 400)
933 user = User.get_cached_by_id(user.id)
934 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
936 assert %{"error" => "Can not unfollow yourself"} =
937 json_response_and_validate_schema(conn_res, 400)
939 # self follow via uri
940 user = User.get_cached_by_id(user.id)
942 assert %{"error" => "Can not follow yourself"} =
944 |> put_req_header("content-type", "multipart/form-data")
945 |> post("/api/v1/follows", %{"uri" => user.nickname})
946 |> json_response_and_validate_schema(400)
948 # follow non existing user
949 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
950 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
952 # follow non existing user via uri
955 |> put_req_header("content-type", "multipart/form-data")
956 |> post("/api/v1/follows", %{"uri" => "doesntexist"})
958 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
960 # unfollow non existing user
961 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
962 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
966 describe "mute/unmute" do
967 setup do: oauth_access(["write:mutes"])
969 test "with notifications", %{conn: conn} do
970 other_user = insert(:user)
972 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} =
974 |> post("/api/v1/accounts/#{other_user.id}/mute")
975 |> json_response_and_validate_schema(200)
977 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
979 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
980 json_response_and_validate_schema(conn, 200)
983 test "without notifications", %{conn: conn} do
984 other_user = insert(:user)
988 |> put_req_header("content-type", "multipart/form-data")
989 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
991 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} =
992 json_response_and_validate_schema(ret_conn, 200)
994 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
996 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
997 json_response_and_validate_schema(conn, 200)
1001 describe "pinned statuses" do
1003 user = insert(:user)
1004 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1005 %{conn: conn} = oauth_access(["read:statuses"], user: user)
1007 [conn: conn, user: user, activity: activity]
1010 test "returns pinned statuses", %{conn: conn, user: user, activity: %{id: activity_id}} do
1011 {:ok, _} = CommonAPI.pin(activity_id, user)
1013 assert [%{"id" => ^activity_id, "pinned" => true}] =
1015 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1016 |> json_response_and_validate_schema(200)
1020 test "blocking / unblocking a user" do
1021 %{conn: conn} = oauth_access(["follow"])
1022 other_user = insert(:user)
1024 ret_conn = post(conn, "/api/v1/accounts/#{other_user.id}/block")
1026 assert %{"id" => _id, "blocking" => true} = json_response_and_validate_schema(ret_conn, 200)
1028 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unblock")
1030 assert %{"id" => _id, "blocking" => false} = json_response_and_validate_schema(conn, 200)
1033 describe "create account by app" do
1037 email: "lain@example.org",
1038 password: "PlzDontHackLain",
1042 [valid_params: valid_params]
1045 test "registers and logs in without :account_activation_required / :account_approval_required",
1047 clear_config([:instance, :account_activation_required], false)
1048 clear_config([:instance, :account_approval_required], false)
1052 |> put_req_header("content-type", "application/json")
1053 |> post("/api/v1/apps", %{
1054 client_name: "client_name",
1055 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1056 scopes: "read, write, follow"
1060 "client_id" => client_id,
1061 "client_secret" => client_secret,
1063 "name" => "client_name",
1064 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1067 } = json_response_and_validate_schema(conn, 200)
1070 post(conn, "/oauth/token", %{
1071 grant_type: "client_credentials",
1072 client_id: client_id,
1073 client_secret: client_secret
1076 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1077 json_response(conn, 200)
1080 token_from_db = Repo.get_by(Token, token: token)
1081 assert token_from_db
1083 assert scope == "read write follow"
1085 clear_config([User, :email_blacklist], ["example.org"])
1089 email: "lain@example.org",
1090 password: "PlzDontHackLain",
1097 |> put_req_header("content-type", "multipart/form-data")
1098 |> put_req_header("authorization", "Bearer " <> token)
1099 |> post("/api/v1/accounts", params)
1101 assert %{"error" => "{\"email\":[\"Invalid email\"]}"} =
1102 json_response_and_validate_schema(conn, 400)
1104 clear_config([User, :email_blacklist], [])
1108 |> put_req_header("content-type", "multipart/form-data")
1109 |> put_req_header("authorization", "Bearer " <> token)
1110 |> post("/api/v1/accounts", params)
1113 "access_token" => token,
1114 "created_at" => _created_at,
1116 "token_type" => "Bearer"
1117 } = json_response_and_validate_schema(conn, 200)
1119 token_from_db = Repo.get_by(Token, token: token)
1120 assert token_from_db
1121 user = Repo.preload(token_from_db, :user).user
1124 assert user.is_confirmed
1125 assert user.is_approved
1128 test "registers but does not log in with :account_activation_required", %{conn: conn} do
1129 clear_config([:instance, :account_activation_required], true)
1130 clear_config([:instance, :account_approval_required], false)
1134 |> put_req_header("content-type", "application/json")
1135 |> post("/api/v1/apps", %{
1136 client_name: "client_name",
1137 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1138 scopes: "read, write, follow"
1142 "client_id" => client_id,
1143 "client_secret" => client_secret,
1145 "name" => "client_name",
1146 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1149 } = json_response_and_validate_schema(conn, 200)
1152 post(conn, "/oauth/token", %{
1153 grant_type: "client_credentials",
1154 client_id: client_id,
1155 client_secret: client_secret
1158 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1159 json_response(conn, 200)
1162 token_from_db = Repo.get_by(Token, token: token)
1163 assert token_from_db
1165 assert scope == "read write follow"
1169 |> put_req_header("content-type", "multipart/form-data")
1170 |> put_req_header("authorization", "Bearer " <> token)
1171 |> post("/api/v1/accounts", %{
1173 email: "lain@example.org",
1174 password: "PlzDontHackLain",
1179 response = json_response_and_validate_schema(conn, 200)
1180 assert %{"identifier" => "missing_confirmed_email"} = response
1181 refute response["access_token"]
1182 refute response["token_type"]
1184 user = Repo.get_by(User, email: "lain@example.org")
1185 refute user.is_confirmed
1188 test "registers but does not log in with :account_approval_required", %{conn: conn} do
1189 clear_config([:instance, :account_approval_required], true)
1190 clear_config([:instance, :account_activation_required], false)
1194 |> put_req_header("content-type", "application/json")
1195 |> post("/api/v1/apps", %{
1196 client_name: "client_name",
1197 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1198 scopes: "read, write, follow"
1202 "client_id" => client_id,
1203 "client_secret" => client_secret,
1205 "name" => "client_name",
1206 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1209 } = json_response_and_validate_schema(conn, 200)
1212 post(conn, "/oauth/token", %{
1213 grant_type: "client_credentials",
1214 client_id: client_id,
1215 client_secret: client_secret
1218 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1219 json_response(conn, 200)
1222 token_from_db = Repo.get_by(Token, token: token)
1223 assert token_from_db
1225 assert scope == "read write follow"
1229 |> put_req_header("content-type", "multipart/form-data")
1230 |> put_req_header("authorization", "Bearer " <> token)
1231 |> post("/api/v1/accounts", %{
1233 email: "lain@example.org",
1234 password: "PlzDontHackLain",
1237 reason: "I'm a cool dude, bro"
1240 response = json_response_and_validate_schema(conn, 200)
1241 assert %{"identifier" => "awaiting_approval"} = response
1242 refute response["access_token"]
1243 refute response["token_type"]
1245 user = Repo.get_by(User, email: "lain@example.org")
1247 refute user.is_approved
1248 assert user.registration_reason == "I'm a cool dude, bro"
1251 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
1252 _user = insert(:user, email: "lain@example.org")
1253 app_token = insert(:oauth_token, user: nil)
1257 |> put_req_header("authorization", "Bearer " <> app_token.token)
1258 |> put_req_header("content-type", "application/json")
1259 |> post("/api/v1/accounts", valid_params)
1261 assert json_response_and_validate_schema(res, 400) == %{
1262 "error" => "{\"email\":[\"has already been taken\"]}"
1266 test "returns bad_request if missing required params", %{
1268 valid_params: valid_params
1270 app_token = insert(:oauth_token, user: nil)
1274 |> put_req_header("authorization", "Bearer " <> app_token.token)
1275 |> put_req_header("content-type", "application/json")
1277 res = post(conn, "/api/v1/accounts", valid_params)
1278 assert json_response_and_validate_schema(res, 200)
1280 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
1281 |> Stream.zip(Map.delete(valid_params, :email))
1282 |> Enum.each(fn {ip, {attr, _}} ->
1285 |> Map.put(:remote_ip, ip)
1286 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
1287 |> json_response_and_validate_schema(400)
1290 "error" => "Missing field: #{attr}.",
1293 "message" => "Missing field: #{attr}",
1294 "source" => %{"pointer" => "/#{attr}"},
1295 "title" => "Invalid value"
1302 test "returns bad_request if missing email params when :account_activation_required is enabled",
1303 %{conn: conn, valid_params: valid_params} do
1304 clear_config([:instance, :account_activation_required], true)
1306 app_token = insert(:oauth_token, user: nil)
1310 |> put_req_header("authorization", "Bearer " <> app_token.token)
1311 |> put_req_header("content-type", "application/json")
1315 |> Map.put(:remote_ip, {127, 0, 0, 5})
1316 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1318 assert json_response_and_validate_schema(res, 400) ==
1319 %{"error" => "Missing parameter: email"}
1323 |> Map.put(:remote_ip, {127, 0, 0, 6})
1324 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1326 assert json_response_and_validate_schema(res, 400) == %{
1327 "error" => "{\"email\":[\"can't be blank\"]}"
1331 test "allow registration without an email", %{conn: conn, valid_params: valid_params} do
1332 app_token = insert(:oauth_token, user: nil)
1333 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1337 |> put_req_header("content-type", "application/json")
1338 |> Map.put(:remote_ip, {127, 0, 0, 7})
1339 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1341 assert json_response_and_validate_schema(res, 200)
1344 test "allow registration with an empty email", %{conn: conn, valid_params: valid_params} do
1345 app_token = insert(:oauth_token, user: nil)
1346 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1350 |> put_req_header("content-type", "application/json")
1351 |> Map.put(:remote_ip, {127, 0, 0, 8})
1352 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1354 assert json_response_and_validate_schema(res, 200)
1357 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
1360 |> put_req_header("authorization", "Bearer " <> "invalid-token")
1361 |> put_req_header("content-type", "multipart/form-data")
1362 |> post("/api/v1/accounts", valid_params)
1364 assert json_response_and_validate_schema(res, 403) == %{"error" => "Invalid credentials"}
1367 test "registration from trusted app" do
1368 clear_config([Pleroma.Captcha, :enabled], true)
1369 app = insert(:oauth_app, trusted: true, scopes: ["read", "write", "follow", "push"])
1373 |> post("/oauth/token", %{
1374 "grant_type" => "client_credentials",
1375 "client_id" => app.client_id,
1376 "client_secret" => app.client_secret
1379 assert %{"access_token" => token, "token_type" => "Bearer"} = json_response(conn, 200)
1383 |> Plug.Conn.put_req_header("authorization", "Bearer " <> token)
1384 |> put_req_header("content-type", "multipart/form-data")
1385 |> post("/api/v1/accounts", %{
1386 nickname: "nickanme",
1388 email: "email@example.com",
1391 password: "some_password",
1392 confirm: "some_password"
1394 |> json_response_and_validate_schema(200)
1397 "access_token" => access_token,
1399 "scope" => "read write follow push",
1400 "token_type" => "Bearer"
1405 |> Plug.Conn.put_req_header("authorization", "Bearer " <> access_token)
1406 |> get("/api/v1/accounts/verify_credentials")
1407 |> json_response_and_validate_schema(200)
1412 "display_name" => "Lain",
1413 "follow_requests_count" => 0,
1414 "followers_count" => 0,
1415 "following_count" => 0,
1422 "actor_type" => "Person",
1423 "discoverable" => false,
1424 "no_rich_text" => false,
1427 "privacy" => "public",
1428 "sensitive" => false
1430 "statuses_count" => 0,
1431 "username" => "Lain"
1436 describe "create account by app / rate limit" do
1437 setup do: clear_config([:rate_limit, :app_account_creation], {10_000, 2})
1439 test "respects rate limit setting", %{conn: conn} do
1440 app_token = insert(:oauth_token, user: nil)
1444 |> put_req_header("authorization", "Bearer " <> app_token.token)
1445 |> Map.put(:remote_ip, {15, 15, 15, 15})
1446 |> put_req_header("content-type", "multipart/form-data")
1451 |> post("/api/v1/accounts", %{
1452 username: "#{i}lain",
1453 email: "#{i}lain@example.org",
1454 password: "PlzDontHackLain",
1459 "access_token" => token,
1460 "created_at" => _created_at,
1462 "token_type" => "Bearer"
1463 } = json_response_and_validate_schema(conn, 200)
1465 token_from_db = Repo.get_by(Token, token: token)
1466 assert token_from_db
1467 token_from_db = Repo.preload(token_from_db, :user)
1468 assert token_from_db.user
1472 post(conn, "/api/v1/accounts", %{
1474 email: "6lain@example.org",
1475 password: "PlzDontHackLain",
1479 assert json_response_and_validate_schema(conn, :too_many_requests) == %{
1480 "error" => "Throttled"
1485 describe "create account with enabled captcha" do
1486 setup %{conn: conn} do
1487 app_token = insert(:oauth_token, user: nil)
1491 |> put_req_header("authorization", "Bearer " <> app_token.token)
1492 |> put_req_header("content-type", "multipart/form-data")
1497 setup do: clear_config([Pleroma.Captcha, :enabled], true)
1499 test "creates an account and returns 200 if captcha is valid", %{conn: conn} do
1500 %{token: token, answer_data: answer_data} = Pleroma.Captcha.new()
1504 email: "lain@example.org",
1505 password: "PlzDontHackLain",
1507 captcha_solution: Pleroma.Captcha.Mock.solution(),
1508 captcha_token: token,
1509 captcha_answer_data: answer_data
1513 "access_token" => access_token,
1516 "token_type" => "Bearer"
1519 |> post("/api/v1/accounts", params)
1520 |> json_response_and_validate_schema(:ok)
1522 assert Token |> Repo.get_by(token: access_token) |> Repo.preload(:user) |> Map.get(:user)
1525 test "returns 400 if any captcha field is not provided", %{conn: conn} do
1526 captcha_fields = [:captcha_solution, :captcha_token, :captcha_answer_data]
1530 email: "lain@example.org",
1531 password: "PlzDontHackLain",
1533 captcha_solution: "xx",
1534 captcha_token: "xx",
1535 captcha_answer_data: "xx"
1538 for field <- captcha_fields do
1540 "error" => "{\"captcha\":[\"Invalid CAPTCHA (Missing parameter: #{field})\"]}"
1545 |> post("/api/v1/accounts", Map.delete(valid_params, field))
1546 |> json_response_and_validate_schema(:bad_request)
1550 test "returns an error if captcha is invalid", %{conn: conn} do
1553 email: "lain@example.org",
1554 password: "PlzDontHackLain",
1556 captcha_solution: "cofe",
1557 captcha_token: "cofe",
1558 captcha_answer_data: "cofe"
1561 assert %{"error" => "{\"captcha\":[\"Invalid answer data\"]}"} ==
1563 |> post("/api/v1/accounts", params)
1564 |> json_response_and_validate_schema(:bad_request)
1568 describe "GET /api/v1/accounts/:id/lists - account_lists" do
1569 test "returns lists to which the account belongs" do
1570 %{user: user, conn: conn} = oauth_access(["read:lists"])
1571 other_user = insert(:user)
1572 assert {:ok, %Pleroma.List{id: _list_id} = list} = Pleroma.List.create("Test List", user)
1573 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
1575 assert [%{"id" => _list_id, "title" => "Test List"}] =
1577 |> get("/api/v1/accounts/#{other_user.id}/lists")
1578 |> json_response_and_validate_schema(200)
1582 describe "verify_credentials" do
1583 test "verify_credentials" do
1584 %{user: user, conn: conn} = oauth_access(["read:accounts"])
1586 [notification | _] =
1587 insert_list(7, :notification, user: user, activity: insert(:note_activity))
1589 Pleroma.Notification.set_read_up_to(user, notification.id)
1590 conn = get(conn, "/api/v1/accounts/verify_credentials")
1592 response = json_response_and_validate_schema(conn, 200)
1594 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
1595 assert response["pleroma"]["chat_token"]
1596 assert response["pleroma"]["unread_notifications_count"] == 6
1597 assert id == to_string(user.id)
1600 test "verify_credentials default scope unlisted" do
1601 user = insert(:user, default_scope: "unlisted")
1602 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1604 conn = get(conn, "/api/v1/accounts/verify_credentials")
1606 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} =
1607 json_response_and_validate_schema(conn, 200)
1609 assert id == to_string(user.id)
1612 test "locked accounts" do
1613 user = insert(:user, default_scope: "private")
1614 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1616 conn = get(conn, "/api/v1/accounts/verify_credentials")
1618 assert %{"id" => id, "source" => %{"privacy" => "private"}} =
1619 json_response_and_validate_schema(conn, 200)
1621 assert id == to_string(user.id)
1625 describe "user relationships" do
1626 setup do: oauth_access(["read:follows"])
1628 test "returns the relationships for the current user", %{user: user, conn: conn} do
1629 %{id: other_user_id} = other_user = insert(:user)
1630 {:ok, _user, _other_user} = User.follow(user, other_user)
1632 assert [%{"id" => ^other_user_id}] =
1634 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
1635 |> json_response_and_validate_schema(200)
1637 assert [%{"id" => ^other_user_id}] =
1639 |> get("/api/v1/accounts/relationships?id[]=#{other_user.id}")
1640 |> json_response_and_validate_schema(200)
1643 test "returns an empty list on a bad request", %{conn: conn} do
1644 conn = get(conn, "/api/v1/accounts/relationships", %{})
1646 assert [] = json_response_and_validate_schema(conn, 200)
1650 test "getting a list of mutes" do
1651 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1652 %{id: id1} = other_user1 = insert(:user)
1653 %{id: id2} = other_user2 = insert(:user)
1654 %{id: id3} = other_user3 = insert(:user)
1656 {:ok, _user_relationships} = User.mute(user, other_user1)
1657 {:ok, _user_relationships} = User.mute(user, other_user2)
1658 {:ok, _user_relationships} = User.mute(user, other_user3)
1662 |> get("/api/v1/mutes")
1663 |> json_response_and_validate_schema(200)
1665 assert [id1, id2, id3] == Enum.map(result, & &1["id"])
1669 |> get("/api/v1/mutes?limit=1")
1670 |> json_response_and_validate_schema(200)
1672 assert [%{"id" => ^id1}] = result
1676 |> get("/api/v1/mutes?since_id=#{id1}")
1677 |> json_response_and_validate_schema(200)
1679 assert [%{"id" => ^id2}, %{"id" => ^id3}] = result
1683 |> get("/api/v1/mutes?since_id=#{id1}&max_id=#{id3}")
1684 |> json_response_and_validate_schema(200)
1686 assert [%{"id" => ^id2}] = result
1690 |> get("/api/v1/mutes?since_id=#{id1}&limit=1")
1691 |> json_response_and_validate_schema(200)
1693 assert [%{"id" => ^id2}] = result
1696 test "list of mutes with with_relationships parameter" do
1697 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1698 %{id: id1} = other_user1 = insert(:user)
1699 %{id: id2} = other_user2 = insert(:user)
1700 %{id: id3} = other_user3 = insert(:user)
1702 {:ok, _, _} = User.follow(other_user1, user)
1703 {:ok, _, _} = User.follow(other_user2, user)
1704 {:ok, _, _} = User.follow(other_user3, user)
1706 {:ok, _} = User.mute(user, other_user1)
1707 {:ok, _} = User.mute(user, other_user2)
1708 {:ok, _} = User.mute(user, other_user3)
1713 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1717 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1721 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1725 |> get("/api/v1/mutes?with_relationships=true")
1726 |> json_response_and_validate_schema(200)
1729 test "getting a list of blocks" do
1730 %{user: user, conn: conn} = oauth_access(["read:blocks"])
1731 %{id: id1} = other_user1 = insert(:user)
1732 %{id: id2} = other_user2 = insert(:user)
1733 %{id: id3} = other_user3 = insert(:user)
1735 {:ok, _user_relationship} = User.block(user, other_user1)
1736 {:ok, _user_relationship} = User.block(user, other_user3)
1737 {:ok, _user_relationship} = User.block(user, other_user2)
1741 |> assign(:user, user)
1742 |> get("/api/v1/blocks")
1743 |> json_response_and_validate_schema(200)
1745 assert [id1, id2, id3] == Enum.map(result, & &1["id"])
1749 |> assign(:user, user)
1750 |> get("/api/v1/blocks?limit=1")
1751 |> json_response_and_validate_schema(200)
1753 assert [%{"id" => ^id1}] = result
1757 |> assign(:user, user)
1758 |> get("/api/v1/blocks?since_id=#{id1}")
1759 |> json_response_and_validate_schema(200)
1761 assert [%{"id" => ^id2}, %{"id" => ^id3}] = result
1765 |> assign(:user, user)
1766 |> get("/api/v1/blocks?since_id=#{id1}&max_id=#{id3}")
1767 |> json_response_and_validate_schema(200)
1769 assert [%{"id" => ^id2}] = result
1773 |> assign(:user, user)
1774 |> get("/api/v1/blocks?since_id=#{id1}&limit=1")
1775 |> json_response_and_validate_schema(200)
1777 assert [%{"id" => ^id2}] = result
1780 test "account lookup", %{conn: conn} do
1781 %{nickname: acct} = insert(:user, %{nickname: "nickname"})
1782 %{nickname: acct_two} = insert(:user, %{nickname: "nickname@notlocaldoma.in"})
1786 |> get("/api/v1/accounts/lookup?acct=#{acct}")
1787 |> json_response_and_validate_schema(200)
1789 assert %{"acct" => ^acct} = result
1793 |> get("/api/v1/accounts/lookup?acct=#{acct_two}")
1794 |> json_response_and_validate_schema(200)
1796 assert %{"acct" => ^acct_two} = result
1800 |> get("/api/v1/accounts/lookup?acct=unexisting_nickname")
1801 |> json_response_and_validate_schema(404)