errors on endpoints

This commit is contained in:
Alexander 2019-12-06 17:50:53 +03:00 committed by Alexander Strizhakov
parent 5cacb988b9
commit fea734ca70
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
4 changed files with 118 additions and 80 deletions

View File

@ -665,19 +665,6 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
- 404 Not Found `"Not found"` - 404 Not Found `"Not found"`
- On success: 200 OK `{}` - On success: 200 OK `{}`
## `GET /api/pleroma/admin/config/migrate_to_db`
### Run mix task pleroma.config migrate_to_db
Copies `pleroma` environment settings to the database.
- Params: none
- Response:
```json
{}
```
## `GET /api/pleroma/admin/config/migrate_from_db` ## `GET /api/pleroma/admin/config/migrate_from_db`
### Run mix task pleroma.config migrate_from_db ### Run mix task pleroma.config migrate_from_db
@ -686,6 +673,8 @@ Copies all settings from database to `config/{env}.exported_from_db.secret.exs`
- Params: none - Params: none
- Response: - Response:
- On failure:
- 400 Bad Request `"To use this endpoint you need to enable dynamic configuration."`
```json ```json
{} {}
@ -699,6 +688,9 @@ Copies all settings from database to `config/{env}.exported_from_db.secret.exs`
- Params: none - Params: none
- Response: - Response:
- On failure:
- 400 Bad Request `"To use this endpoint you need to enable dynamic configuration."`
- 400 Bad Request `"To use dynamic configuration migrate your settings to database."`
```json ```json
{ {
@ -831,7 +823,8 @@ config :quack,
``` ```
- Response: - Response:
- On failure:
- 400 Bad Request `"To use this endpoint you need to enable dynamic configuration."`
```json ```json
{ {
configs: [ configs: [

View File

@ -778,71 +778,77 @@ def list_log(conn, params) do
|> render("index.json", %{log: log}) |> render("index.json", %{log: log})
end 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 def config_descriptions(conn, _params) do
conn conn
|> Plug.Conn.put_resp_content_type("application/json") |> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.send_resp(200, @descriptions_json) |> Plug.Conn.send_resp(200, @descriptions_json)
end end
def config_show(conn, _params) do def migrate_from_db(conn, _params) do
configs = Pleroma.Repo.all(Config) with :ok <- check_dynamic_configuration(conn) do
Mix.Tasks.Pleroma.Config.run([
"migrate_from_db",
"--env",
to_string(Pleroma.Config.get(:env)),
"-d"
])
conn json(conn, %{})
|> put_view(ConfigView) end
|> render("index.json", %{configs: configs}) end
def config_show(conn, _params) do
with :ok <- check_dynamic_configuration(conn) do
configs = Pleroma.Repo.all(Config)
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 end
def config_update(conn, %{"configs" => configs}) do def config_update(conn, %{"configs" => configs}) do
updated = with :ok <- check_dynamic_configuration(conn) do
if Pleroma.Config.get([:instance, :dynamic_configuration]) do updated =
updated = Enum.map(configs, fn
Enum.map(configs, fn %{"group" => group, "key" => key, "delete" => "true"} = params ->
%{"group" => group, "key" => key, "delete" => "true"} = params -> with {:ok, config} <-
with {:ok, config} <- Config.delete(%{group: group, key: key, subkeys: params["subkeys"]}) do
Config.delete(%{group: group, key: key, subkeys: params["subkeys"]}) do config
config end
end
%{"group" => group, "key" => key, "value" => value} -> %{"group" => group, "key" => key, "value" => value} ->
with {:ok, config} <- with {:ok, config} <-
Config.update_or_create(%{group: group, key: key, value: value}) do Config.update_or_create(%{group: group, key: key, value: value}) do
config config
end end
end) end)
|> Enum.reject(&is_nil(&1)) |> Enum.reject(&is_nil(&1))
Pleroma.Config.TransferTask.load_and_update_env() Pleroma.Config.TransferTask.load_and_update_env()
Mix.Tasks.Pleroma.Config.run([ Mix.Tasks.Pleroma.Config.run([
"migrate_from_db", "migrate_from_db",
"--env", "--env",
to_string(Pleroma.Config.get(:env)) to_string(Pleroma.Config.get(:env))
]) ])
updated conn
else |> put_view(ConfigView)
[] |> render("index.json", %{configs: updated})
end end
end
conn defp check_dynamic_configuration(conn) do
|> put_view(ConfigView) if Pleroma.Config.get([:instance, :dynamic_configuration]) do
|> render("index.json", %{configs: updated}) :ok
else
errors(conn, {:error, "To use this endpoint you need to enable dynamic configuration."})
end
end end
def reload_emoji(conn, _params) do def reload_emoji(conn, _params) do

View File

@ -24,8 +24,8 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
end end
test "settings are migrated to db" do test "settings are migrated to db" do
initial = Application.get_all_env(:quack) initial = Application.get_env(:quack, :level)
on_exit(fn -> Application.put_all_env([{:quack, initial}]) end) on_exit(fn -> Application.put_env(:quack, :level, initial) end)
assert Repo.all(Config) == [] assert Repo.all(Config) == []
Application.put_env(:pleroma, :first_setting, key: "value", key2: [Repo]) Application.put_env(:pleroma, :first_setting, key: "value", key2: [Repo])

View File

@ -1929,16 +1929,31 @@ test "returns error when status is not exist", %{conn: conn} do
end end
describe "GET /api/pleroma/admin/config" do 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 setup %{conn: conn} do
admin = insert(:user, is_admin: true) admin = insert(:user, is_admin: true)
%{conn: assign(conn, :user, admin)} %{conn: assign(conn, :user, admin)}
end 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 test "without any settings in db", %{conn: conn} do
conn = get(conn, "/api/pleroma/admin/config") 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 end
test "with settings in db", %{conn: conn} do test "with settings in db", %{conn: conn} do
@ -1966,6 +1981,18 @@ test "with settings in db", %{conn: conn} do
end end
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 describe "POST /api/pleroma/admin/config" do
setup %{conn: conn} do setup %{conn: conn} do
admin = insert(:user, is_admin: true) admin = insert(:user, is_admin: true)
@ -2101,8 +2128,15 @@ test "create new config setting in db", %{conn: conn} do
end end
test "save config setting without key", %{conn: conn} do test "save config setting without key", %{conn: conn} do
initial = Application.get_all_env(:quack) level = Application.get_env(:quack, :level)
on_exit(fn -> Application.put_all_env([{:quack, initial}]) end) 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 = conn =
post(conn, "/api/pleroma/admin/config", %{ post(conn, "/api/pleroma/admin/config", %{
@ -2640,16 +2674,13 @@ test "delete part of settings by atom subkeys", %{conn: conn} do
setup %{conn: conn} do setup %{conn: conn} do
admin = insert(:user, is_admin: true) admin = insert(:user, is_admin: true)
temp_file = "config/test.exported_from_db.secret.exs"
Mix.shell(Mix.Shell.Quiet) Mix.shell(Mix.Shell.Quiet)
on_exit(fn -> on_exit(fn ->
Mix.shell(Mix.Shell.IO) Mix.shell(Mix.Shell.IO)
:ok = File.rm(temp_file)
end) end)
%{conn: assign(conn, :user, admin), admin: admin} %{conn: assign(conn, :user, admin)}
end end
clear_config([:instance, :dynamic_configuration]) do clear_config([:instance, :dynamic_configuration]) do
@ -2660,20 +2691,28 @@ test "delete part of settings by atom subkeys", %{conn: conn} do
Pleroma.Config.put([:feed, :post_title], %{max_length: 100, omission: ""}) Pleroma.Config.put([:feed, :post_title], %{max_length: 100, omission: ""})
end 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) == [] assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == []
conn = get(conn, "/api/pleroma/admin/config/migrate_to_db") Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
assert json_response(conn, 200) == %{}
assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) > 0 assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) > 0
conn = conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
build_conn()
|> assign(:user, admin)
|> get("/api/pleroma/admin/config/migrate_from_db")
assert json_response(conn, 200) == %{} assert json_response(conn, 200) == %{}
assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == [] assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == []
end 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 end
describe "GET /api/pleroma/admin/users/:nickname/statuses" do describe "GET /api/pleroma/admin/users/:nickname/statuses" do