Merge remote-tracking branch 'remotes/origin/develop' into 1505-threads-federation
[akkoma] / test / web / admin_api / admin_api_controller_test.exs
index f4cdaebf9411cd3c0b5520e449ea205474da2e05..60db581445586f908971ee28fcb5e945f6390b43 100644 (file)
@@ -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,26 +1889,19 @@ 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) ==
                "To use this endpoint you need to enable configuration from database."
     end
 
-    test "without any settings in db", %{conn: conn} do
-      conn = get(conn, "/api/pleroma/admin/config")
-
-      assert json_response(conn, 400) ==
-               "To use configuration from database migrate your settings to database."
-    end
-
     test "with settings only in db", %{conn: conn} do
       config1 = insert(:config)
       config2 = insert(:config)
@@ -2043,16 +2039,19 @@ 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)
-        :ok = File.rm("config/test.exported_from_db.secret.exs")
+        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
     test "create new config setting in db", %{conn: conn} do
+      ueberauth = Application.get_env(:ueberauth, Ueberauth)
+      on_exit(fn -> Application.put_env(:ueberauth, Ueberauth, ueberauth) end)
+
       conn =
         post(conn, "/api/pleroma/admin/config", %{
           configs: [
@@ -2167,7 +2166,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []}
     end
 
-    test "save config setting without key", %{conn: conn} do
+    test "save configs setting without explicit key", %{conn: conn} do
       level = Application.get_env(:quack, :level)
       meta = Application.get_env(:quack, :meta)
       webhook_url = Application.get_env(:quack, :webhook_url)
@@ -2253,6 +2252,106 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
              }
     end
 
+    test "saving config which need pleroma 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"],
+                   "group" => ":pleroma",
+                   "key" => ":chat",
+                   "value" => [%{"tuple" => [":enabled", true]}]
+                 }
+               ],
+               "need_reboot" => true
+             }
+
+      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"],
+                   "group" => ":pleroma",
+                   "key" => ":chat",
+                   "value" => [%{"tuple" => [":enabled", true]}]
+                 }
+               ],
+               "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
       config =
         insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: [k1: 1, k2: 2]))
@@ -2387,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..."
@@ -2420,25 +2519,26 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
              }
     end
 
-    test "update config setting & delete", %{conn: conn} do
+    test "update config setting & delete with fallback to default value", %{
+      conn: conn,
+      admin: admin,
+      token: token
+    } do
+      ueberauth = Application.get_env(:ueberauth, Ueberauth)
       config1 = insert(:config, key: ":keyaa1")
       config2 = insert(:config, key: ":keyaa2")
 
-      insert(:config,
-        group: "ueberauth",
-        key: "Ueberauth.Strategy.Microsoft.OAuth"
-      )
+      config3 =
+        insert(:config,
+          group: ":ueberauth",
+          key: "Ueberauth"
+        )
 
       conn =
         post(conn, "/api/pleroma/admin/config", %{
           configs: [
             %{group: config1.group, key: config1.key, value: "another_value"},
-            %{group: config2.group, key: config2.key, delete: true},
-            %{
-              group: "ueberauth",
-              key: "Ueberauth.Strategy.Microsoft.OAuth",
-              delete: true
-            }
+            %{group: config2.group, key: config2.key, value: "another_value"}
           ]
         })
 
@@ -2449,12 +2549,41 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "key" => config1.key,
                    "value" => "another_value",
                    "db" => [":keyaa1"]
+                 },
+                 %{
+                   "group" => ":pleroma",
+                   "key" => config2.key,
+                   "value" => "another_value",
+                   "db" => [":keyaa2"]
                  }
                ]
              }
 
       assert Application.get_env(:pleroma, :keyaa1) == "another_value"
-      refute Application.get_env(:pleroma, :keyaa2)
+      assert Application.get_env(:pleroma, :keyaa2) == "another_value"
+      assert Application.get_env(:ueberauth, Ueberauth) == ConfigDB.from_binary(config3.value)
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> assign(:token, token)
+        |> post("/api/pleroma/admin/config", %{
+          configs: [
+            %{group: config2.group, key: config2.key, delete: true},
+            %{
+              group: ":ueberauth",
+              key: "Ueberauth",
+              delete: true
+            }
+          ]
+        })
+
+      assert json_response(conn, 200) == %{
+               "configs" => []
+             }
+
+      assert Application.get_env(:ueberauth, Ueberauth) == ueberauth
+      refute Keyword.has_key?(Application.get_all_env(:pleroma), :keyaa2)
     end
 
     test "common config example", %{conn: conn} do
@@ -2490,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" => [
@@ -2924,47 +3053,17 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
     end
   end
 
-  describe "config mix tasks run" do
-    setup do
-      Mix.shell(Mix.Shell.Quiet)
-
-      on_exit(fn ->
-        Mix.shell(Mix.Shell.IO)
-      end)
-
-      :ok
-    end
-
+  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
 
-    clear_config([:feed, :post_title]) do
-      Pleroma.Config.put([:feed, :post_title], %{max_length: 100, omission: "…"})
-    end
-
-    test "transfer settings to DB and to file", %{conn: conn} do
-      assert Repo.all(Pleroma.ConfigDB) == []
-      Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
-      assert Repo.aggregate(Pleroma.ConfigDB, :count, :id) > 0
-
-      conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
-
-      assert json_response(conn, 200) == %{}
-      assert Repo.all(Pleroma.ConfigDB) == []
-    end
-
-    test "returns error if configuration from database is off", %{conn: conn} do
-      initial = Pleroma.Config.get(:configurable_from_database)
-      on_exit(fn -> Pleroma.Config.put(:configurable_from_database, initial) end)
-      Pleroma.Config.put(:configurable_from_database, false)
-
-      conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
-
-      assert json_response(conn, 400) ==
-               "To use this endpoint you need to enable configuration from database."
+    test "pleroma restarts", %{conn: conn} do
+      capture_log(fn ->
+        assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{}
+      end) =~ "pleroma restarted"
 
-      assert Repo.all(Pleroma.ConfigDB) == []
+      refute Restarter.Pleroma.need_reboot?()
     end
   end