569343ed5847f067d872eb61efb212ca7159a922
[akkoma] / test / pleroma / web / admin_api / controllers / user_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.UserControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
8
9 import Mock
10 import Pleroma.Factory
11
12 alias Pleroma.HTML
13 alias Pleroma.ModerationLog
14 alias Pleroma.Repo
15 alias Pleroma.Tests.ObanHelpers
16 alias Pleroma.User
17 alias Pleroma.Web
18 alias Pleroma.Web.ActivityPub.Relay
19 alias Pleroma.Web.CommonAPI
20 alias Pleroma.Web.MediaProxy
21
22 setup_all do
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24
25 :ok
26 end
27
28 setup do
29 admin = insert(:user, is_admin: true)
30 token = insert(:oauth_admin_token, user: admin)
31
32 conn =
33 build_conn()
34 |> assign(:user, admin)
35 |> assign(:token, token)
36
37 {:ok, %{admin: admin, token: token, conn: conn}}
38 end
39
40 test "with valid `admin_token` query parameter, skips OAuth scopes check" do
41 clear_config([:admin_token], "password123")
42
43 user = insert(:user)
44
45 conn = get(build_conn(), "/api/pleroma/admin/users/#{user.nickname}?admin_token=password123")
46
47 assert json_response(conn, 200)
48 end
49
50 describe "with [:auth, :enforce_oauth_admin_scope_usage]," do
51 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], true)
52
53 test "GET /api/pleroma/admin/users/:nickname requires admin:read:accounts or broader scope",
54 %{admin: admin} do
55 user = insert(:user)
56 url = "/api/pleroma/admin/users/#{user.nickname}"
57
58 good_token1 = insert(:oauth_token, user: admin, scopes: ["admin"])
59 good_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read"])
60 good_token3 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts"])
61
62 bad_token1 = insert(:oauth_token, user: admin, scopes: ["read:accounts"])
63 bad_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts:partial"])
64 bad_token3 = nil
65
66 for good_token <- [good_token1, good_token2, good_token3] do
67 conn =
68 build_conn()
69 |> assign(:user, admin)
70 |> assign(:token, good_token)
71 |> get(url)
72
73 assert json_response(conn, 200)
74 end
75
76 for good_token <- [good_token1, good_token2, good_token3] do
77 conn =
78 build_conn()
79 |> assign(:user, nil)
80 |> assign(:token, good_token)
81 |> get(url)
82
83 assert json_response(conn, :forbidden)
84 end
85
86 for bad_token <- [bad_token1, bad_token2, bad_token3] do
87 conn =
88 build_conn()
89 |> assign(:user, admin)
90 |> assign(:token, bad_token)
91 |> get(url)
92
93 assert json_response(conn, :forbidden)
94 end
95 end
96 end
97
98 describe "unless [:auth, :enforce_oauth_admin_scope_usage]," do
99 setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
100
101 test "GET /api/pleroma/admin/users/:nickname requires " <>
102 "read:accounts or admin:read:accounts or broader scope",
103 %{admin: admin} do
104 user = insert(:user)
105 url = "/api/pleroma/admin/users/#{user.nickname}"
106
107 good_token1 = insert(:oauth_token, user: admin, scopes: ["admin"])
108 good_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read"])
109 good_token3 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts"])
110 good_token4 = insert(:oauth_token, user: admin, scopes: ["read:accounts"])
111 good_token5 = insert(:oauth_token, user: admin, scopes: ["read"])
112
113 good_tokens = [good_token1, good_token2, good_token3, good_token4, good_token5]
114
115 bad_token1 = insert(:oauth_token, user: admin, scopes: ["read:accounts:partial"])
116 bad_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts:partial"])
117 bad_token3 = nil
118
119 for good_token <- good_tokens do
120 conn =
121 build_conn()
122 |> assign(:user, admin)
123 |> assign(:token, good_token)
124 |> get(url)
125
126 assert json_response(conn, 200)
127 end
128
129 for good_token <- good_tokens do
130 conn =
131 build_conn()
132 |> assign(:user, nil)
133 |> assign(:token, good_token)
134 |> get(url)
135
136 assert json_response(conn, :forbidden)
137 end
138
139 for bad_token <- [bad_token1, bad_token2, bad_token3] do
140 conn =
141 build_conn()
142 |> assign(:user, admin)
143 |> assign(:token, bad_token)
144 |> get(url)
145
146 assert json_response(conn, :forbidden)
147 end
148 end
149 end
150
151 describe "DELETE /api/pleroma/admin/users" do
152 test "single user", %{admin: admin, conn: conn} do
153 clear_config([:instance, :federating], true)
154
155 user =
156 insert(:user,
157 avatar: %{"url" => [%{"href" => "https://someurl"}]},
158 banner: %{"url" => [%{"href" => "https://somebanner"}]},
159 bio: "Hello world!",
160 name: "A guy"
161 )
162
163 # Create some activities to check they got deleted later
164 follower = insert(:user)
165 {:ok, _} = CommonAPI.post(user, %{status: "test"})
166 {:ok, _, _, _} = CommonAPI.follow(user, follower)
167 {:ok, _, _, _} = CommonAPI.follow(follower, user)
168 user = Repo.get(User, user.id)
169 assert user.note_count == 1
170 assert user.follower_count == 1
171 assert user.following_count == 1
172 refute user.deactivated
173
174 with_mock Pleroma.Web.Federator,
175 publish: fn _ -> nil end,
176 perform: fn _, _ -> nil end do
177 conn =
178 conn
179 |> put_req_header("accept", "application/json")
180 |> delete("/api/pleroma/admin/users?nickname=#{user.nickname}")
181
182 ObanHelpers.perform_all()
183
184 assert User.get_by_nickname(user.nickname).deactivated
185
186 log_entry = Repo.one(ModerationLog)
187
188 assert ModerationLog.get_log_entry_message(log_entry) ==
189 "@#{admin.nickname} deleted users: @#{user.nickname}"
190
191 assert json_response(conn, 200) == [user.nickname]
192
193 user = Repo.get(User, user.id)
194 assert user.deactivated
195
196 assert user.avatar == %{}
197 assert user.banner == %{}
198 assert user.note_count == 0
199 assert user.follower_count == 0
200 assert user.following_count == 0
201 assert user.bio == ""
202 assert user.name == nil
203
204 assert called(Pleroma.Web.Federator.publish(:_))
205 end
206 end
207
208 test "multiple users", %{admin: admin, conn: conn} do
209 user_one = insert(:user)
210 user_two = insert(:user)
211
212 conn =
213 conn
214 |> put_req_header("accept", "application/json")
215 |> delete("/api/pleroma/admin/users", %{
216 nicknames: [user_one.nickname, user_two.nickname]
217 })
218
219 log_entry = Repo.one(ModerationLog)
220
221 assert ModerationLog.get_log_entry_message(log_entry) ==
222 "@#{admin.nickname} deleted users: @#{user_one.nickname}, @#{user_two.nickname}"
223
224 response = json_response(conn, 200)
225 assert response -- [user_one.nickname, user_two.nickname] == []
226 end
227 end
228
229 describe "/api/pleroma/admin/users" do
230 test "Create", %{conn: conn} do
231 conn =
232 conn
233 |> put_req_header("accept", "application/json")
234 |> post("/api/pleroma/admin/users", %{
235 "users" => [
236 %{
237 "nickname" => "lain",
238 "email" => "lain@example.org",
239 "password" => "test"
240 },
241 %{
242 "nickname" => "lain2",
243 "email" => "lain2@example.org",
244 "password" => "test"
245 }
246 ]
247 })
248
249 response = json_response(conn, 200) |> Enum.map(&Map.get(&1, "type"))
250 assert response == ["success", "success"]
251
252 log_entry = Repo.one(ModerationLog)
253
254 assert ["lain", "lain2"] -- Enum.map(log_entry.data["subjects"], & &1["nickname"]) == []
255 end
256
257 test "Cannot create user with existing email", %{conn: conn} do
258 user = insert(:user)
259
260 conn =
261 conn
262 |> put_req_header("accept", "application/json")
263 |> post("/api/pleroma/admin/users", %{
264 "users" => [
265 %{
266 "nickname" => "lain",
267 "email" => user.email,
268 "password" => "test"
269 }
270 ]
271 })
272
273 assert json_response(conn, 409) == [
274 %{
275 "code" => 409,
276 "data" => %{
277 "email" => user.email,
278 "nickname" => "lain"
279 },
280 "error" => "email has already been taken",
281 "type" => "error"
282 }
283 ]
284 end
285
286 test "Cannot create user with existing nickname", %{conn: conn} do
287 user = insert(:user)
288
289 conn =
290 conn
291 |> put_req_header("accept", "application/json")
292 |> post("/api/pleroma/admin/users", %{
293 "users" => [
294 %{
295 "nickname" => user.nickname,
296 "email" => "someuser@plerama.social",
297 "password" => "test"
298 }
299 ]
300 })
301
302 assert json_response(conn, 409) == [
303 %{
304 "code" => 409,
305 "data" => %{
306 "email" => "someuser@plerama.social",
307 "nickname" => user.nickname
308 },
309 "error" => "nickname has already been taken",
310 "type" => "error"
311 }
312 ]
313 end
314
315 test "Multiple user creation works in transaction", %{conn: conn} do
316 user = insert(:user)
317
318 conn =
319 conn
320 |> put_req_header("accept", "application/json")
321 |> post("/api/pleroma/admin/users", %{
322 "users" => [
323 %{
324 "nickname" => "newuser",
325 "email" => "newuser@pleroma.social",
326 "password" => "test"
327 },
328 %{
329 "nickname" => "lain",
330 "email" => user.email,
331 "password" => "test"
332 }
333 ]
334 })
335
336 assert json_response(conn, 409) == [
337 %{
338 "code" => 409,
339 "data" => %{
340 "email" => user.email,
341 "nickname" => "lain"
342 },
343 "error" => "email has already been taken",
344 "type" => "error"
345 },
346 %{
347 "code" => 409,
348 "data" => %{
349 "email" => "newuser@pleroma.social",
350 "nickname" => "newuser"
351 },
352 "error" => "",
353 "type" => "error"
354 }
355 ]
356
357 assert User.get_by_nickname("newuser") === nil
358 end
359 end
360
361 describe "/api/pleroma/admin/users/:nickname" do
362 test "Show", %{conn: conn} do
363 user = insert(:user)
364
365 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}")
366
367 assert user_response(user) == json_response(conn, 200)
368 end
369
370 test "when the user doesn't exist", %{conn: conn} do
371 user = build(:user)
372
373 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}")
374
375 assert %{"error" => "Not found"} == json_response(conn, 404)
376 end
377 end
378
379 describe "/api/pleroma/admin/users/follow" do
380 test "allows to force-follow another user", %{admin: admin, conn: conn} do
381 user = insert(:user)
382 follower = insert(:user)
383
384 conn
385 |> put_req_header("accept", "application/json")
386 |> post("/api/pleroma/admin/users/follow", %{
387 "follower" => follower.nickname,
388 "followed" => user.nickname
389 })
390
391 user = User.get_cached_by_id(user.id)
392 follower = User.get_cached_by_id(follower.id)
393
394 assert User.following?(follower, user)
395
396 log_entry = Repo.one(ModerationLog)
397
398 assert ModerationLog.get_log_entry_message(log_entry) ==
399 "@#{admin.nickname} made @#{follower.nickname} follow @#{user.nickname}"
400 end
401 end
402
403 describe "/api/pleroma/admin/users/unfollow" do
404 test "allows to force-unfollow another user", %{admin: admin, conn: conn} do
405 user = insert(:user)
406 follower = insert(:user)
407
408 User.follow(follower, user)
409
410 conn
411 |> put_req_header("accept", "application/json")
412 |> post("/api/pleroma/admin/users/unfollow", %{
413 "follower" => follower.nickname,
414 "followed" => user.nickname
415 })
416
417 user = User.get_cached_by_id(user.id)
418 follower = User.get_cached_by_id(follower.id)
419
420 refute User.following?(follower, user)
421
422 log_entry = Repo.one(ModerationLog)
423
424 assert ModerationLog.get_log_entry_message(log_entry) ==
425 "@#{admin.nickname} made @#{follower.nickname} unfollow @#{user.nickname}"
426 end
427 end
428
429 describe "GET /api/pleroma/admin/users" do
430 test "renders users array for the first page", %{conn: conn, admin: admin} do
431 user = insert(:user, local: false, tags: ["foo", "bar"])
432 user2 = insert(:user, approval_pending: true, registration_reason: "I'm a chill dude")
433
434 conn = get(conn, "/api/pleroma/admin/users?page=1")
435
436 users =
437 [
438 user_response(
439 admin,
440 %{"roles" => %{"admin" => true, "moderator" => false}}
441 ),
442 user_response(user, %{"local" => false, "tags" => ["foo", "bar"]}),
443 user_response(
444 user2,
445 %{
446 "local" => true,
447 "approval_pending" => true,
448 "registration_reason" => "I'm a chill dude",
449 "actor_type" => "Person"
450 }
451 )
452 ]
453 |> Enum.sort_by(& &1["nickname"])
454
455 assert json_response(conn, 200) == %{
456 "count" => 3,
457 "page_size" => 50,
458 "users" => users
459 }
460 end
461
462 test "pagination works correctly with service users", %{conn: conn} do
463 service1 = User.get_or_create_service_actor_by_ap_id(Web.base_url() <> "/meido", "meido")
464
465 insert_list(25, :user)
466
467 assert %{"count" => 26, "page_size" => 10, "users" => users1} =
468 conn
469 |> get("/api/pleroma/admin/users?page=1&filters=", %{page_size: "10"})
470 |> json_response(200)
471
472 assert Enum.count(users1) == 10
473 assert service1 not in users1
474
475 assert %{"count" => 26, "page_size" => 10, "users" => users2} =
476 conn
477 |> get("/api/pleroma/admin/users?page=2&filters=", %{page_size: "10"})
478 |> json_response(200)
479
480 assert Enum.count(users2) == 10
481 assert service1 not in users2
482
483 assert %{"count" => 26, "page_size" => 10, "users" => users3} =
484 conn
485 |> get("/api/pleroma/admin/users?page=3&filters=", %{page_size: "10"})
486 |> json_response(200)
487
488 assert Enum.count(users3) == 6
489 assert service1 not in users3
490 end
491
492 test "renders empty array for the second page", %{conn: conn} do
493 insert(:user)
494
495 conn = get(conn, "/api/pleroma/admin/users?page=2")
496
497 assert json_response(conn, 200) == %{
498 "count" => 2,
499 "page_size" => 50,
500 "users" => []
501 }
502 end
503
504 test "regular search", %{conn: conn} do
505 user = insert(:user, nickname: "bob")
506
507 conn = get(conn, "/api/pleroma/admin/users?query=bo")
508
509 assert json_response(conn, 200) == %{
510 "count" => 1,
511 "page_size" => 50,
512 "users" => [user_response(user, %{"local" => true})]
513 }
514 end
515
516 test "search by domain", %{conn: conn} do
517 user = insert(:user, nickname: "nickname@domain.com")
518 insert(:user)
519
520 conn = get(conn, "/api/pleroma/admin/users?query=domain.com")
521
522 assert json_response(conn, 200) == %{
523 "count" => 1,
524 "page_size" => 50,
525 "users" => [user_response(user)]
526 }
527 end
528
529 test "search by full nickname", %{conn: conn} do
530 user = insert(:user, nickname: "nickname@domain.com")
531 insert(:user)
532
533 conn = get(conn, "/api/pleroma/admin/users?query=nickname@domain.com")
534
535 assert json_response(conn, 200) == %{
536 "count" => 1,
537 "page_size" => 50,
538 "users" => [user_response(user)]
539 }
540 end
541
542 test "search by display name", %{conn: conn} do
543 user = insert(:user, name: "Display name")
544 insert(:user)
545
546 conn = get(conn, "/api/pleroma/admin/users?name=display")
547
548 assert json_response(conn, 200) == %{
549 "count" => 1,
550 "page_size" => 50,
551 "users" => [user_response(user)]
552 }
553 end
554
555 test "search by email", %{conn: conn} do
556 user = insert(:user, email: "email@example.com")
557 insert(:user)
558
559 conn = get(conn, "/api/pleroma/admin/users?email=email@example.com")
560
561 assert json_response(conn, 200) == %{
562 "count" => 1,
563 "page_size" => 50,
564 "users" => [user_response(user)]
565 }
566 end
567
568 test "regular search with page size", %{conn: conn} do
569 user = insert(:user, nickname: "aalice")
570 user2 = insert(:user, nickname: "alice")
571
572 conn1 = get(conn, "/api/pleroma/admin/users?query=a&page_size=1&page=1")
573
574 assert json_response(conn1, 200) == %{
575 "count" => 2,
576 "page_size" => 1,
577 "users" => [user_response(user)]
578 }
579
580 conn2 = get(conn, "/api/pleroma/admin/users?query=a&page_size=1&page=2")
581
582 assert json_response(conn2, 200) == %{
583 "count" => 2,
584 "page_size" => 1,
585 "users" => [user_response(user2)]
586 }
587 end
588
589 test "only local users" do
590 admin = insert(:user, is_admin: true, nickname: "john")
591 token = insert(:oauth_admin_token, user: admin)
592 user = insert(:user, nickname: "bob")
593
594 insert(:user, nickname: "bobb", local: false)
595
596 conn =
597 build_conn()
598 |> assign(:user, admin)
599 |> assign(:token, token)
600 |> get("/api/pleroma/admin/users?query=bo&filters=local")
601
602 assert json_response(conn, 200) == %{
603 "count" => 1,
604 "page_size" => 50,
605 "users" => [user_response(user)]
606 }
607 end
608
609 test "only local users with no query", %{conn: conn, admin: old_admin} do
610 admin = insert(:user, is_admin: true, nickname: "john")
611 user = insert(:user, nickname: "bob")
612
613 insert(:user, nickname: "bobb", local: false)
614
615 conn = get(conn, "/api/pleroma/admin/users?filters=local")
616
617 users =
618 [
619 user_response(user),
620 user_response(admin, %{
621 "roles" => %{"admin" => true, "moderator" => false}
622 }),
623 user_response(old_admin, %{
624 "deactivated" => false,
625 "roles" => %{"admin" => true, "moderator" => false}
626 })
627 ]
628 |> Enum.sort_by(& &1["nickname"])
629
630 assert json_response(conn, 200) == %{
631 "count" => 3,
632 "page_size" => 50,
633 "users" => users
634 }
635 end
636
637 test "only unconfirmed users", %{conn: conn} do
638 sad_user = insert(:user, nickname: "sadboy", is_confirmed: false)
639 old_user = insert(:user, nickname: "oldboy", is_confirmed: false)
640
641 insert(:user, nickname: "happyboy", approval_pending: false)
642 insert(:user, is_confirmed: true)
643
644 result =
645 conn
646 |> get("/api/pleroma/admin/users?filters=unconfirmed")
647 |> json_response(200)
648
649 users =
650 Enum.map([old_user, sad_user], fn user ->
651 user_response(user, %{
652 "is_confirmed" => false,
653 "approval_pending" => false
654 })
655 end)
656 |> Enum.sort_by(& &1["nickname"])
657
658 assert result == %{"count" => 2, "page_size" => 50, "users" => users}
659 end
660
661 test "only unapproved users", %{conn: conn} do
662 user =
663 insert(:user,
664 nickname: "sadboy",
665 approval_pending: true,
666 registration_reason: "Plz let me in!"
667 )
668
669 insert(:user, nickname: "happyboy", approval_pending: false)
670
671 conn = get(conn, "/api/pleroma/admin/users?filters=need_approval")
672
673 users = [
674 user_response(
675 user,
676 %{"approval_pending" => true, "registration_reason" => "Plz let me in!"}
677 )
678 ]
679
680 assert json_response(conn, 200) == %{
681 "count" => 1,
682 "page_size" => 50,
683 "users" => users
684 }
685 end
686
687 test "load only admins", %{conn: conn, admin: admin} do
688 second_admin = insert(:user, is_admin: true)
689 insert(:user)
690 insert(:user)
691
692 conn = get(conn, "/api/pleroma/admin/users?filters=is_admin")
693
694 users =
695 [
696 user_response(admin, %{
697 "deactivated" => false,
698 "roles" => %{"admin" => true, "moderator" => false}
699 }),
700 user_response(second_admin, %{
701 "deactivated" => false,
702 "roles" => %{"admin" => true, "moderator" => false}
703 })
704 ]
705 |> Enum.sort_by(& &1["nickname"])
706
707 assert json_response(conn, 200) == %{
708 "count" => 2,
709 "page_size" => 50,
710 "users" => users
711 }
712 end
713
714 test "load only moderators", %{conn: conn} do
715 moderator = insert(:user, is_moderator: true)
716 insert(:user)
717 insert(:user)
718
719 conn = get(conn, "/api/pleroma/admin/users?filters=is_moderator")
720
721 assert json_response(conn, 200) == %{
722 "count" => 1,
723 "page_size" => 50,
724 "users" => [
725 user_response(moderator, %{
726 "deactivated" => false,
727 "roles" => %{"admin" => false, "moderator" => true}
728 })
729 ]
730 }
731 end
732
733 test "load users with actor_type is Person", %{admin: admin, conn: conn} do
734 insert(:user, actor_type: "Service")
735 insert(:user, actor_type: "Application")
736
737 user1 = insert(:user)
738 user2 = insert(:user)
739
740 response =
741 conn
742 |> get(user_path(conn, :list), %{actor_types: ["Person"]})
743 |> json_response(200)
744
745 users =
746 [
747 user_response(admin, %{"roles" => %{"admin" => true, "moderator" => false}}),
748 user_response(user1),
749 user_response(user2)
750 ]
751 |> Enum.sort_by(& &1["nickname"])
752
753 assert response == %{"count" => 3, "page_size" => 50, "users" => users}
754 end
755
756 test "load users with actor_type is Person and Service", %{admin: admin, conn: conn} do
757 user_service = insert(:user, actor_type: "Service")
758 insert(:user, actor_type: "Application")
759
760 user1 = insert(:user)
761 user2 = insert(:user)
762
763 response =
764 conn
765 |> get(user_path(conn, :list), %{actor_types: ["Person", "Service"]})
766 |> json_response(200)
767
768 users =
769 [
770 user_response(admin, %{"roles" => %{"admin" => true, "moderator" => false}}),
771 user_response(user1),
772 user_response(user2),
773 user_response(user_service, %{"actor_type" => "Service"})
774 ]
775 |> Enum.sort_by(& &1["nickname"])
776
777 assert response == %{"count" => 4, "page_size" => 50, "users" => users}
778 end
779
780 test "load users with actor_type is Service", %{conn: conn} do
781 user_service = insert(:user, actor_type: "Service")
782 insert(:user, actor_type: "Application")
783 insert(:user)
784 insert(:user)
785
786 response =
787 conn
788 |> get(user_path(conn, :list), %{actor_types: ["Service"]})
789 |> json_response(200)
790
791 users = [user_response(user_service, %{"actor_type" => "Service"})]
792
793 assert response == %{"count" => 1, "page_size" => 50, "users" => users}
794 end
795
796 test "load users with tags list", %{conn: conn} do
797 user1 = insert(:user, tags: ["first"])
798 user2 = insert(:user, tags: ["second"])
799 insert(:user)
800 insert(:user)
801
802 conn = get(conn, "/api/pleroma/admin/users?tags[]=first&tags[]=second")
803
804 users =
805 [
806 user_response(user1, %{"tags" => ["first"]}),
807 user_response(user2, %{"tags" => ["second"]})
808 ]
809 |> Enum.sort_by(& &1["nickname"])
810
811 assert json_response(conn, 200) == %{
812 "count" => 2,
813 "page_size" => 50,
814 "users" => users
815 }
816 end
817
818 test "`active` filters out users pending approval", %{token: token} do
819 insert(:user, approval_pending: true)
820 %{id: user_id} = insert(:user, approval_pending: false)
821 %{id: admin_id} = token.user
822
823 conn =
824 build_conn()
825 |> assign(:user, token.user)
826 |> assign(:token, token)
827 |> get("/api/pleroma/admin/users?filters=active")
828
829 assert %{
830 "count" => 2,
831 "page_size" => 50,
832 "users" => [
833 %{"id" => ^admin_id},
834 %{"id" => ^user_id}
835 ]
836 } = json_response(conn, 200)
837 end
838
839 test "it works with multiple filters" do
840 admin = insert(:user, nickname: "john", is_admin: true)
841 token = insert(:oauth_admin_token, user: admin)
842 user = insert(:user, nickname: "bob", local: false, deactivated: true)
843
844 insert(:user, nickname: "ken", local: true, deactivated: true)
845 insert(:user, nickname: "bobb", local: false, deactivated: false)
846
847 conn =
848 build_conn()
849 |> assign(:user, admin)
850 |> assign(:token, token)
851 |> get("/api/pleroma/admin/users?filters=deactivated,external")
852
853 assert json_response(conn, 200) == %{
854 "count" => 1,
855 "page_size" => 50,
856 "users" => [user_response(user)]
857 }
858 end
859
860 test "it omits relay user", %{admin: admin, conn: conn} do
861 assert %User{} = Relay.get_actor()
862
863 conn = get(conn, "/api/pleroma/admin/users")
864
865 assert json_response(conn, 200) == %{
866 "count" => 1,
867 "page_size" => 50,
868 "users" => [
869 user_response(admin, %{"roles" => %{"admin" => true, "moderator" => false}})
870 ]
871 }
872 end
873 end
874
875 test "PATCH /api/pleroma/admin/users/activate", %{admin: admin, conn: conn} do
876 user_one = insert(:user, deactivated: true)
877 user_two = insert(:user, deactivated: true)
878
879 conn =
880 patch(
881 conn,
882 "/api/pleroma/admin/users/activate",
883 %{nicknames: [user_one.nickname, user_two.nickname]}
884 )
885
886 response = json_response(conn, 200)
887 assert Enum.map(response["users"], & &1["deactivated"]) == [false, false]
888
889 log_entry = Repo.one(ModerationLog)
890
891 assert ModerationLog.get_log_entry_message(log_entry) ==
892 "@#{admin.nickname} activated users: @#{user_one.nickname}, @#{user_two.nickname}"
893 end
894
895 test "PATCH /api/pleroma/admin/users/deactivate", %{admin: admin, conn: conn} do
896 user_one = insert(:user, deactivated: false)
897 user_two = insert(:user, deactivated: false)
898
899 conn =
900 patch(
901 conn,
902 "/api/pleroma/admin/users/deactivate",
903 %{nicknames: [user_one.nickname, user_two.nickname]}
904 )
905
906 response = json_response(conn, 200)
907 assert Enum.map(response["users"], & &1["deactivated"]) == [true, true]
908
909 log_entry = Repo.one(ModerationLog)
910
911 assert ModerationLog.get_log_entry_message(log_entry) ==
912 "@#{admin.nickname} deactivated users: @#{user_one.nickname}, @#{user_two.nickname}"
913 end
914
915 test "PATCH /api/pleroma/admin/users/approve", %{admin: admin, conn: conn} do
916 user_one = insert(:user, approval_pending: true)
917 user_two = insert(:user, approval_pending: true)
918
919 conn =
920 patch(
921 conn,
922 "/api/pleroma/admin/users/approve",
923 %{nicknames: [user_one.nickname, user_two.nickname]}
924 )
925
926 response = json_response(conn, 200)
927 assert Enum.map(response["users"], & &1["approval_pending"]) == [false, false]
928
929 log_entry = Repo.one(ModerationLog)
930
931 assert ModerationLog.get_log_entry_message(log_entry) ==
932 "@#{admin.nickname} approved users: @#{user_one.nickname}, @#{user_two.nickname}"
933 end
934
935 test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admin, conn: conn} do
936 user = insert(:user)
937
938 conn = patch(conn, "/api/pleroma/admin/users/#{user.nickname}/toggle_activation")
939
940 assert json_response(conn, 200) ==
941 user_response(
942 user,
943 %{"deactivated" => !user.deactivated}
944 )
945
946 log_entry = Repo.one(ModerationLog)
947
948 assert ModerationLog.get_log_entry_message(log_entry) ==
949 "@#{admin.nickname} deactivated users: @#{user.nickname}"
950 end
951
952 defp user_response(user, attrs \\ %{}) do
953 %{
954 "deactivated" => user.deactivated,
955 "id" => user.id,
956 "email" => user.email,
957 "nickname" => user.nickname,
958 "roles" => %{"admin" => false, "moderator" => false},
959 "local" => user.local,
960 "tags" => [],
961 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
962 "display_name" => HTML.strip_tags(user.name || user.nickname),
963 "is_confirmed" => true,
964 "approval_pending" => false,
965 "url" => user.ap_id,
966 "registration_reason" => nil,
967 "actor_type" => "Person"
968 }
969 |> Map.merge(attrs)
970 end
971 end