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.AdminAPI.UserControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
10 import Pleroma.Factory
13 alias Pleroma.ModerationLog
15 alias Pleroma.Tests.ObanHelpers
18 alias Pleroma.Web.ActivityPub.Relay
19 alias Pleroma.Web.CommonAPI
20 alias Pleroma.Web.MediaProxy
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
29 admin = insert(:user, is_admin: true)
30 token = insert(:oauth_admin_token, user: admin)
34 |> assign(:user, admin)
35 |> assign(:token, token)
37 {:ok, %{admin: admin, token: token, conn: conn}}
40 test "with valid `admin_token` query parameter, skips OAuth scopes check" do
41 clear_config([:admin_token], "password123")
45 conn = get(build_conn(), "/api/pleroma/admin/users/#{user.nickname}?admin_token=password123")
47 assert json_response(conn, 200)
50 test "GET /api/pleroma/admin/users/:nickname requires admin:read:accounts or broader scope",
53 url = "/api/pleroma/admin/users/#{user.nickname}"
55 good_token1 = insert(:oauth_token, user: admin, scopes: ["admin"])
56 good_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read"])
57 good_token3 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts"])
59 bad_token1 = insert(:oauth_token, user: admin, scopes: ["read:accounts"])
60 bad_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts:partial"])
63 for good_token <- [good_token1, good_token2, good_token3] do
66 |> assign(:user, admin)
67 |> assign(:token, good_token)
70 assert json_response(conn, 200)
73 for good_token <- [good_token1, good_token2, good_token3] do
77 |> assign(:token, good_token)
80 assert json_response(conn, :forbidden)
83 for bad_token <- [bad_token1, bad_token2, bad_token3] do
86 |> assign(:user, admin)
87 |> assign(:token, bad_token)
90 assert json_response(conn, :forbidden)
94 describe "DELETE /api/pleroma/admin/users" do
95 test "single user", %{admin: admin, conn: conn} do
96 clear_config([:instance, :federating], true)
100 avatar: %{"url" => [%{"href" => "https://someurl"}]},
101 banner: %{"url" => [%{"href" => "https://somebanner"}]},
106 # Create some activities to check they got deleted later
107 follower = insert(:user)
108 {:ok, _} = CommonAPI.post(user, %{status: "test"})
109 {:ok, _, _, _} = CommonAPI.follow(user, follower)
110 {:ok, _, _, _} = CommonAPI.follow(follower, user)
111 user = Repo.get(User, user.id)
112 assert user.note_count == 1
113 assert user.follower_count == 1
114 assert user.following_count == 1
115 assert user.is_active
117 with_mock Pleroma.Web.Federator,
118 publish: fn _ -> nil end,
119 perform: fn _, _ -> nil end do
122 |> put_req_header("accept", "application/json")
123 |> delete("/api/pleroma/admin/users?nickname=#{user.nickname}")
125 ObanHelpers.perform_all()
127 refute User.get_by_nickname(user.nickname).is_active
129 log_entry = Repo.one(ModerationLog)
131 assert ModerationLog.get_log_entry_message(log_entry) ==
132 "@#{admin.nickname} deleted users: @#{user.nickname}"
134 assert json_response(conn, 200) == [user.nickname]
136 user = Repo.get(User, user.id)
137 refute user.is_active
139 assert user.avatar == %{}
140 assert user.banner == %{}
141 assert user.note_count == 0
142 assert user.follower_count == 0
143 assert user.following_count == 0
144 assert user.bio == ""
145 assert user.name == nil
147 assert called(Pleroma.Web.Federator.publish(:_))
151 test "multiple users", %{admin: admin, conn: conn} do
152 user_one = insert(:user)
153 user_two = insert(:user)
157 |> put_req_header("accept", "application/json")
158 |> delete("/api/pleroma/admin/users", %{
159 nicknames: [user_one.nickname, user_two.nickname]
162 log_entry = Repo.one(ModerationLog)
164 assert ModerationLog.get_log_entry_message(log_entry) ==
165 "@#{admin.nickname} deleted users: @#{user_one.nickname}, @#{user_two.nickname}"
167 response = json_response(conn, 200)
168 assert response -- [user_one.nickname, user_two.nickname] == []
172 describe "/api/pleroma/admin/users" do
173 test "Create", %{conn: conn} do
176 |> put_req_header("accept", "application/json")
177 |> post("/api/pleroma/admin/users", %{
180 "nickname" => "lain",
181 "email" => "lain@example.org",
185 "nickname" => "lain2",
186 "email" => "lain2@example.org",
192 response = json_response(conn, 200) |> Enum.map(&Map.get(&1, "type"))
193 assert response == ["success", "success"]
195 log_entry = Repo.one(ModerationLog)
197 assert ["lain", "lain2"] -- Enum.map(log_entry.data["subjects"], & &1["nickname"]) == []
200 test "Cannot create user with existing email", %{conn: conn} do
205 |> put_req_header("accept", "application/json")
206 |> post("/api/pleroma/admin/users", %{
209 "nickname" => "lain",
210 "email" => user.email,
216 assert json_response(conn, 409) == [
220 "email" => user.email,
223 "error" => "email has already been taken",
229 test "Cannot create user with existing nickname", %{conn: conn} do
234 |> put_req_header("accept", "application/json")
235 |> post("/api/pleroma/admin/users", %{
238 "nickname" => user.nickname,
239 "email" => "someuser@plerama.social",
245 assert json_response(conn, 409) == [
249 "email" => "someuser@plerama.social",
250 "nickname" => user.nickname
252 "error" => "nickname has already been taken",
258 test "Multiple user creation works in transaction", %{conn: conn} do
263 |> put_req_header("accept", "application/json")
264 |> post("/api/pleroma/admin/users", %{
267 "nickname" => "newuser",
268 "email" => "newuser@pleroma.social",
272 "nickname" => "lain",
273 "email" => user.email,
279 assert json_response(conn, 409) == [
283 "email" => user.email,
286 "error" => "email has already been taken",
292 "email" => "newuser@pleroma.social",
293 "nickname" => "newuser"
300 assert User.get_by_nickname("newuser") === nil
304 describe "/api/pleroma/admin/users/:nickname" do
305 test "Show", %{conn: conn} do
308 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}")
310 assert user_response(user) == json_response(conn, 200)
313 test "when the user doesn't exist", %{conn: conn} do
316 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}")
318 assert %{"error" => "Not found"} == json_response(conn, 404)
322 describe "/api/pleroma/admin/users/follow" do
323 test "allows to force-follow another user", %{admin: admin, conn: conn} do
325 follower = insert(:user)
328 |> put_req_header("accept", "application/json")
329 |> post("/api/pleroma/admin/users/follow", %{
330 "follower" => follower.nickname,
331 "followed" => user.nickname
334 user = User.get_cached_by_id(user.id)
335 follower = User.get_cached_by_id(follower.id)
337 assert User.following?(follower, user)
339 log_entry = Repo.one(ModerationLog)
341 assert ModerationLog.get_log_entry_message(log_entry) ==
342 "@#{admin.nickname} made @#{follower.nickname} follow @#{user.nickname}"
346 describe "/api/pleroma/admin/users/unfollow" do
347 test "allows to force-unfollow another user", %{admin: admin, conn: conn} do
349 follower = insert(:user)
351 User.follow(follower, user)
354 |> put_req_header("accept", "application/json")
355 |> post("/api/pleroma/admin/users/unfollow", %{
356 "follower" => follower.nickname,
357 "followed" => user.nickname
360 user = User.get_cached_by_id(user.id)
361 follower = User.get_cached_by_id(follower.id)
363 refute User.following?(follower, user)
365 log_entry = Repo.one(ModerationLog)
367 assert ModerationLog.get_log_entry_message(log_entry) ==
368 "@#{admin.nickname} made @#{follower.nickname} unfollow @#{user.nickname}"
372 describe "GET /api/pleroma/admin/users" do
373 test "renders users array for the first page", %{conn: conn, admin: admin} do
374 user = insert(:user, local: false, tags: ["foo", "bar"])
375 user2 = insert(:user, is_approved: false, registration_reason: "I'm a chill dude")
377 conn = get(conn, "/api/pleroma/admin/users?page=1")
383 %{"roles" => %{"admin" => true, "moderator" => false}}
385 user_response(user, %{"local" => false, "tags" => ["foo", "bar"]}),
390 "is_approved" => false,
391 "registration_reason" => "I'm a chill dude",
392 "actor_type" => "Person"
396 |> Enum.sort_by(& &1["nickname"])
398 assert json_response(conn, 200) == %{
405 test "pagination works correctly with service users", %{conn: conn} do
406 service1 = User.get_or_create_service_actor_by_ap_id(Web.base_url() <> "/meido", "meido")
408 insert_list(25, :user)
410 assert %{"count" => 26, "page_size" => 10, "users" => users1} =
412 |> get("/api/pleroma/admin/users?page=1&filters=", %{page_size: "10"})
413 |> json_response(200)
415 assert Enum.count(users1) == 10
416 assert service1 not in users1
418 assert %{"count" => 26, "page_size" => 10, "users" => users2} =
420 |> get("/api/pleroma/admin/users?page=2&filters=", %{page_size: "10"})
421 |> json_response(200)
423 assert Enum.count(users2) == 10
424 assert service1 not in users2
426 assert %{"count" => 26, "page_size" => 10, "users" => users3} =
428 |> get("/api/pleroma/admin/users?page=3&filters=", %{page_size: "10"})
429 |> json_response(200)
431 assert Enum.count(users3) == 6
432 assert service1 not in users3
435 test "renders empty array for the second page", %{conn: conn} do
438 conn = get(conn, "/api/pleroma/admin/users?page=2")
440 assert json_response(conn, 200) == %{
447 test "regular search", %{conn: conn} do
448 user = insert(:user, nickname: "bob")
450 conn = get(conn, "/api/pleroma/admin/users?query=bo")
452 assert json_response(conn, 200) == %{
455 "users" => [user_response(user, %{"local" => true})]
459 test "search by domain", %{conn: conn} do
460 user = insert(:user, nickname: "nickname@domain.com")
463 conn = get(conn, "/api/pleroma/admin/users?query=domain.com")
465 assert json_response(conn, 200) == %{
468 "users" => [user_response(user)]
472 test "search by full nickname", %{conn: conn} do
473 user = insert(:user, nickname: "nickname@domain.com")
476 conn = get(conn, "/api/pleroma/admin/users?query=nickname@domain.com")
478 assert json_response(conn, 200) == %{
481 "users" => [user_response(user)]
485 test "search by display name", %{conn: conn} do
486 user = insert(:user, name: "Display name")
489 conn = get(conn, "/api/pleroma/admin/users?name=display")
491 assert json_response(conn, 200) == %{
494 "users" => [user_response(user)]
498 test "search by email", %{conn: conn} do
499 user = insert(:user, email: "email@example.com")
502 conn = get(conn, "/api/pleroma/admin/users?email=email@example.com")
504 assert json_response(conn, 200) == %{
507 "users" => [user_response(user)]
511 test "regular search with page size", %{conn: conn} do
512 user = insert(:user, nickname: "aalice")
513 user2 = insert(:user, nickname: "alice")
515 conn1 = get(conn, "/api/pleroma/admin/users?query=a&page_size=1&page=1")
517 assert json_response(conn1, 200) == %{
520 "users" => [user_response(user)]
523 conn2 = get(conn, "/api/pleroma/admin/users?query=a&page_size=1&page=2")
525 assert json_response(conn2, 200) == %{
528 "users" => [user_response(user2)]
532 test "only local users" do
533 admin = insert(:user, is_admin: true, nickname: "john")
534 token = insert(:oauth_admin_token, user: admin)
535 user = insert(:user, nickname: "bob")
537 insert(:user, nickname: "bobb", local: false)
541 |> assign(:user, admin)
542 |> assign(:token, token)
543 |> get("/api/pleroma/admin/users?query=bo&filters=local")
545 assert json_response(conn, 200) == %{
548 "users" => [user_response(user)]
552 test "only local users with no query", %{conn: conn, admin: old_admin} do
553 admin = insert(:user, is_admin: true, nickname: "john")
554 user = insert(:user, nickname: "bob")
556 insert(:user, nickname: "bobb", local: false)
558 conn = get(conn, "/api/pleroma/admin/users?filters=local")
563 user_response(admin, %{
564 "roles" => %{"admin" => true, "moderator" => false}
566 user_response(old_admin, %{
568 "roles" => %{"admin" => true, "moderator" => false}
571 |> Enum.sort_by(& &1["nickname"])
573 assert json_response(conn, 200) == %{
580 test "only unconfirmed users", %{conn: conn} do
581 sad_user = insert(:user, nickname: "sadboy", is_confirmed: false)
582 old_user = insert(:user, nickname: "oldboy", is_confirmed: false)
584 insert(:user, nickname: "happyboy", is_approved: true)
585 insert(:user, is_confirmed: true)
589 |> get("/api/pleroma/admin/users?filters=unconfirmed")
590 |> json_response(200)
593 Enum.map([old_user, sad_user], fn user ->
594 user_response(user, %{
595 "is_confirmed" => false,
596 "is_approved" => true
599 |> Enum.sort_by(& &1["nickname"])
601 assert result == %{"count" => 2, "page_size" => 50, "users" => users}
604 test "only unapproved users", %{conn: conn} do
609 registration_reason: "Plz let me in!"
612 insert(:user, nickname: "happyboy", is_approved: true)
614 conn = get(conn, "/api/pleroma/admin/users?filters=need_approval")
619 %{"is_approved" => false, "registration_reason" => "Plz let me in!"}
623 assert json_response(conn, 200) == %{
630 test "load only admins", %{conn: conn, admin: admin} do
631 second_admin = insert(:user, is_admin: true)
635 conn = get(conn, "/api/pleroma/admin/users?filters=is_admin")
639 user_response(admin, %{
641 "roles" => %{"admin" => true, "moderator" => false}
643 user_response(second_admin, %{
645 "roles" => %{"admin" => true, "moderator" => false}
648 |> Enum.sort_by(& &1["nickname"])
650 assert json_response(conn, 200) == %{
657 test "load only moderators", %{conn: conn} do
658 moderator = insert(:user, is_moderator: true)
662 conn = get(conn, "/api/pleroma/admin/users?filters=is_moderator")
664 assert json_response(conn, 200) == %{
668 user_response(moderator, %{
670 "roles" => %{"admin" => false, "moderator" => true}
676 test "load users with actor_type is Person", %{admin: admin, conn: conn} do
677 insert(:user, actor_type: "Service")
678 insert(:user, actor_type: "Application")
680 user1 = insert(:user)
681 user2 = insert(:user)
685 |> get(user_path(conn, :list), %{actor_types: ["Person"]})
686 |> json_response(200)
690 user_response(admin, %{"roles" => %{"admin" => true, "moderator" => false}}),
691 user_response(user1),
694 |> Enum.sort_by(& &1["nickname"])
696 assert response == %{"count" => 3, "page_size" => 50, "users" => users}
699 test "load users with actor_type is Person and Service", %{admin: admin, conn: conn} do
700 user_service = insert(:user, actor_type: "Service")
701 insert(:user, actor_type: "Application")
703 user1 = insert(:user)
704 user2 = insert(:user)
708 |> get(user_path(conn, :list), %{actor_types: ["Person", "Service"]})
709 |> json_response(200)
713 user_response(admin, %{"roles" => %{"admin" => true, "moderator" => false}}),
714 user_response(user1),
715 user_response(user2),
716 user_response(user_service, %{"actor_type" => "Service"})
718 |> Enum.sort_by(& &1["nickname"])
720 assert response == %{"count" => 4, "page_size" => 50, "users" => users}
723 test "load users with actor_type is Service", %{conn: conn} do
724 user_service = insert(:user, actor_type: "Service")
725 insert(:user, actor_type: "Application")
731 |> get(user_path(conn, :list), %{actor_types: ["Service"]})
732 |> json_response(200)
734 users = [user_response(user_service, %{"actor_type" => "Service"})]
736 assert response == %{"count" => 1, "page_size" => 50, "users" => users}
739 test "load users with tags list", %{conn: conn} do
740 user1 = insert(:user, tags: ["first"])
741 user2 = insert(:user, tags: ["second"])
745 conn = get(conn, "/api/pleroma/admin/users?tags[]=first&tags[]=second")
749 user_response(user1, %{"tags" => ["first"]}),
750 user_response(user2, %{"tags" => ["second"]})
752 |> Enum.sort_by(& &1["nickname"])
754 assert json_response(conn, 200) == %{
761 test "`active` filters out users pending approval", %{token: token} do
762 insert(:user, is_approved: false)
763 %{id: user_id} = insert(:user, is_approved: true)
764 %{id: admin_id} = token.user
768 |> assign(:user, token.user)
769 |> assign(:token, token)
770 |> get("/api/pleroma/admin/users?filters=active")
776 %{"id" => ^admin_id},
779 } = json_response(conn, 200)
782 test "it works with multiple filters" do
783 admin = insert(:user, nickname: "john", is_admin: true)
784 token = insert(:oauth_admin_token, user: admin)
785 user = insert(:user, nickname: "bob", local: false, is_active: false)
787 insert(:user, nickname: "ken", local: true, is_active: false)
788 insert(:user, nickname: "bobb", local: false, is_active: true)
792 |> assign(:user, admin)
793 |> assign(:token, token)
794 |> get("/api/pleroma/admin/users?filters=deactivated,external")
796 assert json_response(conn, 200) == %{
799 "users" => [user_response(user)]
803 test "it omits relay user", %{admin: admin, conn: conn} do
804 assert %User{} = Relay.get_actor()
806 conn = get(conn, "/api/pleroma/admin/users")
808 assert json_response(conn, 200) == %{
812 user_response(admin, %{"roles" => %{"admin" => true, "moderator" => false}})
818 test "PATCH /api/pleroma/admin/users/activate", %{admin: admin, conn: conn} do
819 user_one = insert(:user, is_active: false)
820 user_two = insert(:user, is_active: false)
825 "/api/pleroma/admin/users/activate",
826 %{nicknames: [user_one.nickname, user_two.nickname]}
829 response = json_response(conn, 200)
830 assert Enum.map(response["users"], & &1["is_active"]) == [true, true]
832 log_entry = Repo.one(ModerationLog)
834 assert ModerationLog.get_log_entry_message(log_entry) ==
835 "@#{admin.nickname} activated users: @#{user_one.nickname}, @#{user_two.nickname}"
838 test "PATCH /api/pleroma/admin/users/deactivate", %{admin: admin, conn: conn} do
839 user_one = insert(:user, is_active: true)
840 user_two = insert(:user, is_active: true)
845 "/api/pleroma/admin/users/deactivate",
846 %{nicknames: [user_one.nickname, user_two.nickname]}
849 response = json_response(conn, 200)
850 assert Enum.map(response["users"], & &1["is_active"]) == [false, false]
852 log_entry = Repo.one(ModerationLog)
854 assert ModerationLog.get_log_entry_message(log_entry) ==
855 "@#{admin.nickname} deactivated users: @#{user_one.nickname}, @#{user_two.nickname}"
858 test "PATCH /api/pleroma/admin/users/approve", %{admin: admin, conn: conn} do
859 user_one = insert(:user, is_approved: false)
860 user_two = insert(:user, is_approved: false)
865 "/api/pleroma/admin/users/approve",
866 %{nicknames: [user_one.nickname, user_two.nickname]}
869 response = json_response(conn, 200)
870 assert Enum.map(response["users"], & &1["is_approved"]) == [true, true]
872 log_entry = Repo.one(ModerationLog)
874 assert ModerationLog.get_log_entry_message(log_entry) ==
875 "@#{admin.nickname} approved users: @#{user_one.nickname}, @#{user_two.nickname}"
878 test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admin, conn: conn} do
881 conn = patch(conn, "/api/pleroma/admin/users/#{user.nickname}/toggle_activation")
883 assert json_response(conn, 200) ==
886 %{"is_active" => !user.is_active}
889 log_entry = Repo.one(ModerationLog)
891 assert ModerationLog.get_log_entry_message(log_entry) ==
892 "@#{admin.nickname} deactivated users: @#{user.nickname}"
895 defp user_response(user, attrs \\ %{}) do
897 "is_active" => user.is_active,
899 "email" => user.email,
900 "nickname" => user.nickname,
901 "roles" => %{"admin" => false, "moderator" => false},
902 "local" => user.local,
904 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
905 "display_name" => HTML.strip_tags(user.name || user.nickname),
906 "is_confirmed" => true,
907 "is_approved" => true,
909 "registration_reason" => nil,
910 "actor_type" => "Person"