X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=test%2Fweb%2Fadmin_api%2Fcontrollers%2Fconfig_controller_test.exs;h=4e897455f03bbc0b241fa04052898def8556639e;hb=8bacdc36806efd01a7897359ff0fd2c8e24730d2;hp=9bc6fd91c786b284a5fb3c631beb32d7109814c1;hpb=d1ee3527ef8062c34e222a1c7084c207b80fe4db;p=akkoma diff --git a/test/web/admin_api/controllers/config_controller_test.exs b/test/web/admin_api/controllers/config_controller_test.exs index 9bc6fd91c..4e897455f 100644 --- a/test/web/admin_api/controllers/config_controller_test.exs +++ b/test/web/admin_api/controllers/config_controller_test.exs @@ -30,7 +30,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do Config.put(:configurable_from_database, false) conn = get(conn, "/api/pleroma/admin/config") - assert json_response(conn, 400) == + assert json_response_and_validate_schema(conn, 400) == %{ "error" => "To use this endpoint you need to enable configuration from database." } @@ -40,7 +40,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do config1 = insert(:config) config2 = insert(:config) - conn = get(conn, "/api/pleroma/admin/config", %{"only_db" => true}) + conn = get(conn, "/api/pleroma/admin/config?only_db=true") %{ "configs" => [ @@ -55,19 +55,19 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "value" => _ } ] - } = json_response(conn, 200) + } = json_response_and_validate_schema(conn, 200) - assert key1 == config1.key - assert key2 == config2.key + assert key1 == inspect(config1.key) + assert key2 == inspect(config2.key) end test "db is added to settings that are in db", %{conn: conn} do - _config = insert(:config, key: ":instance", value: ConfigDB.to_binary(name: "Some name")) + _config = insert(:config, key: ":instance", value: [name: "Some name"]) %{"configs" => configs} = conn |> get("/api/pleroma/admin/config") - |> json_response(200) + |> json_response_and_validate_schema(200) [instance_config] = Enum.filter(configs, fn %{"group" => group, "key" => key} -> @@ -83,78 +83,92 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do config3 = insert(:config, - value: ConfigDB.to_binary(k1: :v1, k2: :v2) + value: [k1: :v1, k2: :v2] ) %{"configs" => configs} = conn |> get("/api/pleroma/admin/config") - |> json_response(200) + |> json_response_and_validate_schema(200) assert length(configs) > 3 + saved_configs = [config1, config2, config3] + keys = Enum.map(saved_configs, &inspect(&1.key)) + received_configs = Enum.filter(configs, fn %{"group" => group, "key" => key} -> - group == ":pleroma" and key in [config1.key, config2.key, config3.key] + group == ":pleroma" and key in keys end) assert length(received_configs) == 3 db_keys = config3.value - |> ConfigDB.from_binary() |> Keyword.keys() - |> ConfigDB.convert() + |> ConfigDB.to_json_types() + + keys = Enum.map(saved_configs -- [config3], &inspect(&1.key)) + + values = Enum.map(saved_configs, &ConfigDB.to_json_types(&1.value)) + + mapset_keys = MapSet.new(keys ++ db_keys) Enum.each(received_configs, fn %{"value" => value, "db" => db} -> - assert db in [[config1.key], [config2.key], db_keys] + db = MapSet.new(db) + assert MapSet.subset?(db, mapset_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) - ] + assert value in values end) end test "subkeys with full update right merge", %{conn: conn} do - config1 = - insert(:config, - key: ":emoji", - value: ConfigDB.to_binary(groups: [a: 1, b: 2], key: [a: 1]) - ) + insert(:config, + key: ":emoji", + value: [groups: [a: 1, b: 2], key: [a: 1]] + ) - config2 = - insert(:config, - key: ":assets", - value: ConfigDB.to_binary(mascots: [a: 1, b: 2], key: [a: 1]) - ) + insert(:config, + key: ":assets", + value: [mascots: [a: 1, b: 2], key: [a: 1]] + ) %{"configs" => configs} = conn |> get("/api/pleroma/admin/config") - |> json_response(200) + |> json_response_and_validate_schema(200) vals = Enum.filter(configs, fn %{"group" => group, "key" => key} -> - group == ":pleroma" and key in [config1.key, config2.key] + group == ":pleroma" and key in [":emoji", ":assets"] end) emoji = Enum.find(vals, fn %{"key" => key} -> key == ":emoji" end) assets = Enum.find(vals, fn %{"key" => key} -> key == ":assets" end) - emoji_val = ConfigDB.transform_with_out_binary(emoji["value"]) - assets_val = ConfigDB.transform_with_out_binary(assets["value"]) + emoji_val = ConfigDB.to_elixir_types(emoji["value"]) + assets_val = ConfigDB.to_elixir_types(assets["value"]) assert emoji_val[:groups] == [a: 1, b: 2] assert assets_val[:mascots] == [a: 1, b: 2] end + + test "with valid `admin_token` query parameter, skips OAuth scopes check" do + clear_config([:admin_token], "password123") + + build_conn() + |> get("/api/pleroma/admin/config?admin_token=password123") + |> json_response_and_validate_schema(200) + end end test "POST /api/pleroma/admin/config error", %{conn: conn} do - conn = post(conn, "/api/pleroma/admin/config", %{"configs" => []}) + conn = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{"configs" => []}) - assert json_response(conn, 400) == + assert json_response_and_validate_schema(conn, 400) == %{"error" => "To use this endpoint you need to enable configuration from database."} end @@ -185,7 +199,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do on_exit(fn -> Application.put_env(:ueberauth, Ueberauth, ueberauth) end) conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{group: ":pleroma", key: ":key1", value: "value1"}, %{ @@ -225,7 +241,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":pleroma", @@ -272,7 +288,8 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "value" => %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}, "db" => [":key5"] } - ] + ], + "need_reboot" => false } assert Application.get_env(:pleroma, :key1) == "value1" @@ -310,7 +327,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do end) conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ group: ":quack", @@ -330,7 +349,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":quack", @@ -350,7 +369,8 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "value" => "https://hooks.slack.com/services/KEY", "db" => [":webhook_url"] } - ] + ], + "need_reboot" => false } assert Application.get_env(:quack, :level) == :info @@ -359,16 +379,18 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do end test "saving config with partial update", %{conn: conn} do - config = insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: 2)) + insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: 2)) conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ - %{group: config.group, key: config.key, value: [%{"tuple" => [":key3", 3]}]} + %{group: ":pleroma", key: ":key1", value: [%{"tuple" => [":key3", 3]}]} ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":pleroma", @@ -380,7 +402,8 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ], "db" => [":key1", ":key2", ":key3"] } - ] + ], + "need_reboot" => false } end @@ -388,8 +411,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do chat = Config.get(:chat) on_exit(fn -> Config.put(:chat, chat) end) - assert post( - conn, + assert conn + |> put_req_header("content-type", "application/json") + |> post( "/api/pleroma/admin/config", %{ configs: [ @@ -397,7 +421,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] } ) - |> json_response(200) == %{ + |> json_response_and_validate_schema(200) == %{ "configs" => [ %{ "db" => [":enabled"], @@ -412,18 +436,19 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do configs = conn |> get("/api/pleroma/admin/config") - |> json_response(200) + |> json_response_and_validate_schema(200) assert configs["need_reboot"] capture_log(fn -> - assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{} + assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == + %{} end) =~ "pleroma restarted" configs = conn |> get("/api/pleroma/admin/config") - |> json_response(200) + |> json_response_and_validate_schema(200) assert configs["need_reboot"] == false end @@ -432,8 +457,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do chat = Config.get(:chat) on_exit(fn -> Config.put(:chat, chat) end) - assert post( - conn, + assert conn + |> put_req_header("content-type", "application/json") + |> post( "/api/pleroma/admin/config", %{ configs: [ @@ -441,7 +467,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] } ) - |> json_response(200) == %{ + |> json_response_and_validate_schema(200) == %{ "configs" => [ %{ "db" => [":enabled"], @@ -453,12 +479,14 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "need_reboot" => true } - assert post(conn, "/api/pleroma/admin/config", %{ + assert conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{group: ":pleroma", key: ":key1", value: [%{"tuple" => [":key3", 3]}]} ] }) - |> json_response(200) == %{ + |> json_response_and_validate_schema(200) == %{ "configs" => [ %{ "group" => ":pleroma", @@ -473,27 +501,29 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do } capture_log(fn -> - assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{} + assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == + %{} end) =~ "pleroma restarted" configs = conn |> get("/api/pleroma/admin/config") - |> json_response(200) + |> json_response_and_validate_schema(200) assert configs["need_reboot"] == false end test "saving config with nested merge", %{conn: conn} do - config = - insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: [k1: 1, k2: 2])) + insert(:config, key: :key1, value: [key1: 1, key2: [k1: 1, k2: 2]]) conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ - group: config.group, - key: config.key, + group: ":pleroma", + key: ":key1", value: [ %{"tuple" => [":key3", 3]}, %{ @@ -510,7 +540,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":pleroma", @@ -531,13 +561,16 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ], "db" => [":key1", ":key3", ":key2"] } - ] + ], + "need_reboot" => false } end test "saving special atoms", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ "configs" => [ %{ "group" => ":pleroma", @@ -554,7 +587,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":pleroma", @@ -569,7 +602,8 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ], "db" => [":ssl_options"] } - ] + ], + "need_reboot" => false } assert Application.get_env(:pleroma, :key1) == [ @@ -581,29 +615,30 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do backends = Application.get_env(:logger, :backends) on_exit(fn -> Application.put_env(:logger, :backends, backends) end) - config = - insert(:config, - group: ":logger", - key: ":backends", - value: :erlang.term_to_binary([]) - ) + insert(:config, + group: :logger, + key: :backends, + value: [] + ) Pleroma.Config.TransferTask.load_and_update_env([], false) assert Application.get_env(:logger, :backends) == [] conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ - group: config.group, - key: config.key, + group: ":logger", + key: ":backends", value: [":console"] } ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":logger", @@ -613,7 +648,8 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ], "db" => [":backends"] } - ] + ], + "need_reboot" => false } assert Application.get_env(:logger, :backends) == [ @@ -622,21 +658,22 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do 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) - ) + insert(:config, + group: :tesla, + key: :adapter, + value: Tesla.Adapter.Hackey + ) conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ - %{group: config.group, key: config.key, value: "Tesla.Adapter.Httpc"} + %{group: ":tesla", key: ":adapter", value: "Tesla.Adapter.Httpc"} ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":tesla", @@ -644,7 +681,8 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "value" => "Tesla.Adapter.Httpc", "db" => [":adapter"] } - ] + ], + "need_reboot" => false } end @@ -654,51 +692,55 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do token: token } do ueberauth = Application.get_env(:ueberauth, Ueberauth) - config1 = insert(:config, key: ":keyaa1") - config2 = insert(:config, key: ":keyaa2") + insert(:config, key: :keyaa1) + insert(:config, key: :keyaa2) config3 = insert(:config, - group: ":ueberauth", - key: "Ueberauth" + group: :ueberauth, + key: Ueberauth ) conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ - %{group: config1.group, key: config1.key, value: "another_value"}, - %{group: config2.group, key: config2.key, value: "another_value"} + %{group: ":pleroma", key: ":keyaa1", value: "another_value"}, + %{group: ":pleroma", key: ":keyaa2", value: "another_value"} ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":pleroma", - "key" => config1.key, + "key" => ":keyaa1", "value" => "another_value", "db" => [":keyaa1"] }, %{ "group" => ":pleroma", - "key" => config2.key, + "key" => ":keyaa2", "value" => "another_value", "db" => [":keyaa2"] } - ] + ], + "need_reboot" => false } assert Application.get_env(:pleroma, :keyaa1) == "another_value" assert Application.get_env(:pleroma, :keyaa2) == "another_value" - assert Application.get_env(:ueberauth, Ueberauth) == ConfigDB.from_binary(config3.value) + assert Application.get_env(:ueberauth, Ueberauth) == config3.value conn = build_conn() |> assign(:user, admin) |> assign(:token, token) + |> put_req_header("content-type", "application/json") |> post("/api/pleroma/admin/config", %{ configs: [ - %{group: config2.group, key: config2.key, delete: true}, + %{group: ":pleroma", key: ":keyaa2", delete: true}, %{ group: ":ueberauth", key: "Ueberauth", @@ -707,8 +749,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == %{ - "configs" => [] + assert json_response_and_validate_schema(conn, 200) == %{ + "configs" => [], + "need_reboot" => false } assert Application.get_env(:ueberauth, Ueberauth) == ueberauth @@ -717,7 +760,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do test "common config example", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ "group" => ":pleroma", @@ -741,7 +786,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do assert Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma" - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":pleroma", @@ -773,13 +818,16 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ":name" ] } - ] + ], + "need_reboot" => false } end test "tuples with more than two values", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ "group" => ":pleroma", @@ -843,7 +891,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":pleroma", @@ -905,13 +953,16 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ], "db" => [":http"] } - ] + ], + "need_reboot" => false } end test "settings with nesting map", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ "group" => ":pleroma", @@ -940,7 +991,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ @@ -968,13 +1019,16 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ], "db" => [":key2", ":key3"] } - ] + ], + "need_reboot" => false } end test "value as map", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ "group" => ":pleroma", @@ -984,7 +1038,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ @@ -993,13 +1047,16 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "value" => %{"key" => "some_val"}, "db" => [":key1"] } - ] + ], + "need_reboot" => false } end test "queues key as atom", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ "group" => ":oban", @@ -1017,7 +1074,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":oban", @@ -1041,30 +1098,32 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do ":background" ] } - ] + ], + "need_reboot" => false } end test "delete part of settings by atom subkeys", %{conn: conn} do - config = - insert(:config, - key: ":keyaa1", - value: :erlang.term_to_binary(subkey1: "val1", subkey2: "val2", subkey3: "val3") - ) + insert(:config, + key: :keyaa1, + value: [subkey1: "val1", subkey2: "val2", subkey3: "val3"] + ) conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ - group: config.group, - key: config.key, + group: ":pleroma", + key: ":keyaa1", subkeys: [":subkey1", ":subkey3"], delete: true } ] }) - assert json_response(conn, 200) == %{ + assert json_response_and_validate_schema(conn, 200) == %{ "configs" => [ %{ "group" => ":pleroma", @@ -1072,13 +1131,16 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "value" => [%{"tuple" => [":subkey2", "val2"]}], "db" => [":subkey2"] } - ] + ], + "need_reboot" => false } end test "proxy tuple localhost", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ group: ":pleroma", @@ -1099,7 +1161,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "db" => db } ] - } = json_response(conn, 200) + } = json_response_and_validate_schema(conn, 200) assert %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]} in value assert ":proxy_url" in db @@ -1107,7 +1169,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do test "proxy tuple domain", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ group: ":pleroma", @@ -1128,7 +1192,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "db" => db } ] - } = json_response(conn, 200) + } = json_response_and_validate_schema(conn, 200) assert %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]} in value assert ":proxy_url" in db @@ -1136,7 +1200,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do test "proxy tuple ip", %{conn: conn} do conn = - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{ group: ":pleroma", @@ -1157,7 +1223,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "db" => db } ] - } = json_response(conn, 200) + } = json_response_and_validate_schema(conn, 200) assert %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]} in value assert ":proxy_url" in db @@ -1172,7 +1238,9 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do {:not_real} ]) - post(conn, "/api/pleroma/admin/config", %{ + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ configs: [ %{group: ":pleroma", key: ":key1", value: "value1"}, %{group: ":pleroma", key: ":key2", value: "value2"}, @@ -1190,6 +1258,159 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do assert Application.get_env(:pleroma, Pleroma.Captcha.NotReal) == "value5" assert Application.get_env(:not_real, :anything) == "value6" end + + test "args for Pleroma.Upload.Filter.Mogrify with custom tuples", %{conn: conn} do + clear_config(Pleroma.Upload.Filter.Mogrify) + + assert conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ + configs: [ + %{ + group: ":pleroma", + key: "Pleroma.Upload.Filter.Mogrify", + value: [ + %{"tuple" => [":args", ["auto-orient", "strip"]]} + ] + } + ] + }) + |> json_response_and_validate_schema(200) == %{ + "configs" => [ + %{ + "group" => ":pleroma", + "key" => "Pleroma.Upload.Filter.Mogrify", + "value" => [ + %{"tuple" => [":args", ["auto-orient", "strip"]]} + ], + "db" => [":args"] + } + ], + "need_reboot" => false + } + + assert Config.get(Pleroma.Upload.Filter.Mogrify) == [args: ["auto-orient", "strip"]] + + assert conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{ + configs: [ + %{ + group: ":pleroma", + key: "Pleroma.Upload.Filter.Mogrify", + value: [ + %{ + "tuple" => [ + ":args", + [ + "auto-orient", + "strip", + "{\"implode\", \"1\"}", + "{\"resize\", \"3840x1080>\"}" + ] + ] + } + ] + } + ] + }) + |> json_response(200) == %{ + "configs" => [ + %{ + "group" => ":pleroma", + "key" => "Pleroma.Upload.Filter.Mogrify", + "value" => [ + %{ + "tuple" => [ + ":args", + [ + "auto-orient", + "strip", + "{\"implode\", \"1\"}", + "{\"resize\", \"3840x1080>\"}" + ] + ] + } + ], + "db" => [":args"] + } + ], + "need_reboot" => false + } + + assert Config.get(Pleroma.Upload.Filter.Mogrify) == [ + args: ["auto-orient", "strip", {"implode", "1"}, {"resize", "3840x1080>"}] + ] + end + + test "enables the welcome messages", %{conn: conn} do + clear_config([:welcome]) + + params = %{ + "group" => ":pleroma", + "key" => ":welcome", + "value" => [ + %{ + "tuple" => [ + ":direct_message", + [ + %{"tuple" => [":enabled", true]}, + %{"tuple" => [":message", "Welcome to Pleroma!"]}, + %{"tuple" => [":sender_nickname", "pleroma"]} + ] + ] + }, + %{ + "tuple" => [ + ":chat_message", + [ + %{"tuple" => [":enabled", true]}, + %{"tuple" => [":message", "Welcome to Pleroma!"]}, + %{"tuple" => [":sender_nickname", "pleroma"]} + ] + ] + }, + %{ + "tuple" => [ + ":email", + [ + %{"tuple" => [":enabled", true]}, + %{"tuple" => [":sender", %{"tuple" => ["pleroma@dev.dev", "Pleroma"]}]}, + %{"tuple" => [":subject", "Welcome to <%= instance_name %>!"]}, + %{"tuple" => [":html", "Welcome to <%= instance_name %>!"]}, + %{"tuple" => [":text", "Welcome to <%= instance_name %>!"]} + ] + ] + } + ] + } + + refute Pleroma.User.WelcomeEmail.enabled?() + refute Pleroma.User.WelcomeMessage.enabled?() + refute Pleroma.User.WelcomeChatMessage.enabled?() + + res = + assert conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/config", %{"configs" => [params]}) + |> json_response_and_validate_schema(200) + + assert Pleroma.User.WelcomeEmail.enabled?() + assert Pleroma.User.WelcomeMessage.enabled?() + assert Pleroma.User.WelcomeChatMessage.enabled?() + + assert res == %{ + "configs" => [ + %{ + "db" => [":direct_message", ":chat_message", ":email"], + "group" => ":pleroma", + "key" => ":welcome", + "value" => params["value"] + } + ], + "need_reboot" => false + } + end end describe "GET /api/pleroma/admin/config/descriptions" do @@ -1200,7 +1421,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do assign(conn, :user, admin) |> get("/api/pleroma/admin/config/descriptions") - assert [child | _others] = json_response(conn, 200) + assert [child | _others] = json_response_and_validate_schema(conn, 200) assert child["children"] assert child["key"] @@ -1222,7 +1443,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do assign(conn, :user, admin) |> get("/api/pleroma/admin/config/descriptions") - children = json_response(conn, 200) + children = json_response_and_validate_schema(conn, 200) assert length(children) == 4