config editing through database

This commit is contained in:
Alex S 2019-09-29 11:17:38 +03:00 committed by Alexander Strizhakov
commit 2753285b77
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
20 changed files with 1133 additions and 472 deletions

View file

@ -14,14 +14,14 @@ defmodule Pleroma.Config.TransferTaskTest do
refute Application.get_env(:idna, :test_key)
Pleroma.Web.AdminAPI.Config.create(%{
group: "pleroma",
key: "test_key",
group: ":pleroma",
key: ":test_key",
value: [live: 2, com: 3]
})
Pleroma.Web.AdminAPI.Config.create(%{
group: "idna",
key: "test_key",
group: ":idna",
key: ":test_key",
value: [live: 15, com: 35]
})
@ -38,14 +38,14 @@ defmodule Pleroma.Config.TransferTaskTest do
test "non existing atom" do
Pleroma.Web.AdminAPI.Config.create(%{
group: "pleroma",
key: "undefined_atom_key",
group: ":pleroma",
key: ":undefined_atom_key",
value: [live: 2, com: 3]
})
assert ExUnit.CaptureLog.capture_log(fn ->
Pleroma.Config.TransferTask.start_link([])
end) =~
"updating env causes error, key: \"undefined_atom_key\", error: %ArgumentError{message: \"argument error\"}"
"updating env causes error, key: \":undefined_atom_key\", error: %ArgumentError{message: \"argument error\"}"
end
end

View file

@ -0,0 +1,211 @@
defmodule Pleroma.Docs.GeneratorTest do
use ExUnit.Case, async: true
alias Pleroma.Docs.Generator
@descriptions [
%{
group: :pleroma,
key: Pleroma.Upload,
type: :group,
description: "",
children: [
%{
key: :uploader,
type: :module,
description: "",
suggestions:
Generator.list_modules_in_dir(
"lib/pleroma/upload/filter",
"Elixir.Pleroma.Upload.Filter."
)
},
%{
key: :filters,
type: {:list, :module},
description: "",
suggestions:
Generator.list_modules_in_dir(
"lib/pleroma/web/activity_pub/mrf",
"Elixir.Pleroma.Web.ActivityPub.MRF."
)
},
%{
key: Pleroma.Upload,
type: :string,
description: "",
suggestions: [""]
},
%{
key: :some_key,
type: :keyword,
description: "",
suggestions: [],
children: [
%{
key: :another_key,
type: :integer,
description: "",
suggestions: [5]
},
%{
key: :another_key_with_label,
label: "Another label",
type: :integer,
description: "",
suggestions: [7]
}
]
},
%{
key: :key1,
type: :atom,
description: "",
suggestions: [
:atom,
Pleroma.Upload,
{:tuple, "string", 8080},
[:atom, Pleroma.Upload, {:atom, Pleroma.Upload}]
]
},
%{
key: Pleroma.Upload,
label: "Special Label",
type: :string,
description: "",
suggestions: [""]
},
%{
group: {:subgroup, Swoosh.Adapters.SMTP},
key: :auth,
type: :atom,
description: "`Swoosh.Adapters.SMTP` adapter specific setting",
suggestions: [:always, :never, :if_available]
},
%{
key: "application/xml",
type: {:list, :string},
suggestions: ["xml"]
}
]
},
%{
group: :tesla,
key: :adapter,
type: :group,
description: ""
},
%{
group: :cors_plug,
type: :group,
children: [%{key: :key1, type: :string, suggestions: [""]}]
},
%{group: "Some string group", key: "Some string key", type: :group}
]
describe "convert_to_strings/1" do
test "group, key, label" do
[desc1, desc2 | _] = Generator.convert_to_strings(@descriptions)
assert desc1[:group] == ":pleroma"
assert desc1[:key] == "Pleroma.Upload"
assert desc1[:label] == "Pleroma.Upload"
assert desc2[:group] == ":tesla"
assert desc2[:key] == ":adapter"
assert desc2[:label] == "Adapter"
end
test "group without key" do
descriptions = Generator.convert_to_strings(@descriptions)
desc = Enum.at(descriptions, 2)
assert desc[:group] == ":cors_plug"
refute desc[:key]
assert desc[:label] == "Cors plug"
end
test "children key, label, type" do
[%{children: [child1, child2, child3, child4 | _]} | _] =
Generator.convert_to_strings(@descriptions)
assert child1[:key] == ":uploader"
assert child1[:label] == "Uploader"
assert child1[:type] == :module
assert child2[:key] == ":filters"
assert child2[:label] == "Filters"
assert child2[:type] == {:list, :module}
assert child3[:key] == "Pleroma.Upload"
assert child3[:label] == "Pleroma.Upload"
assert child3[:type] == :string
assert child4[:key] == ":some_key"
assert child4[:label] == "Some key"
assert child4[:type] == :keyword
end
test "child with predefined label" do
[%{children: children} | _] = Generator.convert_to_strings(@descriptions)
child = Enum.at(children, 5)
assert child[:key] == "Pleroma.Upload"
assert child[:label] == "Special Label"
end
test "subchild" do
[%{children: children} | _] = Generator.convert_to_strings(@descriptions)
child = Enum.at(children, 3)
%{children: [subchild | _]} = child
assert subchild[:key] == ":another_key"
assert subchild[:label] == "Another key"
assert subchild[:type] == :integer
end
test "subchild with predefined label" do
[%{children: children} | _] = Generator.convert_to_strings(@descriptions)
child = Enum.at(children, 3)
%{children: subchildren} = child
subchild = Enum.at(subchildren, 1)
assert subchild[:key] == ":another_key_with_label"
assert subchild[:label] == "Another label"
end
test "module suggestions" do
[%{children: [%{suggestions: suggestions} | _]} | _] =
Generator.convert_to_strings(@descriptions)
Enum.each(suggestions, fn suggestion ->
assert String.starts_with?(suggestion, "Pleroma.")
end)
end
test "atoms in suggestions with leading `:`" do
[%{children: children} | _] = Generator.convert_to_strings(@descriptions)
%{suggestions: suggestions} = Enum.at(children, 4)
assert Enum.at(suggestions, 0) == ":atom"
assert Enum.at(suggestions, 1) == "Pleroma.Upload"
assert Enum.at(suggestions, 2) == {":tuple", "string", 8080}
assert Enum.at(suggestions, 3) == [":atom", "Pleroma.Upload", {":atom", "Pleroma.Upload"}]
%{suggestions: suggestions} = Enum.at(children, 6)
assert Enum.at(suggestions, 0) == ":always"
assert Enum.at(suggestions, 1) == ":never"
assert Enum.at(suggestions, 2) == ":if_available"
end
test "group, key as string in main desc" do
descriptions = Generator.convert_to_strings(@descriptions)
desc = Enum.at(descriptions, 3)
assert desc[:group] == "Some string group"
assert desc[:key] == "Some string key"
end
test "key as string subchild" do
[%{children: children} | _] = Generator.convert_to_strings(@descriptions)
child = Enum.at(children, 7)
assert child[:key] == "application/xml"
end
end
end

View file

@ -377,8 +377,8 @@ defmodule Pleroma.Factory do
def config_factory do
%Pleroma.Web.AdminAPI.Config{
key: sequence(:key, &"some_key_#{&1}"),
group: "pleroma",
key: sequence(:key, &":some_key_#{&1}"),
group: ":pleroma",
value:
sequence(
:value,

View file

@ -9,16 +9,14 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
setup_all do
Mix.shell(Mix.Shell.Process)
temp_file = "config/temp.exported_from_db.secret.exs"
on_exit(fn ->
Mix.shell(Mix.Shell.IO)
Application.delete_env(:pleroma, :first_setting)
Application.delete_env(:pleroma, :second_setting)
:ok = File.rm(temp_file)
end)
{:ok, temp_file: temp_file}
:ok
end
clear_config_all([:instance, :dynamic_configuration]) do
@ -28,38 +26,44 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
test "settings are migrated to db" do
assert Repo.all(Config) == []
Application.put_env(:pleroma, :first_setting, key: "value", key2: [Pleroma.Repo])
Application.put_env(:pleroma, :second_setting, key: "value2", key2: [Pleroma.Activity])
Application.put_env(:pleroma, :first_setting, key: "value", key2: [Repo])
Application.put_env(:pleroma, :second_setting, key: "value2", key2: ["Activity"])
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
first_db = Config.get_by_params(%{group: "pleroma", key: ":first_setting"})
second_db = Config.get_by_params(%{group: "pleroma", key: ":second_setting"})
refute Config.get_by_params(%{group: "pleroma", key: "Pleroma.Repo"})
config1 = Config.get_by_params(%{group: ":pleroma", key: ":first_setting"})
config2 = Config.get_by_params(%{group: ":pleroma", key: ":second_setting"})
refute Config.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
assert Config.from_binary(first_db.value) == [key: "value", key2: [Pleroma.Repo]]
assert Config.from_binary(second_db.value) == [key: "value2", key2: [Pleroma.Activity]]
assert Config.from_binary(config1.value) == [key: "value", key2: [Repo]]
assert Config.from_binary(config2.value) == [key: "value2", key2: ["Activity"]]
end
test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
test "settings are migrated to file and deleted from db" do
env = "temp"
config_file = "config/#{env}.exported_from_db.secret.exs"
on_exit(fn ->
:ok = File.rm(config_file)
end)
Config.create(%{
group: "pleroma",
group: ":pleroma",
key: ":setting_first",
value: [key: "value", key2: [Pleroma.Activity]]
value: [key: "value", key2: ["Activity"]]
})
Config.create(%{
group: "pleroma",
group: ":pleroma",
key: ":setting_second",
value: [key: "valu2", key2: [Pleroma.Repo]]
value: [key: "value2", key2: [Repo]]
})
Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "temp", "true"])
Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", env, "-d"])
assert Repo.all(Config) == []
assert File.exists?(temp_file)
{:ok, file} = File.read(temp_file)
file = File.read!(config_file)
assert file =~ "config :pleroma, :setting_first,"
assert file =~ "config :pleroma, :setting_second,"
end

View file

@ -1950,6 +1950,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
%{
"configs" => [
%{
"group" => ":pleroma",
"key" => key1,
"value" => _
},
@ -1995,15 +1996,15 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{group: "pleroma", key: "key1", value: "value1"},
%{group: ":pleroma", key: ":key1", value: "value1"},
%{
group: "ueberauth",
group: ":ueberauth",
key: "Ueberauth.Strategy.Twitter.OAuth",
value: [%{"tuple" => [":consumer_secret", "aaaa"]}]
},
%{
group: "pleroma",
key: "key2",
group: ":pleroma",
key: ":key2",
value: %{
":nested_1" => "nested_value1",
":nested_2" => [
@ -2013,21 +2014,21 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
}
},
%{
group: "pleroma",
key: "key3",
group: ":pleroma",
key: ":key3",
value: [
%{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
%{"nested_4" => true}
]
},
%{
group: "pleroma",
key: "key4",
group: ":pleroma",
key: ":key4",
value: %{":nested_5" => ":upload", "endpoint" => "https://example.com"}
},
%{
group: "idna",
key: "key5",
group: ":idna",
key: ":key5",
value: %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}
}
]
@ -2036,18 +2037,18 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => "pleroma",
"key" => "key1",
"group" => ":pleroma",
"key" => ":key1",
"value" => "value1"
},
%{
"group" => "ueberauth",
"group" => ":ueberauth",
"key" => "Ueberauth.Strategy.Twitter.OAuth",
"value" => [%{"tuple" => [":consumer_secret", "aaaa"]}]
},
%{
"group" => "pleroma",
"key" => "key2",
"group" => ":pleroma",
"key" => ":key2",
"value" => %{
":nested_1" => "nested_value1",
":nested_2" => [
@ -2057,21 +2058,21 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
}
},
%{
"group" => "pleroma",
"key" => "key3",
"group" => ":pleroma",
"key" => ":key3",
"value" => [
%{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
%{"nested_4" => true}
]
},
%{
"group" => "pleroma",
"key" => "key4",
"group" => ":pleroma",
"key" => ":key4",
"value" => %{"endpoint" => "https://example.com", ":nested_5" => ":upload"}
},
%{
"group" => "idna",
"key" => "key5",
"group" => ":idna",
"key" => ":key5",
"value" => %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}
}
]
@ -2101,8 +2102,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
test "update config setting & delete", %{conn: conn} do
config1 = insert(:config, key: "keyaa1")
config2 = insert(:config, key: "keyaa2")
config1 = insert(:config, key: ":keyaa1")
config2 = insert(:config, key: ":keyaa2")
insert(:config,
group: "ueberauth",
@ -2126,7 +2127,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => config1.key,
"value" => "another_value"
}
@ -2138,11 +2139,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
test "common config example", %{conn: conn} do
adapter = Application.get_env(:tesla, :adapter)
on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end)
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => "Pleroma.Captcha.NotReal",
"value" => [
%{"tuple" => [":enabled", false]},
@ -2154,16 +2158,21 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
%{"tuple" => [":regex1", "~r/https:\/\/example.com/"]},
%{"tuple" => [":regex2", "~r/https:\/\/example.com/u"]},
%{"tuple" => [":regex3", "~r/https:\/\/example.com/i"]},
%{"tuple" => [":regex4", "~r/https:\/\/example.com/s"]}
%{"tuple" => [":regex4", "~r/https:\/\/example.com/s"]},
%{"tuple" => [":name", "Pleroma"]}
]
}
},
%{"group" => ":tesla", "key" => ":adapter", "value" => "Tesla.Adapter.Httpc"}
]
})
assert Application.get_env(:tesla, :adapter) == Tesla.Adapter.Httpc
assert Pleroma.Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma"
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => "Pleroma.Captcha.NotReal",
"value" => [
%{"tuple" => [":enabled", false]},
@ -2175,9 +2184,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
%{"tuple" => [":regex1", "~r/https:\\/\\/example.com/"]},
%{"tuple" => [":regex2", "~r/https:\\/\\/example.com/u"]},
%{"tuple" => [":regex3", "~r/https:\\/\\/example.com/i"]},
%{"tuple" => [":regex4", "~r/https:\\/\\/example.com/s"]}
%{"tuple" => [":regex4", "~r/https:\\/\\/example.com/s"]},
%{"tuple" => [":name", "Pleroma"]}
]
}
},
%{"group" => ":tesla", "key" => ":adapter", "value" => "Tesla.Adapter.Httpc"}
]
}
end
@ -2187,7 +2198,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
@ -2251,7 +2262,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
@ -2318,7 +2329,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => ":key1",
"value" => [
%{"tuple" => [":key2", "some_val"]},
@ -2348,7 +2359,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
%{
"configs" => [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => ":key1",
"value" => [
%{"tuple" => [":key2", "some_val"]},
@ -2380,7 +2391,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => ":key1",
"value" => %{"key" => "some_val"}
}
@ -2391,7 +2402,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
%{
"configs" => [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => ":key1",
"value" => %{"key" => "some_val"}
}
@ -2404,7 +2415,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
@ -2437,7 +2448,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => "pleroma",
"group" => ":pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
@ -2467,7 +2478,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "oban",
"group" => ":oban",
"key" => ":queues",
"value" => [
%{"tuple" => [":federator_incoming", 50]},
@ -2485,7 +2496,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => "oban",
"group" => ":oban",
"key" => ":queues",
"value" => [
%{"tuple" => [":federator_incoming", 50]},
@ -2504,7 +2515,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
test "delete part of settings by atom subkeys", %{conn: conn} do
config =
insert(:config,
key: "keyaa1",
key: ":keyaa1",
value: :erlang.term_to_binary(subkey1: "val1", subkey2: "val2", subkey3: "val3")
)
@ -2524,8 +2535,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
json_response(conn, 200) == %{
"configs" => [
%{
"group" => "pleroma",
"key" => "keyaa1",
"group" => ":pleroma",
"key" => ":keyaa1",
"value" => [%{"tuple" => [":subkey2", "val2"]}]
}
]
@ -3099,6 +3110,21 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert ReportNote |> Repo.all() |> length() == 1
end
end
test "GET /api/pleroma/admin/config/descriptions", %{conn: conn} do
admin = insert(:user, is_admin: true)
conn =
assign(conn, :user, admin)
|> get("/api/pleroma/admin/config/descriptions")
assert [child | _others] = json_response(conn, 200)
assert child["children"]
assert child["key"]
assert String.starts_with?(child["group"], ":")
assert child["description"]
end
end
# Needed for testing

View file

@ -91,14 +91,26 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
assert Config.from_binary(binary) == Pleroma.Bookmark
end
test "pleroma string" do
binary = Config.transform("Pleroma")
assert binary == :erlang.term_to_binary("Pleroma")
assert Config.from_binary(binary) == "Pleroma"
end
test "phoenix module" do
binary = Config.transform("Phoenix.Socket.V1.JSONSerializer")
assert binary == :erlang.term_to_binary(Phoenix.Socket.V1.JSONSerializer)
assert Config.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
end
test "tesla module" do
binary = Config.transform("Tesla.Adapter.Hackney")
assert binary == :erlang.term_to_binary(Tesla.Adapter.Hackney)
assert Config.from_binary(binary) == Tesla.Adapter.Hackney
end
test "sigil" do
binary = Config.transform("~r/comp[lL][aA][iI][nN]er/")
binary = Config.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/
end
@ -109,10 +121,10 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
assert Config.from_binary(binary) == ~r/https:\/\/example.com/
end
test "link sigil with u modifier" do
binary = Config.transform("~r/https:\/\/example.com/u")
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/u)
assert Config.from_binary(binary) == ~r/https:\/\/example.com/u
test "link sigil with um modifiers" do
binary = Config.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
end
test "link sigil with i modifier" do
@ -127,6 +139,12 @@ defmodule Pleroma.Web.AdminAPI.ConfigTest do
assert Config.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")
end
end
test "2 child tuple" do
binary = Config.transform(%{"tuple" => ["v1", ":v2"]})
assert binary == :erlang.term_to_binary({"v1", :v2})