|> render("index.json", %{log: log})
end
- def migrate_to_db(conn, _params) do
- Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
- json(conn, %{})
- end
-
- def migrate_from_db(conn, _params) do
- Mix.Tasks.Pleroma.Config.run([
- "migrate_from_db",
- "--env",
- to_string(Pleroma.Config.get(:env)),
- "-d"
- ])
-
- json(conn, %{})
- end
-
def config_descriptions(conn, _params) do
conn
|> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.send_resp(200, @descriptions_json)
end
- def config_show(conn, _params) do
- configs = Pleroma.Repo.all(Config)
+ def migrate_from_db(conn, _params) do
+ with :ok <- check_dynamic_configuration(conn) do
+ Mix.Tasks.Pleroma.Config.run([
+ "migrate_from_db",
+ "--env",
+ to_string(Pleroma.Config.get(:env)),
+ "-d"
+ ])
- conn
- |> put_view(ConfigView)
- |> render("index.json", %{configs: configs})
+ json(conn, %{})
+ end
end
- def config_update(conn, %{"configs" => configs}) do
- updated =
- if Pleroma.Config.get([:instance, :dynamic_configuration]) do
- updated =
- Enum.map(configs, fn
- %{"group" => group, "key" => key, "delete" => "true"} = params ->
- with {:ok, config} <-
- Config.delete(%{group: group, key: key, subkeys: params["subkeys"]}) do
- config
- end
-
- %{"group" => group, "key" => key, "value" => value} ->
- with {:ok, config} <-
- Config.update_or_create(%{group: group, key: key, value: value}) do
- config
- end
- end)
- |> Enum.reject(&is_nil(&1))
-
- Pleroma.Config.TransferTask.load_and_update_env()
-
- Mix.Tasks.Pleroma.Config.run([
- "migrate_from_db",
- "--env",
- to_string(Pleroma.Config.get(:env))
- ])
+ def config_show(conn, _params) do
+ with :ok <- check_dynamic_configuration(conn) do
+ configs = Pleroma.Repo.all(Config)
- updated
+ if configs == [] do
+ errors(conn, {:error, "To use dynamic configuration migrate your settings to database."})
else
- []
+ conn
+ |> put_view(ConfigView)
+ |> render("index.json", %{configs: configs})
end
+ end
+ end
- conn
- |> put_view(ConfigView)
- |> render("index.json", %{configs: updated})
+ def config_update(conn, %{"configs" => configs}) do
+ with :ok <- check_dynamic_configuration(conn) do
+ updated =
+ Enum.map(configs, fn
+ %{"group" => group, "key" => key, "delete" => "true"} = params ->
+ with {:ok, config} <-
+ Config.delete(%{group: group, key: key, subkeys: params["subkeys"]}) do
+ config
+ end
+
+ %{"group" => group, "key" => key, "value" => value} ->
+ with {:ok, config} <-
+ Config.update_or_create(%{group: group, key: key, value: value}) do
+ config
+ end
+ end)
+ |> Enum.reject(&is_nil(&1))
+
+ Pleroma.Config.TransferTask.load_and_update_env()
+
+ Mix.Tasks.Pleroma.Config.run([
+ "migrate_from_db",
+ "--env",
+ to_string(Pleroma.Config.get(:env))
+ ])
+
+ conn
+ |> put_view(ConfigView)
+ |> render("index.json", %{configs: updated})
+ end
+ end
+
+ defp check_dynamic_configuration(conn) do
+ if Pleroma.Config.get([:instance, :dynamic_configuration]) do
+ :ok
+ else
+ errors(conn, {:error, "To use this endpoint you need to enable dynamic configuration."})
+ end
end
def reload_emoji(conn, _params) do
end
describe "GET /api/pleroma/admin/config" do
+ clear_config([:instance, :dynamic_configuration]) do
+ Pleroma.Config.put([:instance, :dynamic_configuration], true)
+ end
+
setup %{conn: conn} do
admin = insert(:user, is_admin: true)
%{conn: assign(conn, :user, admin)}
end
+ test "when dynamic configuration is off", %{conn: conn} do
+ initial = Pleroma.Config.get([:instance, :dynamic_configuration])
+ Pleroma.Config.put([:instance, :dynamic_configuration], false)
+ on_exit(fn -> Pleroma.Config.put([:instance, :dynamic_configuration], initial) end)
+ conn = get(conn, "/api/pleroma/admin/config")
+
+ assert json_response(conn, 400) ==
+ "To use this endpoint you need to enable dynamic configuration."
+ end
+
test "without any settings in db", %{conn: conn} do
conn = get(conn, "/api/pleroma/admin/config")
- assert json_response(conn, 200) == %{"configs" => []}
+ assert json_response(conn, 400) ==
+ "To use dynamic configuration migrate your settings to database."
end
test "with settings in db", %{conn: conn} do
end
end
+ test "POST /api/pleroma/admin/config error" do
+ admin = insert(:user, is_admin: true)
+
+ conn =
+ build_conn()
+ |> assign(:user, admin)
+ |> post("/api/pleroma/admin/config", %{"configs" => []})
+
+ assert json_response(conn, 400) ==
+ "To use this endpoint you need to enable dynamic configuration."
+ end
+
describe "POST /api/pleroma/admin/config" do
setup %{conn: conn} do
admin = insert(:user, is_admin: true)
end
test "save config setting without key", %{conn: conn} do
- initial = Application.get_all_env(:quack)
- on_exit(fn -> Application.put_all_env([{:quack, initial}]) end)
+ level = Application.get_env(:quack, :level)
+ meta = Application.get_env(:quack, :meta)
+ webhook_url = Application.get_env(:quack, :webhook_url)
+
+ on_exit(fn ->
+ Application.put_env(:quack, :level, level)
+ Application.put_env(:quack, :meta, meta)
+ Application.put_env(:quack, :webhook_url, webhook_url)
+ end)
conn =
post(conn, "/api/pleroma/admin/config", %{
setup %{conn: conn} do
admin = insert(:user, is_admin: true)
- temp_file = "config/test.exported_from_db.secret.exs"
-
Mix.shell(Mix.Shell.Quiet)
on_exit(fn ->
Mix.shell(Mix.Shell.IO)
- :ok = File.rm(temp_file)
end)
- %{conn: assign(conn, :user, admin), admin: admin}
+ %{conn: assign(conn, :user, admin)}
end
clear_config([:instance, :dynamic_configuration]) do
Pleroma.Config.put([:feed, :post_title], %{max_length: 100, omission: "…"})
end
- test "transfer settings to DB and to file", %{conn: conn, admin: admin} do
+ test "transfer settings to DB and to file", %{conn: conn} do
+ on_exit(fn -> :ok = File.rm("config/test.exported_from_db.secret.exs") end)
assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == []
- conn = get(conn, "/api/pleroma/admin/config/migrate_to_db")
- assert json_response(conn, 200) == %{}
+ Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) > 0
- conn =
- build_conn()
- |> assign(:user, admin)
- |> get("/api/pleroma/admin/config/migrate_from_db")
+ conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
assert json_response(conn, 200) == %{}
assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == []
end
+
+ test "returns error if dynamic configuration is off", %{conn: conn} do
+ initial = Pleroma.Config.get([:instance, :dynamic_configuration])
+ on_exit(fn -> Pleroma.Config.put([:instance, :dynamic_configuration], initial) end)
+ Pleroma.Config.put([:instance, :dynamic_configuration], false)
+
+ conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
+
+ assert json_response(conn, 400) ==
+ "To use this endpoint you need to enable dynamic configuration."
+ end
end
describe "GET /api/pleroma/admin/users/:nickname/statuses" do