Pleroma.Web.AdminAPI.Config -> Pleroma.ConfigDB
This commit is contained in:
Alexander Strizhakov 2020-01-16 08:50:27 +03:00
parent 59ba5c80b9
commit 29155137fd
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
10 changed files with 173 additions and 171 deletions

View file

@ -5,8 +5,8 @@
defmodule Mix.Tasks.Pleroma.Config do
use Mix.Task
import Mix.Pleroma
alias Pleroma.ConfigDB
alias Pleroma.Repo
alias Pleroma.Web.AdminAPI.Config
@shortdoc "Manages the location of the config"
@moduledoc File.read!("docs/administration/CLI_tasks/config.md")
@ -55,7 +55,7 @@ defmodule Mix.Tasks.Pleroma.Config do
{:ok, file} <- File.open(config_path, [:write, :utf8]) do
IO.write(file, "use Mix.Config\r\n")
Config
ConfigDB
|> Repo.all()
|> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db]))
@ -81,7 +81,7 @@ defmodule Mix.Tasks.Pleroma.Config do
end)
|> Enum.each(fn {key, value} ->
key = inspect(key)
{:ok, _} = Config.update_or_create(%{group: inspect(group), key: key, value: value})
{:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value})
Mix.shell().info("settings for key #{key} migrated.")
end)
@ -93,7 +93,7 @@ defmodule Mix.Tasks.Pleroma.Config do
IO.write(
file,
"config #{config.group}, #{config.key}, #{
inspect(Config.from_binary(config.value), limit: :infinity)
inspect(ConfigDB.from_binary(config.value), limit: :infinity)
}\r\n\r\n"
)

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.Config do
defmodule Pleroma.ConfigDB do
use Ecto.Schema
import Ecto.Changeset
import Pleroma.Web.Gettext
@ -19,10 +19,10 @@ defmodule Pleroma.Web.AdminAPI.Config do
timestamps()
end
@spec get_by_params(map()) :: Config.t() | nil
def get_by_params(params), do: Repo.get_by(Config, params)
@spec get_by_params(map()) :: ConfigDB.t() | nil
def get_by_params(params), do: Repo.get_by(ConfigDB, params)
@spec changeset(Config.t(), map()) :: Changeset.t()
@spec changeset(ConfigDB.t(), map()) :: Changeset.t()
def changeset(config, params \\ %{}) do
params = Map.put(params, :value, transform(params[:value]))
@ -32,15 +32,15 @@ defmodule Pleroma.Web.AdminAPI.Config do
|> unique_constraint(:key, name: :config_group_key_index)
end
@spec create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
@spec create(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
def create(params) do
%Config{}
%ConfigDB{}
|> changeset(params)
|> Repo.insert()
end
@spec update(Config.t(), map()) :: {:ok, Config} | {:error, Changeset.t()}
def update(%Config{} = config, %{value: value}) do
@spec update(ConfigDB.t(), map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
def update(%ConfigDB{} = config, %{value: value}) do
config
|> changeset(%{value: value})
|> Repo.update()
@ -57,9 +57,9 @@ defmodule Pleroma.Web.AdminAPI.Config do
{:logger, :backends}
]
defp only_full_update?(%Config{} = config) do
config_group = Config.from_string(config.group)
config_key = Config.from_string(config.key)
defp only_full_update?(%ConfigDB{} = config) do
config_group = ConfigDB.from_string(config.group)
config_key = ConfigDB.from_string(config.key)
Enum.any?(@full_key_update, fn
{group, key} when is_list(key) ->
@ -70,39 +70,39 @@ defmodule Pleroma.Web.AdminAPI.Config do
end)
end
defp can_be_partially_updated?(%Config{} = config), do: not only_full_update?(config)
defp can_be_partially_updated?(%ConfigDB{} = config), do: not only_full_update?(config)
@spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
@spec update_or_create(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
def update_or_create(params) do
search_opts = Map.take(params, [:group, :key])
with %Config{} = config <- Config.get_by_params(search_opts),
with %ConfigDB{} = config <- ConfigDB.get_by_params(search_opts),
{:partial_update, true, config} <-
{:partial_update, can_be_partially_updated?(config), config},
old_value <- from_binary(config.value),
transformed_value <- do_transform(params[:value]),
{:can_be_merged, true, config} <- {:can_be_merged, is_list(transformed_value), config},
new_value <- DeepMerge.deep_merge(old_value, transformed_value) do
Config.update(config, %{value: new_value, transformed?: true})
ConfigDB.update(config, %{value: new_value, transformed?: true})
else
{reason, false, config} when reason in [:partial_update, :can_be_merged] ->
Config.update(config, params)
ConfigDB.update(config, params)
nil ->
Config.create(params)
ConfigDB.create(params)
end
end
@spec delete(map()) :: {:ok, Config.t()} | {:error, Changeset.t()} | {:ok, nil}
@spec delete(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()} | {:ok, nil}
def delete(params) do
search_opts = Map.delete(params, :subkeys)
with %Config{} = config <- Config.get_by_params(search_opts),
with %ConfigDB{} = config <- ConfigDB.get_by_params(search_opts),
{config, sub_keys} when is_list(sub_keys) <- {config, params[:subkeys]},
old_value <- from_binary(config.value),
keys <- Enum.map(sub_keys, &do_transform_string(&1)),
new_value <- Keyword.drop(old_value, keys) do
Config.update(config, %{value: new_value})
ConfigDB.update(config, %{value: new_value})
else
{config, nil} ->
Repo.delete(config)

View file

@ -5,10 +5,10 @@
defmodule Pleroma.Config.TransferTask do
use Task
require Logger
alias Pleroma.ConfigDB
alias Pleroma.Repo
alias Pleroma.Web.AdminAPI.Config
require Logger
def start_link(_) do
load_and_update_env()
@ -21,7 +21,7 @@ defmodule Pleroma.Config.TransferTask do
true <- Ecto.Adapters.SQL.table_exists?(Repo, "config"),
started_applications <- Application.started_applications() do
# We need to restart applications for loaded settings take effect
Config
ConfigDB
|> Repo.all()
|> Enum.map(&update_env(&1))
|> Enum.uniq()
@ -33,9 +33,9 @@ defmodule Pleroma.Config.TransferTask do
defp update_env(setting) do
try do
key = Config.from_string(setting.key)
group = Config.from_string(setting.group)
value = Config.from_binary(setting.value)
key = ConfigDB.from_string(setting.key)
group = ConfigDB.from_string(setting.group)
value = ConfigDB.from_binary(setting.value)
if group != :phoenix and key != :serve_endpoints do
:ok = Application.put_env(group, key, value)

View file

@ -8,6 +8,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
alias Pleroma.Activity
alias Pleroma.ConfigDB
alias Pleroma.ModerationLog
alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.ReportNote
@ -17,7 +18,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
alias Pleroma.Web.ActivityPub.Relay
alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.AdminAPI.AccountView
alias Pleroma.Web.AdminAPI.Config
alias Pleroma.Web.AdminAPI.ConfigView
alias Pleroma.Web.AdminAPI.ModerationLogView
alias Pleroma.Web.AdminAPI.Report
@ -799,7 +799,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
def config_show(conn, _params) do
with :ok <- configurable_from_database(conn) do
configs = Pleroma.Repo.all(Config)
configs = Pleroma.Repo.all(ConfigDB)
if configs == [] do
errors(
@ -820,13 +820,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
Enum.map(configs, fn
%{"group" => group, "key" => key, "delete" => true} = params ->
with {:ok, config} <-
Config.delete(%{group: group, key: key, subkeys: params["subkeys"]}) do
ConfigDB.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
ConfigDB.update_or_create(%{group: group, key: key, value: value}) do
config
end
end)

View file

@ -15,7 +15,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigView do
%{
key: config.key,
group: config.group,
value: Pleroma.Web.AdminAPI.Config.from_binary_with_convert(config.value)
value: Pleroma.ConfigDB.from_binary_with_convert(config.value)
}
end
end

View file

@ -2,27 +2,27 @@
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.ConfigTest do
defmodule Pleroma.ConfigDBTest do
use Pleroma.DataCase, async: true
import Pleroma.Factory
alias Pleroma.Web.AdminAPI.Config
alias Pleroma.ConfigDB
test "get_by_key/1" do
config = insert(:config)
insert(:config)
assert config == Config.get_by_params(%{group: config.group, key: config.key})
assert config == ConfigDB.get_by_params(%{group: config.group, key: config.key})
end
test "create/1" do
{:ok, config} = Config.create(%{group: "pleroma", key: "some_key", value: "some_value"})
assert config == Config.get_by_params(%{group: "pleroma", key: "some_key"})
{:ok, config} = ConfigDB.create(%{group: "pleroma", key: "some_key", value: "some_value"})
assert config == ConfigDB.get_by_params(%{group: "pleroma", key: "some_key"})
end
test "update/1" do
config = insert(:config)
{:ok, updated} = Config.update(config, %{value: "some_value"})
loaded = Config.get_by_params(%{group: config.group, key: config.key})
{:ok, updated} = ConfigDB.update(config, %{value: "some_value"})
loaded = ConfigDB.get_by_params(%{group: config.group, key: config.key})
assert loaded == updated
end
@ -36,32 +36,32 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
%{group: config.group, key: config.key, value: "new_value"}
]
assert Repo.all(Config) |> length() == 1
assert Repo.all(ConfigDB) |> length() == 1
Enum.each(params, &Config.update_or_create(&1))
Enum.each(params, &ConfigDB.update_or_create(&1))
assert Repo.all(Config) |> length() == 2
assert Repo.all(ConfigDB) |> length() == 2
config1 = Config.get_by_params(%{group: config.group, key: config.key})
config2 = Config.get_by_params(%{group: "pleroma", key: key2})
config1 = ConfigDB.get_by_params(%{group: config.group, key: config.key})
config2 = ConfigDB.get_by_params(%{group: "pleroma", key: key2})
assert config1.value == Config.transform("new_value")
assert config2.value == Config.transform("another_value")
assert config1.value == ConfigDB.transform("new_value")
assert config2.value == ConfigDB.transform("another_value")
end
test "partial update" do
config = insert(:config, value: Config.to_binary(key1: "val1", key2: :val2))
config = insert(:config, value: ConfigDB.to_binary(key1: "val1", key2: :val2))
{:ok, _config} =
Config.update_or_create(%{
ConfigDB.update_or_create(%{
group: config.group,
key: config.key,
value: [key1: :val1, key3: :val3]
})
updated = Config.get_by_params(%{group: config.group, key: config.key})
updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
value = Config.from_binary(updated.value)
value = ConfigDB.from_binary(updated.value)
assert length(value) == 3
assert value[:key1] == :val1
assert value[:key2] == :val2
@ -69,48 +69,50 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
end
test "deep merge" do
config = insert(:config, value: Config.to_binary(key1: "val1", key2: [k1: :v1, k2: "v2"]))
config = insert(:config, value: ConfigDB.to_binary(key1: "val1", key2: [k1: :v1, k2: "v2"]))
{:ok, config} =
Config.update_or_create(%{
ConfigDB.update_or_create(%{
group: config.group,
key: config.key,
value: [key1: :val1, key2: [k2: :v2, k3: :v3], key3: :val3]
})
updated = Config.get_by_params(%{group: config.group, key: config.key})
updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
assert config.value == updated.value
value = Config.from_binary(updated.value)
value = ConfigDB.from_binary(updated.value)
assert value[:key1] == :val1
assert value[:key2] == [k1: :v1, k2: :v2, k3: :v3]
assert value[:key3] == :val3
end
test "only full update for some keys" do
config1 = insert(:config, key: ":ecto_repos", value: Config.to_binary(repo: Pleroma.Repo))
config2 = insert(:config, group: ":cors_plug", key: ":max_age", value: Config.to_binary(18))
config1 = insert(:config, key: ":ecto_repos", value: ConfigDB.to_binary(repo: Pleroma.Repo))
config2 =
insert(:config, group: ":cors_plug", key: ":max_age", value: ConfigDB.to_binary(18))
{:ok, _config} =
Config.update_or_create(%{
ConfigDB.update_or_create(%{
group: config1.group,
key: config1.key,
value: [another_repo: [Pleroma.Repo]]
})
{:ok, _config} =
Config.update_or_create(%{
ConfigDB.update_or_create(%{
group: config2.group,
key: config2.key,
value: 777
})
updated1 = Config.get_by_params(%{group: config1.group, key: config1.key})
updated2 = Config.get_by_params(%{group: config2.group, key: config2.key})
updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})
assert Config.from_binary(updated1.value) == [another_repo: [Pleroma.Repo]]
assert Config.from_binary(updated2.value) == 777
assert ConfigDB.from_binary(updated1.value) == [another_repo: [Pleroma.Repo]]
assert ConfigDB.from_binary(updated2.value) == 777
end
test "full update if value is not keyword" do
@ -118,176 +120,176 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
insert(:config,
group: ":tesla",
key: ":adapter",
value: Config.to_binary(Tesla.Adapter.Hackney)
value: ConfigDB.to_binary(Tesla.Adapter.Hackney)
)
{:ok, _config} =
Config.update_or_create(%{
ConfigDB.update_or_create(%{
group: config.group,
key: config.key,
value: Tesla.Adapter.Httpc
})
updated = Config.get_by_params(%{group: config.group, key: config.key})
updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
assert Config.from_binary(updated.value) == Tesla.Adapter.Httpc
assert ConfigDB.from_binary(updated.value) == Tesla.Adapter.Httpc
end
end
test "delete/1" do
config = insert(:config)
{:ok, _} = Config.delete(%{key: config.key, group: config.group})
refute Config.get_by_params(%{key: config.key, group: config.group})
{:ok, _} = ConfigDB.delete(%{key: config.key, group: config.group})
refute ConfigDB.get_by_params(%{key: config.key, group: config.group})
end
describe "transform/1" do
test "string" do
binary = Config.transform("value as string")
binary = ConfigDB.transform("value as string")
assert binary == :erlang.term_to_binary("value as string")
assert Config.from_binary(binary) == "value as string"
assert ConfigDB.from_binary(binary) == "value as string"
end
test "boolean" do
binary = Config.transform(false)
binary = ConfigDB.transform(false)
assert binary == :erlang.term_to_binary(false)
assert Config.from_binary(binary) == false
assert ConfigDB.from_binary(binary) == false
end
test "nil" do
binary = Config.transform(nil)
binary = ConfigDB.transform(nil)
assert binary == :erlang.term_to_binary(nil)
assert Config.from_binary(binary) == nil
assert ConfigDB.from_binary(binary) == nil
end
test "integer" do
binary = Config.transform(150)
binary = ConfigDB.transform(150)
assert binary == :erlang.term_to_binary(150)
assert Config.from_binary(binary) == 150
assert ConfigDB.from_binary(binary) == 150
end
test "atom" do
binary = Config.transform(":atom")
binary = ConfigDB.transform(":atom")
assert binary == :erlang.term_to_binary(:atom)
assert Config.from_binary(binary) == :atom
assert ConfigDB.from_binary(binary) == :atom
end
test "ssl options" do
binary = Config.transform([":tlsv1", ":tlsv1.1", ":tlsv1.2"])
binary = ConfigDB.transform([":tlsv1", ":tlsv1.1", ":tlsv1.2"])
assert binary == :erlang.term_to_binary([:tlsv1, :"tlsv1.1", :"tlsv1.2"])
assert Config.from_binary(binary) == [:tlsv1, :"tlsv1.1", :"tlsv1.2"]
assert ConfigDB.from_binary(binary) == [:tlsv1, :"tlsv1.1", :"tlsv1.2"]
end
test "pleroma module" do
binary = Config.transform("Pleroma.Bookmark")
binary = ConfigDB.transform("Pleroma.Bookmark")
assert binary == :erlang.term_to_binary(Pleroma.Bookmark)
assert Config.from_binary(binary) == Pleroma.Bookmark
assert ConfigDB.from_binary(binary) == Pleroma.Bookmark
end
test "pleroma string" do
binary = Config.transform("Pleroma")
binary = ConfigDB.transform("Pleroma")
assert binary == :erlang.term_to_binary("Pleroma")
assert Config.from_binary(binary) == "Pleroma"
assert ConfigDB.from_binary(binary) == "Pleroma"
end
test "phoenix module" do
binary = Config.transform("Phoenix.Socket.V1.JSONSerializer")
binary = ConfigDB.transform("Phoenix.Socket.V1.JSONSerializer")
assert binary == :erlang.term_to_binary(Phoenix.Socket.V1.JSONSerializer)
assert Config.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
assert ConfigDB.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
end
test "tesla module" do
binary = Config.transform("Tesla.Adapter.Hackney")
binary = ConfigDB.transform("Tesla.Adapter.Hackney")
assert binary == :erlang.term_to_binary(Tesla.Adapter.Hackney)
assert Config.from_binary(binary) == Tesla.Adapter.Hackney
assert ConfigDB.from_binary(binary) == Tesla.Adapter.Hackney
end
test "ExSyslogger module" do
binary = Config.transform("ExSyslogger")
binary = ConfigDB.transform("ExSyslogger")
assert binary == :erlang.term_to_binary(ExSyslogger)
assert Config.from_binary(binary) == ExSyslogger
assert ConfigDB.from_binary(binary) == ExSyslogger
end
test "Quack.Logger module" do
binary = Config.transform("Quack.Logger")
binary = ConfigDB.transform("Quack.Logger")
assert binary == :erlang.term_to_binary(Quack.Logger)
assert Config.from_binary(binary) == Quack.Logger
assert ConfigDB.from_binary(binary) == Quack.Logger
end
test "sigil" do
binary = Config.transform("~r[comp[lL][aA][iI][nN]er]")
binary = ConfigDB.transform("~r[comp[lL][aA][iI][nN]er]")
assert binary == :erlang.term_to_binary(~r/comp[lL][aA][iI][nN]er/)
assert Config.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
assert ConfigDB.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
end
test "link sigil" do
binary = Config.transform("~r/https:\/\/example.com/")
binary = ConfigDB.transform("~r/https:\/\/example.com/")
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/)
assert Config.from_binary(binary) == ~r/https:\/\/example.com/
assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/
end
test "link sigil with um modifiers" do
binary = Config.transform("~r/https:\/\/example.com/um")
binary = ConfigDB.transform("~r/https:\/\/example.com/um")
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/um)
assert Config.from_binary(binary) == ~r/https:\/\/example.com/um
assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/um
end
test "link sigil with i modifier" do
binary = Config.transform("~r/https:\/\/example.com/i")
binary = ConfigDB.transform("~r/https:\/\/example.com/i")
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i)
assert Config.from_binary(binary) == ~r/https:\/\/example.com/i
assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/i
end
test "link sigil with s modifier" do
binary = Config.transform("~r/https:\/\/example.com/s")
binary = ConfigDB.transform("~r/https:\/\/example.com/s")
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s)
assert Config.from_binary(binary) == ~r/https:\/\/example.com/s
assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/s
end
test "raise if valid delimiter not found" do
assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
Config.transform("~r/https://[]{}<>\"'()|example.com/s")
ConfigDB.transform("~r/https://[]{}<>\"'()|example.com/s")
end
end
test "2 child tuple" do
binary = Config.transform(%{"tuple" => ["v1", ":v2"]})
binary = ConfigDB.transform(%{"tuple" => ["v1", ":v2"]})
assert binary == :erlang.term_to_binary({"v1", :v2})
assert Config.from_binary(binary) == {"v1", :v2}
assert ConfigDB.from_binary(binary) == {"v1", :v2}
end
test "proxy tuple with localhost" do
binary =
Config.transform(%{
ConfigDB.transform(%{
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
})
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, :localhost, 1234}})
assert Config.from_binary(binary) == {:proxy_url, {:socks5, :localhost, 1234}}
assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, :localhost, 1234}}
end
test "proxy tuple with domain" do
binary =
Config.transform(%{
ConfigDB.transform(%{
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
})
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, 'domain.com', 1234}})
assert Config.from_binary(binary) == {:proxy_url, {:socks5, 'domain.com', 1234}}
assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, 'domain.com', 1234}}
end
test "proxy tuple with ip" do
binary =
Config.transform(%{
ConfigDB.transform(%{
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
})
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}})
assert Config.from_binary(binary) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
end
test "tuple with n childs" do
binary =
Config.transform(%{
ConfigDB.transform(%{
"tuple" => [
"v1",
":v2",
@ -303,12 +305,12 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
{"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
)
assert Config.from_binary(binary) ==
assert ConfigDB.from_binary(binary) ==
{"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
end
test "tuple with dispatch key" do
binary = Config.transform(%{"tuple" => [":dispatch", ["{:_,
binary = ConfigDB.transform(%{"tuple" => [":dispatch", ["{:_,
[
{\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
@ -332,7 +334,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
]}
)
assert Config.from_binary(binary) ==
assert ConfigDB.from_binary(binary) ==
{:dispatch,
[
{:_,
@ -347,38 +349,38 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
end
test "map with string key" do
binary = Config.transform(%{"key" => "value"})
binary = ConfigDB.transform(%{"key" => "value"})
assert binary == :erlang.term_to_binary(%{"key" => "value"})
assert Config.from_binary(binary) == %{"key" => "value"}
assert ConfigDB.from_binary(binary) == %{"key" => "value"}
end
test "map with atom key" do
binary = Config.transform(%{":key" => "value"})
binary = ConfigDB.transform(%{":key" => "value"})
assert binary == :erlang.term_to_binary(%{key: "value"})
assert Config.from_binary(binary) == %{key: "value"}
assert ConfigDB.from_binary(binary) == %{key: "value"}
end
test "list of strings" do
binary = Config.transform(["v1", "v2", "v3"])
binary = ConfigDB.transform(["v1", "v2", "v3"])
assert binary == :erlang.term_to_binary(["v1", "v2", "v3"])
assert Config.from_binary(binary) == ["v1", "v2", "v3"]
assert ConfigDB.from_binary(binary) == ["v1", "v2", "v3"]
end
test "list of modules" do
binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"])
binary = ConfigDB.transform(["Pleroma.Repo", "Pleroma.Activity"])
assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
assert ConfigDB.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
end
test "list of atoms" do
binary = Config.transform([":v1", ":v2", ":v3"])
binary = ConfigDB.transform([":v1", ":v2", ":v3"])
assert binary == :erlang.term_to_binary([:v1, :v2, :v3])
assert Config.from_binary(binary) == [:v1, :v2, :v3]
assert ConfigDB.from_binary(binary) == [:v1, :v2, :v3]
end
test "list of mixed values" do
binary =
Config.transform([
ConfigDB.transform([
"v1",
":v2",
"Pleroma.Repo",
@ -397,7 +399,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
false
])
assert Config.from_binary(binary) == [
assert ConfigDB.from_binary(binary) == [
"v1",
:v2,
Pleroma.Repo,
@ -408,23 +410,23 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
end
test "simple keyword" do
binary = Config.transform([%{"tuple" => [":key", "value"]}])
binary = ConfigDB.transform([%{"tuple" => [":key", "value"]}])
assert binary == :erlang.term_to_binary([{:key, "value"}])
assert Config.from_binary(binary) == [{:key, "value"}]
assert Config.from_binary(binary) == [key: "value"]
assert ConfigDB.from_binary(binary) == [{:key, "value"}]
assert ConfigDB.from_binary(binary) == [key: "value"]
end
test "keyword with partial_chain key" do
binary =
Config.transform([%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}])
ConfigDB.transform([%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}])
assert binary == :erlang.term_to_binary(partial_chain: &:hackney_connect.partial_chain/1)
assert Config.from_binary(binary) == [partial_chain: &:hackney_connect.partial_chain/1]
assert ConfigDB.from_binary(binary) == [partial_chain: &:hackney_connect.partial_chain/1]
end
test "keyword" do
binary =
Config.transform([
ConfigDB.transform([
%{"tuple" => [":types", "Pleroma.PostgresTypes"]},
%{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
%{"tuple" => [":migration_lock", nil]},
@ -441,7 +443,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
key2: "string"
)
assert Config.from_binary(binary) == [
assert ConfigDB.from_binary(binary) == [
types: Pleroma.PostgresTypes,
telemetry_event: [Pleroma.Repo.Instrumenter],
migration_lock: nil,
@ -452,7 +454,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
test "complex keyword with nested mixed childs" do
binary =
Config.transform([
ConfigDB.transform([
%{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
%{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
%{"tuple" => [":link_name", true]},
@ -492,7 +494,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
]
)
assert Config.from_binary(binary) ==
assert ConfigDB.from_binary(binary) ==
[
uploader: Pleroma.Uploaders.Local,
filters: [Pleroma.Upload.Filter.Dedupe],
@ -512,7 +514,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
test "common keyword" do
binary =
Config.transform([
ConfigDB.transform([
%{"tuple" => [":level", ":warn"]},
%{"tuple" => [":meta", [":all"]]},
%{"tuple" => [":path", ""]},
@ -529,7 +531,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
)
assert Config.from_binary(binary) == [
assert ConfigDB.from_binary(binary) == [
level: :warn,
meta: [:all],
path: "",
@ -540,7 +542,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
test "complex keyword with sigil" do
binary =
Config.transform([
ConfigDB.transform([
%{"tuple" => [":federated_timeline_removal", []]},
%{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
%{"tuple" => [":replace", []]}
@ -553,13 +555,13 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
replace: []
)
assert Config.from_binary(binary) ==
assert ConfigDB.from_binary(binary) ==
[federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
end
test "complex keyword with tuples with more than 2 values" do
binary =
Config.transform([
ConfigDB.transform([
%{
"tuple" => [
":http",
@ -630,7 +632,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
]
)
assert Config.from_binary(binary) == [
assert ConfigDB.from_binary(binary) == [
http: [
key1: [
{:_,

View file

@ -5,7 +5,7 @@
defmodule Pleroma.Config.TransferTaskTest do
use Pleroma.DataCase
alias Pleroma.Web.AdminAPI.Config
alias Pleroma.ConfigDB
clear_config(:configurable_from_database) do
Pleroma.Config.put(:configurable_from_database, true)
@ -16,19 +16,19 @@ defmodule Pleroma.Config.TransferTaskTest do
refute Application.get_env(:idna, :test_key)
refute Application.get_env(:quack, :test_key)
Config.create(%{
ConfigDB.create(%{
group: ":pleroma",
key: ":test_key",
value: [live: 2, com: 3]
})
Config.create(%{
ConfigDB.create(%{
group: ":idna",
key: ":test_key",
value: [live: 15, com: 35]
})
Config.create(%{
ConfigDB.create(%{
group: ":quack",
key: ":test_key",
value: [:test_value1, :test_value2]
@ -48,7 +48,7 @@ defmodule Pleroma.Config.TransferTaskTest do
end
test "non existing atom" do
Config.create(%{
ConfigDB.create(%{
group: ":pleroma",
key: ":undefined_atom_key",
value: [live: 2, com: 3]

View file

@ -394,7 +394,7 @@ defmodule Pleroma.Factory do
end
def config_factory do
%Pleroma.Web.AdminAPI.Config{
%Pleroma.ConfigDB{
key:
sequence(:key, fn key ->
# Atom dynamic registration hack in tests

View file

@ -4,8 +4,8 @@
defmodule Mix.Tasks.Pleroma.ConfigTest do
use Pleroma.DataCase
alias Pleroma.ConfigDB
alias Pleroma.Repo
alias Pleroma.Web.AdminAPI.Config
setup_all do
Mix.shell(Mix.Shell.Process)
@ -26,7 +26,7 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
test "settings are migrated to db" do
initial = Application.get_env(:quack, :level)
on_exit(fn -> Application.put_env(:quack, :level, initial) end)
assert Repo.all(Config) == []
assert Repo.all(ConfigDB) == []
Application.put_env(:pleroma, :first_setting, key: "value", key2: [Repo])
Application.put_env(:pleroma, :second_setting, key: "value2", key2: ["Activity"])
@ -34,14 +34,14 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
config1 = Config.get_by_params(%{group: ":pleroma", key: ":first_setting"})
config2 = Config.get_by_params(%{group: ":pleroma", key: ":second_setting"})
config3 = Config.get_by_params(%{group: ":quack", key: ":level"})
refute Config.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})
config3 = ConfigDB.get_by_params(%{group: ":quack", key: ":level"})
refute ConfigDB.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
assert Config.from_binary(config1.value) == [key: "value", key2: [Repo]]
assert Config.from_binary(config2.value) == [key: "value2", key2: ["Activity"]]
assert Config.from_binary(config3.value) == :info
assert ConfigDB.from_binary(config1.value) == [key: "value", key2: [Repo]]
assert ConfigDB.from_binary(config2.value) == [key: "value2", key2: ["Activity"]]
assert ConfigDB.from_binary(config3.value) == :info
end
describe "with deletion temp file" do
@ -56,23 +56,23 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
end
test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
Config.create(%{
ConfigDB.create(%{
group: ":pleroma",
key: ":setting_first",
value: [key: "value", key2: ["Activity"]]
})
Config.create(%{
ConfigDB.create(%{
group: ":pleroma",
key: ":setting_second",
value: [key: "value2", key2: [Repo]]
})
Config.create(%{group: ":quack", key: ":level", value: :info})
ConfigDB.create(%{group: ":quack", key: ":level", value: :info})
Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", "temp", "-d"])
assert Repo.all(Config) == []
assert Repo.all(ConfigDB) == []
file = File.read!(temp_file)
assert file =~ "config :pleroma, :setting_first,"
@ -81,7 +81,7 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
end
test "load a settings with large values and pass to file", %{temp_file: temp_file} do
Config.create(%{
ConfigDB.create(%{
group: ":pleroma",
key: ":instance",
value: [
@ -151,7 +151,7 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", "temp", "-d"])
assert Repo.all(Config) == []
assert Repo.all(ConfigDB) == []
assert File.exists?(temp_file)
{:ok, file} = File.read(temp_file)

View file

@ -2832,14 +2832,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest 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 Repo.all(Pleroma.Web.AdminAPI.Config) == []
assert Repo.all(Pleroma.ConfigDB) == []
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
assert Repo.aggregate(Pleroma.Web.AdminAPI.Config, :count, :id) > 0
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.Web.AdminAPI.Config) == []
assert Repo.all(Pleroma.ConfigDB) == []
end
test "returns error if configuration from database is off", %{conn: conn} do
@ -2852,7 +2852,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 400) ==
"To use this endpoint you need to enable configuration from database."
assert Repo.all(Pleroma.Web.AdminAPI.Config) == []
assert Repo.all(Pleroma.ConfigDB) == []
end
end