From 0656816c77875d87d64d89e0e549f73104104cfb Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 6 Dec 2019 08:21:30 +0300 Subject: [PATCH] tests for setttings without an explicit key --- lib/mix/tasks/pleroma/config.ex | 6 +-- test/config/transfer_task_test.exs | 17 +++++-- test/tasks/config_test.exs | 8 ++++ .../admin_api/admin_api_controller_test.exs | 46 +++++++++++++++++-- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index bb126463c..cef02b864 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -76,10 +76,10 @@ defp load_and_create(group) do |> Application.get_all_env() |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end) |> 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_str} migrated.") + Mix.shell().info("settings for key #{key} migrated.") end) Mix.shell().info("settings for group :#{group} migrated.") diff --git a/test/config/transfer_task_test.exs b/test/config/transfer_task_test.exs index 4b3dd8bbd..d1314cf99 100644 --- a/test/config/transfer_task_test.exs +++ b/test/config/transfer_task_test.exs @@ -5,6 +5,8 @@ defmodule Pleroma.Config.TransferTaskTest do use Pleroma.DataCase + alias Pleroma.Web.AdminAPI.Config + clear_config([:instance, :dynamic_configuration]) do Pleroma.Config.put([:instance, :dynamic_configuration], true) end @@ -12,32 +14,41 @@ defmodule Pleroma.Config.TransferTaskTest do test "transfer config values from db to env" do refute Application.get_env(:pleroma, :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", key: ":test_key", value: [live: 2, com: 3] }) - Pleroma.Web.AdminAPI.Config.create(%{ + Config.create(%{ group: ":idna", key: ":test_key", value: [live: 15, com: 35] }) + Config.create(%{ + group: ":quack", + key: ":test_key", + value: [:test_value1, :test_value2] + }) + Pleroma.Config.TransferTask.start_link([]) 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(:quack, :test_key) == [:test_value1, :test_value2] on_exit(fn -> Application.delete_env(:pleroma, :test_key) Application.delete_env(:idna, :test_key) + Application.delete_env(:quack, :test_key) end) end test "non existing atom" do - Pleroma.Web.AdminAPI.Config.create(%{ + Config.create(%{ group: ":pleroma", key: ":undefined_atom_key", value: [live: 2, com: 3] diff --git a/test/tasks/config_test.exs b/test/tasks/config_test.exs index 055f678b9..dfe3904ca 100644 --- a/test/tasks/config_test.exs +++ b/test/tasks/config_test.exs @@ -24,19 +24,24 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do end 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) == [] 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"]) 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"}) 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 end test "settings are migrated to file and deleted from db" do @@ -59,6 +64,8 @@ test "settings are migrated to file and deleted from db" do value: [key: "value2", key2: [Repo]] }) + Config.create(%{group: ":quack", key: ":level", value: :info}) + Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", env, "-d"]) assert Repo.all(Config) == [] @@ -66,6 +73,7 @@ test "settings are migrated to file and deleted from db" do file = File.read!(config_file) assert file =~ "config :pleroma, :setting_first," assert file =~ "config :pleroma, :setting_second," + assert file =~ "config :quack, :level, :info" end test "load a settings with large values and pass to file", %{temp_file: temp_file} do diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index fd54504ac..1372edcab 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1970,8 +1970,6 @@ test "with settings in db", %{conn: conn} do setup %{conn: conn} do admin = insert(:user, is_admin: true) - temp_file = "config/test.exported_from_db.secret.exs" - on_exit(fn -> Application.delete_env(:pleroma, :key1) Application.delete_env(:pleroma, :key2) @@ -1981,7 +1979,7 @@ test "with settings in db", %{conn: conn} do Application.delete_env(:pleroma, :keyaa2) Application.delete_env(:pleroma, Pleroma.Web.Endpoint.NotReal) Application.delete_env(:pleroma, Pleroma.Captcha.NotReal) - :ok = File.rm(temp_file) + :ok = File.rm("config/test.exported_from_db.secret.exs") end) %{conn: assign(conn, :user, admin)} @@ -2101,6 +2099,48 @@ test "create new config setting in db", %{conn: conn} do assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []} 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 config1 = insert(:config, key: ":keyaa1") config2 = insert(:config, key: ":keyaa2")