1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.ModerationLog
13 alias Pleroma.ReportNote
14 alias Pleroma.Tests.ObanHelpers
16 alias Pleroma.UserInviteToken
17 alias Pleroma.Web.ActivityPub.Relay
18 alias Pleroma.Web.CommonAPI
19 alias Pleroma.Web.MastodonAPI.StatusView
20 alias Pleroma.Web.MediaProxy
21 import Pleroma.Factory
24 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
30 admin = insert(:user, is_admin: true)
31 token = insert(:oauth_admin_token, user: admin)
35 |> assign(:user, admin)
36 |> assign(:token, token)
38 {:ok, %{admin: admin, token: token, conn: conn}}
41 describe "with [:auth, :enforce_oauth_admin_scope_usage]," do
42 clear_config([:auth, :enforce_oauth_admin_scope_usage]) do
43 Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], true)
46 test "GET /api/pleroma/admin/users/:nickname requires admin:read:accounts or broader scope",
49 url = "/api/pleroma/admin/users/#{user.nickname}"
51 good_token1 = insert(:oauth_token, user: admin, scopes: ["admin"])
52 good_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read"])
53 good_token3 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts"])
55 bad_token1 = insert(:oauth_token, user: admin, scopes: ["read:accounts"])
56 bad_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts:partial"])
59 for good_token <- [good_token1, good_token2, good_token3] do
62 |> assign(:user, admin)
63 |> assign(:token, good_token)
66 assert json_response(conn, 200)
69 for good_token <- [good_token1, good_token2, good_token3] do
73 |> assign(:token, good_token)
76 assert json_response(conn, :forbidden)
79 for bad_token <- [bad_token1, bad_token2, bad_token3] do
82 |> assign(:user, admin)
83 |> assign(:token, bad_token)
86 assert json_response(conn, :forbidden)
91 describe "unless [:auth, :enforce_oauth_admin_scope_usage]," do
92 clear_config([:auth, :enforce_oauth_admin_scope_usage]) do
93 Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], false)
96 test "GET /api/pleroma/admin/users/:nickname requires " <>
97 "read:accounts or admin:read:accounts or broader scope",
100 url = "/api/pleroma/admin/users/#{user.nickname}"
102 good_token1 = insert(:oauth_token, user: admin, scopes: ["admin"])
103 good_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read"])
104 good_token3 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts"])
105 good_token4 = insert(:oauth_token, user: admin, scopes: ["read:accounts"])
106 good_token5 = insert(:oauth_token, user: admin, scopes: ["read"])
108 good_tokens = [good_token1, good_token2, good_token3, good_token4, good_token5]
110 bad_token1 = insert(:oauth_token, user: admin, scopes: ["read:accounts:partial"])
111 bad_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts:partial"])
114 for good_token <- good_tokens do
117 |> assign(:user, admin)
118 |> assign(:token, good_token)
121 assert json_response(conn, 200)
124 for good_token <- good_tokens do
127 |> assign(:user, nil)
128 |> assign(:token, good_token)
131 assert json_response(conn, :forbidden)
134 for bad_token <- [bad_token1, bad_token2, bad_token3] do
137 |> assign(:user, admin)
138 |> assign(:token, bad_token)
141 assert json_response(conn, :forbidden)
146 describe "DELETE /api/pleroma/admin/users" do
147 test "single user", %{admin: admin, conn: conn} do
152 |> put_req_header("accept", "application/json")
153 |> delete("/api/pleroma/admin/users?nickname=#{user.nickname}")
155 log_entry = Repo.one(ModerationLog)
157 assert ModerationLog.get_log_entry_message(log_entry) ==
158 "@#{admin.nickname} deleted users: @#{user.nickname}"
160 assert json_response(conn, 200) == user.nickname
163 test "multiple users", %{admin: admin, conn: conn} do
164 user_one = insert(:user)
165 user_two = insert(:user)
169 |> put_req_header("accept", "application/json")
170 |> delete("/api/pleroma/admin/users", %{
171 nicknames: [user_one.nickname, user_two.nickname]
174 log_entry = Repo.one(ModerationLog)
176 assert ModerationLog.get_log_entry_message(log_entry) ==
177 "@#{admin.nickname} deleted users: @#{user_one.nickname}, @#{user_two.nickname}"
179 response = json_response(conn, 200)
180 assert response -- [user_one.nickname, user_two.nickname] == []
184 describe "/api/pleroma/admin/users" do
185 test "Create", %{conn: conn} do
188 |> put_req_header("accept", "application/json")
189 |> post("/api/pleroma/admin/users", %{
192 "nickname" => "lain",
193 "email" => "lain@example.org",
197 "nickname" => "lain2",
198 "email" => "lain2@example.org",
204 response = json_response(conn, 200) |> Enum.map(&Map.get(&1, "type"))
205 assert response == ["success", "success"]
207 log_entry = Repo.one(ModerationLog)
209 assert ["lain", "lain2"] -- Enum.map(log_entry.data["subjects"], & &1["nickname"]) == []
212 test "Cannot create user with existing email", %{conn: conn} do
217 |> put_req_header("accept", "application/json")
218 |> post("/api/pleroma/admin/users", %{
221 "nickname" => "lain",
222 "email" => user.email,
228 assert json_response(conn, 409) == [
232 "email" => user.email,
235 "error" => "email has already been taken",
241 test "Cannot create user with existing nickname", %{conn: conn} do
246 |> put_req_header("accept", "application/json")
247 |> post("/api/pleroma/admin/users", %{
250 "nickname" => user.nickname,
251 "email" => "someuser@plerama.social",
257 assert json_response(conn, 409) == [
261 "email" => "someuser@plerama.social",
262 "nickname" => user.nickname
264 "error" => "nickname has already been taken",
270 test "Multiple user creation works in transaction", %{conn: conn} do
275 |> put_req_header("accept", "application/json")
276 |> post("/api/pleroma/admin/users", %{
279 "nickname" => "newuser",
280 "email" => "newuser@pleroma.social",
284 "nickname" => "lain",
285 "email" => user.email,
291 assert json_response(conn, 409) == [
295 "email" => user.email,
298 "error" => "email has already been taken",
304 "email" => "newuser@pleroma.social",
305 "nickname" => "newuser"
312 assert User.get_by_nickname("newuser") === nil
316 describe "/api/pleroma/admin/users/:nickname" do
317 test "Show", %{conn: conn} do
320 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}")
323 "deactivated" => false,
324 "id" => to_string(user.id),
326 "nickname" => user.nickname,
327 "roles" => %{"admin" => false, "moderator" => false},
329 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
330 "display_name" => HTML.strip_tags(user.name || user.nickname),
331 "confirmation_pending" => false
334 assert expected == json_response(conn, 200)
337 test "when the user doesn't exist", %{conn: conn} do
340 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}")
342 assert "Not found" == json_response(conn, 404)
346 describe "/api/pleroma/admin/users/follow" do
347 test "allows to force-follow another user", %{admin: admin, conn: conn} do
349 follower = insert(:user)
352 |> put_req_header("accept", "application/json")
353 |> post("/api/pleroma/admin/users/follow", %{
354 "follower" => follower.nickname,
355 "followed" => user.nickname
358 user = User.get_cached_by_id(user.id)
359 follower = User.get_cached_by_id(follower.id)
361 assert User.following?(follower, user)
363 log_entry = Repo.one(ModerationLog)
365 assert ModerationLog.get_log_entry_message(log_entry) ==
366 "@#{admin.nickname} made @#{follower.nickname} follow @#{user.nickname}"
370 describe "/api/pleroma/admin/users/unfollow" do
371 test "allows to force-unfollow another user", %{admin: admin, conn: conn} do
373 follower = insert(:user)
375 User.follow(follower, user)
378 |> put_req_header("accept", "application/json")
379 |> post("/api/pleroma/admin/users/unfollow", %{
380 "follower" => follower.nickname,
381 "followed" => user.nickname
384 user = User.get_cached_by_id(user.id)
385 follower = User.get_cached_by_id(follower.id)
387 refute User.following?(follower, user)
389 log_entry = Repo.one(ModerationLog)
391 assert ModerationLog.get_log_entry_message(log_entry) ==
392 "@#{admin.nickname} made @#{follower.nickname} unfollow @#{user.nickname}"
396 describe "PUT /api/pleroma/admin/users/tag" do
397 setup %{conn: conn} do
398 user1 = insert(:user, %{tags: ["x"]})
399 user2 = insert(:user, %{tags: ["y"]})
400 user3 = insert(:user, %{tags: ["unchanged"]})
404 |> put_req_header("accept", "application/json")
406 "/api/pleroma/admin/users/tag?nicknames[]=#{user1.nickname}&nicknames[]=" <>
407 "#{user2.nickname}&tags[]=foo&tags[]=bar"
410 %{conn: conn, user1: user1, user2: user2, user3: user3}
413 test "it appends specified tags to users with specified nicknames", %{
419 assert json_response(conn, :no_content)
420 assert User.get_cached_by_id(user1.id).tags == ["x", "foo", "bar"]
421 assert User.get_cached_by_id(user2.id).tags == ["y", "foo", "bar"]
423 log_entry = Repo.one(ModerationLog)
426 [user1.nickname, user2.nickname]
427 |> Enum.map(&"@#{&1}")
430 tags = ["foo", "bar"] |> Enum.join(", ")
432 assert ModerationLog.get_log_entry_message(log_entry) ==
433 "@#{admin.nickname} added tags: #{tags} to users: #{users}"
436 test "it does not modify tags of not specified users", %{conn: conn, user3: user3} do
437 assert json_response(conn, :no_content)
438 assert User.get_cached_by_id(user3.id).tags == ["unchanged"]
442 describe "DELETE /api/pleroma/admin/users/tag" do
443 setup %{conn: conn} do
444 user1 = insert(:user, %{tags: ["x"]})
445 user2 = insert(:user, %{tags: ["y", "z"]})
446 user3 = insert(:user, %{tags: ["unchanged"]})
450 |> put_req_header("accept", "application/json")
452 "/api/pleroma/admin/users/tag?nicknames[]=#{user1.nickname}&nicknames[]=" <>
453 "#{user2.nickname}&tags[]=x&tags[]=z"
456 %{conn: conn, user1: user1, user2: user2, user3: user3}
459 test "it removes specified tags from users with specified nicknames", %{
465 assert json_response(conn, :no_content)
466 assert User.get_cached_by_id(user1.id).tags == []
467 assert User.get_cached_by_id(user2.id).tags == ["y"]
469 log_entry = Repo.one(ModerationLog)
472 [user1.nickname, user2.nickname]
473 |> Enum.map(&"@#{&1}")
476 tags = ["x", "z"] |> Enum.join(", ")
478 assert ModerationLog.get_log_entry_message(log_entry) ==
479 "@#{admin.nickname} removed tags: #{tags} from users: #{users}"
482 test "it does not modify tags of not specified users", %{conn: conn, user3: user3} do
483 assert json_response(conn, :no_content)
484 assert User.get_cached_by_id(user3.id).tags == ["unchanged"]
488 describe "/api/pleroma/admin/users/:nickname/permission_group" do
489 test "GET is giving user_info", %{admin: admin, conn: conn} do
492 |> put_req_header("accept", "application/json")
493 |> get("/api/pleroma/admin/users/#{admin.nickname}/permission_group/")
495 assert json_response(conn, 200) == %{
497 "is_moderator" => false
501 test "/:right POST, can add to a permission group", %{admin: admin, conn: conn} do
506 |> put_req_header("accept", "application/json")
507 |> post("/api/pleroma/admin/users/#{user.nickname}/permission_group/admin")
509 assert json_response(conn, 200) == %{
513 log_entry = Repo.one(ModerationLog)
515 assert ModerationLog.get_log_entry_message(log_entry) ==
516 "@#{admin.nickname} made @#{user.nickname} admin"
519 test "/:right POST, can add to a permission group (multiple)", %{admin: admin, conn: conn} do
520 user_one = insert(:user)
521 user_two = insert(:user)
525 |> put_req_header("accept", "application/json")
526 |> post("/api/pleroma/admin/users/permission_group/admin", %{
527 nicknames: [user_one.nickname, user_two.nickname]
530 assert json_response(conn, 200) == %{"is_admin" => true}
532 log_entry = Repo.one(ModerationLog)
534 assert ModerationLog.get_log_entry_message(log_entry) ==
535 "@#{admin.nickname} made @#{user_one.nickname}, @#{user_two.nickname} admin"
538 test "/:right DELETE, can remove from a permission group", %{admin: admin, conn: conn} do
539 user = insert(:user, is_admin: true)
543 |> put_req_header("accept", "application/json")
544 |> delete("/api/pleroma/admin/users/#{user.nickname}/permission_group/admin")
546 assert json_response(conn, 200) == %{"is_admin" => false}
548 log_entry = Repo.one(ModerationLog)
550 assert ModerationLog.get_log_entry_message(log_entry) ==
551 "@#{admin.nickname} revoked admin role from @#{user.nickname}"
554 test "/:right DELETE, can remove from a permission group (multiple)", %{
558 user_one = insert(:user, is_admin: true)
559 user_two = insert(:user, is_admin: true)
563 |> put_req_header("accept", "application/json")
564 |> delete("/api/pleroma/admin/users/permission_group/admin", %{
565 nicknames: [user_one.nickname, user_two.nickname]
568 assert json_response(conn, 200) == %{"is_admin" => false}
570 log_entry = Repo.one(ModerationLog)
572 assert ModerationLog.get_log_entry_message(log_entry) ==
573 "@#{admin.nickname} revoked admin role from @#{user_one.nickname}, @#{
579 describe "POST /api/pleroma/admin/email_invite, with valid config" do
580 clear_config([:instance, :registrations_open]) do
581 Pleroma.Config.put([:instance, :registrations_open], false)
584 clear_config([:instance, :invites_enabled]) do
585 Pleroma.Config.put([:instance, :invites_enabled], true)
588 test "sends invitation and returns 204", %{admin: admin, conn: conn} do
589 recipient_email = "foo@bar.com"
590 recipient_name = "J. D."
595 "/api/pleroma/admin/users/email_invite?email=#{recipient_email}&name=#{recipient_name}"
598 assert json_response(conn, :no_content)
600 token_record = List.last(Pleroma.Repo.all(Pleroma.UserInviteToken))
602 refute token_record.used
604 notify_email = Pleroma.Config.get([:instance, :notify_email])
605 instance_name = Pleroma.Config.get([:instance, :name])
608 Pleroma.Emails.UserEmail.user_invitation_email(
615 Swoosh.TestAssertions.assert_email_sent(
616 from: {instance_name, notify_email},
617 to: {recipient_name, recipient_email},
618 html_body: email.html_body
622 test "it returns 403 if requested by a non-admin" do
623 non_admin_user = insert(:user)
624 token = insert(:oauth_token, user: non_admin_user)
628 |> assign(:user, non_admin_user)
629 |> assign(:token, token)
630 |> post("/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD")
632 assert json_response(conn, :forbidden)
636 describe "POST /api/pleroma/admin/users/email_invite, with invalid config" do
637 clear_config([:instance, :registrations_open])
638 clear_config([:instance, :invites_enabled])
640 test "it returns 500 if `invites_enabled` is not enabled", %{conn: conn} do
641 Pleroma.Config.put([:instance, :registrations_open], false)
642 Pleroma.Config.put([:instance, :invites_enabled], false)
644 conn = post(conn, "/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD")
646 assert json_response(conn, :internal_server_error)
649 test "it returns 500 if `registrations_open` is enabled", %{conn: conn} do
650 Pleroma.Config.put([:instance, :registrations_open], true)
651 Pleroma.Config.put([:instance, :invites_enabled], true)
653 conn = post(conn, "/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD")
655 assert json_response(conn, :internal_server_error)
659 test "/api/pleroma/admin/users/:nickname/password_reset", %{conn: conn} do
664 |> put_req_header("accept", "application/json")
665 |> get("/api/pleroma/admin/users/#{user.nickname}/password_reset")
667 resp = json_response(conn, 200)
669 assert Regex.match?(~r/(http:\/\/|https:\/\/)/, resp["link"])
672 describe "GET /api/pleroma/admin/users" do
673 test "renders users array for the first page", %{conn: conn, admin: admin} do
674 user = insert(:user, local: false, tags: ["foo", "bar"])
675 conn = get(conn, "/api/pleroma/admin/users?page=1")
680 "deactivated" => admin.deactivated,
682 "nickname" => admin.nickname,
683 "roles" => %{"admin" => true, "moderator" => false},
686 "avatar" => User.avatar_url(admin) |> MediaProxy.url(),
687 "display_name" => HTML.strip_tags(admin.name || admin.nickname),
688 "confirmation_pending" => false
691 "deactivated" => user.deactivated,
693 "nickname" => user.nickname,
694 "roles" => %{"admin" => false, "moderator" => false},
696 "tags" => ["foo", "bar"],
697 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
698 "display_name" => HTML.strip_tags(user.name || user.nickname),
699 "confirmation_pending" => false
702 |> Enum.sort_by(& &1["nickname"])
704 assert json_response(conn, 200) == %{
711 test "renders empty array for the second page", %{conn: conn} do
714 conn = get(conn, "/api/pleroma/admin/users?page=2")
716 assert json_response(conn, 200) == %{
723 test "regular search", %{conn: conn} do
724 user = insert(:user, nickname: "bob")
726 conn = get(conn, "/api/pleroma/admin/users?query=bo")
728 assert json_response(conn, 200) == %{
733 "deactivated" => user.deactivated,
735 "nickname" => user.nickname,
736 "roles" => %{"admin" => false, "moderator" => false},
739 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
740 "display_name" => HTML.strip_tags(user.name || user.nickname),
741 "confirmation_pending" => false
747 test "search by domain", %{conn: conn} do
748 user = insert(:user, nickname: "nickname@domain.com")
751 conn = get(conn, "/api/pleroma/admin/users?query=domain.com")
753 assert json_response(conn, 200) == %{
758 "deactivated" => user.deactivated,
760 "nickname" => user.nickname,
761 "roles" => %{"admin" => false, "moderator" => false},
764 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
765 "display_name" => HTML.strip_tags(user.name || user.nickname),
766 "confirmation_pending" => false
772 test "search by full nickname", %{conn: conn} do
773 user = insert(:user, nickname: "nickname@domain.com")
776 conn = get(conn, "/api/pleroma/admin/users?query=nickname@domain.com")
778 assert json_response(conn, 200) == %{
783 "deactivated" => user.deactivated,
785 "nickname" => user.nickname,
786 "roles" => %{"admin" => false, "moderator" => false},
789 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
790 "display_name" => HTML.strip_tags(user.name || user.nickname),
791 "confirmation_pending" => false
797 test "search by display name", %{conn: conn} do
798 user = insert(:user, name: "Display name")
801 conn = get(conn, "/api/pleroma/admin/users?name=display")
803 assert json_response(conn, 200) == %{
808 "deactivated" => user.deactivated,
810 "nickname" => user.nickname,
811 "roles" => %{"admin" => false, "moderator" => false},
814 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
815 "display_name" => HTML.strip_tags(user.name || user.nickname),
816 "confirmation_pending" => false
822 test "search by email", %{conn: conn} do
823 user = insert(:user, email: "email@example.com")
826 conn = get(conn, "/api/pleroma/admin/users?email=email@example.com")
828 assert json_response(conn, 200) == %{
833 "deactivated" => user.deactivated,
835 "nickname" => user.nickname,
836 "roles" => %{"admin" => false, "moderator" => false},
839 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
840 "display_name" => HTML.strip_tags(user.name || user.nickname),
841 "confirmation_pending" => false
847 test "regular search with page size", %{conn: conn} do
848 user = insert(:user, nickname: "aalice")
849 user2 = insert(:user, nickname: "alice")
851 conn1 = get(conn, "/api/pleroma/admin/users?query=a&page_size=1&page=1")
853 assert json_response(conn1, 200) == %{
858 "deactivated" => user.deactivated,
860 "nickname" => user.nickname,
861 "roles" => %{"admin" => false, "moderator" => false},
864 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
865 "display_name" => HTML.strip_tags(user.name || user.nickname),
866 "confirmation_pending" => false
871 conn2 = get(conn, "/api/pleroma/admin/users?query=a&page_size=1&page=2")
873 assert json_response(conn2, 200) == %{
878 "deactivated" => user2.deactivated,
880 "nickname" => user2.nickname,
881 "roles" => %{"admin" => false, "moderator" => false},
884 "avatar" => User.avatar_url(user2) |> MediaProxy.url(),
885 "display_name" => HTML.strip_tags(user2.name || user2.nickname),
886 "confirmation_pending" => false
892 test "only local users" do
893 admin = insert(:user, is_admin: true, nickname: "john")
894 token = insert(:oauth_admin_token, user: admin)
895 user = insert(:user, nickname: "bob")
897 insert(:user, nickname: "bobb", local: false)
901 |> assign(:user, admin)
902 |> assign(:token, token)
903 |> get("/api/pleroma/admin/users?query=bo&filters=local")
905 assert json_response(conn, 200) == %{
910 "deactivated" => user.deactivated,
912 "nickname" => user.nickname,
913 "roles" => %{"admin" => false, "moderator" => false},
916 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
917 "display_name" => HTML.strip_tags(user.name || user.nickname),
918 "confirmation_pending" => false
924 test "only local users with no query", %{conn: conn, admin: old_admin} do
925 admin = insert(:user, is_admin: true, nickname: "john")
926 user = insert(:user, nickname: "bob")
928 insert(:user, nickname: "bobb", local: false)
930 conn = get(conn, "/api/pleroma/admin/users?filters=local")
935 "deactivated" => user.deactivated,
937 "nickname" => user.nickname,
938 "roles" => %{"admin" => false, "moderator" => false},
941 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
942 "display_name" => HTML.strip_tags(user.name || user.nickname),
943 "confirmation_pending" => false
946 "deactivated" => admin.deactivated,
948 "nickname" => admin.nickname,
949 "roles" => %{"admin" => true, "moderator" => false},
952 "avatar" => User.avatar_url(admin) |> MediaProxy.url(),
953 "display_name" => HTML.strip_tags(admin.name || admin.nickname),
954 "confirmation_pending" => false
957 "deactivated" => false,
958 "id" => old_admin.id,
960 "nickname" => old_admin.nickname,
961 "roles" => %{"admin" => true, "moderator" => false},
963 "avatar" => User.avatar_url(old_admin) |> MediaProxy.url(),
964 "display_name" => HTML.strip_tags(old_admin.name || old_admin.nickname),
965 "confirmation_pending" => false
968 |> Enum.sort_by(& &1["nickname"])
970 assert json_response(conn, 200) == %{
977 test "load only admins", %{conn: conn, admin: admin} do
978 second_admin = insert(:user, is_admin: true)
982 conn = get(conn, "/api/pleroma/admin/users?filters=is_admin")
987 "deactivated" => false,
989 "nickname" => admin.nickname,
990 "roles" => %{"admin" => true, "moderator" => false},
991 "local" => admin.local,
993 "avatar" => User.avatar_url(admin) |> MediaProxy.url(),
994 "display_name" => HTML.strip_tags(admin.name || admin.nickname),
995 "confirmation_pending" => false
998 "deactivated" => false,
999 "id" => second_admin.id,
1000 "nickname" => second_admin.nickname,
1001 "roles" => %{"admin" => true, "moderator" => false},
1002 "local" => second_admin.local,
1004 "avatar" => User.avatar_url(second_admin) |> MediaProxy.url(),
1005 "display_name" => HTML.strip_tags(second_admin.name || second_admin.nickname),
1006 "confirmation_pending" => false
1009 |> Enum.sort_by(& &1["nickname"])
1011 assert json_response(conn, 200) == %{
1018 test "load only moderators", %{conn: conn} do
1019 moderator = insert(:user, is_moderator: true)
1023 conn = get(conn, "/api/pleroma/admin/users?filters=is_moderator")
1025 assert json_response(conn, 200) == %{
1030 "deactivated" => false,
1031 "id" => moderator.id,
1032 "nickname" => moderator.nickname,
1033 "roles" => %{"admin" => false, "moderator" => true},
1034 "local" => moderator.local,
1036 "avatar" => User.avatar_url(moderator) |> MediaProxy.url(),
1037 "display_name" => HTML.strip_tags(moderator.name || moderator.nickname),
1038 "confirmation_pending" => false
1044 test "load users with tags list", %{conn: conn} do
1045 user1 = insert(:user, tags: ["first"])
1046 user2 = insert(:user, tags: ["second"])
1050 conn = get(conn, "/api/pleroma/admin/users?tags[]=first&tags[]=second")
1055 "deactivated" => false,
1057 "nickname" => user1.nickname,
1058 "roles" => %{"admin" => false, "moderator" => false},
1059 "local" => user1.local,
1060 "tags" => ["first"],
1061 "avatar" => User.avatar_url(user1) |> MediaProxy.url(),
1062 "display_name" => HTML.strip_tags(user1.name || user1.nickname),
1063 "confirmation_pending" => false
1066 "deactivated" => false,
1068 "nickname" => user2.nickname,
1069 "roles" => %{"admin" => false, "moderator" => false},
1070 "local" => user2.local,
1071 "tags" => ["second"],
1072 "avatar" => User.avatar_url(user2) |> MediaProxy.url(),
1073 "display_name" => HTML.strip_tags(user2.name || user2.nickname),
1074 "confirmation_pending" => false
1077 |> Enum.sort_by(& &1["nickname"])
1079 assert json_response(conn, 200) == %{
1086 test "it works with multiple filters" do
1087 admin = insert(:user, nickname: "john", is_admin: true)
1088 token = insert(:oauth_admin_token, user: admin)
1089 user = insert(:user, nickname: "bob", local: false, deactivated: true)
1091 insert(:user, nickname: "ken", local: true, deactivated: true)
1092 insert(:user, nickname: "bobb", local: false, deactivated: false)
1096 |> assign(:user, admin)
1097 |> assign(:token, token)
1098 |> get("/api/pleroma/admin/users?filters=deactivated,external")
1100 assert json_response(conn, 200) == %{
1105 "deactivated" => user.deactivated,
1107 "nickname" => user.nickname,
1108 "roles" => %{"admin" => false, "moderator" => false},
1109 "local" => user.local,
1111 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
1112 "display_name" => HTML.strip_tags(user.name || user.nickname),
1113 "confirmation_pending" => false
1119 test "it omits relay user", %{admin: admin, conn: conn} do
1120 assert %User{} = Relay.get_actor()
1122 conn = get(conn, "/api/pleroma/admin/users")
1124 assert json_response(conn, 200) == %{
1129 "deactivated" => admin.deactivated,
1131 "nickname" => admin.nickname,
1132 "roles" => %{"admin" => true, "moderator" => false},
1135 "avatar" => User.avatar_url(admin) |> MediaProxy.url(),
1136 "display_name" => HTML.strip_tags(admin.name || admin.nickname),
1137 "confirmation_pending" => false
1144 test "PATCH /api/pleroma/admin/users/activate", %{admin: admin, conn: conn} do
1145 user_one = insert(:user, deactivated: true)
1146 user_two = insert(:user, deactivated: true)
1151 "/api/pleroma/admin/users/activate",
1152 %{nicknames: [user_one.nickname, user_two.nickname]}
1155 response = json_response(conn, 200)
1156 assert Enum.map(response["users"], & &1["deactivated"]) == [false, false]
1158 log_entry = Repo.one(ModerationLog)
1160 assert ModerationLog.get_log_entry_message(log_entry) ==
1161 "@#{admin.nickname} activated users: @#{user_one.nickname}, @#{user_two.nickname}"
1164 test "PATCH /api/pleroma/admin/users/deactivate", %{admin: admin, conn: conn} do
1165 user_one = insert(:user, deactivated: false)
1166 user_two = insert(:user, deactivated: false)
1171 "/api/pleroma/admin/users/deactivate",
1172 %{nicknames: [user_one.nickname, user_two.nickname]}
1175 response = json_response(conn, 200)
1176 assert Enum.map(response["users"], & &1["deactivated"]) == [true, true]
1178 log_entry = Repo.one(ModerationLog)
1180 assert ModerationLog.get_log_entry_message(log_entry) ==
1181 "@#{admin.nickname} deactivated users: @#{user_one.nickname}, @#{user_two.nickname}"
1184 test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admin, conn: conn} do
1185 user = insert(:user)
1187 conn = patch(conn, "/api/pleroma/admin/users/#{user.nickname}/toggle_activation")
1189 assert json_response(conn, 200) ==
1191 "deactivated" => !user.deactivated,
1193 "nickname" => user.nickname,
1194 "roles" => %{"admin" => false, "moderator" => false},
1197 "avatar" => User.avatar_url(user) |> MediaProxy.url(),
1198 "display_name" => HTML.strip_tags(user.name || user.nickname),
1199 "confirmation_pending" => false
1202 log_entry = Repo.one(ModerationLog)
1204 assert ModerationLog.get_log_entry_message(log_entry) ==
1205 "@#{admin.nickname} deactivated users: @#{user.nickname}"
1208 describe "POST /api/pleroma/admin/users/invite_token" do
1209 test "without options", %{conn: conn} do
1210 conn = post(conn, "/api/pleroma/admin/users/invite_token")
1212 invite_json = json_response(conn, 200)
1213 invite = UserInviteToken.find_by_token!(invite_json["token"])
1215 refute invite.expires_at
1216 refute invite.max_use
1217 assert invite.invite_type == "one_time"
1220 test "with expires_at", %{conn: conn} do
1222 post(conn, "/api/pleroma/admin/users/invite_token", %{
1223 "expires_at" => Date.to_string(Date.utc_today())
1226 invite_json = json_response(conn, 200)
1227 invite = UserInviteToken.find_by_token!(invite_json["token"])
1230 assert invite.expires_at == Date.utc_today()
1231 refute invite.max_use
1232 assert invite.invite_type == "date_limited"
1235 test "with max_use", %{conn: conn} do
1236 conn = post(conn, "/api/pleroma/admin/users/invite_token", %{"max_use" => 150})
1238 invite_json = json_response(conn, 200)
1239 invite = UserInviteToken.find_by_token!(invite_json["token"])
1241 refute invite.expires_at
1242 assert invite.max_use == 150
1243 assert invite.invite_type == "reusable"
1246 test "with max use and expires_at", %{conn: conn} do
1248 post(conn, "/api/pleroma/admin/users/invite_token", %{
1250 "expires_at" => Date.to_string(Date.utc_today())
1253 invite_json = json_response(conn, 200)
1254 invite = UserInviteToken.find_by_token!(invite_json["token"])
1256 assert invite.expires_at == Date.utc_today()
1257 assert invite.max_use == 150
1258 assert invite.invite_type == "reusable_date_limited"
1262 describe "GET /api/pleroma/admin/users/invites" do
1263 test "no invites", %{conn: conn} do
1264 conn = get(conn, "/api/pleroma/admin/users/invites")
1266 assert json_response(conn, 200) == %{"invites" => []}
1269 test "with invite", %{conn: conn} do
1270 {:ok, invite} = UserInviteToken.create_invite()
1272 conn = get(conn, "/api/pleroma/admin/users/invites")
1274 assert json_response(conn, 200) == %{
1277 "expires_at" => nil,
1279 "invite_type" => "one_time",
1281 "token" => invite.token,
1290 describe "POST /api/pleroma/admin/users/revoke_invite" do
1291 test "with token", %{conn: conn} do
1292 {:ok, invite} = UserInviteToken.create_invite()
1294 conn = post(conn, "/api/pleroma/admin/users/revoke_invite", %{"token" => invite.token})
1296 assert json_response(conn, 200) == %{
1297 "expires_at" => nil,
1299 "invite_type" => "one_time",
1301 "token" => invite.token,
1307 test "with invalid token", %{conn: conn} do
1308 conn = post(conn, "/api/pleroma/admin/users/revoke_invite", %{"token" => "foo"})
1310 assert json_response(conn, :not_found) == "Not found"
1314 describe "GET /api/pleroma/admin/reports/:id" do
1315 test "returns report by its id", %{conn: conn} do
1316 [reporter, target_user] = insert_pair(:user)
1317 activity = insert(:note_activity, user: target_user)
1319 {:ok, %{id: report_id}} =
1320 CommonAPI.report(reporter, %{
1321 "account_id" => target_user.id,
1322 "comment" => "I feel offended",
1323 "status_ids" => [activity.id]
1328 |> get("/api/pleroma/admin/reports/#{report_id}")
1329 |> json_response(:ok)
1331 assert response["id"] == report_id
1334 test "returns 404 when report id is invalid", %{conn: conn} do
1335 conn = get(conn, "/api/pleroma/admin/reports/test")
1337 assert json_response(conn, :not_found) == "Not found"
1341 describe "PATCH /api/pleroma/admin/reports" do
1343 [reporter, target_user] = insert_pair(:user)
1344 activity = insert(:note_activity, user: target_user)
1346 {:ok, %{id: report_id}} =
1347 CommonAPI.report(reporter, %{
1348 "account_id" => target_user.id,
1349 "comment" => "I feel offended",
1350 "status_ids" => [activity.id]
1353 {:ok, %{id: second_report_id}} =
1354 CommonAPI.report(reporter, %{
1355 "account_id" => target_user.id,
1356 "comment" => "I feel very offended",
1357 "status_ids" => [activity.id]
1362 second_report_id: second_report_id
1366 test "mark report as resolved", %{conn: conn, id: id, admin: admin} do
1368 |> patch("/api/pleroma/admin/reports", %{
1370 %{"state" => "resolved", "id" => id}
1373 |> json_response(:no_content)
1375 activity = Activity.get_by_id(id)
1376 assert activity.data["state"] == "resolved"
1378 log_entry = Repo.one(ModerationLog)
1380 assert ModerationLog.get_log_entry_message(log_entry) ==
1381 "@#{admin.nickname} updated report ##{id} with 'resolved' state"
1384 test "closes report", %{conn: conn, id: id, admin: admin} do
1386 |> patch("/api/pleroma/admin/reports", %{
1388 %{"state" => "closed", "id" => id}
1391 |> json_response(:no_content)
1393 activity = Activity.get_by_id(id)
1394 assert activity.data["state"] == "closed"
1396 log_entry = Repo.one(ModerationLog)
1398 assert ModerationLog.get_log_entry_message(log_entry) ==
1399 "@#{admin.nickname} updated report ##{id} with 'closed' state"
1402 test "returns 400 when state is unknown", %{conn: conn, id: id} do
1405 |> patch("/api/pleroma/admin/reports", %{
1407 %{"state" => "test", "id" => id}
1411 assert hd(json_response(conn, :bad_request))["error"] == "Unsupported state"
1414 test "returns 404 when report is not exist", %{conn: conn} do
1417 |> patch("/api/pleroma/admin/reports", %{
1419 %{"state" => "closed", "id" => "test"}
1423 assert hd(json_response(conn, :bad_request))["error"] == "not_found"
1426 test "updates state of multiple reports", %{
1430 second_report_id: second_report_id
1433 |> patch("/api/pleroma/admin/reports", %{
1435 %{"state" => "resolved", "id" => id},
1436 %{"state" => "closed", "id" => second_report_id}
1439 |> json_response(:no_content)
1441 activity = Activity.get_by_id(id)
1442 second_activity = Activity.get_by_id(second_report_id)
1443 assert activity.data["state"] == "resolved"
1444 assert second_activity.data["state"] == "closed"
1446 [first_log_entry, second_log_entry] = Repo.all(ModerationLog)
1448 assert ModerationLog.get_log_entry_message(first_log_entry) ==
1449 "@#{admin.nickname} updated report ##{id} with 'resolved' state"
1451 assert ModerationLog.get_log_entry_message(second_log_entry) ==
1452 "@#{admin.nickname} updated report ##{second_report_id} with 'closed' state"
1456 describe "GET /api/pleroma/admin/reports" do
1457 test "returns empty response when no reports created", %{conn: conn} do
1460 |> get("/api/pleroma/admin/reports")
1461 |> json_response(:ok)
1463 assert Enum.empty?(response["reports"])
1464 assert response["total"] == 0
1467 test "returns reports", %{conn: conn} do
1468 [reporter, target_user] = insert_pair(:user)
1469 activity = insert(:note_activity, user: target_user)
1471 {:ok, %{id: report_id}} =
1472 CommonAPI.report(reporter, %{
1473 "account_id" => target_user.id,
1474 "comment" => "I feel offended",
1475 "status_ids" => [activity.id]
1480 |> get("/api/pleroma/admin/reports")
1481 |> json_response(:ok)
1483 [report] = response["reports"]
1485 assert length(response["reports"]) == 1
1486 assert report["id"] == report_id
1488 assert response["total"] == 1
1491 test "returns reports with specified state", %{conn: conn} do
1492 [reporter, target_user] = insert_pair(:user)
1493 activity = insert(:note_activity, user: target_user)
1495 {:ok, %{id: first_report_id}} =
1496 CommonAPI.report(reporter, %{
1497 "account_id" => target_user.id,
1498 "comment" => "I feel offended",
1499 "status_ids" => [activity.id]
1502 {:ok, %{id: second_report_id}} =
1503 CommonAPI.report(reporter, %{
1504 "account_id" => target_user.id,
1505 "comment" => "I don't like this user"
1508 CommonAPI.update_report_state(second_report_id, "closed")
1512 |> get("/api/pleroma/admin/reports", %{
1515 |> json_response(:ok)
1517 [open_report] = response["reports"]
1519 assert length(response["reports"]) == 1
1520 assert open_report["id"] == first_report_id
1522 assert response["total"] == 1
1526 |> get("/api/pleroma/admin/reports", %{
1529 |> json_response(:ok)
1531 [closed_report] = response["reports"]
1533 assert length(response["reports"]) == 1
1534 assert closed_report["id"] == second_report_id
1536 assert response["total"] == 1
1540 |> get("/api/pleroma/admin/reports", %{
1541 "state" => "resolved"
1543 |> json_response(:ok)
1545 assert Enum.empty?(response["reports"])
1546 assert response["total"] == 0
1549 test "returns 403 when requested by a non-admin" do
1550 user = insert(:user)
1551 token = insert(:oauth_token, user: user)
1555 |> assign(:user, user)
1556 |> assign(:token, token)
1557 |> get("/api/pleroma/admin/reports")
1559 assert json_response(conn, :forbidden) ==
1560 %{"error" => "User is not an admin or OAuth admin scope is not granted."}
1563 test "returns 403 when requested by anonymous" do
1564 conn = get(build_conn(), "/api/pleroma/admin/reports")
1566 assert json_response(conn, :forbidden) == %{"error" => "Invalid credentials."}
1570 describe "GET /api/pleroma/admin/grouped_reports" do
1572 [reporter, target_user] = insert_pair(:user)
1574 date1 = (DateTime.to_unix(DateTime.utc_now()) + 1000) |> DateTime.from_unix!()
1575 date2 = (DateTime.to_unix(DateTime.utc_now()) + 2000) |> DateTime.from_unix!()
1576 date3 = (DateTime.to_unix(DateTime.utc_now()) + 3000) |> DateTime.from_unix!()
1579 insert(:note_activity, user: target_user, data_attrs: %{"published" => date1})
1582 insert(:note_activity, user: target_user, data_attrs: %{"published" => date2})
1585 insert(:note_activity, user: target_user, data_attrs: %{"published" => date3})
1587 {:ok, first_report} =
1588 CommonAPI.report(reporter, %{
1589 "account_id" => target_user.id,
1590 "status_ids" => [first_status.id, second_status.id, third_status.id]
1593 {:ok, second_report} =
1594 CommonAPI.report(reporter, %{
1595 "account_id" => target_user.id,
1596 "status_ids" => [first_status.id, second_status.id]
1599 {:ok, third_report} =
1600 CommonAPI.report(reporter, %{
1601 "account_id" => target_user.id,
1602 "status_ids" => [first_status.id]
1606 first_status: Activity.get_by_ap_id_with_object(first_status.data["id"]),
1607 second_status: Activity.get_by_ap_id_with_object(second_status.data["id"]),
1608 third_status: Activity.get_by_ap_id_with_object(third_status.data["id"]),
1609 first_report: first_report,
1610 first_status_reports: [first_report, second_report, third_report],
1611 second_status_reports: [first_report, second_report],
1612 third_status_reports: [first_report],
1613 target_user: target_user,
1618 test "returns reports grouped by status", %{
1620 first_status: first_status,
1621 second_status: second_status,
1622 third_status: third_status,
1623 first_status_reports: first_status_reports,
1624 second_status_reports: second_status_reports,
1625 third_status_reports: third_status_reports,
1626 target_user: target_user,
1631 |> get("/api/pleroma/admin/grouped_reports")
1632 |> json_response(:ok)
1634 assert length(response["reports"]) == 3
1636 first_group = Enum.find(response["reports"], &(&1["status"]["id"] == first_status.id))
1638 second_group = Enum.find(response["reports"], &(&1["status"]["id"] == second_status.id))
1640 third_group = Enum.find(response["reports"], &(&1["status"]["id"] == third_status.id))
1642 assert length(first_group["reports"]) == 3
1643 assert length(second_group["reports"]) == 2
1644 assert length(third_group["reports"]) == 1
1646 assert first_group["date"] ==
1647 Enum.max_by(first_status_reports, fn act ->
1648 NaiveDateTime.from_iso8601!(act.data["published"])
1649 end).data["published"]
1651 assert first_group["status"] ==
1653 stringify_keys(StatusView.render("show.json", %{activity: first_status})),
1658 assert(first_group["account"]["id"] == target_user.id)
1660 assert length(first_group["actors"]) == 1
1661 assert hd(first_group["actors"])["id"] == reporter.id
1663 assert Enum.map(first_group["reports"], & &1["id"]) --
1664 Enum.map(first_status_reports, & &1.id) == []
1666 assert second_group["date"] ==
1667 Enum.max_by(second_status_reports, fn act ->
1668 NaiveDateTime.from_iso8601!(act.data["published"])
1669 end).data["published"]
1671 assert second_group["status"] ==
1673 stringify_keys(StatusView.render("show.json", %{activity: second_status})),
1678 assert second_group["account"]["id"] == target_user.id
1680 assert length(second_group["actors"]) == 1
1681 assert hd(second_group["actors"])["id"] == reporter.id
1683 assert Enum.map(second_group["reports"], & &1["id"]) --
1684 Enum.map(second_status_reports, & &1.id) == []
1686 assert third_group["date"] ==
1687 Enum.max_by(third_status_reports, fn act ->
1688 NaiveDateTime.from_iso8601!(act.data["published"])
1689 end).data["published"]
1691 assert third_group["status"] ==
1693 stringify_keys(StatusView.render("show.json", %{activity: third_status})),
1698 assert third_group["account"]["id"] == target_user.id
1700 assert length(third_group["actors"]) == 1
1701 assert hd(third_group["actors"])["id"] == reporter.id
1703 assert Enum.map(third_group["reports"], & &1["id"]) --
1704 Enum.map(third_status_reports, & &1.id) == []
1707 test "reopened report renders status data", %{
1709 first_report: first_report,
1710 first_status: first_status
1712 {:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved")
1716 |> get("/api/pleroma/admin/grouped_reports")
1717 |> json_response(:ok)
1719 first_group = Enum.find(response["reports"], &(&1["status"]["id"] == first_status.id))
1721 assert first_group["status"] ==
1723 stringify_keys(StatusView.render("show.json", %{activity: first_status})),
1729 test "reopened report does not render status data if status has been deleted", %{
1731 first_report: first_report,
1732 first_status: first_status,
1733 target_user: target_user
1735 {:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved")
1736 {:ok, _} = CommonAPI.delete(first_status.id, target_user)
1738 refute Activity.get_by_ap_id(first_status.id)
1742 |> get("/api/pleroma/admin/grouped_reports")
1743 |> json_response(:ok)
1745 assert Enum.find(response["reports"], &(&1["status"]["deleted"] == true))["status"][
1749 assert length(Enum.filter(response["reports"], &(&1["status"]["deleted"] == false))) == 2
1752 test "account not empty if status was deleted", %{
1754 first_report: first_report,
1755 first_status: first_status,
1756 target_user: target_user
1758 {:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved")
1759 {:ok, _} = CommonAPI.delete(first_status.id, target_user)
1761 refute Activity.get_by_ap_id(first_status.id)
1765 |> get("/api/pleroma/admin/grouped_reports")
1766 |> json_response(:ok)
1768 assert Enum.find(response["reports"], &(&1["status"]["deleted"] == true))["account"]
1772 describe "PUT /api/pleroma/admin/statuses/:id" do
1774 activity = insert(:note_activity)
1779 test "toggle sensitive flag", %{conn: conn, id: id, admin: admin} do
1782 |> put("/api/pleroma/admin/statuses/#{id}", %{"sensitive" => "true"})
1783 |> json_response(:ok)
1785 assert response["sensitive"]
1787 log_entry = Repo.one(ModerationLog)
1789 assert ModerationLog.get_log_entry_message(log_entry) ==
1790 "@#{admin.nickname} updated status ##{id}, set sensitive: 'true'"
1794 |> put("/api/pleroma/admin/statuses/#{id}", %{"sensitive" => "false"})
1795 |> json_response(:ok)
1797 refute response["sensitive"]
1800 test "change visibility flag", %{conn: conn, id: id, admin: admin} do
1803 |> put("/api/pleroma/admin/statuses/#{id}", %{"visibility" => "public"})
1804 |> json_response(:ok)
1806 assert response["visibility"] == "public"
1808 log_entry = Repo.one(ModerationLog)
1810 assert ModerationLog.get_log_entry_message(log_entry) ==
1811 "@#{admin.nickname} updated status ##{id}, set visibility: 'public'"
1815 |> put("/api/pleroma/admin/statuses/#{id}", %{"visibility" => "private"})
1816 |> json_response(:ok)
1818 assert response["visibility"] == "private"
1822 |> put("/api/pleroma/admin/statuses/#{id}", %{"visibility" => "unlisted"})
1823 |> json_response(:ok)
1825 assert response["visibility"] == "unlisted"
1828 test "returns 400 when visibility is unknown", %{conn: conn, id: id} do
1829 conn = put(conn, "/api/pleroma/admin/statuses/#{id}", %{"visibility" => "test"})
1831 assert json_response(conn, :bad_request) == "Unsupported visibility"
1835 describe "DELETE /api/pleroma/admin/statuses/:id" do
1837 activity = insert(:note_activity)
1842 test "deletes status", %{conn: conn, id: id, admin: admin} do
1844 |> delete("/api/pleroma/admin/statuses/#{id}")
1845 |> json_response(:ok)
1847 refute Activity.get_by_id(id)
1849 log_entry = Repo.one(ModerationLog)
1851 assert ModerationLog.get_log_entry_message(log_entry) ==
1852 "@#{admin.nickname} deleted status ##{id}"
1855 test "returns error when status is not exist", %{conn: conn} do
1856 conn = delete(conn, "/api/pleroma/admin/statuses/test")
1858 assert json_response(conn, :bad_request) == "Could not delete"
1862 describe "GET /api/pleroma/admin/config" do
1863 test "without any settings in db", %{conn: conn} do
1864 conn = get(conn, "/api/pleroma/admin/config")
1866 assert json_response(conn, 200) == %{"configs" => []}
1869 test "with settings in db", %{conn: conn} do
1870 config1 = insert(:config)
1871 config2 = insert(:config)
1873 conn = get(conn, "/api/pleroma/admin/config")
1886 } = json_response(conn, 200)
1888 assert key1 == config1.key
1889 assert key2 == config2.key
1893 describe "POST /api/pleroma/admin/config" do
1895 temp_file = "config/test.exported_from_db.secret.exs"
1898 Application.delete_env(:pleroma, :key1)
1899 Application.delete_env(:pleroma, :key2)
1900 Application.delete_env(:pleroma, :key3)
1901 Application.delete_env(:pleroma, :key4)
1902 Application.delete_env(:pleroma, :keyaa1)
1903 Application.delete_env(:pleroma, :keyaa2)
1904 Application.delete_env(:pleroma, Pleroma.Web.Endpoint.NotReal)
1905 Application.delete_env(:pleroma, Pleroma.Captcha.NotReal)
1906 :ok = File.rm(temp_file)
1910 clear_config([:instance, :dynamic_configuration]) do
1911 Pleroma.Config.put([:instance, :dynamic_configuration], true)
1914 @tag capture_log: true
1915 test "create new config setting in db", %{conn: conn} do
1917 post(conn, "/api/pleroma/admin/config", %{
1919 %{group: "pleroma", key: "key1", value: "value1"},
1922 key: "Ueberauth.Strategy.Twitter.OAuth",
1923 value: [%{"tuple" => [":consumer_secret", "aaaa"]}]
1929 ":nested_1" => "nested_value1",
1931 %{":nested_22" => "nested_value222"},
1932 %{":nested_33" => %{":nested_44" => "nested_444"}}
1940 %{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
1941 %{"nested_4" => true}
1947 value: %{":nested_5" => ":upload", "endpoint" => "https://example.com"}
1952 value: %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}
1957 assert json_response(conn, 200) == %{
1960 "group" => "pleroma",
1965 "group" => "ueberauth",
1966 "key" => "Ueberauth.Strategy.Twitter.OAuth",
1967 "value" => [%{"tuple" => [":consumer_secret", "aaaa"]}]
1970 "group" => "pleroma",
1973 ":nested_1" => "nested_value1",
1975 %{":nested_22" => "nested_value222"},
1976 %{":nested_33" => %{":nested_44" => "nested_444"}}
1981 "group" => "pleroma",
1984 %{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
1985 %{"nested_4" => true}
1989 "group" => "pleroma",
1991 "value" => %{"endpoint" => "https://example.com", ":nested_5" => ":upload"}
1996 "value" => %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}
2001 assert Application.get_env(:pleroma, :key1) == "value1"
2003 assert Application.get_env(:pleroma, :key2) == %{
2004 nested_1: "nested_value1",
2006 %{nested_22: "nested_value222"},
2007 %{nested_33: %{nested_44: "nested_444"}}
2011 assert Application.get_env(:pleroma, :key3) == [
2012 %{"nested_3" => :nested_3, "nested_33" => "nested_33"},
2013 %{"nested_4" => true}
2016 assert Application.get_env(:pleroma, :key4) == %{
2017 "endpoint" => "https://example.com",
2021 assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []}
2024 test "update config setting & delete", %{conn: conn} do
2025 config1 = insert(:config, key: "keyaa1")
2026 config2 = insert(:config, key: "keyaa2")
2030 key: "Ueberauth.Strategy.Microsoft.OAuth",
2031 value: :erlang.term_to_binary([])
2035 post(conn, "/api/pleroma/admin/config", %{
2037 %{group: config1.group, key: config1.key, value: "another_value"},
2038 %{group: config2.group, key: config2.key, delete: "true"},
2041 key: "Ueberauth.Strategy.Microsoft.OAuth",
2047 assert json_response(conn, 200) == %{
2050 "group" => "pleroma",
2051 "key" => config1.key,
2052 "value" => "another_value"
2057 assert Application.get_env(:pleroma, :keyaa1) == "another_value"
2058 refute Application.get_env(:pleroma, :keyaa2)
2061 test "common config example", %{conn: conn} do
2063 post(conn, "/api/pleroma/admin/config", %{
2066 "group" => "pleroma",
2067 "key" => "Pleroma.Captcha.NotReal",
2069 %{"tuple" => [":enabled", false]},
2070 %{"tuple" => [":method", "Pleroma.Captcha.Kocaptcha"]},
2071 %{"tuple" => [":seconds_valid", 60]},
2072 %{"tuple" => [":path", ""]},
2073 %{"tuple" => [":key1", nil]},
2074 %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]},
2075 %{"tuple" => [":regex1", "~r/https:\/\/example.com/"]},
2076 %{"tuple" => [":regex2", "~r/https:\/\/example.com/u"]},
2077 %{"tuple" => [":regex3", "~r/https:\/\/example.com/i"]},
2078 %{"tuple" => [":regex4", "~r/https:\/\/example.com/s"]}
2084 assert json_response(conn, 200) == %{
2087 "group" => "pleroma",
2088 "key" => "Pleroma.Captcha.NotReal",
2090 %{"tuple" => [":enabled", false]},
2091 %{"tuple" => [":method", "Pleroma.Captcha.Kocaptcha"]},
2092 %{"tuple" => [":seconds_valid", 60]},
2093 %{"tuple" => [":path", ""]},
2094 %{"tuple" => [":key1", nil]},
2095 %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]},
2096 %{"tuple" => [":regex1", "~r/https:\\/\\/example.com/"]},
2097 %{"tuple" => [":regex2", "~r/https:\\/\\/example.com/u"]},
2098 %{"tuple" => [":regex3", "~r/https:\\/\\/example.com/i"]},
2099 %{"tuple" => [":regex4", "~r/https:\\/\\/example.com/s"]}
2106 test "tuples with more than two values", %{conn: conn} do
2108 post(conn, "/api/pleroma/admin/config", %{
2111 "group" => "pleroma",
2112 "key" => "Pleroma.Web.Endpoint.NotReal",
2128 "/api/v1/streaming",
2129 "Pleroma.Web.MastodonAPI.WebsocketHandler",
2136 "Phoenix.Endpoint.CowboyWebSocket",
2139 "Phoenix.Transports.WebSocket",
2142 "Pleroma.Web.Endpoint",
2143 "Pleroma.Web.UserSocket",
2154 "Phoenix.Endpoint.Cowboy2Handler",
2155 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
2172 assert json_response(conn, 200) == %{
2175 "group" => "pleroma",
2176 "key" => "Pleroma.Web.Endpoint.NotReal",
2192 "/api/v1/streaming",
2193 "Pleroma.Web.MastodonAPI.WebsocketHandler",
2200 "Phoenix.Endpoint.CowboyWebSocket",
2203 "Phoenix.Transports.WebSocket",
2206 "Pleroma.Web.Endpoint",
2207 "Pleroma.Web.UserSocket",
2218 "Phoenix.Endpoint.Cowboy2Handler",
2219 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
2237 test "settings with nesting map", %{conn: conn} do
2239 post(conn, "/api/pleroma/admin/config", %{
2242 "group" => "pleroma",
2245 %{"tuple" => [":key2", "some_val"]},
2250 ":max_options" => 20,
2251 ":max_option_chars" => 200,
2252 ":min_expiration" => 0,
2253 ":max_expiration" => 31_536_000,
2255 ":max_options" => 20,
2256 ":max_option_chars" => 200,
2257 ":min_expiration" => 0,
2258 ":max_expiration" => 31_536_000
2268 assert json_response(conn, 200) ==
2272 "group" => "pleroma",
2275 %{"tuple" => [":key2", "some_val"]},
2280 ":max_expiration" => 31_536_000,
2281 ":max_option_chars" => 200,
2282 ":max_options" => 20,
2283 ":min_expiration" => 0,
2285 ":max_expiration" => 31_536_000,
2286 ":max_option_chars" => 200,
2287 ":max_options" => 20,
2288 ":min_expiration" => 0
2299 test "value as map", %{conn: conn} do
2301 post(conn, "/api/pleroma/admin/config", %{
2304 "group" => "pleroma",
2306 "value" => %{"key" => "some_val"}
2311 assert json_response(conn, 200) ==
2315 "group" => "pleroma",
2317 "value" => %{"key" => "some_val"}
2323 test "dispatch setting", %{conn: conn} do
2325 post(conn, "/api/pleroma/admin/config", %{
2328 "group" => "pleroma",
2329 "key" => "Pleroma.Web.Endpoint.NotReal",
2335 %{"tuple" => [":ip", %{"tuple" => [127, 0, 0, 1]}]},
2336 %{"tuple" => [":dispatch", ["{:_,
2338 {\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
2339 {\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
2340 {Phoenix.Transports.WebSocket,
2341 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
2342 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
2353 "{:_, [{\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []}, " <>
2354 "{\"/websocket\", Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket, " <>
2355 "{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}}, " <>
2356 "{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}]}"
2358 assert json_response(conn, 200) == %{
2361 "group" => "pleroma",
2362 "key" => "Pleroma.Web.Endpoint.NotReal",
2368 %{"tuple" => [":ip", %{"tuple" => [127, 0, 0, 1]}]},
2386 test "queues key as atom", %{conn: conn} do
2388 post(conn, "/api/pleroma/admin/config", %{
2394 %{"tuple" => [":federator_incoming", 50]},
2395 %{"tuple" => [":federator_outgoing", 50]},
2396 %{"tuple" => [":web_push", 50]},
2397 %{"tuple" => [":mailer", 10]},
2398 %{"tuple" => [":transmogrifier", 20]},
2399 %{"tuple" => [":scheduled_activities", 10]},
2400 %{"tuple" => [":background", 5]}
2406 assert json_response(conn, 200) == %{
2412 %{"tuple" => [":federator_incoming", 50]},
2413 %{"tuple" => [":federator_outgoing", 50]},
2414 %{"tuple" => [":web_push", 50]},
2415 %{"tuple" => [":mailer", 10]},
2416 %{"tuple" => [":transmogrifier", 20]},
2417 %{"tuple" => [":scheduled_activities", 10]},
2418 %{"tuple" => [":background", 5]}
2425 test "delete part of settings by atom subkeys", %{conn: conn} do
2429 value: :erlang.term_to_binary(subkey1: "val1", subkey2: "val2", subkey3: "val3")
2433 post(conn, "/api/pleroma/admin/config", %{
2436 group: config.group,
2438 subkeys: [":subkey1", ":subkey3"],
2445 json_response(conn, 200) == %{
2448 "group" => "pleroma",
2450 "value" => [%{"tuple" => [":subkey2", "val2"]}]
2458 describe "config mix tasks run" do
2460 temp_file = "config/test.exported_from_db.secret.exs"
2462 Mix.shell(Mix.Shell.Quiet)
2465 Mix.shell(Mix.Shell.IO)
2466 :ok = File.rm(temp_file)
2472 clear_config([:instance, :dynamic_configuration]) do
2473 Pleroma.Config.put([:instance, :dynamic_configuration], true)
2476 clear_config([:feed, :post_title]) do
2477 Pleroma.Config.put([:feed, :post_title], %{max_length: 100, omission: "…"})
2480 test "transfer settings to DB and to file", %{conn: conn} do
2481 assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == []
2482 ret_conn = get(conn, "/api/pleroma/admin/config/migrate_to_db")
2483 assert json_response(ret_conn, 200) == %{}
2484 assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) > 0
2486 ret_conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
2488 assert json_response(ret_conn, 200) == %{}
2489 assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == []
2493 describe "GET /api/pleroma/admin/users/:nickname/statuses" do
2495 user = insert(:user)
2497 date1 = (DateTime.to_unix(DateTime.utc_now()) + 2000) |> DateTime.from_unix!()
2498 date2 = (DateTime.to_unix(DateTime.utc_now()) + 1000) |> DateTime.from_unix!()
2499 date3 = (DateTime.to_unix(DateTime.utc_now()) + 3000) |> DateTime.from_unix!()
2501 insert(:note_activity, user: user, published: date1)
2502 insert(:note_activity, user: user, published: date2)
2503 insert(:note_activity, user: user, published: date3)
2508 test "renders user's statuses", %{conn: conn, user: user} do
2509 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses")
2511 assert json_response(conn, 200) |> length() == 3
2514 test "renders user's statuses with a limit", %{conn: conn, user: user} do
2515 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=2")
2517 assert json_response(conn, 200) |> length() == 2
2520 test "doesn't return private statuses by default", %{conn: conn, user: user} do
2521 {:ok, _private_status} =
2522 CommonAPI.post(user, %{"status" => "private", "visibility" => "private"})
2524 {:ok, _public_status} =
2525 CommonAPI.post(user, %{"status" => "public", "visibility" => "public"})
2527 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses")
2529 assert json_response(conn, 200) |> length() == 4
2532 test "returns private statuses with godmode on", %{conn: conn, user: user} do
2533 {:ok, _private_status} =
2534 CommonAPI.post(user, %{"status" => "private", "visibility" => "private"})
2536 {:ok, _public_status} =
2537 CommonAPI.post(user, %{"status" => "public", "visibility" => "public"})
2539 conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses?godmode=true")
2541 assert json_response(conn, 200) |> length() == 5
2545 describe "GET /api/pleroma/admin/moderation_log" do
2547 moderator = insert(:user, is_moderator: true)
2549 %{moderator: moderator}
2552 test "returns the log", %{conn: conn, admin: admin} do
2553 Repo.insert(%ModerationLog{
2557 "nickname" => admin.nickname,
2560 action: "relay_follow",
2561 target: "https://example.org/relay"
2563 inserted_at: NaiveDateTime.truncate(~N[2017-08-15 15:47:06.597036], :second)
2566 Repo.insert(%ModerationLog{
2570 "nickname" => admin.nickname,
2573 action: "relay_unfollow",
2574 target: "https://example.org/relay"
2576 inserted_at: NaiveDateTime.truncate(~N[2017-08-16 15:47:06.597036], :second)
2579 conn = get(conn, "/api/pleroma/admin/moderation_log")
2581 response = json_response(conn, 200)
2582 [first_entry, second_entry] = response["items"]
2584 assert response["total"] == 2
2585 assert first_entry["data"]["action"] == "relay_unfollow"
2587 assert first_entry["message"] ==
2588 "@#{admin.nickname} unfollowed relay: https://example.org/relay"
2590 assert second_entry["data"]["action"] == "relay_follow"
2592 assert second_entry["message"] ==
2593 "@#{admin.nickname} followed relay: https://example.org/relay"
2596 test "returns the log with pagination", %{conn: conn, admin: admin} do
2597 Repo.insert(%ModerationLog{
2601 "nickname" => admin.nickname,
2604 action: "relay_follow",
2605 target: "https://example.org/relay"
2607 inserted_at: NaiveDateTime.truncate(~N[2017-08-15 15:47:06.597036], :second)
2610 Repo.insert(%ModerationLog{
2614 "nickname" => admin.nickname,
2617 action: "relay_unfollow",
2618 target: "https://example.org/relay"
2620 inserted_at: NaiveDateTime.truncate(~N[2017-08-16 15:47:06.597036], :second)
2623 conn1 = get(conn, "/api/pleroma/admin/moderation_log?page_size=1&page=1")
2625 response1 = json_response(conn1, 200)
2626 [first_entry] = response1["items"]
2628 assert response1["total"] == 2
2629 assert response1["items"] |> length() == 1
2630 assert first_entry["data"]["action"] == "relay_unfollow"
2632 assert first_entry["message"] ==
2633 "@#{admin.nickname} unfollowed relay: https://example.org/relay"
2635 conn2 = get(conn, "/api/pleroma/admin/moderation_log?page_size=1&page=2")
2637 response2 = json_response(conn2, 200)
2638 [second_entry] = response2["items"]
2640 assert response2["total"] == 2
2641 assert response2["items"] |> length() == 1
2642 assert second_entry["data"]["action"] == "relay_follow"
2644 assert second_entry["message"] ==
2645 "@#{admin.nickname} followed relay: https://example.org/relay"
2648 test "filters log by date", %{conn: conn, admin: admin} do
2649 first_date = "2017-08-15T15:47:06Z"
2650 second_date = "2017-08-20T15:47:06Z"
2652 Repo.insert(%ModerationLog{
2656 "nickname" => admin.nickname,
2659 action: "relay_follow",
2660 target: "https://example.org/relay"
2662 inserted_at: NaiveDateTime.from_iso8601!(first_date)
2665 Repo.insert(%ModerationLog{
2669 "nickname" => admin.nickname,
2672 action: "relay_unfollow",
2673 target: "https://example.org/relay"
2675 inserted_at: NaiveDateTime.from_iso8601!(second_date)
2681 "/api/pleroma/admin/moderation_log?start_date=#{second_date}"
2684 response1 = json_response(conn1, 200)
2685 [first_entry] = response1["items"]
2687 assert response1["total"] == 1
2688 assert first_entry["data"]["action"] == "relay_unfollow"
2690 assert first_entry["message"] ==
2691 "@#{admin.nickname} unfollowed relay: https://example.org/relay"
2694 test "returns log filtered by user", %{conn: conn, admin: admin, moderator: moderator} do
2695 Repo.insert(%ModerationLog{
2699 "nickname" => admin.nickname,
2702 action: "relay_follow",
2703 target: "https://example.org/relay"
2707 Repo.insert(%ModerationLog{
2710 "id" => moderator.id,
2711 "nickname" => moderator.nickname,
2714 action: "relay_unfollow",
2715 target: "https://example.org/relay"
2719 conn1 = get(conn, "/api/pleroma/admin/moderation_log?user_id=#{moderator.id}")
2721 response1 = json_response(conn1, 200)
2722 [first_entry] = response1["items"]
2724 assert response1["total"] == 1
2725 assert get_in(first_entry, ["data", "actor", "id"]) == moderator.id
2728 test "returns log filtered by search", %{conn: conn, moderator: moderator} do
2729 ModerationLog.insert_log(%{
2731 action: "relay_follow",
2732 target: "https://example.org/relay"
2735 ModerationLog.insert_log(%{
2737 action: "relay_unfollow",
2738 target: "https://example.org/relay"
2741 conn1 = get(conn, "/api/pleroma/admin/moderation_log?search=unfo")
2743 response1 = json_response(conn1, 200)
2744 [first_entry] = response1["items"]
2746 assert response1["total"] == 1
2748 assert get_in(first_entry, ["data", "message"]) ==
2749 "@#{moderator.nickname} unfollowed relay: https://example.org/relay"
2753 describe "PATCH /users/:nickname/force_password_reset" do
2754 test "sets password_reset_pending to true", %{conn: conn} do
2755 user = insert(:user)
2756 assert user.password_reset_pending == false
2759 patch(conn, "/api/pleroma/admin/users/force_password_reset", %{nicknames: [user.nickname]})
2761 assert json_response(conn, 204) == ""
2763 ObanHelpers.perform_all()
2765 assert User.get_by_id(user.id).password_reset_pending == true
2769 describe "relays" do
2770 test "POST /relay", %{conn: conn, admin: admin} do
2772 post(conn, "/api/pleroma/admin/relay", %{
2773 relay_url: "http://mastodon.example.org/users/admin"
2776 assert json_response(conn, 200) == "http://mastodon.example.org/users/admin"
2778 log_entry = Repo.one(ModerationLog)
2780 assert ModerationLog.get_log_entry_message(log_entry) ==
2781 "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
2784 test "GET /relay", %{conn: conn} do
2785 relay_user = Pleroma.Web.ActivityPub.Relay.get_actor()
2787 ["http://mastodon.example.org/users/admin", "https://mstdn.io/users/mayuutann"]
2788 |> Enum.each(fn ap_id ->
2789 {:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
2790 User.follow(relay_user, user)
2793 conn = get(conn, "/api/pleroma/admin/relay")
2795 assert json_response(conn, 200)["relays"] -- ["mastodon.example.org", "mstdn.io"] == []
2798 test "DELETE /relay", %{conn: conn, admin: admin} do
2799 post(conn, "/api/pleroma/admin/relay", %{
2800 relay_url: "http://mastodon.example.org/users/admin"
2804 delete(conn, "/api/pleroma/admin/relay", %{
2805 relay_url: "http://mastodon.example.org/users/admin"
2808 assert json_response(conn, 200) == "http://mastodon.example.org/users/admin"
2810 [log_entry_one, log_entry_two] = Repo.all(ModerationLog)
2812 assert ModerationLog.get_log_entry_message(log_entry_one) ==
2813 "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
2815 assert ModerationLog.get_log_entry_message(log_entry_two) ==
2816 "@#{admin.nickname} unfollowed relay: http://mastodon.example.org/users/admin"
2820 describe "instances" do
2821 test "GET /instances/:instance/statuses", %{conn: conn} do
2822 user = insert(:user, local: false, nickname: "archaeme@archae.me")
2823 user2 = insert(:user, local: false, nickname: "test@test.com")
2824 insert_pair(:note_activity, user: user)
2825 insert(:note_activity, user: user2)
2827 ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses")
2829 response = json_response(ret_conn, 200)
2831 assert length(response) == 2
2833 ret_conn = get(conn, "/api/pleroma/admin/instances/test.com/statuses")
2835 response = json_response(ret_conn, 200)
2837 assert length(response) == 1
2839 ret_conn = get(conn, "/api/pleroma/admin/instances/nonexistent.com/statuses")
2841 response = json_response(ret_conn, 200)
2843 assert length(response) == 0
2847 describe "PATCH /confirm_email" do
2848 test "it confirms emails of two users", %{conn: conn, admin: admin} do
2849 [first_user, second_user] = insert_pair(:user, confirmation_pending: true)
2851 assert first_user.confirmation_pending == true
2852 assert second_user.confirmation_pending == true
2855 patch(conn, "/api/pleroma/admin/users/confirm_email", %{
2857 first_user.nickname,
2858 second_user.nickname
2862 assert ret_conn.status == 200
2864 assert first_user.confirmation_pending == true
2865 assert second_user.confirmation_pending == true
2867 log_entry = Repo.one(ModerationLog)
2869 assert ModerationLog.get_log_entry_message(log_entry) ==
2870 "@#{admin.nickname} confirmed email for users: @#{first_user.nickname}, @#{
2871 second_user.nickname
2876 describe "PATCH /resend_confirmation_email" do
2877 test "it resend emails for two users", %{conn: conn, admin: admin} do
2878 [first_user, second_user] = insert_pair(:user, confirmation_pending: true)
2881 patch(conn, "/api/pleroma/admin/users/resend_confirmation_email", %{
2883 first_user.nickname,
2884 second_user.nickname
2888 assert ret_conn.status == 200
2890 log_entry = Repo.one(ModerationLog)
2892 assert ModerationLog.get_log_entry_message(log_entry) ==
2893 "@#{admin.nickname} re-sent confirmation email for users: @#{first_user.nickname}, @#{
2894 second_user.nickname
2899 describe "POST /reports/:id/notes" do
2900 setup %{conn: conn, admin: admin} do
2901 [reporter, target_user] = insert_pair(:user)
2902 activity = insert(:note_activity, user: target_user)
2904 {:ok, %{id: report_id}} =
2905 CommonAPI.report(reporter, %{
2906 "account_id" => target_user.id,
2907 "comment" => "I feel offended",
2908 "status_ids" => [activity.id]
2911 post(conn, "/api/pleroma/admin/reports/#{report_id}/notes", %{
2912 content: "this is disgusting!"
2915 post(conn, "/api/pleroma/admin/reports/#{report_id}/notes", %{
2916 content: "this is disgusting2!"
2921 report_id: report_id
2925 test "it creates report note", %{admin_id: admin_id, report_id: report_id} do
2926 [note, _] = Repo.all(ReportNote)
2929 activity_id: ^report_id,
2930 content: "this is disgusting!",
2935 test "it returns reports with notes", %{conn: conn, admin: admin} do
2936 conn = get(conn, "/api/pleroma/admin/reports")
2938 response = json_response(conn, 200)
2939 notes = hd(response["reports"])["notes"]
2942 assert note["user"]["nickname"] == admin.nickname
2943 assert note["content"] == "this is disgusting!"
2944 assert note["created_at"]
2945 assert response["total"] == 1
2948 test "it deletes the note", %{conn: conn, report_id: report_id} do
2949 assert ReportNote |> Repo.all() |> length() == 2
2951 [note, _] = Repo.all(ReportNote)
2953 delete(conn, "/api/pleroma/admin/reports/#{report_id}/notes/#{note.id}")
2955 assert ReportNote |> Repo.all() |> length() == 1
2960 # Needed for testing
2961 defmodule Pleroma.Web.Endpoint.NotReal do
2964 defmodule Pleroma.Captcha.NotReal do