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
14 alias Pleroma.Web.Plugs.SetLocalePlug
16 import Pleroma.Factory
18 describe "account fetching" do
20 %User{id: user_id} = insert(:user)
22 assert %{"id" => ^user_id} =
24 |> get("/api/v1/accounts/#{user_id}")
25 |> json_response_and_validate_schema(200)
27 assert %{"error" => "Can't find user"} =
29 |> get("/api/v1/accounts/-1")
30 |> json_response_and_validate_schema(404)
33 test "relationship field" do
34 %{conn: conn, user: user} = oauth_access(["read"])
36 other_user = insert(:user)
40 |> get("/api/v1/accounts/#{other_user.id}")
41 |> json_response_and_validate_schema(200)
43 assert response["id"] == other_user.id
44 assert response["pleroma"]["relationship"] == %{}
46 assert %{"pleroma" => %{"relationship" => %{"following" => false, "followed_by" => false}}} =
48 |> get("/api/v1/accounts/#{other_user.id}?with_relationships=true")
49 |> json_response_and_validate_schema(200)
51 {:ok, _, %{id: other_id}} = User.follow(user, other_user)
55 "pleroma" => %{"relationship" => %{"following" => true, "followed_by" => false}}
58 |> get("/api/v1/accounts/#{other_id}?with_relationships=true")
59 |> json_response_and_validate_schema(200)
61 {:ok, _, _} = User.follow(other_user, user)
65 "pleroma" => %{"relationship" => %{"following" => true, "followed_by" => true}}
68 |> get("/api/v1/accounts/#{other_id}?with_relationships=true")
69 |> json_response_and_validate_schema(200)
72 test "works by nickname" do
75 assert %{"id" => _user_id} =
77 |> get("/api/v1/accounts/#{user.nickname}")
78 |> json_response_and_validate_schema(200)
81 test "works by nickname for remote users" do
82 clear_config([:instance, :limit_to_local_content], false)
84 user = insert(:user, nickname: "user@example.com", local: false)
86 assert %{"id" => _user_id} =
88 |> get("/api/v1/accounts/#{user.nickname}")
89 |> json_response_and_validate_schema(200)
92 test "respects limit_to_local_content == :all for remote user nicknames" do
93 clear_config([:instance, :limit_to_local_content], :all)
95 user = insert(:user, nickname: "user@example.com", local: false)
98 |> get("/api/v1/accounts/#{user.nickname}")
99 |> json_response_and_validate_schema(404)
102 test "respects limit_to_local_content == :unauthenticated for remote user nicknames" do
103 clear_config([:instance, :limit_to_local_content], :unauthenticated)
105 user = insert(:user, nickname: "user@example.com", local: false)
106 reading_user = insert(:user)
110 |> get("/api/v1/accounts/#{user.nickname}")
112 assert json_response_and_validate_schema(conn, 404)
116 |> assign(:user, reading_user)
117 |> assign(:token, insert(:oauth_token, user: reading_user, scopes: ["read:accounts"]))
118 |> get("/api/v1/accounts/#{user.nickname}")
120 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
124 test "accounts fetches correct account for nicknames beginning with numbers", %{conn: conn} do
125 # Need to set an old-style integer ID to reproduce the problem
126 # (these are no longer assigned to new accounts but were preserved
127 # for existing accounts during the migration to flakeIDs)
128 user_one = insert(:user, %{id: 1212})
129 user_two = insert(:user, %{nickname: "#{user_one.id}garbage"})
133 |> get("/api/v1/accounts/#{user_one.id}")
134 |> json_response_and_validate_schema(:ok)
138 |> get("/api/v1/accounts/#{user_two.nickname}")
139 |> json_response_and_validate_schema(:ok)
143 |> get("/api/v1/accounts/#{user_two.id}")
144 |> json_response_and_validate_schema(:ok)
146 refute acc_one == acc_two
147 assert acc_two == acc_three
150 test "returns 404 when user is invisible", %{conn: conn} do
151 user = insert(:user, %{invisible: true})
153 assert %{"error" => "Can't find user"} =
155 |> get("/api/v1/accounts/#{user.nickname}")
156 |> json_response_and_validate_schema(404)
159 test "returns 404 for internal.fetch actor", %{conn: conn} do
160 %User{nickname: "internal.fetch"} = InternalFetchActor.get_actor()
162 assert %{"error" => "Can't find user"} =
164 |> get("/api/v1/accounts/internal.fetch")
165 |> json_response_and_validate_schema(404)
168 test "returns 404 for deactivated user", %{conn: conn} do
169 user = insert(:user, is_active: false)
171 assert %{"error" => "Can't find user"} =
173 |> get("/api/v1/accounts/#{user.id}")
174 |> json_response_and_validate_schema(:not_found)
178 defp local_and_remote_users do
179 local = insert(:user)
180 remote = insert(:user, local: false)
181 {:ok, local: local, remote: remote}
184 describe "user fetching with restrict unauthenticated profiles for local and remote" do
185 setup do: local_and_remote_users()
187 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
189 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
191 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
192 assert %{"error" => "This API requires an authenticated user"} ==
194 |> get("/api/v1/accounts/#{local.id}")
195 |> json_response_and_validate_schema(:unauthorized)
197 assert %{"error" => "This API requires an authenticated user"} ==
199 |> get("/api/v1/accounts/#{remote.id}")
200 |> json_response_and_validate_schema(:unauthorized)
203 test "if user is authenticated", %{local: local, remote: remote} do
204 %{conn: conn} = oauth_access(["read"])
206 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
207 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
209 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
210 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
214 describe "user fetching with restrict unauthenticated profiles for local" do
215 setup do: local_and_remote_users()
217 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
219 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
220 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
222 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
223 "error" => "This API requires an authenticated user"
226 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
227 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
230 test "if user is authenticated", %{local: local, remote: remote} do
231 %{conn: conn} = oauth_access(["read"])
233 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
234 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
236 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
237 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
241 describe "user fetching with restrict unauthenticated profiles for remote" do
242 setup do: local_and_remote_users()
244 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
246 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
247 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
248 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
250 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
252 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
253 "error" => "This API requires an authenticated user"
257 test "if user is authenticated", %{local: local, remote: remote} do
258 %{conn: conn} = oauth_access(["read"])
260 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
261 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
263 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
264 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
268 describe "user timelines" do
269 setup do: oauth_access(["read:statuses"])
271 test "works with announces that are just addressed to public", %{conn: conn} do
272 user = insert(:user, ap_id: "https://honktest/u/test", local: false)
273 other_user = insert(:user)
275 {:ok, post} = CommonAPI.post(other_user, %{status: "bonkeronk"})
279 "@context" => "https://www.w3.org/ns/activitystreams",
280 "actor" => "https://honktest/u/test",
281 "id" => "https://honktest/u/test/bonk/1793M7B9MQ48847vdx",
282 "object" => post.data["object"],
283 "published" => "2019-06-25T19:33:58Z",
284 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
287 |> ActivityPub.persist(local: false)
291 |> get("/api/v1/accounts/#{user.id}/statuses")
292 |> json_response_and_validate_schema(200)
294 assert [%{"id" => id}] = resp
295 assert id == announce.id
298 test "deactivated user", %{conn: conn} do
299 user = insert(:user, is_active: false)
301 assert %{"error" => "Can't find user"} ==
303 |> get("/api/v1/accounts/#{user.id}/statuses")
304 |> json_response_and_validate_schema(:not_found)
307 test "returns 404 when user is invisible", %{conn: conn} do
308 user = insert(:user, %{invisible: true})
310 assert %{"error" => "Can't find user"} =
312 |> get("/api/v1/accounts/#{user.id}")
313 |> json_response_and_validate_schema(404)
316 test "respects blocks", %{user: user_one, conn: conn} do
317 user_two = insert(:user)
318 user_three = insert(:user)
320 User.block(user_one, user_two)
322 {:ok, activity} = CommonAPI.post(user_two, %{status: "User one sux0rz"})
323 {:ok, repeat} = CommonAPI.repeat(activity.id, user_three)
327 |> get("/api/v1/accounts/#{user_two.id}/statuses")
328 |> json_response_and_validate_schema(200)
330 assert [%{"id" => id}] = resp
331 assert id == activity.id
333 # Even a blocked user will deliver the full user timeline, there would be
334 # no point in looking at a blocked users timeline otherwise
337 |> get("/api/v1/accounts/#{user_two.id}/statuses")
338 |> json_response_and_validate_schema(200)
340 assert [%{"id" => id}] = resp
341 assert id == activity.id
343 # Third user's timeline includes the repeat when viewed by unauthenticated user
346 |> get("/api/v1/accounts/#{user_three.id}/statuses")
347 |> json_response_and_validate_schema(200)
349 assert [%{"id" => id}] = resp
350 assert id == repeat.id
352 # When viewing a third user's timeline, the blocked users' statuses will NOT be shown
353 resp = get(conn, "/api/v1/accounts/#{user_three.id}/statuses")
355 assert [] == json_response_and_validate_schema(resp, 200)
358 test "gets users statuses", %{conn: conn} do
359 user_one = insert(:user)
360 user_two = insert(:user)
361 user_three = insert(:user)
363 {:ok, _user_three, _user_one} = User.follow(user_three, user_one)
365 {:ok, activity} = CommonAPI.post(user_one, %{status: "HI!!!"})
367 {:ok, direct_activity} =
368 CommonAPI.post(user_one, %{
369 status: "Hi, @#{user_two.nickname}.",
373 {:ok, private_activity} =
374 CommonAPI.post(user_one, %{status: "private", visibility: "private"})
379 |> get("/api/v1/accounts/#{user_one.id}/statuses")
380 |> json_response_and_validate_schema(200)
382 assert [%{"id" => id}] = resp
383 assert id == to_string(activity.id)
387 |> assign(:user, user_two)
388 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
389 |> get("/api/v1/accounts/#{user_one.id}/statuses")
390 |> json_response_and_validate_schema(200)
392 assert [%{"id" => id_one}, %{"id" => id_two}] = resp
393 assert id_one == to_string(direct_activity.id)
394 assert id_two == to_string(activity.id)
398 |> assign(:user, user_three)
399 |> assign(:token, insert(:oauth_token, user: user_three, scopes: ["read:statuses"]))
400 |> get("/api/v1/accounts/#{user_one.id}/statuses")
401 |> json_response_and_validate_schema(200)
403 assert [%{"id" => id_one}, %{"id" => id_two}] = resp
404 assert id_one == to_string(private_activity.id)
405 assert id_two == to_string(activity.id)
408 test "unimplemented pinned statuses feature", %{conn: conn} do
409 note = insert(:note_activity)
410 user = User.get_cached_by_ap_id(note.data["actor"])
412 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?pinned=true")
414 assert json_response_and_validate_schema(conn, 200) == []
417 test "gets local-only statuses for authenticated users", %{user: _user, conn: conn} do
418 user_one = insert(:user)
420 {:ok, activity} = CommonAPI.post(user_one, %{status: "HI!!!", visibility: "local"})
424 |> get("/api/v1/accounts/#{user_one.id}/statuses")
425 |> json_response_and_validate_schema(200)
427 assert [%{"id" => id}] = resp
428 assert id == to_string(activity.id)
431 test "gets an users media, excludes reblogs", %{conn: conn} do
432 note = insert(:note_activity)
433 user = User.get_cached_by_ap_id(note.data["actor"])
434 other_user = insert(:user)
437 content_type: "image/jpeg",
438 path: Path.absname("test/fixtures/image.jpg"),
439 filename: "an_image.jpg"
442 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: user.ap_id)
444 {:ok, %{id: image_post_id}} = CommonAPI.post(user, %{status: "cofe", media_ids: [media_id]})
446 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: other_user.ap_id)
448 {:ok, %{id: other_image_post_id}} =
449 CommonAPI.post(other_user, %{status: "cofe2", media_ids: [media_id]})
451 {:ok, _announce} = CommonAPI.repeat(other_image_post_id, user)
453 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?only_media=true")
455 assert [%{"id" => ^image_post_id}] = json_response_and_validate_schema(conn, 200)
457 conn = get(build_conn(), "/api/v1/accounts/#{user.id}/statuses?only_media=1")
459 assert [%{"id" => ^image_post_id}] = json_response_and_validate_schema(conn, 200)
462 test "gets a user's statuses without reblogs", %{user: user, conn: conn} do
463 {:ok, %{id: post_id}} = CommonAPI.post(user, %{status: "HI!!!"})
464 {:ok, _} = CommonAPI.repeat(post_id, user)
466 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_reblogs=true")
467 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
469 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_reblogs=1")
470 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
473 test "filters user's statuses by a hashtag", %{user: user, conn: conn} do
474 {:ok, %{id: post_id}} = CommonAPI.post(user, %{status: "#hashtag"})
475 {:ok, _post} = CommonAPI.post(user, %{status: "hashtag"})
477 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?tagged=hashtag")
478 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
481 test "the user views their own timelines and excludes direct messages", %{
485 {:ok, %{id: public_activity_id}} =
486 CommonAPI.post(user, %{status: ".", visibility: "public"})
488 {:ok, _direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
490 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_visibilities[]=direct")
491 assert [%{"id" => ^public_activity_id}] = json_response_and_validate_schema(conn, 200)
494 test "muted reactions", %{user: user, conn: conn} do
495 user2 = insert(:user)
496 User.mute(user, user2)
497 {:ok, activity} = CommonAPI.post(user, %{status: "."})
498 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
502 |> get("/api/v1/accounts/#{user.id}/statuses")
503 |> json_response_and_validate_schema(200)
508 "emoji_reactions" => []
515 |> get("/api/v1/accounts/#{user.id}/statuses?with_muted=true")
516 |> json_response_and_validate_schema(200)
521 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
527 test "paginates a user's statuses", %{user: user, conn: conn} do
528 {:ok, post_1} = CommonAPI.post(user, %{status: "first post"})
529 {:ok, post_2} = CommonAPI.post(user, %{status: "second post"})
531 response_1 = get(conn, "/api/v1/accounts/#{user.id}/statuses?limit=1")
532 assert [res] = json_response_and_validate_schema(response_1, 200)
533 assert res["id"] == post_2.id
535 response_2 = get(conn, "/api/v1/accounts/#{user.id}/statuses?limit=1&max_id=#{res["id"]}")
536 assert [res] = json_response_and_validate_schema(response_2, 200)
537 assert res["id"] == post_1.id
539 refute response_1 == response_2
543 defp local_and_remote_activities(%{local: local, remote: remote}) do
544 insert(:note_activity, user: local)
545 insert(:note_activity, user: remote, local: false)
550 describe "statuses with restrict unauthenticated profiles for local and remote" do
551 setup do: local_and_remote_users()
552 setup :local_and_remote_activities
554 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
556 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
558 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
559 assert %{"error" => "This API requires an authenticated user"} ==
561 |> get("/api/v1/accounts/#{local.id}/statuses")
562 |> json_response_and_validate_schema(:unauthorized)
564 assert %{"error" => "This API requires an authenticated user"} ==
566 |> get("/api/v1/accounts/#{remote.id}/statuses")
567 |> json_response_and_validate_schema(:unauthorized)
570 test "if user is authenticated", %{local: local, remote: remote} do
571 %{conn: conn} = oauth_access(["read"])
573 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
574 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
576 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
577 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
581 describe "statuses with restrict unauthenticated profiles for local" do
582 setup do: local_and_remote_users()
583 setup :local_and_remote_activities
585 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
587 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
588 assert %{"error" => "This API requires an authenticated user"} ==
590 |> get("/api/v1/accounts/#{local.id}/statuses")
591 |> json_response_and_validate_schema(:unauthorized)
593 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
594 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
597 test "if user is authenticated", %{local: local, remote: remote} do
598 %{conn: conn} = oauth_access(["read"])
600 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
601 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
603 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
604 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
608 describe "statuses with restrict unauthenticated profiles for remote" do
609 setup do: local_and_remote_users()
610 setup :local_and_remote_activities
612 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
614 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
615 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
616 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
618 assert %{"error" => "This API requires an authenticated user"} ==
620 |> get("/api/v1/accounts/#{remote.id}/statuses")
621 |> json_response_and_validate_schema(:unauthorized)
624 test "if user is authenticated", %{local: local, remote: remote} do
625 %{conn: conn} = oauth_access(["read"])
627 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
628 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
630 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
631 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
635 describe "followers" do
636 setup do: oauth_access(["read:accounts"])
638 test "getting followers", %{user: user, conn: conn} do
639 other_user = insert(:user)
640 {:ok, %{id: user_id}, other_user} = User.follow(user, other_user)
642 conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers")
644 assert [%{"id" => ^user_id}] = json_response_and_validate_schema(conn, 200)
647 test "following with relationship", %{conn: conn, user: user} do
648 other_user = insert(:user)
649 {:ok, %{id: id}, _} = User.follow(other_user, user)
657 "following" => false,
658 "followed_by" => true
664 |> get("/api/v1/accounts/#{user.id}/followers?with_relationships=true")
665 |> json_response_and_validate_schema(200)
667 {:ok, _, _} = User.follow(user, other_user)
676 "followed_by" => true
682 |> get("/api/v1/accounts/#{user.id}/followers?with_relationships=true")
683 |> json_response_and_validate_schema(200)
686 test "getting followers, hide_followers", %{user: user, conn: conn} do
687 other_user = insert(:user, hide_followers: true)
688 {:ok, _user, _other_user} = User.follow(user, other_user)
690 conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers")
692 assert [] == json_response_and_validate_schema(conn, 200)
695 test "getting followers, hide_followers, same user requesting" do
697 other_user = insert(:user, hide_followers: true)
698 {:ok, _user, _other_user} = User.follow(user, other_user)
702 |> assign(:user, other_user)
703 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
704 |> get("/api/v1/accounts/#{other_user.id}/followers")
706 refute [] == json_response_and_validate_schema(conn, 200)
709 test "getting followers, pagination", %{user: user, conn: conn} do
710 {:ok, %User{id: follower1_id}, _user} = :user |> insert() |> User.follow(user)
711 {:ok, %User{id: follower2_id}, _user} = :user |> insert() |> User.follow(user)
712 {:ok, %User{id: follower3_id}, _user} = :user |> insert() |> User.follow(user)
714 assert [%{"id" => ^follower3_id}, %{"id" => ^follower2_id}] =
716 |> get("/api/v1/accounts/#{user.id}/followers?since_id=#{follower1_id}")
717 |> json_response_and_validate_schema(200)
719 assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] =
721 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3_id}")
722 |> json_response_and_validate_schema(200)
724 assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] =
727 "/api/v1/accounts/#{user.id}/followers?id=#{user.id}&limit=20&max_id=#{follower3_id}"
729 |> json_response_and_validate_schema(200)
731 res_conn = get(conn, "/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3_id}")
733 assert [%{"id" => ^follower2_id}] = json_response_and_validate_schema(res_conn, 200)
735 assert [link_header] = get_resp_header(res_conn, "link")
736 assert link_header =~ ~r/min_id=#{follower2_id}/
737 assert link_header =~ ~r/max_id=#{follower2_id}/
741 describe "following" do
742 setup do: oauth_access(["read:accounts"])
744 test "getting following", %{user: user, conn: conn} do
745 other_user = insert(:user)
746 {:ok, user, other_user} = User.follow(user, other_user)
748 conn = get(conn, "/api/v1/accounts/#{user.id}/following")
750 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
751 assert id == to_string(other_user.id)
754 test "following with relationship", %{conn: conn, user: user} do
755 other_user = insert(:user)
756 {:ok, user, other_user} = User.follow(user, other_user)
758 conn = get(conn, "/api/v1/accounts/#{user.id}/following?with_relationships=true")
766 "relationship" => %{"id" => ^id, "following" => true, "followed_by" => false}
769 ] = json_response_and_validate_schema(conn, 200)
772 test "getting following, hide_follows, other user requesting" do
773 user = insert(:user, hide_follows: true)
774 other_user = insert(:user)
775 {:ok, user, other_user} = User.follow(user, other_user)
779 |> assign(:user, other_user)
780 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
781 |> get("/api/v1/accounts/#{user.id}/following")
783 assert [] == json_response_and_validate_schema(conn, 200)
786 test "getting following, hide_follows, same user requesting" do
787 user = insert(:user, hide_follows: true)
788 other_user = insert(:user)
789 {:ok, user, _other_user} = User.follow(user, other_user)
793 |> assign(:user, user)
794 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read:accounts"]))
795 |> get("/api/v1/accounts/#{user.id}/following")
797 refute [] == json_response_and_validate_schema(conn, 200)
800 test "getting following, pagination", %{user: user, conn: conn} do
801 following1 = insert(:user)
802 following2 = insert(:user)
803 following3 = insert(:user)
804 {:ok, _, _} = User.follow(user, following1)
805 {:ok, _, _} = User.follow(user, following2)
806 {:ok, _, _} = User.follow(user, following3)
808 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
810 assert [%{"id" => id3}, %{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
811 assert id3 == following3.id
812 assert id2 == following2.id
814 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
816 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
817 assert id2 == following2.id
818 assert id1 == following1.id
823 "/api/v1/accounts/#{user.id}/following?id=#{user.id}&limit=20&max_id=#{following3.id}"
826 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
827 assert id2 == following2.id
828 assert id1 == following1.id
831 get(conn, "/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
833 assert [%{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
834 assert id2 == following2.id
836 assert [link_header] = get_resp_header(res_conn, "link")
837 assert link_header =~ ~r/min_id=#{following2.id}/
838 assert link_header =~ ~r/max_id=#{following2.id}/
842 describe "follow/unfollow" do
843 setup do: oauth_access(["follow"])
845 test "following / unfollowing a user", %{conn: conn} do
846 %{id: other_user_id, nickname: other_user_nickname} = insert(:user)
848 assert %{"id" => _id, "following" => true} =
850 |> post("/api/v1/accounts/#{other_user_id}/follow")
851 |> json_response_and_validate_schema(200)
853 assert %{"id" => _id, "following" => false} =
855 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
856 |> json_response_and_validate_schema(200)
858 assert %{"id" => ^other_user_id} =
860 |> put_req_header("content-type", "application/json")
861 |> post("/api/v1/follows", %{"uri" => other_user_nickname})
862 |> json_response_and_validate_schema(200)
865 test "cancelling follow request", %{conn: conn} do
866 %{id: other_user_id} = insert(:user, %{is_locked: true})
868 assert %{"id" => ^other_user_id, "following" => false, "requested" => true} =
870 |> post("/api/v1/accounts/#{other_user_id}/follow")
871 |> json_response_and_validate_schema(:ok)
873 assert %{"id" => ^other_user_id, "following" => false, "requested" => false} =
875 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
876 |> json_response_and_validate_schema(:ok)
879 test "following without reblogs" do
880 %{conn: conn} = oauth_access(["follow", "read:statuses"])
881 followed = insert(:user)
882 other_user = insert(:user)
886 |> put_req_header("content-type", "application/json")
887 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
889 assert %{"showing_reblogs" => false} = json_response_and_validate_schema(ret_conn, 200)
891 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
892 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
896 |> get("/api/v1/timelines/home")
897 |> json_response_and_validate_schema(200)
899 assert %{"showing_reblogs" => true} =
901 |> put_req_header("content-type", "application/json")
902 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: true})
903 |> json_response_and_validate_schema(200)
905 assert [%{"id" => ^reblog_id}] =
907 |> get("/api/v1/timelines/home")
908 |> json_response_and_validate_schema(200)
911 test "following with reblogs" do
912 %{conn: conn} = oauth_access(["follow", "read:statuses"])
913 followed = insert(:user)
914 other_user = insert(:user)
916 ret_conn = post(conn, "/api/v1/accounts/#{followed.id}/follow")
918 assert %{"showing_reblogs" => true} = json_response_and_validate_schema(ret_conn, 200)
920 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
921 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
923 assert [%{"id" => ^reblog_id}] =
925 |> get("/api/v1/timelines/home")
926 |> json_response_and_validate_schema(200)
928 assert %{"showing_reblogs" => false} =
930 |> put_req_header("content-type", "application/json")
931 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
932 |> json_response_and_validate_schema(200)
936 |> get("/api/v1/timelines/home")
937 |> json_response_and_validate_schema(200)
940 test "following with subscription and unsubscribing" do
941 %{conn: conn} = oauth_access(["follow"])
942 followed = insert(:user)
946 |> put_req_header("content-type", "application/json")
947 |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: true})
949 assert %{"id" => _id, "subscribing" => true} =
950 json_response_and_validate_schema(ret_conn, 200)
954 |> put_req_header("content-type", "application/json")
955 |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: false})
957 assert %{"id" => _id, "subscribing" => false} =
958 json_response_and_validate_schema(ret_conn, 200)
961 test "following / unfollowing errors", %{user: user, conn: conn} do
963 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
965 assert %{"error" => "Can not follow yourself"} =
966 json_response_and_validate_schema(conn_res, 400)
969 user = User.get_cached_by_id(user.id)
970 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
972 assert %{"error" => "Can not unfollow yourself"} =
973 json_response_and_validate_schema(conn_res, 400)
975 # self follow via uri
976 user = User.get_cached_by_id(user.id)
978 assert %{"error" => "Can not follow yourself"} =
980 |> put_req_header("content-type", "multipart/form-data")
981 |> post("/api/v1/follows", %{"uri" => user.nickname})
982 |> json_response_and_validate_schema(400)
984 # follow non existing user
985 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
986 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
988 # follow non existing user via uri
991 |> put_req_header("content-type", "multipart/form-data")
992 |> post("/api/v1/follows", %{"uri" => "doesntexist"})
994 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
996 # unfollow non existing user
997 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
998 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
1002 describe "mute/unmute" do
1003 setup do: oauth_access(["write:mutes"])
1005 test "with notifications", %{conn: conn} do
1006 other_user = insert(:user)
1008 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} =
1010 |> post("/api/v1/accounts/#{other_user.id}/mute")
1011 |> json_response_and_validate_schema(200)
1013 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
1015 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
1016 json_response_and_validate_schema(conn, 200)
1019 test "without notifications", %{conn: conn} do
1020 other_user = insert(:user)
1024 |> put_req_header("content-type", "multipart/form-data")
1025 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
1027 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} =
1028 json_response_and_validate_schema(ret_conn, 200)
1030 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
1032 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
1033 json_response_and_validate_schema(conn, 200)
1037 describe "pinned statuses" do
1039 user = insert(:user)
1040 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1041 %{conn: conn} = oauth_access(["read:statuses"], user: user)
1043 [conn: conn, user: user, activity: activity]
1046 test "returns pinned statuses", %{conn: conn, user: user, activity: %{id: activity_id}} do
1047 {:ok, _} = CommonAPI.pin(activity_id, user)
1049 assert [%{"id" => ^activity_id, "pinned" => true}] =
1051 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1052 |> json_response_and_validate_schema(200)
1056 test "blocking / unblocking a user" do
1057 %{conn: conn} = oauth_access(["follow"])
1058 other_user = insert(:user)
1060 ret_conn = post(conn, "/api/v1/accounts/#{other_user.id}/block")
1062 assert %{"id" => _id, "blocking" => true} = json_response_and_validate_schema(ret_conn, 200)
1064 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unblock")
1066 assert %{"id" => _id, "blocking" => false} = json_response_and_validate_schema(conn, 200)
1069 describe "create account by app" do
1073 email: "lain@example.org",
1074 password: "PlzDontHackLain",
1078 [valid_params: valid_params]
1081 test "registers and logs in without :account_activation_required / :account_approval_required",
1083 clear_config([:instance, :account_activation_required], false)
1084 clear_config([:instance, :account_approval_required], false)
1088 |> put_req_header("content-type", "application/json")
1089 |> post("/api/v1/apps", %{
1090 client_name: "client_name",
1091 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1092 scopes: "read, write, follow"
1096 "client_id" => client_id,
1097 "client_secret" => client_secret,
1099 "name" => "client_name",
1100 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1103 } = json_response_and_validate_schema(conn, 200)
1106 post(conn, "/oauth/token", %{
1107 grant_type: "client_credentials",
1108 client_id: client_id,
1109 client_secret: client_secret
1112 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1113 json_response(conn, 200)
1116 token_from_db = Repo.get_by(Token, token: token)
1117 assert token_from_db
1119 assert scope == "read write follow"
1121 clear_config([User, :email_blacklist], ["example.org"])
1125 email: "lain@example.org",
1126 password: "PlzDontHackLain",
1133 |> put_req_header("content-type", "multipart/form-data")
1134 |> put_req_header("authorization", "Bearer " <> token)
1135 |> post("/api/v1/accounts", params)
1137 assert %{"error" => "{\"email\":[\"Invalid email\"]}"} =
1138 json_response_and_validate_schema(conn, 400)
1140 clear_config([User, :email_blacklist], [])
1144 |> put_req_header("content-type", "multipart/form-data")
1145 |> put_req_header("authorization", "Bearer " <> token)
1146 |> post("/api/v1/accounts", params)
1149 "access_token" => token,
1150 "created_at" => _created_at,
1152 "token_type" => "Bearer"
1153 } = json_response_and_validate_schema(conn, 200)
1155 token_from_db = Repo.get_by(Token, token: token)
1156 assert token_from_db
1157 user = Repo.preload(token_from_db, :user).user
1160 assert user.is_confirmed
1161 assert user.is_approved
1164 test "registers but does not log in with :account_activation_required", %{conn: conn} do
1165 clear_config([:instance, :account_activation_required], true)
1166 clear_config([:instance, :account_approval_required], false)
1170 |> put_req_header("content-type", "application/json")
1171 |> post("/api/v1/apps", %{
1172 client_name: "client_name",
1173 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1174 scopes: "read, write, follow"
1178 "client_id" => client_id,
1179 "client_secret" => client_secret,
1181 "name" => "client_name",
1182 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1185 } = json_response_and_validate_schema(conn, 200)
1188 post(conn, "/oauth/token", %{
1189 grant_type: "client_credentials",
1190 client_id: client_id,
1191 client_secret: client_secret
1194 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1195 json_response(conn, 200)
1198 token_from_db = Repo.get_by(Token, token: token)
1199 assert token_from_db
1201 assert scope == "read write follow"
1205 |> put_req_header("content-type", "multipart/form-data")
1206 |> put_req_header("authorization", "Bearer " <> token)
1207 |> post("/api/v1/accounts", %{
1209 email: "lain@example.org",
1210 password: "PlzDontHackLain",
1215 response = json_response_and_validate_schema(conn, 200)
1216 assert %{"identifier" => "missing_confirmed_email"} = response
1217 refute response["access_token"]
1218 refute response["token_type"]
1220 user = Repo.get_by(User, email: "lain@example.org")
1221 refute user.is_confirmed
1224 test "registers but does not log in with :account_approval_required", %{conn: conn} do
1225 clear_config([:instance, :account_approval_required], true)
1226 clear_config([:instance, :account_activation_required], false)
1230 |> put_req_header("content-type", "application/json")
1231 |> post("/api/v1/apps", %{
1232 client_name: "client_name",
1233 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
1234 scopes: "read, write, follow"
1238 "client_id" => client_id,
1239 "client_secret" => client_secret,
1241 "name" => "client_name",
1242 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1245 } = json_response_and_validate_schema(conn, 200)
1248 post(conn, "/oauth/token", %{
1249 grant_type: "client_credentials",
1250 client_id: client_id,
1251 client_secret: client_secret
1254 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1255 json_response(conn, 200)
1258 token_from_db = Repo.get_by(Token, token: token)
1259 assert token_from_db
1261 assert scope == "read write follow"
1265 |> put_req_header("content-type", "multipart/form-data")
1266 |> put_req_header("authorization", "Bearer " <> token)
1267 |> post("/api/v1/accounts", %{
1269 email: "lain@example.org",
1270 password: "PlzDontHackLain",
1273 reason: "I'm a cool dude, bro"
1276 response = json_response_and_validate_schema(conn, 200)
1277 assert %{"identifier" => "awaiting_approval"} = response
1278 refute response["access_token"]
1279 refute response["token_type"]
1281 user = Repo.get_by(User, email: "lain@example.org")
1283 refute user.is_approved
1284 assert user.registration_reason == "I'm a cool dude, bro"
1287 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
1288 _user = insert(:user, email: "lain@example.org")
1289 app_token = insert(:oauth_token, user: nil)
1293 |> put_req_header("authorization", "Bearer " <> app_token.token)
1294 |> put_req_header("content-type", "application/json")
1295 |> post("/api/v1/accounts", valid_params)
1297 assert json_response_and_validate_schema(res, 400) == %{
1298 "error" => "{\"email\":[\"has already been taken\"]}"
1302 test "returns bad_request if missing required params", %{
1304 valid_params: valid_params
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")
1313 res = post(conn, "/api/v1/accounts", valid_params)
1314 assert json_response_and_validate_schema(res, 200)
1316 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
1317 |> Stream.zip(Map.delete(valid_params, :email))
1318 |> Enum.each(fn {ip, {attr, _}} ->
1321 |> Map.put(:remote_ip, ip)
1322 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
1323 |> json_response_and_validate_schema(400)
1326 "error" => "Missing field: #{attr}.",
1329 "message" => "Missing field: #{attr}",
1330 "source" => %{"pointer" => "/#{attr}"},
1331 "title" => "Invalid value"
1338 test "returns bad_request if missing email params when :account_activation_required is enabled",
1339 %{conn: conn, valid_params: valid_params} do
1340 clear_config([:instance, :account_activation_required], true)
1342 app_token = insert(:oauth_token, user: nil)
1346 |> put_req_header("authorization", "Bearer " <> app_token.token)
1347 |> put_req_header("content-type", "application/json")
1351 |> Map.put(:remote_ip, {127, 0, 0, 5})
1352 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1354 assert json_response_and_validate_schema(res, 400) ==
1355 %{"error" => "Missing parameter: email"}
1359 |> Map.put(:remote_ip, {127, 0, 0, 6})
1360 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1362 assert json_response_and_validate_schema(res, 400) == %{
1363 "error" => "{\"email\":[\"can't be blank\"]}"
1367 test "allow registration without an email", %{conn: conn, valid_params: valid_params} do
1368 app_token = insert(:oauth_token, user: nil)
1369 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1373 |> put_req_header("content-type", "application/json")
1374 |> Map.put(:remote_ip, {127, 0, 0, 7})
1375 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1377 assert json_response_and_validate_schema(res, 200)
1380 test "allow registration with an empty email", %{conn: conn, valid_params: valid_params} do
1381 app_token = insert(:oauth_token, user: nil)
1382 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1386 |> put_req_header("content-type", "application/json")
1387 |> Map.put(:remote_ip, {127, 0, 0, 8})
1388 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1390 assert json_response_and_validate_schema(res, 200)
1393 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
1396 |> put_req_header("authorization", "Bearer " <> "invalid-token")
1397 |> put_req_header("content-type", "multipart/form-data")
1398 |> post("/api/v1/accounts", valid_params)
1400 assert json_response_and_validate_schema(res, 403) == %{"error" => "Invalid credentials"}
1403 test "registration from trusted app" do
1404 clear_config([Pleroma.Captcha, :enabled], true)
1405 app = insert(:oauth_app, trusted: true, scopes: ["read", "write", "follow", "push"])
1409 |> post("/oauth/token", %{
1410 "grant_type" => "client_credentials",
1411 "client_id" => app.client_id,
1412 "client_secret" => app.client_secret
1415 assert %{"access_token" => token, "token_type" => "Bearer"} = json_response(conn, 200)
1419 |> Plug.Conn.put_req_header("authorization", "Bearer " <> token)
1420 |> put_req_header("content-type", "multipart/form-data")
1421 |> post("/api/v1/accounts", %{
1422 nickname: "nickanme",
1424 email: "email@example.com",
1427 password: "some_password",
1428 confirm: "some_password"
1430 |> json_response_and_validate_schema(200)
1433 "access_token" => access_token,
1435 "scope" => "read write follow push",
1436 "token_type" => "Bearer"
1441 |> Plug.Conn.put_req_header("authorization", "Bearer " <> access_token)
1442 |> get("/api/v1/accounts/verify_credentials")
1443 |> json_response_and_validate_schema(200)
1448 "display_name" => "Lain",
1449 "follow_requests_count" => 0,
1450 "followers_count" => 0,
1451 "following_count" => 0,
1458 "actor_type" => "Person",
1459 "discoverable" => false,
1460 "no_rich_text" => false,
1463 "privacy" => "public",
1464 "sensitive" => false
1466 "statuses_count" => 0,
1467 "username" => "Lain"
1472 describe "create account by app / rate limit" do
1473 setup do: clear_config([:rate_limit, :app_account_creation], {10_000, 2})
1475 test "respects rate limit setting", %{conn: conn} do
1476 app_token = insert(:oauth_token, user: nil)
1480 |> put_req_header("authorization", "Bearer " <> app_token.token)
1481 |> Map.put(:remote_ip, {15, 15, 15, 15})
1482 |> put_req_header("content-type", "multipart/form-data")
1487 |> post("/api/v1/accounts", %{
1488 username: "#{i}lain",
1489 email: "#{i}lain@example.org",
1490 password: "PlzDontHackLain",
1495 "access_token" => token,
1496 "created_at" => _created_at,
1498 "token_type" => "Bearer"
1499 } = json_response_and_validate_schema(conn, 200)
1501 token_from_db = Repo.get_by(Token, token: token)
1502 assert token_from_db
1503 token_from_db = Repo.preload(token_from_db, :user)
1504 assert token_from_db.user
1508 post(conn, "/api/v1/accounts", %{
1510 email: "6lain@example.org",
1511 password: "PlzDontHackLain",
1515 assert json_response_and_validate_schema(conn, :too_many_requests) == %{
1516 "error" => "Throttled"
1521 describe "create account with enabled captcha" do
1522 setup %{conn: conn} do
1523 app_token = insert(:oauth_token, user: nil)
1527 |> put_req_header("authorization", "Bearer " <> app_token.token)
1528 |> put_req_header("content-type", "multipart/form-data")
1533 setup do: clear_config([Pleroma.Captcha, :enabled], true)
1535 test "creates an account and returns 200 if captcha is valid", %{conn: conn} do
1536 %{token: token, answer_data: answer_data} = Pleroma.Captcha.new()
1540 email: "lain@example.org",
1541 password: "PlzDontHackLain",
1543 captcha_solution: Pleroma.Captcha.Mock.solution(),
1544 captcha_token: token,
1545 captcha_answer_data: answer_data
1549 "access_token" => access_token,
1552 "token_type" => "Bearer"
1555 |> post("/api/v1/accounts", params)
1556 |> json_response_and_validate_schema(:ok)
1558 assert Token |> Repo.get_by(token: access_token) |> Repo.preload(:user) |> Map.get(:user)
1561 test "returns 400 if any captcha field is not provided", %{conn: conn} do
1562 captcha_fields = [:captcha_solution, :captcha_token, :captcha_answer_data]
1566 email: "lain@example.org",
1567 password: "PlzDontHackLain",
1569 captcha_solution: "xx",
1570 captcha_token: "xx",
1571 captcha_answer_data: "xx"
1574 for field <- captcha_fields do
1576 "error" => "{\"captcha\":[\"Invalid CAPTCHA (Missing parameter: #{field})\"]}"
1581 |> post("/api/v1/accounts", Map.delete(valid_params, field))
1582 |> json_response_and_validate_schema(:bad_request)
1586 test "returns an error if captcha is invalid", %{conn: conn} do
1589 email: "lain@example.org",
1590 password: "PlzDontHackLain",
1592 captcha_solution: "cofe",
1593 captcha_token: "cofe",
1594 captcha_answer_data: "cofe"
1597 assert %{"error" => "{\"captcha\":[\"Invalid answer data\"]}"} ==
1599 |> post("/api/v1/accounts", params)
1600 |> json_response_and_validate_schema(:bad_request)
1604 describe "create account with language" do
1605 setup %{conn: conn} do
1606 app_token = insert(:oauth_token, user: nil)
1610 |> put_req_header("authorization", "Bearer " <> app_token.token)
1611 |> put_req_header("content-type", "multipart/form-data")
1612 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
1613 |> SetLocalePlug.call([])
1618 test "creates an account with language parameter", %{conn: conn} do
1621 email: "foo@example.org",
1629 |> post("/api/v1/accounts", params)
1631 assert json_response_and_validate_schema(res, 200)
1633 assert %{language: "ru"} = Pleroma.User.get_by_nickname("foo")
1636 test "language parameter should be normalized", %{conn: conn} do
1639 email: "foo@example.org",
1647 |> post("/api/v1/accounts", params)
1649 assert json_response_and_validate_schema(res, 200)
1651 assert %{language: "ru_RU"} = Pleroma.User.get_by_nickname("foo")
1654 test "createing an account without language parameter should fallback to cookie/header language",
1658 email: "foo2@example.org",
1665 |> post("/api/v1/accounts", params)
1667 assert json_response_and_validate_schema(res, 200)
1669 assert %{language: "zh_Hans"} = Pleroma.User.get_by_nickname("foo2")
1673 describe "GET /api/v1/accounts/:id/lists - account_lists" do
1674 test "returns lists to which the account belongs" do
1675 %{user: user, conn: conn} = oauth_access(["read:lists"])
1676 other_user = insert(:user)
1677 assert {:ok, %Pleroma.List{id: _list_id} = list} = Pleroma.List.create("Test List", user)
1678 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
1680 assert [%{"id" => _list_id, "title" => "Test List"}] =
1682 |> get("/api/v1/accounts/#{other_user.id}/lists")
1683 |> json_response_and_validate_schema(200)
1687 describe "verify_credentials" do
1688 test "verify_credentials" do
1689 %{user: user, conn: conn} = oauth_access(["read:accounts"])
1691 [notification | _] =
1692 insert_list(7, :notification, user: user, activity: insert(:note_activity))
1694 Pleroma.Notification.set_read_up_to(user, notification.id)
1695 conn = get(conn, "/api/v1/accounts/verify_credentials")
1697 response = json_response_and_validate_schema(conn, 200)
1699 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
1700 assert response["pleroma"]["unread_notifications_count"] == 6
1701 assert id == to_string(user.id)
1704 test "verify_credentials default scope unlisted" do
1705 user = insert(:user, default_scope: "unlisted")
1706 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1708 conn = get(conn, "/api/v1/accounts/verify_credentials")
1710 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} =
1711 json_response_and_validate_schema(conn, 200)
1713 assert id == to_string(user.id)
1716 test "locked accounts" do
1717 user = insert(:user, default_scope: "private")
1718 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1720 conn = get(conn, "/api/v1/accounts/verify_credentials")
1722 assert %{"id" => id, "source" => %{"privacy" => "private"}} =
1723 json_response_and_validate_schema(conn, 200)
1725 assert id == to_string(user.id)
1729 describe "user relationships" do
1730 setup do: oauth_access(["read:follows"])
1732 test "returns the relationships for the current user", %{user: user, conn: conn} do
1733 %{id: other_user_id} = other_user = insert(:user)
1734 {:ok, _user, _other_user} = User.follow(user, other_user)
1736 assert [%{"id" => ^other_user_id}] =
1738 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
1739 |> json_response_and_validate_schema(200)
1741 assert [%{"id" => ^other_user_id}] =
1743 |> get("/api/v1/accounts/relationships?id[]=#{other_user.id}")
1744 |> json_response_and_validate_schema(200)
1747 test "returns an empty list on a bad request", %{conn: conn} do
1748 conn = get(conn, "/api/v1/accounts/relationships", %{})
1750 assert [] = json_response_and_validate_schema(conn, 200)
1754 test "getting a list of mutes" do
1755 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1756 %{id: id1} = other_user1 = insert(:user)
1757 %{id: id2} = other_user2 = insert(:user)
1758 %{id: id3} = other_user3 = insert(:user)
1760 {:ok, _user_relationships} = User.mute(user, other_user1)
1761 {:ok, _user_relationships} = User.mute(user, other_user2)
1762 {:ok, _user_relationships} = User.mute(user, other_user3)
1766 |> get("/api/v1/mutes")
1767 |> json_response_and_validate_schema(200)
1769 assert [id1, id2, id3] == Enum.map(result, & &1["id"])
1773 |> get("/api/v1/mutes?limit=1")
1774 |> json_response_and_validate_schema(200)
1776 assert [%{"id" => ^id1}] = result
1780 |> get("/api/v1/mutes?since_id=#{id1}")
1781 |> json_response_and_validate_schema(200)
1783 assert [%{"id" => ^id2}, %{"id" => ^id3}] = result
1787 |> get("/api/v1/mutes?since_id=#{id1}&max_id=#{id3}")
1788 |> json_response_and_validate_schema(200)
1790 assert [%{"id" => ^id2}] = result
1794 |> get("/api/v1/mutes?since_id=#{id1}&limit=1")
1795 |> json_response_and_validate_schema(200)
1797 assert [%{"id" => ^id2}] = result
1800 test "list of mutes with with_relationships parameter" do
1801 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1802 %{id: id1} = other_user1 = insert(:user)
1803 %{id: id2} = other_user2 = insert(:user)
1804 %{id: id3} = other_user3 = insert(:user)
1806 {:ok, _, _} = User.follow(other_user1, user)
1807 {:ok, _, _} = User.follow(other_user2, user)
1808 {:ok, _, _} = User.follow(other_user3, user)
1810 {:ok, _} = User.mute(user, other_user1)
1811 {:ok, _} = User.mute(user, other_user2)
1812 {:ok, _} = User.mute(user, other_user3)
1817 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1821 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1825 "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
1829 |> get("/api/v1/mutes?with_relationships=true")
1830 |> json_response_and_validate_schema(200)
1833 test "getting a list of blocks" do
1834 %{user: user, conn: conn} = oauth_access(["read:blocks"])
1835 %{id: id1} = other_user1 = insert(:user)
1836 %{id: id2} = other_user2 = insert(:user)
1837 %{id: id3} = other_user3 = insert(:user)
1839 {:ok, _user_relationship} = User.block(user, other_user1)
1840 {:ok, _user_relationship} = User.block(user, other_user3)
1841 {:ok, _user_relationship} = User.block(user, other_user2)
1845 |> assign(:user, user)
1846 |> get("/api/v1/blocks")
1847 |> json_response_and_validate_schema(200)
1849 assert [id1, id2, id3] == Enum.map(result, & &1["id"])
1853 |> assign(:user, user)
1854 |> get("/api/v1/blocks?limit=1")
1855 |> json_response_and_validate_schema(200)
1857 assert [%{"id" => ^id1}] = result
1861 |> assign(:user, user)
1862 |> get("/api/v1/blocks?since_id=#{id1}")
1863 |> json_response_and_validate_schema(200)
1865 assert [%{"id" => ^id2}, %{"id" => ^id3}] = result
1869 |> assign(:user, user)
1870 |> get("/api/v1/blocks?since_id=#{id1}&max_id=#{id3}")
1871 |> json_response_and_validate_schema(200)
1873 assert [%{"id" => ^id2}] = result
1877 |> assign(:user, user)
1878 |> get("/api/v1/blocks?since_id=#{id1}&limit=1")
1879 |> json_response_and_validate_schema(200)
1881 assert [%{"id" => ^id2}] = result
1884 test "account lookup", %{conn: conn} do
1885 %{nickname: acct} = insert(:user, %{nickname: "nickname"})
1886 %{nickname: acct_two} = insert(:user, %{nickname: "nickname@notlocaldoma.in"})
1890 |> get("/api/v1/accounts/lookup?acct=#{acct}")
1891 |> json_response_and_validate_schema(200)
1893 assert %{"acct" => ^acct} = result
1897 |> get("/api/v1/accounts/lookup?acct=#{acct_two}")
1898 |> json_response_and_validate_schema(200)
1900 assert %{"acct" => ^acct_two} = result
1904 |> get("/api/v1/accounts/lookup?acct=unexisting_nickname")
1905 |> json_response_and_validate_schema(404)
1908 test "create a note on a user" do
1909 %{conn: conn} = oauth_access(["write:accounts", "read:follows"])
1910 other_user = insert(:user)
1913 |> put_req_header("content-type", "application/json")
1914 |> post("/api/v1/accounts/#{other_user.id}/note", %{
1915 "comment" => "Example note"
1918 assert [%{"note" => "Example note"}] =
1920 |> put_req_header("content-type", "application/json")
1921 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
1922 |> json_response_and_validate_schema(200)