tests for setttings without an explicit key

This commit is contained in:
Alexander 2019-12-06 08:21:30 +03:00 committed by Alexander Strizhakov
parent 2753285b77
commit 0656816c77
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
4 changed files with 68 additions and 9 deletions

View file

@ -76,10 +76,10 @@ defmodule Mix.Tasks.Pleroma.Config do
|> Application.get_all_env() |> Application.get_all_env()
|> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end) |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
|> Enum.each(fn {key, value} -> |> Enum.each(fn {key, value} ->
key_str = inspect(key) key = inspect(key)
{:ok, _} = Config.update_or_create(%{group: inspect(group), key: key, value: value})
{:ok, _} = Config.update_or_create(%{group: ":#{group}", key: key_str, value: value}) Mix.shell().info("settings for key #{key} migrated.")
Mix.shell().info("settings for key #{key_str} migrated.")
end) end)
Mix.shell().info("settings for group :#{group} migrated.") Mix.shell().info("settings for group :#{group} migrated.")

View file

@ -5,6 +5,8 @@
defmodule Pleroma.Config.TransferTaskTest do defmodule Pleroma.Config.TransferTaskTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Web.AdminAPI.Config
clear_config([:instance, :dynamic_configuration]) do clear_config([:instance, :dynamic_configuration]) do
Pleroma.Config.put([:instance, :dynamic_configuration], true) Pleroma.Config.put([:instance, :dynamic_configuration], true)
end end
@ -12,32 +14,41 @@ defmodule Pleroma.Config.TransferTaskTest do
test "transfer config values from db to env" do test "transfer config values from db to env" do
refute Application.get_env(:pleroma, :test_key) refute Application.get_env(:pleroma, :test_key)
refute Application.get_env(:idna, :test_key) refute Application.get_env(:idna, :test_key)
refute Application.get_env(:quack, :test_key)
Pleroma.Web.AdminAPI.Config.create(%{ Config.create(%{
group: ":pleroma", group: ":pleroma",
key: ":test_key", key: ":test_key",
value: [live: 2, com: 3] value: [live: 2, com: 3]
}) })
Pleroma.Web.AdminAPI.Config.create(%{ Config.create(%{
group: ":idna", group: ":idna",
key: ":test_key", key: ":test_key",
value: [live: 15, com: 35] value: [live: 15, com: 35]
}) })
Config.create(%{
group: ":quack",
key: ":test_key",
value: [:test_value1, :test_value2]
})
Pleroma.Config.TransferTask.start_link([]) Pleroma.Config.TransferTask.start_link([])
assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3] assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
assert Application.get_env(:idna, :test_key) == [live: 15, com: 35] assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
assert Application.get_env(:quack, :test_key) == [:test_value1, :test_value2]
on_exit(fn -> on_exit(fn ->
Application.delete_env(:pleroma, :test_key) Application.delete_env(:pleroma, :test_key)
Application.delete_env(:idna, :test_key) Application.delete_env(:idna, :test_key)
Application.delete_env(:quack, :test_key)
end) end)
end end
test "non existing atom" do test "non existing atom" do
Pleroma.Web.AdminAPI.Config.create(%{ Config.create(%{
group: ":pleroma", group: ":pleroma",
key: ":undefined_atom_key", key: ":undefined_atom_key",
value: [live: 2, com: 3] value: [live: 2, com: 3]

View file

@ -24,19 +24,24 @@ 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)
on_exit(fn -> Application.put_all_env([{:quack, 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])
Application.put_env(:pleroma, :second_setting, key: "value2", key2: ["Activity"]) 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.run(["migrate_to_db"])
config1 = Config.get_by_params(%{group: ":pleroma", key: ":first_setting"}) config1 = Config.get_by_params(%{group: ":pleroma", key: ":first_setting"})
config2 = Config.get_by_params(%{group: ":pleroma", key: ":second_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"}) refute Config.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
assert Config.from_binary(config1.value) == [key: "value", key2: [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(config2.value) == [key: "value2", key2: ["Activity"]]
assert Config.from_binary(config3.value) == :info
end end
test "settings are migrated to file and deleted from db" do test "settings are migrated to file and deleted from db" do
@ -59,6 +64,8 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
value: [key: "value2", key2: [Repo]] value: [key: "value2", key2: [Repo]]
}) })
Config.create(%{group: ":quack", key: ":level", value: :info})
Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", env, "-d"]) Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", env, "-d"])
assert Repo.all(Config) == [] assert Repo.all(Config) == []
@ -66,6 +73,7 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
file = File.read!(config_file) file = File.read!(config_file)
assert file =~ "config :pleroma, :setting_first," assert file =~ "config :pleroma, :setting_first,"
assert file =~ "config :pleroma, :setting_second," assert file =~ "config :pleroma, :setting_second,"
assert file =~ "config :quack, :level, :info"
end end
test "load a settings with large values and pass to file", %{temp_file: temp_file} do test "load a settings with large values and pass to file", %{temp_file: temp_file} do

View file

@ -1970,8 +1970,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest 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"
on_exit(fn -> on_exit(fn ->
Application.delete_env(:pleroma, :key1) Application.delete_env(:pleroma, :key1)
Application.delete_env(:pleroma, :key2) Application.delete_env(:pleroma, :key2)
@ -1981,7 +1979,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
Application.delete_env(:pleroma, :keyaa2) Application.delete_env(:pleroma, :keyaa2)
Application.delete_env(:pleroma, Pleroma.Web.Endpoint.NotReal) Application.delete_env(:pleroma, Pleroma.Web.Endpoint.NotReal)
Application.delete_env(:pleroma, Pleroma.Captcha.NotReal) Application.delete_env(:pleroma, Pleroma.Captcha.NotReal)
:ok = File.rm(temp_file) :ok = File.rm("config/test.exported_from_db.secret.exs")
end) end)
%{conn: assign(conn, :user, admin)} %{conn: assign(conn, :user, admin)}
@ -2101,6 +2099,48 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []} assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []}
end 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)
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
group: ":quack",
key: ":level",
value: ":info"
},
%{
group: ":quack",
key: ":meta",
value: [":none"]
},
%{
group: ":quack",
key: ":webhook_url",
value: "https://hooks.slack.com/services/KEY"
}
]
})
assert json_response(conn, 200) == %{
"configs" => [
%{"group" => ":quack", "key" => ":level", "value" => ":info"},
%{"group" => ":quack", "key" => ":meta", "value" => [":none"]},
%{
"group" => ":quack",
"key" => ":webhook_url",
"value" => "https://hooks.slack.com/services/KEY"
}
]
}
assert Application.get_env(:quack, :level) == :info
assert Application.get_env(:quack, :meta) == [:none]
assert Application.get_env(:quack, :webhook_url) == "https://hooks.slack.com/services/KEY"
end
test "update config setting & delete", %{conn: conn} do test "update config setting & delete", %{conn: conn} do
config1 = insert(:config, key: ":keyaa1") config1 = insert(:config, key: ":keyaa1")
config2 = insert(:config, key: ":keyaa2") config2 = insert(:config, key: ":keyaa2")