X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fweb%2Fadmin_api%2Fadmin_api_controller_test.exs;h=60db581445586f908971ee28fcb5e945f6390b43;hb=61d9f43e463a3b3b0c1e4b9c98c22e222797bd82;hp=5fbdf96f6072af4238472ea128fb98e88a6d5bc3;hpb=c85aa6e87f8887ad532580ec3f84811abace05f2;p=akkoma diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 5fbdf96f6..60db58144 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -6,7 +6,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do use Pleroma.Web.ConnCase use Oban.Testing, repo: Pleroma.Repo + import Pleroma.Factory + import ExUnit.CaptureLog + alias Pleroma.Activity + alias Pleroma.Config alias Pleroma.ConfigDB alias Pleroma.HTML alias Pleroma.ModerationLog @@ -19,7 +23,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do alias Pleroma.Web.CommonAPI alias Pleroma.Web.MastodonAPI.StatusView alias Pleroma.Web.MediaProxy - import Pleroma.Factory setup_all do Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) @@ -41,7 +44,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do describe "with [:auth, :enforce_oauth_admin_scope_usage]," do clear_config([:auth, :enforce_oauth_admin_scope_usage]) do - Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], true) + Config.put([:auth, :enforce_oauth_admin_scope_usage], true) end test "GET /api/pleroma/admin/users/:nickname requires admin:read:accounts or broader scope", @@ -91,7 +94,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do describe "unless [:auth, :enforce_oauth_admin_scope_usage]," do clear_config([:auth, :enforce_oauth_admin_scope_usage]) do - Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], false) + Config.put([:auth, :enforce_oauth_admin_scope_usage], false) end test "GET /api/pleroma/admin/users/:nickname requires " <> @@ -579,11 +582,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do describe "POST /api/pleroma/admin/email_invite, with valid config" do clear_config([:instance, :registrations_open]) do - Pleroma.Config.put([:instance, :registrations_open], false) + Config.put([:instance, :registrations_open], false) end clear_config([:instance, :invites_enabled]) do - Pleroma.Config.put([:instance, :invites_enabled], true) + Config.put([:instance, :invites_enabled], true) end test "sends invitation and returns 204", %{admin: admin, conn: conn} do @@ -602,8 +605,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do assert token_record refute token_record.used - notify_email = Pleroma.Config.get([:instance, :notify_email]) - instance_name = Pleroma.Config.get([:instance, :name]) + notify_email = Config.get([:instance, :notify_email]) + instance_name = Config.get([:instance, :name]) email = Pleroma.Emails.UserEmail.user_invitation_email( @@ -639,8 +642,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do clear_config([:instance, :invites_enabled]) test "it returns 500 if `invites_enabled` is not enabled", %{conn: conn} do - Pleroma.Config.put([:instance, :registrations_open], false) - Pleroma.Config.put([:instance, :invites_enabled], false) + Config.put([:instance, :registrations_open], false) + Config.put([:instance, :invites_enabled], false) conn = post(conn, "/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD") @@ -648,8 +651,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end test "it returns 500 if `registrations_open` is enabled", %{conn: conn} do - Pleroma.Config.put([:instance, :registrations_open], true) - Pleroma.Config.put([:instance, :invites_enabled], true) + Config.put([:instance, :registrations_open], true) + Config.put([:instance, :invites_enabled], true) conn = post(conn, "/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD") @@ -1886,13 +1889,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do describe "GET /api/pleroma/admin/config" do clear_config(:configurable_from_database) do - Pleroma.Config.put(:configurable_from_database, true) + Config.put(:configurable_from_database, true) end test "when configuration from database is off", %{conn: conn} do - initial = Pleroma.Config.get(:configurable_from_database) - Pleroma.Config.put(:configurable_from_database, false) - on_exit(fn -> Pleroma.Config.put(:configurable_from_database, initial) end) + initial = Config.get(:configurable_from_database) + Config.put(:configurable_from_database, false) + on_exit(fn -> Config.put(:configurable_from_database, initial) end) conn = get(conn, "/api/pleroma/admin/config") assert json_response(conn, 400) == @@ -2036,11 +2039,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do Application.delete_env(:pleroma, Pleroma.Captcha.NotReal) Application.put_env(:pleroma, :http, http) Application.put_env(:tesla, :adapter, Tesla.Mock) + Restarter.Pleroma.refresh() end) end clear_config(:configurable_from_database) do - Pleroma.Config.put(:configurable_from_database, true) + Config.put(:configurable_from_database, true) end @tag capture_log: true @@ -2249,21 +2253,63 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end test "saving config which need pleroma reboot", %{conn: conn} do - chat = Pleroma.Config.get(:chat) - on_exit(fn -> Pleroma.Config.put(:chat, chat) end) + chat = Config.get(:chat) + on_exit(fn -> Config.put(:chat, chat) end) - conn = - post( - conn, - "/api/pleroma/admin/config", - %{ - configs: [ - %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]} - ] - } - ) + assert post( + conn, + "/api/pleroma/admin/config", + %{ + configs: [ + %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]} + ] + } + ) + |> json_response(200) == %{ + "configs" => [ + %{ + "db" => [":enabled"], + "group" => ":pleroma", + "key" => ":chat", + "value" => [%{"tuple" => [":enabled", true]}] + } + ], + "need_reboot" => true + } - assert json_response(conn, 200) == %{ + configs = + conn + |> get("/api/pleroma/admin/config") + |> json_response(200) + + assert configs["need_reboot"] + + capture_log(fn -> + assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{} + end) =~ "pleroma restarted" + + configs = + conn + |> get("/api/pleroma/admin/config") + |> json_response(200) + + refute Map.has_key?(configs, "need_reboot") + end + + test "update setting which need reboot, don't change reboot flag until reboot", %{conn: conn} do + chat = Config.get(:chat) + on_exit(fn -> Config.put(:chat, chat) end) + + assert post( + conn, + "/api/pleroma/admin/config", + %{ + configs: [ + %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]} + ] + } + ) + |> json_response(200) == %{ "configs" => [ %{ "db" => [":enabled"], @@ -2274,6 +2320,36 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do ], "need_reboot" => true } + + assert post(conn, "/api/pleroma/admin/config", %{ + configs: [ + %{group: ":pleroma", key: ":key1", value: [%{"tuple" => [":key3", 3]}]} + ] + }) + |> json_response(200) == %{ + "configs" => [ + %{ + "group" => ":pleroma", + "key" => ":key1", + "value" => [ + %{"tuple" => [":key3", 3]} + ], + "db" => [":key3"] + } + ], + "need_reboot" => true + } + + capture_log(fn -> + assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{} + end) =~ "pleroma restarted" + + configs = + conn + |> get("/api/pleroma/admin/config") + |> json_response(200) + + refute Map.has_key?(configs, "need_reboot") end test "saving config with nested merge", %{conn: conn} do @@ -2410,7 +2486,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do {ExSyslogger, :ex_syslogger} ] - ExUnit.CaptureLog.capture_log(fn -> + capture_log(fn -> require Logger Logger.warn("Ooops...") end) =~ "Ooops..." @@ -2543,7 +2619,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do }) assert Application.get_env(:tesla, :adapter) == Tesla.Adapter.Httpc - assert Pleroma.Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma" + assert Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma" assert json_response(conn, 200) == %{ "configs" => [ @@ -2979,13 +3055,15 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do describe "GET /api/pleroma/admin/restart" do clear_config(:configurable_from_database) do - Pleroma.Config.put(:configurable_from_database, true) + Config.put(:configurable_from_database, true) end test "pleroma restarts", %{conn: conn} do - ExUnit.CaptureLog.capture_log(fn -> + capture_log(fn -> assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{} end) =~ "pleroma restarted" + + refute Restarter.Pleroma.need_reboot?() end end