partially settings update

This commit is contained in:
Alexander 2019-12-06 15:12:56 +03:00 committed by Alexander Strizhakov
commit 5cacb988b9
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
5 changed files with 218 additions and 23 deletions

View file

@ -1979,6 +1979,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
Application.delete_env(:pleroma, :keyaa2)
Application.delete_env(:pleroma, Pleroma.Web.Endpoint.NotReal)
Application.delete_env(:pleroma, Pleroma.Captcha.NotReal)
Application.put_env(:tesla, :adapter, Tesla.Mock)
:ok = File.rm("config/test.exported_from_db.secret.exs")
end)
@ -2141,14 +2142,64 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert Application.get_env(:quack, :webhook_url) == "https://hooks.slack.com/services/KEY"
end
test "saving config with partial update", %{conn: conn} do
config = insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: 2))
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{group: config.group, key: config.key, value: [%{"tuple" => [":key3", 3]}]}
]
})
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
"key" => ":key1",
"value" => [
%{"tuple" => [":key1", 1]},
%{"tuple" => [":key2", 2]},
%{"tuple" => [":key3", 3]}
]
}
]
}
end
test "saving full setting if value is not keyword", %{conn: conn} do
config =
insert(:config,
group: ":tesla",
key: ":adapter",
value: :erlang.term_to_binary(Tesla.Adapter.Hackey)
)
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{group: config.group, key: config.key, value: "Tesla.Adapter.Httpc"}
]
})
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => ":tesla",
"key" => ":adapter",
"value" => "Tesla.Adapter.Httpc"
}
]
}
end
test "update config setting & delete", %{conn: conn} do
config1 = insert(:config, key: ":keyaa1")
config2 = insert(:config, key: ":keyaa2")
insert(:config,
group: "ueberauth",
key: "Ueberauth.Strategy.Microsoft.OAuth",
value: :erlang.term_to_binary([])
key: "Ueberauth.Strategy.Microsoft.OAuth"
)
conn =

View file

@ -26,26 +26,92 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
assert loaded == updated
end
test "update_or_create/1" do
config = insert(:config)
key2 = "another_key"
describe "update_or_create/1" do
test "common" do
config = insert(:config)
key2 = "another_key"
params = [
%{group: "pleroma", key: key2, value: "another_value"},
%{group: config.group, key: config.key, value: "new_value"}
]
params = [
%{group: "pleroma", key: key2, value: "another_value"},
%{group: config.group, key: config.key, value: "new_value"}
]
assert Repo.all(Config) |> length() == 1
assert Repo.all(Config) |> length() == 1
Enum.each(params, &Config.update_or_create(&1))
Enum.each(params, &Config.update_or_create(&1))
assert Repo.all(Config) |> length() == 2
assert Repo.all(Config) |> length() == 2
config1 = Config.get_by_params(%{group: config.group, key: config.key})
config2 = Config.get_by_params(%{group: "pleroma", key: key2})
config1 = Config.get_by_params(%{group: config.group, key: config.key})
config2 = Config.get_by_params(%{group: "pleroma", key: key2})
assert config1.value == Config.transform("new_value")
assert config2.value == Config.transform("another_value")
assert config1.value == Config.transform("new_value")
assert config2.value == Config.transform("another_value")
end
test "partial update" do
config = insert(:config, value: Config.to_binary(key1: "val1", key2: :val2))
{:ok, _config} =
Config.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})
value = Config.from_binary(updated.value)
assert length(value) == 3
assert value[:key1] == :val1
assert value[:key2] == :val2
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))
{:ok, _config} =
Config.update_or_create(%{
group: config1.group,
key: config1.key,
value: [another_repo: [Pleroma.Repo]]
})
{:ok, _config} =
Config.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})
assert Config.from_binary(updated1.value) == [another_repo: [Pleroma.Repo]]
assert Config.from_binary(updated2.value) == 777
end
test "full update if value is not keyword" do
config =
insert(:config,
group: ":tesla",
key: ":adapter",
value: Config.to_binary(Tesla.Adapter.Hackney)
)
{:ok, _config} =
Config.update_or_create(%{
group: config.group,
key: config.key,
value: Tesla.Adapter.Httpc
})
updated = Config.get_by_params(%{group: config.group, key: config.key})
assert Config.from_binary(updated.value) == Tesla.Adapter.Httpc
end
end
test "delete/1" do