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