saving to DB only added by user settings

This commit is contained in:
Alexander Strizhakov 2020-01-17 11:45:44 +03:00
commit 60ba2339a2
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
11 changed files with 280 additions and 44 deletions

View file

@ -15,8 +15,8 @@ defmodule Pleroma.ConfigDBTest do
end
test "create/1" do
{:ok, config} = ConfigDB.create(%{group: "pleroma", key: "some_key", value: "some_value"})
assert config == ConfigDB.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
@ -26,6 +26,27 @@ defmodule Pleroma.ConfigDBTest do
assert loaded == updated
end
test "get_all_as_keyword/0" do
insert(:config)
insert(:config, group: ":quack", key: ":level", value: ConfigDB.to_binary(:info))
insert(:config, group: ":quack", key: ":meta", value: ConfigDB.to_binary([:none]))
insert(:config,
group: ":quack",
key: ":webhook_url",
value: ConfigDB.to_binary("https://hooks.slack.com/services/KEY/some_val")
)
assert [
pleroma: [{_, %{another: _, another_key: _}}],
quack: [
level: :info,
meta: [:none],
webhook_url: "https://hooks.slack.com/services/KEY/some_val"
]
] = ConfigDB.get_all_as_keyword()
end
describe "update_or_create/1" do
test "common" do
config = insert(:config)

View file

@ -47,6 +47,35 @@ defmodule Pleroma.Config.TransferTaskTest do
end)
end
test "transfer config values for 1 group and some keys" do
level = Application.get_env(:quack, :level)
meta = Application.get_env(:quack, :meta)
ConfigDB.create(%{
group: ":quack",
key: ":level",
value: :info
})
ConfigDB.create(%{
group: ":quack",
key: ":meta",
value: [:none]
})
Pleroma.Config.TransferTask.start_link([])
assert Application.get_env(:quack, :level) == :info
assert Application.get_env(:quack, :meta) == [:none]
default = Pleroma.Config.Holder.config(:quack, :webhook_url)
assert Application.get_env(:quack, :webhook_url) == default
on_exit(fn ->
Application.put_env(:quack, :level, level)
Application.put_env(:quack, :meta, meta)
end)
end
test "non existing atom" do
ConfigDB.create(%{
group: ":pleroma",

9
test/fixtures/config/temp.secret.exs vendored Normal file
View file

@ -0,0 +1,9 @@
use Mix.Config
config :pleroma, :first_setting, key: "value", key2: [Pleroma.Repo]
config :pleroma, :second_setting, key: "value2", key2: ["Activity"]
config :quack, level: :info
config :pleroma, Pleroma.Repo, pool: Ecto.Adapters.SQL.Sandbox

View file

@ -4,6 +4,9 @@
defmodule Mix.Tasks.Pleroma.ConfigTest do
use Pleroma.DataCase
import ExUnit.CaptureLog
alias Pleroma.ConfigDB
alias Pleroma.Repo
@ -23,16 +26,17 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
Pleroma.Config.put(:configurable_from_database, true)
end
test "warning if file with custom settings doesn't exist" do
assert capture_log(fn -> Mix.Tasks.Pleroma.Config.run(["migrate_to_db"]) end) =~
"to migrate settings, you must define custom settings in config/test.secret.exs"
end
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(ConfigDB) == []
Application.put_env(:pleroma, :first_setting, key: "value", key2: [Repo])
Application.put_env(:pleroma, :second_setting, key: "value2", key2: ["Activity"])
Application.put_env(:quack, :level, :info)
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})

View file

@ -7,6 +7,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
use Oban.Testing, repo: Pleroma.Repo
alias Pleroma.Activity
alias Pleroma.ConfigDB
alias Pleroma.HTML
alias Pleroma.ModerationLog
alias Pleroma.Repo
@ -1881,11 +1882,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
"To use configuration from database migrate your settings to database."
end
test "with settings in db", %{conn: conn} do
test "with settings only in db", %{conn: conn} do
config1 = insert(:config)
config2 = insert(:config)
conn = get(conn, "/api/pleroma/admin/config")
conn = get(conn, "/api/pleroma/admin/config", %{"only_db" => true})
%{
"configs" => [
@ -1895,6 +1896,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
"value" => _
},
%{
"group" => ":pleroma",
"key" => key2,
"value" => _
}
@ -1904,6 +1906,45 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert key1 == config1.key
assert key2 == config2.key
end
test "merged default setting with db settings", %{conn: conn} do
config1 = insert(:config)
config2 = insert(:config)
config3 =
insert(:config,
value: ConfigDB.to_binary(k1: :v1, k2: :v2)
)
conn = get(conn, "/api/pleroma/admin/config")
%{"configs" => configs} = json_response(conn, 200)
assert length(configs) > 3
received_configs =
Enum.filter(configs, fn %{"group" => group, "key" => key} ->
group == ":pleroma" and key in [config1.key, config2.key, config3.key]
end)
assert length(received_configs) == 3
db_keys =
config3.value
|> ConfigDB.from_binary()
|> Keyword.keys()
|> ConfigDB.convert()
Enum.each(received_configs, fn %{"value" => value, "db" => db} ->
assert db in [config1.key, config2.key, db_keys]
assert value in [
ConfigDB.from_binary_with_convert(config1.value),
ConfigDB.from_binary_with_convert(config2.value),
ConfigDB.from_binary_with_convert(config3.value)
]
end)
end
end
test "POST /api/pleroma/admin/config error", %{conn: conn} do
@ -2831,9 +2872,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
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.ConfigDB) == []
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
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")