1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 "works by nickname" do
35 assert %{"id" => user_id} =
37 |> get("/api/v1/accounts/#{user.nickname}")
38 |> json_response_and_validate_schema(200)
41 test "works by nickname for remote users" do
42 clear_config([:instance, :limit_to_local_content], false)
44 user = insert(:user, nickname: "user@example.com", local: false)
46 assert %{"id" => user_id} =
48 |> get("/api/v1/accounts/#{user.nickname}")
49 |> json_response_and_validate_schema(200)
52 test "respects limit_to_local_content == :all for remote user nicknames" do
53 clear_config([:instance, :limit_to_local_content], :all)
55 user = insert(:user, nickname: "user@example.com", local: false)
58 |> get("/api/v1/accounts/#{user.nickname}")
59 |> json_response_and_validate_schema(404)
62 test "respects limit_to_local_content == :unauthenticated for remote user nicknames" do
63 clear_config([:instance, :limit_to_local_content], :unauthenticated)
65 user = insert(:user, nickname: "user@example.com", local: false)
66 reading_user = insert(:user)
70 |> get("/api/v1/accounts/#{user.nickname}")
72 assert json_response_and_validate_schema(conn, 404)
76 |> assign(:user, reading_user)
77 |> assign(:token, insert(:oauth_token, user: reading_user, scopes: ["read:accounts"]))
78 |> get("/api/v1/accounts/#{user.nickname}")
80 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
84 test "accounts fetches correct account for nicknames beginning with numbers", %{conn: conn} do
85 # Need to set an old-style integer ID to reproduce the problem
86 # (these are no longer assigned to new accounts but were preserved
87 # for existing accounts during the migration to flakeIDs)
88 user_one = insert(:user, %{id: 1212})
89 user_two = insert(:user, %{nickname: "#{user_one.id}garbage"})
93 |> get("/api/v1/accounts/#{user_one.id}")
94 |> json_response_and_validate_schema(:ok)
98 |> get("/api/v1/accounts/#{user_two.nickname}")
99 |> json_response_and_validate_schema(:ok)
103 |> get("/api/v1/accounts/#{user_two.id}")
104 |> json_response_and_validate_schema(:ok)
106 refute acc_one == acc_two
107 assert acc_two == acc_three
110 test "returns 404 when user is invisible", %{conn: conn} do
111 user = insert(:user, %{invisible: true})
113 assert %{"error" => "Can't find user"} =
115 |> get("/api/v1/accounts/#{user.nickname}")
116 |> json_response_and_validate_schema(404)
119 test "returns 404 for internal.fetch actor", %{conn: conn} do
120 %User{nickname: "internal.fetch"} = InternalFetchActor.get_actor()
122 assert %{"error" => "Can't find user"} =
124 |> get("/api/v1/accounts/internal.fetch")
125 |> json_response_and_validate_schema(404)
128 test "returns 404 for deactivated user", %{conn: conn} do
129 user = insert(:user, deactivated: true)
131 assert %{"error" => "Can't find user"} =
133 |> get("/api/v1/accounts/#{user.id}")
134 |> json_response_and_validate_schema(:not_found)
138 defp local_and_remote_users do
139 local = insert(:user)
140 remote = insert(:user, local: false)
141 {:ok, local: local, remote: remote}
144 describe "user fetching with restrict unauthenticated profiles for local and remote" do
145 setup do: local_and_remote_users()
147 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
149 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
151 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
152 assert %{"error" => "This API requires an authenticated user"} ==
154 |> get("/api/v1/accounts/#{local.id}")
155 |> json_response_and_validate_schema(:unauthorized)
157 assert %{"error" => "This API requires an authenticated user"} ==
159 |> get("/api/v1/accounts/#{remote.id}")
160 |> json_response_and_validate_schema(:unauthorized)
163 test "if user is authenticated", %{local: local, remote: remote} do
164 %{conn: conn} = oauth_access(["read"])
166 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
167 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
169 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
170 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
174 describe "user fetching with restrict unauthenticated profiles for local" do
175 setup do: local_and_remote_users()
177 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
179 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
180 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
182 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
183 "error" => "This API requires an authenticated user"
186 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
187 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
190 test "if user is authenticated", %{local: local, remote: remote} do
191 %{conn: conn} = oauth_access(["read"])
193 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
194 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
196 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
197 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
201 describe "user fetching with restrict unauthenticated profiles for remote" do
202 setup do: local_and_remote_users()
204 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
206 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
207 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
208 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
210 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
212 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
213 "error" => "This API requires an authenticated user"
217 test "if user is authenticated", %{local: local, remote: remote} do
218 %{conn: conn} = oauth_access(["read"])
220 res_conn = get(conn, "/api/v1/accounts/#{local.id}")
221 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
223 res_conn = get(conn, "/api/v1/accounts/#{remote.id}")
224 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
228 describe "user timelines" do
229 setup do: oauth_access(["read:statuses"])
231 test "works with announces that are just addressed to public", %{conn: conn} do
232 user = insert(:user, ap_id: "https://honktest/u/test", local: false)
233 other_user = insert(:user)
235 {:ok, post} = CommonAPI.post(other_user, %{status: "bonkeronk"})
239 "@context" => "https://www.w3.org/ns/activitystreams",
240 "actor" => "https://honktest/u/test",
241 "id" => "https://honktest/u/test/bonk/1793M7B9MQ48847vdx",
242 "object" => post.data["object"],
243 "published" => "2019-06-25T19:33:58Z",
244 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
247 |> ActivityPub.persist(local: false)
251 |> get("/api/v1/accounts/#{user.id}/statuses")
252 |> json_response_and_validate_schema(200)
254 assert [%{"id" => id}] = resp
255 assert id == announce.id
258 test "deactivated user", %{conn: conn} do
259 user = insert(:user, deactivated: true)
261 assert %{"error" => "Can't find user"} ==
263 |> get("/api/v1/accounts/#{user.id}/statuses")
264 |> json_response_and_validate_schema(:not_found)
267 test "returns 404 when user is invisible", %{conn: conn} do
268 user = insert(:user, %{invisible: true})
270 assert %{"error" => "Can't find user"} =
272 |> get("/api/v1/accounts/#{user.id}")
273 |> json_response_and_validate_schema(404)
276 test "respects blocks", %{user: user_one, conn: conn} do
277 user_two = insert(:user)
278 user_three = insert(:user)
280 User.block(user_one, user_two)
282 {:ok, activity} = CommonAPI.post(user_two, %{status: "User one sux0rz"})
283 {:ok, repeat} = CommonAPI.repeat(activity.id, user_three)
287 |> get("/api/v1/accounts/#{user_two.id}/statuses")
288 |> json_response_and_validate_schema(200)
290 assert [%{"id" => id}] = resp
291 assert id == activity.id
293 # Even a blocked user will deliver the full user timeline, there would be
294 # no point in looking at a blocked users timeline otherwise
297 |> get("/api/v1/accounts/#{user_two.id}/statuses")
298 |> json_response_and_validate_schema(200)
300 assert [%{"id" => id}] = resp
301 assert id == activity.id
303 # Third user's timeline includes the repeat when viewed by unauthenticated user
306 |> get("/api/v1/accounts/#{user_three.id}/statuses")
307 |> json_response_and_validate_schema(200)
309 assert [%{"id" => id}] = resp
310 assert id == repeat.id
312 # When viewing a third user's timeline, the blocked users' statuses will NOT be shown
313 resp = get(conn, "/api/v1/accounts/#{user_three.id}/statuses")
315 assert [] == json_response_and_validate_schema(resp, 200)
318 test "gets users statuses", %{conn: conn} do
319 user_one = insert(:user)
320 user_two = insert(:user)
321 user_three = insert(:user)
323 {:ok, _user_three} = User.follow(user_three, user_one)
325 {:ok, activity} = CommonAPI.post(user_one, %{status: "HI!!!"})
327 {:ok, direct_activity} =
328 CommonAPI.post(user_one, %{
329 status: "Hi, @#{user_two.nickname}.",
333 {:ok, private_activity} =
334 CommonAPI.post(user_one, %{status: "private", visibility: "private"})
339 |> get("/api/v1/accounts/#{user_one.id}/statuses")
340 |> json_response_and_validate_schema(200)
342 assert [%{"id" => id}] = resp
343 assert id == to_string(activity.id)
347 |> assign(:user, user_two)
348 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
349 |> get("/api/v1/accounts/#{user_one.id}/statuses")
350 |> json_response_and_validate_schema(200)
352 assert [%{"id" => id_one}, %{"id" => id_two}] = resp
353 assert id_one == to_string(direct_activity.id)
354 assert id_two == to_string(activity.id)
358 |> assign(:user, user_three)
359 |> assign(:token, insert(:oauth_token, user: user_three, scopes: ["read:statuses"]))
360 |> get("/api/v1/accounts/#{user_one.id}/statuses")
361 |> json_response_and_validate_schema(200)
363 assert [%{"id" => id_one}, %{"id" => id_two}] = resp
364 assert id_one == to_string(private_activity.id)
365 assert id_two == to_string(activity.id)
368 test "unimplemented pinned statuses feature", %{conn: conn} do
369 note = insert(:note_activity)
370 user = User.get_cached_by_ap_id(note.data["actor"])
372 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?pinned=true")
374 assert json_response_and_validate_schema(conn, 200) == []
377 test "gets an users media, excludes reblogs", %{conn: conn} do
378 note = insert(:note_activity)
379 user = User.get_cached_by_ap_id(note.data["actor"])
380 other_user = insert(:user)
383 content_type: "image/jpg",
384 path: Path.absname("test/fixtures/image.jpg"),
385 filename: "an_image.jpg"
388 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: user.ap_id)
390 {:ok, %{id: image_post_id}} = CommonAPI.post(user, %{status: "cofe", media_ids: [media_id]})
392 {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: other_user.ap_id)
394 {:ok, %{id: other_image_post_id}} =
395 CommonAPI.post(other_user, %{status: "cofe2", media_ids: [media_id]})
397 {:ok, _announce} = CommonAPI.repeat(other_image_post_id, user)
399 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?only_media=true")
401 assert [%{"id" => ^image_post_id}] = json_response_and_validate_schema(conn, 200)
403 conn = get(build_conn(), "/api/v1/accounts/#{user.id}/statuses?only_media=1")
405 assert [%{"id" => ^image_post_id}] = json_response_and_validate_schema(conn, 200)
408 test "gets a user's statuses without reblogs", %{user: user, conn: conn} do
409 {:ok, %{id: post_id}} = CommonAPI.post(user, %{status: "HI!!!"})
410 {:ok, _} = CommonAPI.repeat(post_id, user)
412 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_reblogs=true")
413 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
415 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_reblogs=1")
416 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
419 test "filters user's statuses by a hashtag", %{user: user, conn: conn} do
420 {:ok, %{id: post_id}} = CommonAPI.post(user, %{status: "#hashtag"})
421 {:ok, _post} = CommonAPI.post(user, %{status: "hashtag"})
423 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?tagged=hashtag")
424 assert [%{"id" => ^post_id}] = json_response_and_validate_schema(conn, 200)
427 test "the user views their own timelines and excludes direct messages", %{
431 {:ok, %{id: public_activity_id}} =
432 CommonAPI.post(user, %{status: ".", visibility: "public"})
434 {:ok, _direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
436 conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_visibilities[]=direct")
437 assert [%{"id" => ^public_activity_id}] = json_response_and_validate_schema(conn, 200)
441 defp local_and_remote_activities(%{local: local, remote: remote}) do
442 insert(:note_activity, user: local)
443 insert(:note_activity, user: remote, local: false)
448 describe "statuses with restrict unauthenticated profiles for local and remote" do
449 setup do: local_and_remote_users()
450 setup :local_and_remote_activities
452 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
454 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
456 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
457 assert %{"error" => "This API requires an authenticated user"} ==
459 |> get("/api/v1/accounts/#{local.id}/statuses")
460 |> json_response_and_validate_schema(:unauthorized)
462 assert %{"error" => "This API requires an authenticated user"} ==
464 |> get("/api/v1/accounts/#{remote.id}/statuses")
465 |> json_response_and_validate_schema(:unauthorized)
468 test "if user is authenticated", %{local: local, remote: remote} do
469 %{conn: conn} = oauth_access(["read"])
471 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
472 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
474 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
475 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
479 describe "statuses with restrict unauthenticated profiles for local" do
480 setup do: local_and_remote_users()
481 setup :local_and_remote_activities
483 setup do: clear_config([:restrict_unauthenticated, :profiles, :local], true)
485 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
486 assert %{"error" => "This API requires an authenticated user"} ==
488 |> get("/api/v1/accounts/#{local.id}/statuses")
489 |> json_response_and_validate_schema(:unauthorized)
491 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
492 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
495 test "if user is authenticated", %{local: local, remote: remote} do
496 %{conn: conn} = oauth_access(["read"])
498 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
499 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
501 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
502 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
506 describe "statuses with restrict unauthenticated profiles for remote" do
507 setup do: local_and_remote_users()
508 setup :local_and_remote_activities
510 setup do: clear_config([:restrict_unauthenticated, :profiles, :remote], true)
512 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
513 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
514 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
516 assert %{"error" => "This API requires an authenticated user"} ==
518 |> get("/api/v1/accounts/#{remote.id}/statuses")
519 |> json_response_and_validate_schema(:unauthorized)
522 test "if user is authenticated", %{local: local, remote: remote} do
523 %{conn: conn} = oauth_access(["read"])
525 res_conn = get(conn, "/api/v1/accounts/#{local.id}/statuses")
526 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
528 res_conn = get(conn, "/api/v1/accounts/#{remote.id}/statuses")
529 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
533 describe "followers" do
534 setup do: oauth_access(["read:accounts"])
536 test "getting followers", %{user: user, conn: conn} do
537 other_user = insert(:user)
538 {:ok, %{id: user_id}} = User.follow(user, other_user)
540 conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers")
542 assert [%{"id" => ^user_id}] = json_response_and_validate_schema(conn, 200)
545 test "getting followers, hide_followers", %{user: user, conn: conn} do
546 other_user = insert(:user, hide_followers: true)
547 {:ok, _user} = User.follow(user, other_user)
549 conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers")
551 assert [] == json_response_and_validate_schema(conn, 200)
554 test "getting followers, hide_followers, same user requesting" do
556 other_user = insert(:user, hide_followers: true)
557 {:ok, _user} = User.follow(user, other_user)
561 |> assign(:user, other_user)
562 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
563 |> get("/api/v1/accounts/#{other_user.id}/followers")
565 refute [] == json_response_and_validate_schema(conn, 200)
568 test "getting followers, pagination", %{user: user, conn: conn} do
569 {:ok, %User{id: follower1_id}} = :user |> insert() |> User.follow(user)
570 {:ok, %User{id: follower2_id}} = :user |> insert() |> User.follow(user)
571 {:ok, %User{id: follower3_id}} = :user |> insert() |> User.follow(user)
573 assert [%{"id" => ^follower3_id}, %{"id" => ^follower2_id}] =
575 |> get("/api/v1/accounts/#{user.id}/followers?since_id=#{follower1_id}")
576 |> json_response_and_validate_schema(200)
578 assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] =
580 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3_id}")
581 |> json_response_and_validate_schema(200)
583 assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] =
586 "/api/v1/accounts/#{user.id}/followers?id=#{user.id}&limit=20&max_id=#{
590 |> json_response_and_validate_schema(200)
592 res_conn = get(conn, "/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3_id}")
594 assert [%{"id" => ^follower2_id}] = json_response_and_validate_schema(res_conn, 200)
596 assert [link_header] = get_resp_header(res_conn, "link")
597 assert link_header =~ ~r/min_id=#{follower2_id}/
598 assert link_header =~ ~r/max_id=#{follower2_id}/
602 describe "following" do
603 setup do: oauth_access(["read:accounts"])
605 test "getting following", %{user: user, conn: conn} do
606 other_user = insert(:user)
607 {:ok, user} = User.follow(user, other_user)
609 conn = get(conn, "/api/v1/accounts/#{user.id}/following")
611 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
612 assert id == to_string(other_user.id)
615 test "getting following, hide_follows, other user requesting" do
616 user = insert(:user, hide_follows: true)
617 other_user = insert(:user)
618 {:ok, user} = User.follow(user, other_user)
622 |> assign(:user, other_user)
623 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
624 |> get("/api/v1/accounts/#{user.id}/following")
626 assert [] == json_response_and_validate_schema(conn, 200)
629 test "getting following, hide_follows, same user requesting" do
630 user = insert(:user, hide_follows: true)
631 other_user = insert(:user)
632 {:ok, user} = User.follow(user, other_user)
636 |> assign(:user, user)
637 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read:accounts"]))
638 |> get("/api/v1/accounts/#{user.id}/following")
640 refute [] == json_response_and_validate_schema(conn, 200)
643 test "getting following, pagination", %{user: user, conn: conn} do
644 following1 = insert(:user)
645 following2 = insert(:user)
646 following3 = insert(:user)
647 {:ok, _} = User.follow(user, following1)
648 {:ok, _} = User.follow(user, following2)
649 {:ok, _} = User.follow(user, following3)
651 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}")
653 assert [%{"id" => id3}, %{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
654 assert id3 == following3.id
655 assert id2 == following2.id
657 res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?max_id=#{following3.id}")
659 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
660 assert id2 == following2.id
661 assert id1 == following1.id
666 "/api/v1/accounts/#{user.id}/following?id=#{user.id}&limit=20&max_id=#{following3.id}"
669 assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200)
670 assert id2 == following2.id
671 assert id1 == following1.id
674 get(conn, "/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")
676 assert [%{"id" => id2}] = json_response_and_validate_schema(res_conn, 200)
677 assert id2 == following2.id
679 assert [link_header] = get_resp_header(res_conn, "link")
680 assert link_header =~ ~r/min_id=#{following2.id}/
681 assert link_header =~ ~r/max_id=#{following2.id}/
685 describe "follow/unfollow" do
686 setup do: oauth_access(["follow"])
688 test "following / unfollowing a user", %{conn: conn} do
689 %{id: other_user_id, nickname: other_user_nickname} = insert(:user)
691 assert %{"id" => _id, "following" => true} =
693 |> post("/api/v1/accounts/#{other_user_id}/follow")
694 |> json_response_and_validate_schema(200)
696 assert %{"id" => _id, "following" => false} =
698 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
699 |> json_response_and_validate_schema(200)
701 assert %{"id" => ^other_user_id} =
703 |> put_req_header("content-type", "application/json")
704 |> post("/api/v1/follows", %{"uri" => other_user_nickname})
705 |> json_response_and_validate_schema(200)
708 test "cancelling follow request", %{conn: conn} do
709 %{id: other_user_id} = insert(:user, %{locked: true})
711 assert %{"id" => ^other_user_id, "following" => false, "requested" => true} =
713 |> post("/api/v1/accounts/#{other_user_id}/follow")
714 |> json_response_and_validate_schema(:ok)
716 assert %{"id" => ^other_user_id, "following" => false, "requested" => false} =
718 |> post("/api/v1/accounts/#{other_user_id}/unfollow")
719 |> json_response_and_validate_schema(:ok)
722 test "following without reblogs" do
723 %{conn: conn} = oauth_access(["follow", "read:statuses"])
724 followed = insert(:user)
725 other_user = insert(:user)
729 |> put_req_header("content-type", "application/json")
730 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
732 assert %{"showing_reblogs" => false} = json_response_and_validate_schema(ret_conn, 200)
734 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
735 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
739 |> get("/api/v1/timelines/home")
740 |> json_response(200)
742 assert %{"showing_reblogs" => true} =
744 |> put_req_header("content-type", "application/json")
745 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: true})
746 |> json_response_and_validate_schema(200)
748 assert [%{"id" => ^reblog_id}] =
750 |> get("/api/v1/timelines/home")
751 |> json_response(200)
754 test "following with reblogs" do
755 %{conn: conn} = oauth_access(["follow", "read:statuses"])
756 followed = insert(:user)
757 other_user = insert(:user)
759 ret_conn = post(conn, "/api/v1/accounts/#{followed.id}/follow")
761 assert %{"showing_reblogs" => true} = json_response_and_validate_schema(ret_conn, 200)
763 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
764 {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed)
766 assert [%{"id" => ^reblog_id}] =
768 |> get("/api/v1/timelines/home")
769 |> json_response(200)
771 assert %{"showing_reblogs" => false} =
773 |> put_req_header("content-type", "application/json")
774 |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})
775 |> json_response_and_validate_schema(200)
779 |> get("/api/v1/timelines/home")
780 |> json_response(200)
783 test "following / unfollowing errors", %{user: user, conn: conn} do
785 conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
787 assert %{"error" => "Can not follow yourself"} =
788 json_response_and_validate_schema(conn_res, 400)
791 user = User.get_cached_by_id(user.id)
792 conn_res = post(conn, "/api/v1/accounts/#{user.id}/unfollow")
794 assert %{"error" => "Can not unfollow yourself"} =
795 json_response_and_validate_schema(conn_res, 400)
797 # self follow via uri
798 user = User.get_cached_by_id(user.id)
800 assert %{"error" => "Can not follow yourself"} =
802 |> put_req_header("content-type", "multipart/form-data")
803 |> post("/api/v1/follows", %{"uri" => user.nickname})
804 |> json_response_and_validate_schema(400)
806 # follow non existing user
807 conn_res = post(conn, "/api/v1/accounts/doesntexist/follow")
808 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
810 # follow non existing user via uri
813 |> put_req_header("content-type", "multipart/form-data")
814 |> post("/api/v1/follows", %{"uri" => "doesntexist"})
816 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
818 # unfollow non existing user
819 conn_res = post(conn, "/api/v1/accounts/doesntexist/unfollow")
820 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404)
824 describe "mute/unmute" do
825 setup do: oauth_access(["write:mutes"])
827 test "with notifications", %{conn: conn} do
828 other_user = insert(:user)
830 assert %{"id" => _id, "muting" => true, "muting_notifications" => true} =
832 |> post("/api/v1/accounts/#{other_user.id}/mute")
833 |> json_response_and_validate_schema(200)
835 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
837 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
838 json_response_and_validate_schema(conn, 200)
841 test "without notifications", %{conn: conn} do
842 other_user = insert(:user)
846 |> put_req_header("content-type", "multipart/form-data")
847 |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
849 assert %{"id" => _id, "muting" => true, "muting_notifications" => false} =
850 json_response_and_validate_schema(ret_conn, 200)
852 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
854 assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
855 json_response_and_validate_schema(conn, 200)
859 describe "pinned statuses" do
862 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
863 %{conn: conn} = oauth_access(["read:statuses"], user: user)
865 [conn: conn, user: user, activity: activity]
868 test "returns pinned statuses", %{conn: conn, user: user, activity: %{id: activity_id}} do
869 {:ok, _} = CommonAPI.pin(activity_id, user)
871 assert [%{"id" => ^activity_id, "pinned" => true}] =
873 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
874 |> json_response_and_validate_schema(200)
878 test "blocking / unblocking a user" do
879 %{conn: conn} = oauth_access(["follow"])
880 other_user = insert(:user)
882 ret_conn = post(conn, "/api/v1/accounts/#{other_user.id}/block")
884 assert %{"id" => _id, "blocking" => true} = json_response_and_validate_schema(ret_conn, 200)
886 conn = post(conn, "/api/v1/accounts/#{other_user.id}/unblock")
888 assert %{"id" => _id, "blocking" => false} = json_response_and_validate_schema(conn, 200)
891 describe "create account by app" do
895 email: "lain@example.org",
896 password: "PlzDontHackLain",
900 [valid_params: valid_params]
903 test "registers and logs in without :account_activation_required / :account_approval_required",
905 clear_config([:instance, :account_activation_required], false)
906 clear_config([:instance, :account_approval_required], false)
910 |> put_req_header("content-type", "application/json")
911 |> post("/api/v1/apps", %{
912 client_name: "client_name",
913 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
914 scopes: "read, write, follow"
918 "client_id" => client_id,
919 "client_secret" => client_secret,
921 "name" => "client_name",
922 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
925 } = json_response_and_validate_schema(conn, 200)
928 post(conn, "/oauth/token", %{
929 grant_type: "client_credentials",
930 client_id: client_id,
931 client_secret: client_secret
934 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
935 json_response(conn, 200)
938 token_from_db = Repo.get_by(Token, token: token)
941 assert scope == "read write follow"
943 clear_config([User, :email_blacklist], ["example.org"])
947 email: "lain@example.org",
948 password: "PlzDontHackLain",
955 |> put_req_header("content-type", "multipart/form-data")
956 |> put_req_header("authorization", "Bearer " <> token)
957 |> post("/api/v1/accounts", params)
959 assert %{"error" => "{\"email\":[\"Invalid email\"]}"} =
960 json_response_and_validate_schema(conn, 400)
962 Pleroma.Config.put([User, :email_blacklist], [])
966 |> put_req_header("content-type", "multipart/form-data")
967 |> put_req_header("authorization", "Bearer " <> token)
968 |> post("/api/v1/accounts", params)
971 "access_token" => token,
972 "created_at" => _created_at,
974 "token_type" => "Bearer"
975 } = json_response_and_validate_schema(conn, 200)
977 token_from_db = Repo.get_by(Token, token: token)
979 user = Repo.preload(token_from_db, :user).user
982 refute user.confirmation_pending
983 refute user.approval_pending
986 test "registers but does not log in with :account_activation_required", %{conn: conn} do
987 clear_config([:instance, :account_activation_required], true)
988 clear_config([:instance, :account_approval_required], false)
992 |> put_req_header("content-type", "application/json")
993 |> post("/api/v1/apps", %{
994 client_name: "client_name",
995 redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
996 scopes: "read, write, follow"
1000 "client_id" => client_id,
1001 "client_secret" => client_secret,
1003 "name" => "client_name",
1004 "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",
1007 } = json_response_and_validate_schema(conn, 200)
1010 post(conn, "/oauth/token", %{
1011 grant_type: "client_credentials",
1012 client_id: client_id,
1013 client_secret: client_secret
1016 assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} =
1017 json_response(conn, 200)
1020 token_from_db = Repo.get_by(Token, token: token)
1021 assert token_from_db
1023 assert scope == "read write follow"
1027 |> put_req_header("content-type", "multipart/form-data")
1028 |> put_req_header("authorization", "Bearer " <> token)
1029 |> post("/api/v1/accounts", %{
1031 email: "lain@example.org",
1032 password: "PlzDontHackLain",
1037 response = json_response_and_validate_schema(conn, 200)
1038 assert %{"identifier" => "missing_confirmed_email"} = response
1039 refute response["access_token"]
1040 refute response["token_type"]
1042 user = Repo.get_by(User, email: "lain@example.org")
1043 assert user.confirmation_pending
1046 test "registers but does not log in with :account_approval_required", %{conn: conn} do
1047 clear_config([:instance, :account_approval_required], true)
1048 clear_config([:instance, :account_activation_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"
1087 |> put_req_header("content-type", "multipart/form-data")
1088 |> put_req_header("authorization", "Bearer " <> token)
1089 |> post("/api/v1/accounts", %{
1091 email: "lain@example.org",
1092 password: "PlzDontHackLain",
1095 reason: "I'm a cool dude, bro"
1098 response = json_response_and_validate_schema(conn, 200)
1099 assert %{"identifier" => "awaiting_approval"} = response
1100 refute response["access_token"]
1101 refute response["token_type"]
1103 user = Repo.get_by(User, email: "lain@example.org")
1105 assert user.approval_pending
1106 assert user.registration_reason == "I'm a cool dude, bro"
1109 test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
1110 _user = insert(:user, email: "lain@example.org")
1111 app_token = insert(:oauth_token, user: nil)
1115 |> put_req_header("authorization", "Bearer " <> app_token.token)
1116 |> put_req_header("content-type", "application/json")
1117 |> post("/api/v1/accounts", valid_params)
1119 assert json_response_and_validate_schema(res, 400) == %{
1120 "error" => "{\"email\":[\"has already been taken\"]}"
1124 test "returns bad_request if missing required params", %{
1126 valid_params: valid_params
1128 app_token = insert(:oauth_token, user: nil)
1132 |> put_req_header("authorization", "Bearer " <> app_token.token)
1133 |> put_req_header("content-type", "application/json")
1135 res = post(conn, "/api/v1/accounts", valid_params)
1136 assert json_response_and_validate_schema(res, 200)
1138 [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
1139 |> Stream.zip(Map.delete(valid_params, :email))
1140 |> Enum.each(fn {ip, {attr, _}} ->
1143 |> Map.put(:remote_ip, ip)
1144 |> post("/api/v1/accounts", Map.delete(valid_params, attr))
1145 |> json_response_and_validate_schema(400)
1148 "error" => "Missing field: #{attr}.",
1151 "message" => "Missing field: #{attr}",
1152 "source" => %{"pointer" => "/#{attr}"},
1153 "title" => "Invalid value"
1160 test "returns bad_request if missing email params when :account_activation_required is enabled",
1161 %{conn: conn, valid_params: valid_params} do
1162 clear_config([:instance, :account_activation_required], true)
1164 app_token = insert(:oauth_token, user: nil)
1168 |> put_req_header("authorization", "Bearer " <> app_token.token)
1169 |> put_req_header("content-type", "application/json")
1173 |> Map.put(:remote_ip, {127, 0, 0, 5})
1174 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1176 assert json_response_and_validate_schema(res, 400) ==
1177 %{"error" => "Missing parameter: email"}
1181 |> Map.put(:remote_ip, {127, 0, 0, 6})
1182 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1184 assert json_response_and_validate_schema(res, 400) == %{
1185 "error" => "{\"email\":[\"can't be blank\"]}"
1189 test "allow registration without an email", %{conn: conn, valid_params: valid_params} do
1190 app_token = insert(:oauth_token, user: nil)
1191 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1195 |> put_req_header("content-type", "application/json")
1196 |> Map.put(:remote_ip, {127, 0, 0, 7})
1197 |> post("/api/v1/accounts", Map.delete(valid_params, :email))
1199 assert json_response_and_validate_schema(res, 200)
1202 test "allow registration with an empty email", %{conn: conn, valid_params: valid_params} do
1203 app_token = insert(:oauth_token, user: nil)
1204 conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
1208 |> put_req_header("content-type", "application/json")
1209 |> Map.put(:remote_ip, {127, 0, 0, 8})
1210 |> post("/api/v1/accounts", Map.put(valid_params, :email, ""))
1212 assert json_response_and_validate_schema(res, 200)
1215 test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
1218 |> put_req_header("authorization", "Bearer " <> "invalid-token")
1219 |> put_req_header("content-type", "multipart/form-data")
1220 |> post("/api/v1/accounts", valid_params)
1222 assert json_response_and_validate_schema(res, 403) == %{"error" => "Invalid credentials"}
1225 test "registration from trusted app" do
1226 clear_config([Pleroma.Captcha, :enabled], true)
1227 app = insert(:oauth_app, trusted: true, scopes: ["read", "write", "follow", "push"])
1231 |> post("/oauth/token", %{
1232 "grant_type" => "client_credentials",
1233 "client_id" => app.client_id,
1234 "client_secret" => app.client_secret
1237 assert %{"access_token" => token, "token_type" => "Bearer"} = json_response(conn, 200)
1241 |> Plug.Conn.put_req_header("authorization", "Bearer " <> token)
1242 |> put_req_header("content-type", "multipart/form-data")
1243 |> post("/api/v1/accounts", %{
1244 nickname: "nickanme",
1246 email: "email@example.com",
1249 password: "some_password",
1250 confirm: "some_password"
1252 |> json_response_and_validate_schema(200)
1255 "access_token" => access_token,
1257 "scope" => "read write follow push",
1258 "token_type" => "Bearer"
1263 |> Plug.Conn.put_req_header("authorization", "Bearer " <> access_token)
1264 |> get("/api/v1/accounts/verify_credentials")
1265 |> json_response_and_validate_schema(200)
1270 "display_name" => "Lain",
1271 "follow_requests_count" => 0,
1272 "followers_count" => 0,
1273 "following_count" => 0,
1280 "actor_type" => "Person",
1281 "discoverable" => false,
1282 "no_rich_text" => false,
1285 "privacy" => "public",
1286 "sensitive" => false
1288 "statuses_count" => 0,
1289 "username" => "Lain"
1294 describe "create account by app / rate limit" do
1295 setup do: clear_config([:rate_limit, :app_account_creation], {10_000, 2})
1297 test "respects rate limit setting", %{conn: conn} do
1298 app_token = insert(:oauth_token, user: nil)
1302 |> put_req_header("authorization", "Bearer " <> app_token.token)
1303 |> Map.put(:remote_ip, {15, 15, 15, 15})
1304 |> put_req_header("content-type", "multipart/form-data")
1309 |> post("/api/v1/accounts", %{
1310 username: "#{i}lain",
1311 email: "#{i}lain@example.org",
1312 password: "PlzDontHackLain",
1317 "access_token" => token,
1318 "created_at" => _created_at,
1320 "token_type" => "Bearer"
1321 } = json_response_and_validate_schema(conn, 200)
1323 token_from_db = Repo.get_by(Token, token: token)
1324 assert token_from_db
1325 token_from_db = Repo.preload(token_from_db, :user)
1326 assert token_from_db.user
1330 post(conn, "/api/v1/accounts", %{
1332 email: "6lain@example.org",
1333 password: "PlzDontHackLain",
1337 assert json_response_and_validate_schema(conn, :too_many_requests) == %{
1338 "error" => "Throttled"
1343 describe "create account with enabled captcha" do
1344 setup %{conn: conn} do
1345 app_token = insert(:oauth_token, user: nil)
1349 |> put_req_header("authorization", "Bearer " <> app_token.token)
1350 |> put_req_header("content-type", "multipart/form-data")
1355 setup do: clear_config([Pleroma.Captcha, :enabled], true)
1357 test "creates an account and returns 200 if captcha is valid", %{conn: conn} do
1358 %{token: token, answer_data: answer_data} = Pleroma.Captcha.new()
1362 email: "lain@example.org",
1363 password: "PlzDontHackLain",
1365 captcha_solution: Pleroma.Captcha.Mock.solution(),
1366 captcha_token: token,
1367 captcha_answer_data: answer_data
1371 "access_token" => access_token,
1374 "token_type" => "Bearer"
1377 |> post("/api/v1/accounts", params)
1378 |> json_response_and_validate_schema(:ok)
1380 assert Token |> Repo.get_by(token: access_token) |> Repo.preload(:user) |> Map.get(:user)
1382 Cachex.del(:used_captcha_cache, token)
1385 test "returns 400 if any captcha field is not provided", %{conn: conn} do
1386 captcha_fields = [:captcha_solution, :captcha_token, :captcha_answer_data]
1390 email: "lain@example.org",
1391 password: "PlzDontHackLain",
1393 captcha_solution: "xx",
1394 captcha_token: "xx",
1395 captcha_answer_data: "xx"
1398 for field <- captcha_fields do
1400 "error" => "{\"captcha\":[\"Invalid CAPTCHA (Missing parameter: #{field})\"]}"
1405 |> post("/api/v1/accounts", Map.delete(valid_params, field))
1406 |> json_response_and_validate_schema(:bad_request)
1410 test "returns an error if captcha is invalid", %{conn: conn} do
1413 email: "lain@example.org",
1414 password: "PlzDontHackLain",
1416 captcha_solution: "cofe",
1417 captcha_token: "cofe",
1418 captcha_answer_data: "cofe"
1421 assert %{"error" => "{\"captcha\":[\"Invalid answer data\"]}"} ==
1423 |> post("/api/v1/accounts", params)
1424 |> json_response_and_validate_schema(:bad_request)
1428 describe "GET /api/v1/accounts/:id/lists - account_lists" do
1429 test "returns lists to which the account belongs" do
1430 %{user: user, conn: conn} = oauth_access(["read:lists"])
1431 other_user = insert(:user)
1432 assert {:ok, %Pleroma.List{id: list_id} = list} = Pleroma.List.create("Test List", user)
1433 {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
1435 assert [%{"id" => list_id, "title" => "Test List"}] =
1437 |> get("/api/v1/accounts/#{other_user.id}/lists")
1438 |> json_response_and_validate_schema(200)
1442 describe "verify_credentials" do
1443 test "verify_credentials" do
1444 %{user: user, conn: conn} = oauth_access(["read:accounts"])
1446 [notification | _] =
1447 insert_list(7, :notification, user: user, activity: insert(:note_activity))
1449 Pleroma.Notification.set_read_up_to(user, notification.id)
1450 conn = get(conn, "/api/v1/accounts/verify_credentials")
1452 response = json_response_and_validate_schema(conn, 200)
1454 assert %{"id" => id, "source" => %{"privacy" => "public"}} = response
1455 assert response["pleroma"]["chat_token"]
1456 assert response["pleroma"]["unread_notifications_count"] == 6
1457 assert id == to_string(user.id)
1460 test "verify_credentials default scope unlisted" do
1461 user = insert(:user, default_scope: "unlisted")
1462 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1464 conn = get(conn, "/api/v1/accounts/verify_credentials")
1466 assert %{"id" => id, "source" => %{"privacy" => "unlisted"}} =
1467 json_response_and_validate_schema(conn, 200)
1469 assert id == to_string(user.id)
1472 test "locked accounts" do
1473 user = insert(:user, default_scope: "private")
1474 %{conn: conn} = oauth_access(["read:accounts"], user: user)
1476 conn = get(conn, "/api/v1/accounts/verify_credentials")
1478 assert %{"id" => id, "source" => %{"privacy" => "private"}} =
1479 json_response_and_validate_schema(conn, 200)
1481 assert id == to_string(user.id)
1485 describe "user relationships" do
1486 setup do: oauth_access(["read:follows"])
1488 test "returns the relationships for the current user", %{user: user, conn: conn} do
1489 %{id: other_user_id} = other_user = insert(:user)
1490 {:ok, _user} = User.follow(user, other_user)
1492 assert [%{"id" => ^other_user_id}] =
1494 |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
1495 |> json_response_and_validate_schema(200)
1497 assert [%{"id" => ^other_user_id}] =
1499 |> get("/api/v1/accounts/relationships?id[]=#{other_user.id}")
1500 |> json_response_and_validate_schema(200)
1503 test "returns an empty list on a bad request", %{conn: conn} do
1504 conn = get(conn, "/api/v1/accounts/relationships", %{})
1506 assert [] = json_response_and_validate_schema(conn, 200)
1510 test "getting a list of mutes" do
1511 %{user: user, conn: conn} = oauth_access(["read:mutes"])
1512 other_user = insert(:user)
1514 {:ok, _user_relationships} = User.mute(user, other_user)
1516 conn = get(conn, "/api/v1/mutes")
1518 other_user_id = to_string(other_user.id)
1519 assert [%{"id" => ^other_user_id}] = json_response_and_validate_schema(conn, 200)
1522 test "getting a list of blocks" do
1523 %{user: user, conn: conn} = oauth_access(["read:blocks"])
1524 other_user = insert(:user)
1526 {:ok, _user_relationship} = User.block(user, other_user)
1530 |> assign(:user, user)
1531 |> get("/api/v1/blocks")
1533 other_user_id = to_string(other_user.id)
1534 assert [%{"id" => ^other_user_id}] = json_response_and_validate_schema(conn, 200)