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