admin api configure changes

This commit is contained in:
Alexander Strizhakov 2019-07-11 13:02:13 +00:00 committed by kaniini
parent e4e3fd7e55
commit 846ad9a463
7 changed files with 691 additions and 300 deletions

View File

@ -25,10 +25,15 @@ Configuration: `federation_incoming_replies_max_depth` option
- Admin API: Return users' tags when querying reports - Admin API: Return users' tags when querying reports
- Admin API: Return avatar and display name when querying users - Admin API: Return avatar and display name when querying users
- Admin API: Allow querying user by ID - Admin API: Allow querying user by ID
- Admin API: Added support for `tuples`.
- Added synchronization of following/followers counters for external users - Added synchronization of following/followers counters for external users
- Configuration: `enabled` option for `Pleroma.Emails.Mailer`, defaulting to `false`. - Configuration: `enabled` option for `Pleroma.Emails.Mailer`, defaulting to `false`.
- Mastodon API: Add support for categories for custom emojis by reusing the group feature. <https://github.com/tootsuite/mastodon/pull/11196> - Mastodon API: Add support for categories for custom emojis by reusing the group feature. <https://github.com/tootsuite/mastodon/pull/11196>
### Changed
- Configuration: Filter.AnonymizeFilename added ability to retain file extension with custom text
- Admin API: changed json structure for saving config settings.
## [1.0.0] - 2019-06-29 ## [1.0.0] - 2019-06-29
### Security ### Security
- Mastodon API: Fix display names not being sanitized - Mastodon API: Fix display names not being sanitized

View File

@ -573,7 +573,7 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
configs: [ configs: [
{ {
"group": string, "group": string,
"key": string, "key": string or string with leading `:` for atoms,
"value": string or {} or [] or {"tuple": []} "value": string or {} or [] or {"tuple": []}
} }
] ]
@ -583,10 +583,11 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
## `/api/pleroma/admin/config` ## `/api/pleroma/admin/config`
### Update config settings ### Update config settings
Module name can be passed as string, which starts with `Pleroma`, e.g. `"Pleroma.Upload"`. Module name can be passed as string, which starts with `Pleroma`, e.g. `"Pleroma.Upload"`.
Atom or boolean value can be passed with `:` in the beginning, e.g. `":true"`, `":upload"`. For keys it is not needed. Atom keys and values can be passed with `:` in the beginning, e.g. `":upload"`.
Integer with `i:`, e.g. `"i:150"`. Tuples can be passed as `{"tuple": ["first_val", Pleroma.Module, []]}`.
Tuple with more than 2 values with `{"tuple": ["first_val", Pleroma.Module, []]}`.
`{"tuple": ["some_string", "Pleroma.Some.Module", []]}` will be converted to `{"some_string", Pleroma.Some.Module, []}`. `{"tuple": ["some_string", "Pleroma.Some.Module", []]}` will be converted to `{"some_string", Pleroma.Some.Module, []}`.
Keywords can be passed as lists with 2 child tuples, e.g.
`[{"tuple": ["first_val", Pleroma.Module]}, {"tuple": ["second_val", true]}]`.
Compile time settings (need instance reboot): Compile time settings (need instance reboot):
- all settings by this keys: - all settings by this keys:
@ -603,7 +604,7 @@ Compile time settings (need instance reboot):
- Params: - Params:
- `configs` => [ - `configs` => [
- `group` (string) - `group` (string)
- `key` (string) - `key` (string or string with leading `:` for atoms)
- `value` (string, [], {} or {"tuple": []}) - `value` (string, [], {} or {"tuple": []})
- `delete` = true (optional, if parameter must be deleted) - `delete` = true (optional, if parameter must be deleted)
] ]
@ -616,24 +617,25 @@ Compile time settings (need instance reboot):
{ {
"group": "pleroma", "group": "pleroma",
"key": "Pleroma.Upload", "key": "Pleroma.Upload",
"value": { "value": [
"uploader": "Pleroma.Uploaders.Local", {"tuple": [":uploader", "Pleroma.Uploaders.Local"]},
"filters": ["Pleroma.Upload.Filter.Dedupe"], {"tuple": [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
"link_name": ":true", {"tuple": [":link_name", true]},
"proxy_remote": ":false", {"tuple": [":proxy_remote", false]},
"proxy_opts": { {"tuple": [":proxy_opts", [
"redirect_on_failure": ":false", {"tuple": [":redirect_on_failure", false]},
"max_body_length": "i:1048576", {"tuple": [":max_body_length", 1048576]},
"http": { {"tuple": [":http": [
"follow_redirect": ":true", {"tuple": [":follow_redirect", true]},
"pool": ":upload" {"tuple": [":pool", ":upload"]},
} ]]}
}, ]
"dispatch": { ]},
{"tuple": [":dispatch", {
"tuple": ["/api/v1/streaming", "Pleroma.Web.MastodonAPI.WebsocketHandler", []] "tuple": ["/api/v1/streaming", "Pleroma.Web.MastodonAPI.WebsocketHandler", []]
} }]}
} ]
} }
] ]
} }
@ -644,7 +646,7 @@ Compile time settings (need instance reboot):
configs: [ configs: [
{ {
"group": string, "group": string,
"key": string, "key": string or string with leading `:` for atoms,
"value": string or {} or [] or {"tuple": []} "value": string or {} or [] or {"tuple": []}
} }
] ]

View File

@ -371,13 +371,13 @@ def config_update(conn, %{"configs" => configs}) do
if Pleroma.Config.get([:instance, :dynamic_configuration]) do if Pleroma.Config.get([:instance, :dynamic_configuration]) do
updated = updated =
Enum.map(configs, fn Enum.map(configs, fn
%{"group" => group, "key" => key, "value" => value} ->
{:ok, config} = Config.update_or_create(%{group: group, key: key, value: value})
config
%{"group" => group, "key" => key, "delete" => "true"} -> %{"group" => group, "key" => key, "delete" => "true"} ->
{:ok, _} = Config.delete(%{group: group, key: key}) {:ok, _} = Config.delete(%{group: group, key: key})
nil nil
%{"group" => group, "key" => key, "value" => value} ->
{:ok, config} = Config.update_or_create(%{group: group, key: key, value: value})
config
end) end)
|> Enum.reject(&is_nil(&1)) |> Enum.reject(&is_nil(&1))

View File

@ -67,99 +67,86 @@ def delete(params) do
end end
@spec from_binary(binary()) :: term() @spec from_binary(binary()) :: term()
def from_binary(value), do: :erlang.binary_to_term(value) def from_binary(binary), do: :erlang.binary_to_term(binary)
@spec from_binary_to_map(binary()) :: any() @spec from_binary_with_convert(binary()) :: any()
def from_binary_to_map(binary) do def from_binary_with_convert(binary) do
from_binary(binary) from_binary(binary)
|> do_convert() |> do_convert()
end end
defp do_convert([{k, v}] = value) when is_list(value) and length(value) == 1, defp do_convert(entity) when is_list(entity) do
do: %{k => do_convert(v)} for v <- entity, into: [], do: do_convert(v)
end
defp do_convert(values) when is_list(values), do: for(val <- values, do: do_convert(val)) defp do_convert(entity) when is_map(entity) do
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
end
defp do_convert({k, v} = value) when is_tuple(value), defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
do: %{k => do_convert(v)}
defp do_convert(value) when is_tuple(value), do: %{"tuple" => do_convert(Tuple.to_list(value))} defp do_convert(entity) when is_tuple(entity),
do: %{"tuple" => do_convert(Tuple.to_list(entity))}
defp do_convert(value) when is_binary(value) or is_map(value) or is_number(value), do: value defp do_convert(entity) when is_boolean(entity) or is_number(entity) or is_nil(entity),
do: entity
defp do_convert(value) when is_atom(value) do defp do_convert(entity) when is_atom(entity) do
string = to_string(value) string = to_string(entity)
if String.starts_with?(string, "Elixir."), if String.starts_with?(string, "Elixir."),
do: String.trim_leading(string, "Elixir."), do: do_convert(string),
else: value else: ":" <> string
end end
defp do_convert("Elixir." <> module_name), do: module_name
defp do_convert(entity) when is_binary(entity), do: entity
@spec transform(any()) :: binary() @spec transform(any()) :: binary()
def transform(%{"tuple" => _} = entity), do: :erlang.term_to_binary(do_transform(entity)) def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity) do
:erlang.term_to_binary(do_transform(entity))
def transform(entity) when is_map(entity) do
tuples =
for {k, v} <- entity,
into: [],
do: {if(is_atom(k), do: k, else: String.to_atom(k)), do_transform(v)}
Enum.reject(tuples, fn {_k, v} -> is_nil(v) end)
|> Enum.sort()
|> :erlang.term_to_binary()
end
def transform(entity) when is_list(entity) do
list = Enum.map(entity, &do_transform(&1))
:erlang.term_to_binary(list)
end end
def transform(entity), do: :erlang.term_to_binary(entity) def transform(entity), do: :erlang.term_to_binary(entity)
defp do_transform(%Regex{} = value) when is_map(value), do: value defp do_transform(%Regex{} = entity) when is_map(entity), do: entity
defp do_transform(%{"tuple" => [k, values] = entity}) when length(entity) == 2 do defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
{do_transform(k), do_transform(values)} cleaned_string = String.replace(entity, ~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
{dispatch_settings, []} = Code.eval_string(cleaned_string, [], requires: [], macros: [])
{:dispatch, [dispatch_settings]}
end end
defp do_transform(%{"tuple" => values}) do defp do_transform(%{"tuple" => entity}) do
Enum.reduce(values, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end) Enum.reduce(entity, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
end end
defp do_transform(value) when is_map(value) do defp do_transform(entity) when is_map(entity) do
values = for {key, val} <- value, into: [], do: {String.to_atom(key), do_transform(val)} for {k, v} <- entity, into: %{}, do: {do_transform(k), do_transform(v)}
Enum.sort(values)
end end
defp do_transform(value) when is_list(value) do defp do_transform(entity) when is_list(entity) do
Enum.map(value, &do_transform(&1)) for v <- entity, into: [], do: do_transform(v)
end end
defp do_transform(entity) when is_list(entity) and length(entity) == 1, do: hd(entity) defp do_transform(entity) when is_binary(entity) do
String.trim(entity)
defp do_transform(value) when is_binary(value) do
String.trim(value)
|> do_transform_string() |> do_transform_string()
end end
defp do_transform(value), do: value defp do_transform(entity), do: entity
defp do_transform_string(value) when byte_size(value) == 0, do: nil defp do_transform_string("~r/" <> pattern) do
pattern = String.trim_trailing(pattern, "/")
~r/#{pattern}/
end
defp do_transform_string(":" <> atom), do: String.to_atom(atom)
defp do_transform_string(value) do defp do_transform_string(value) do
cond do if String.starts_with?(value, "Pleroma") or String.starts_with?(value, "Phoenix"),
String.starts_with?(value, "Pleroma") or String.starts_with?(value, "Phoenix") -> do: String.to_existing_atom("Elixir." <> value),
String.to_existing_atom("Elixir." <> value) else: value
String.starts_with?(value, ":") ->
String.replace(value, ":", "") |> String.to_existing_atom()
String.starts_with?(value, "i:") ->
String.replace(value, "i:", "") |> String.to_integer()
true ->
value
end
end end
end end

View File

@ -15,7 +15,7 @@ def render("show.json", %{config: config}) do
%{ %{
key: config.key, key: config.key,
group: config.group, group: config.group,
value: Pleroma.Web.AdminAPI.Config.from_binary_to_map(config.value) value: Pleroma.Web.AdminAPI.Config.from_binary_with_convert(config.value)
} }
end end
end end

View File

@ -1407,14 +1407,19 @@ test "create new config setting in db", %{conn: conn} do
post(conn, "/api/pleroma/admin/config", %{ post(conn, "/api/pleroma/admin/config", %{
configs: [ configs: [
%{group: "pleroma", key: "key1", value: "value1"}, %{group: "pleroma", key: "key1", value: "value1"},
%{
group: "ueberauth",
key: "Ueberauth.Strategy.Twitter.OAuth",
value: [%{"tuple" => [":consumer_secret", "aaaa"]}]
},
%{ %{
group: "pleroma", group: "pleroma",
key: "key2", key: "key2",
value: %{ value: %{
"nested_1" => "nested_value1", ":nested_1" => "nested_value1",
"nested_2" => [ ":nested_2" => [
%{"nested_22" => "nested_value222"}, %{":nested_22" => "nested_value222"},
%{"nested_33" => %{"nested_44" => "nested_444"}} %{":nested_33" => %{":nested_44" => "nested_444"}}
] ]
} }
}, },
@ -1423,13 +1428,13 @@ test "create new config setting in db", %{conn: conn} do
key: "key3", key: "key3",
value: [ value: [
%{"nested_3" => ":nested_3", "nested_33" => "nested_33"}, %{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
%{"nested_4" => ":true"} %{"nested_4" => true}
] ]
}, },
%{ %{
group: "pleroma", group: "pleroma",
key: "key4", key: "key4",
value: %{"nested_5" => ":upload", "endpoint" => "https://example.com"} value: %{":nested_5" => ":upload", "endpoint" => "https://example.com"}
}, },
%{ %{
group: "idna", group: "idna",
@ -1446,31 +1451,34 @@ test "create new config setting in db", %{conn: conn} do
"key" => "key1", "key" => "key1",
"value" => "value1" "value" => "value1"
}, },
%{
"group" => "ueberauth",
"key" => "Ueberauth.Strategy.Twitter.OAuth",
"value" => [%{"tuple" => [":consumer_secret", "aaaa"]}]
},
%{ %{
"group" => "pleroma", "group" => "pleroma",
"key" => "key2", "key" => "key2",
"value" => [ "value" => %{
%{"nested_1" => "nested_value1"}, ":nested_1" => "nested_value1",
%{ ":nested_2" => [
"nested_2" => [ %{":nested_22" => "nested_value222"},
%{"nested_22" => "nested_value222"}, %{":nested_33" => %{":nested_44" => "nested_444"}}
%{"nested_33" => %{"nested_44" => "nested_444"}} ]
] }
}
]
}, },
%{ %{
"group" => "pleroma", "group" => "pleroma",
"key" => "key3", "key" => "key3",
"value" => [ "value" => [
[%{"nested_3" => "nested_3"}, %{"nested_33" => "nested_33"}], %{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
%{"nested_4" => true} %{"nested_4" => true}
] ]
}, },
%{ %{
"group" => "pleroma", "group" => "pleroma",
"key" => "key4", "key" => "key4",
"value" => [%{"endpoint" => "https://example.com"}, %{"nested_5" => "upload"}] "value" => %{"endpoint" => "https://example.com", ":nested_5" => ":upload"}
}, },
%{ %{
"group" => "idna", "group" => "idna",
@ -1482,23 +1490,23 @@ test "create new config setting in db", %{conn: conn} do
assert Application.get_env(:pleroma, :key1) == "value1" assert Application.get_env(:pleroma, :key1) == "value1"
assert Application.get_env(:pleroma, :key2) == [ assert Application.get_env(:pleroma, :key2) == %{
nested_1: "nested_value1", nested_1: "nested_value1",
nested_2: [ nested_2: [
[nested_22: "nested_value222"], %{nested_22: "nested_value222"},
[nested_33: [nested_44: "nested_444"]] %{nested_33: %{nested_44: "nested_444"}}
] ]
] }
assert Application.get_env(:pleroma, :key3) == [ assert Application.get_env(:pleroma, :key3) == [
[nested_3: :nested_3, nested_33: "nested_33"], %{"nested_3" => :nested_3, "nested_33" => "nested_33"},
[nested_4: true] %{"nested_4" => true}
] ]
assert Application.get_env(:pleroma, :key4) == [ assert Application.get_env(:pleroma, :key4) == %{
endpoint: "https://example.com", "endpoint" => "https://example.com",
nested_5: :upload nested_5: :upload
] }
assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []} assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []}
end end
@ -1507,11 +1515,22 @@ 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")
insert(:config,
group: "ueberauth",
key: "Ueberauth.Strategy.Microsoft.OAuth",
value: :erlang.term_to_binary([])
)
conn = conn =
post(conn, "/api/pleroma/admin/config", %{ post(conn, "/api/pleroma/admin/config", %{
configs: [ configs: [
%{group: config1.group, key: config1.key, value: "another_value"}, %{group: config1.group, key: config1.key, value: "another_value"},
%{group: config2.group, key: config2.key, delete: "true"} %{group: config2.group, key: config2.key, delete: "true"},
%{
group: "ueberauth",
key: "Ueberauth.Strategy.Microsoft.OAuth",
delete: "true"
}
] ]
}) })
@ -1536,11 +1555,13 @@ test "common config example", %{conn: conn} do
%{ %{
"group" => "pleroma", "group" => "pleroma",
"key" => "Pleroma.Captcha.NotReal", "key" => "Pleroma.Captcha.NotReal",
"value" => %{ "value" => [
"enabled" => ":false", %{"tuple" => [":enabled", false]},
"method" => "Pleroma.Captcha.Kocaptcha", %{"tuple" => [":method", "Pleroma.Captcha.Kocaptcha"]},
"seconds_valid" => "i:60" %{"tuple" => [":seconds_valid", 60]},
} %{"tuple" => [":path", ""]},
%{"tuple" => [":key1", nil]}
]
} }
] ]
}) })
@ -1551,9 +1572,11 @@ test "common config example", %{conn: conn} do
"group" => "pleroma", "group" => "pleroma",
"key" => "Pleroma.Captcha.NotReal", "key" => "Pleroma.Captcha.NotReal",
"value" => [ "value" => [
%{"enabled" => false}, %{"tuple" => [":enabled", false]},
%{"method" => "Pleroma.Captcha.Kocaptcha"}, %{"tuple" => [":method", "Pleroma.Captcha.Kocaptcha"]},
%{"seconds_valid" => 60} %{"tuple" => [":seconds_valid", 60]},
%{"tuple" => [":path", ""]},
%{"tuple" => [":key1", nil]}
] ]
} }
] ]
@ -1569,51 +1592,57 @@ test "tuples with more than two values", %{conn: conn} do
"key" => "Pleroma.Web.Endpoint.NotReal", "key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [ "value" => [
%{ %{
"http" => %{ "tuple" => [
"dispatch" => [ ":http",
[
%{ %{
"tuple" => [ "tuple" => [
":_", ":key2",
[ [
%{
"tuple" => [
"/api/v1/streaming",
"Pleroma.Web.MastodonAPI.WebsocketHandler",
[]
]
},
%{
"tuple" => [
"/websocket",
"Phoenix.Endpoint.CowboyWebSocket",
%{
"tuple" => [
"Phoenix.Transports.WebSocket",
%{
"tuple" => [
"Pleroma.Web.Endpoint",
"Pleroma.Web.UserSocket",
[]
]
}
]
}
]
},
%{ %{
"tuple" => [ "tuple" => [
":_", ":_",
"Phoenix.Endpoint.Cowboy2Handler", [
%{ %{
"tuple" => ["Pleroma.Web.Endpoint", []] "tuple" => [
} "/api/v1/streaming",
"Pleroma.Web.MastodonAPI.WebsocketHandler",
[]
]
},
%{
"tuple" => [
"/websocket",
"Phoenix.Endpoint.CowboyWebSocket",
%{
"tuple" => [
"Phoenix.Transports.WebSocket",
%{
"tuple" => [
"Pleroma.Web.Endpoint",
"Pleroma.Web.UserSocket",
[]
]
}
]
}
]
},
%{
"tuple" => [
":_",
"Phoenix.Endpoint.Cowboy2Handler",
%{"tuple" => ["Pleroma.Web.Endpoint", []]}
]
}
]
] ]
} }
] ]
] ]
} }
] ]
} ]
} }
] ]
} }
@ -1627,41 +1656,206 @@ test "tuples with more than two values", %{conn: conn} do
"key" => "Pleroma.Web.Endpoint.NotReal", "key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [ "value" => [
%{ %{
"http" => %{ "tuple" => [
"dispatch" => %{ ":http",
"_" => [ [
%{ %{
"tuple" => [ "tuple" => [
"/api/v1/streaming", ":key2",
"Pleroma.Web.MastodonAPI.WebsocketHandler", [
[]
]
},
%{
"tuple" => [
"/websocket",
"Phoenix.Endpoint.CowboyWebSocket",
%{ %{
"Elixir.Phoenix.Transports.WebSocket" => %{ "tuple" => [
"tuple" => [ ":_",
"Pleroma.Web.Endpoint", [
"Pleroma.Web.UserSocket", %{
[] "tuple" => [
"/api/v1/streaming",
"Pleroma.Web.MastodonAPI.WebsocketHandler",
[]
]
},
%{
"tuple" => [
"/websocket",
"Phoenix.Endpoint.CowboyWebSocket",
%{
"tuple" => [
"Phoenix.Transports.WebSocket",
%{
"tuple" => [
"Pleroma.Web.Endpoint",
"Pleroma.Web.UserSocket",
[]
]
}
]
}
]
},
%{
"tuple" => [
":_",
"Phoenix.Endpoint.Cowboy2Handler",
%{"tuple" => ["Pleroma.Web.Endpoint", []]}
]
}
] ]
} ]
} }
] ]
}, ]
%{ }
"tuple" => [ ]
"_", ]
"Phoenix.Endpoint.Cowboy2Handler", }
%{"Elixir.Pleroma.Web.Endpoint" => []} ]
] }
]
}
end
test "settings with nesting map", %{conn: conn} do
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "pleroma",
"key" => "key1",
"value" => [
%{"tuple" => [":key2", "some_val"]},
%{
"tuple" => [
":key3",
%{
":max_options" => 20,
":max_option_chars" => 200,
":min_expiration" => 0,
":max_expiration" => 31_536_000,
"nested" => %{
":max_options" => 20,
":max_option_chars" => 200,
":min_expiration" => 0,
":max_expiration" => 31_536_000
}
}
]
}
]
}
]
})
assert json_response(conn, 200) ==
%{
"configs" => [
%{
"group" => "pleroma",
"key" => "key1",
"value" => [
%{"tuple" => [":key2", "some_val"]},
%{
"tuple" => [
":key3",
%{
":max_expiration" => 31_536_000,
":max_option_chars" => 200,
":max_options" => 20,
":min_expiration" => 0,
"nested" => %{
":max_expiration" => 31_536_000,
":max_option_chars" => 200,
":max_options" => 20,
":min_expiration" => 0
} }
] }
} ]
} }
]
}
]
}
end
test "value as map", %{conn: conn} do
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "pleroma",
"key" => "key1",
"value" => %{"key" => "some_val"}
}
]
})
assert json_response(conn, 200) ==
%{
"configs" => [
%{
"group" => "pleroma",
"key" => "key1",
"value" => %{"key" => "some_val"}
}
]
}
end
test "dispatch setting", %{conn: conn} do
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => "pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
"tuple" => [
":http",
[
%{"tuple" => [":ip", %{"tuple" => [127, 0, 0, 1]}]},
%{"tuple" => [":dispatch", ["{:_,
[
{\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
{Phoenix.Transports.WebSocket,
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
]}"]]}
]
]
}
]
}
]
})
dispatch_string =
"{:_, [{\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []}, " <>
"{\"/websocket\", Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket, " <>
"{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}}, " <>
"{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}]}"
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => "pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
"tuple" => [
":http",
[
%{"tuple" => [":ip", %{"tuple" => [127, 0, 0, 1]}]},
%{
"tuple" => [
":dispatch",
[
dispatch_string
]
]
}
]
]
} }
] ]
} }

View File

@ -61,117 +61,306 @@ test "string" do
assert Config.from_binary(binary) == "value as string" assert Config.from_binary(binary) == "value as string"
end end
test "boolean" do
binary = Config.transform(false)
assert binary == :erlang.term_to_binary(false)
assert Config.from_binary(binary) == false
end
test "nil" do
binary = Config.transform(nil)
assert binary == :erlang.term_to_binary(nil)
assert Config.from_binary(binary) == nil
end
test "integer" do
binary = Config.transform(150)
assert binary == :erlang.term_to_binary(150)
assert Config.from_binary(binary) == 150
end
test "atom" do
binary = Config.transform(":atom")
assert binary == :erlang.term_to_binary(:atom)
assert Config.from_binary(binary) == :atom
end
test "pleroma module" do
binary = Config.transform("Pleroma.Bookmark")
assert binary == :erlang.term_to_binary(Pleroma.Bookmark)
assert Config.from_binary(binary) == Pleroma.Bookmark
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 "sigil" do
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
test "2 child tuple" do
binary = Config.transform(%{"tuple" => ["v1", ":v2"]})
assert binary == :erlang.term_to_binary({"v1", :v2})
assert Config.from_binary(binary) == {"v1", :v2}
end
test "tuple with n childs" do
binary =
Config.transform(%{
"tuple" => [
"v1",
":v2",
"Pleroma.Bookmark",
150,
false,
"Phoenix.Socket.V1.JSONSerializer"
]
})
assert binary ==
:erlang.term_to_binary(
{"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
)
assert Config.from_binary(binary) ==
{"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
end
test "tuple with dispatch key" do
binary = Config.transform(%{"tuple" => [":dispatch", ["{:_,
[
{\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
{Phoenix.Transports.WebSocket,
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
]}"]]})
assert binary ==
:erlang.term_to_binary(
{:dispatch,
[
{:_,
[
{"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{"/websocket", Phoenix.Endpoint.CowboyWebSocket,
{Phoenix.Transports.WebSocket,
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
]}
]}
)
assert Config.from_binary(binary) ==
{:dispatch,
[
{:_,
[
{"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{"/websocket", Phoenix.Endpoint.CowboyWebSocket,
{Phoenix.Transports.WebSocket,
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
]}
]}
end
test "map with string key" do
binary = Config.transform(%{"key" => "value"})
assert binary == :erlang.term_to_binary(%{"key" => "value"})
assert Config.from_binary(binary) == %{"key" => "value"}
end
test "map with atom key" do
binary = Config.transform(%{":key" => "value"})
assert binary == :erlang.term_to_binary(%{key: "value"})
assert Config.from_binary(binary) == %{key: "value"}
end
test "list of strings" do
binary = Config.transform(["v1", "v2", "v3"])
assert binary == :erlang.term_to_binary(["v1", "v2", "v3"])
assert Config.from_binary(binary) == ["v1", "v2", "v3"]
end
test "list of modules" do test "list of modules" do
binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"]) binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"])
assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity]) assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity] assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
end end
test "list of strings" do test "list of atoms" do
binary = Config.transform(["string1", "string2"]) binary = Config.transform([":v1", ":v2", ":v3"])
assert binary == :erlang.term_to_binary(["string1", "string2"]) assert binary == :erlang.term_to_binary([:v1, :v2, :v3])
assert Config.from_binary(binary) == ["string1", "string2"] assert Config.from_binary(binary) == [:v1, :v2, :v3]
end end
test "map" do test "list of mixed values" do
binary = binary =
Config.transform(%{ Config.transform([
"types" => "Pleroma.PostgresTypes", "v1",
"telemetry_event" => ["Pleroma.Repo.Instrumenter"], ":v2",
"migration_lock" => "" "Pleroma.Repo",
}) "Phoenix.Socket.V1.JSONSerializer",
15,
false
])
assert binary == assert binary ==
:erlang.term_to_binary( :erlang.term_to_binary([
telemetry_event: [Pleroma.Repo.Instrumenter], "v1",
types: Pleroma.PostgresTypes :v2,
) Pleroma.Repo,
Phoenix.Socket.V1.JSONSerializer,
15,
false
])
assert Config.from_binary(binary) == [ assert Config.from_binary(binary) == [
telemetry_event: [Pleroma.Repo.Instrumenter], "v1",
types: Pleroma.PostgresTypes :v2,
Pleroma.Repo,
Phoenix.Socket.V1.JSONSerializer,
15,
false
] ]
end end
test "complex map with nested integers, lists and atoms" do test "simple keyword" do
binary = binary = Config.transform([%{"tuple" => [":key", "value"]}])
Config.transform(%{ assert binary == :erlang.term_to_binary([{:key, "value"}])
"uploader" => "Pleroma.Uploaders.Local", assert Config.from_binary(binary) == [{:key, "value"}]
"filters" => ["Pleroma.Upload.Filter.Dedupe"], assert Config.from_binary(binary) == [key: "value"]
"link_name" => ":true",
"proxy_remote" => ":false",
"proxy_opts" => %{
"redirect_on_failure" => ":false",
"max_body_length" => "i:1048576",
"http" => %{
"follow_redirect" => ":true",
"pool" => ":upload"
}
}
})
assert binary ==
:erlang.term_to_binary(
filters: [Pleroma.Upload.Filter.Dedupe],
link_name: true,
proxy_opts: [
http: [
follow_redirect: true,
pool: :upload
],
max_body_length: 1_048_576,
redirect_on_failure: false
],
proxy_remote: false,
uploader: Pleroma.Uploaders.Local
)
assert Config.from_binary(binary) ==
[
filters: [Pleroma.Upload.Filter.Dedupe],
link_name: true,
proxy_opts: [
http: [
follow_redirect: true,
pool: :upload
],
max_body_length: 1_048_576,
redirect_on_failure: false
],
proxy_remote: false,
uploader: Pleroma.Uploaders.Local
]
end end
test "keyword" do test "keyword" do
binary = binary =
Config.transform(%{ Config.transform([
"level" => ":warn", %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
"meta" => [":all"], %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
"webhook_url" => "https://hooks.slack.com/services/YOUR-KEY-HERE" %{"tuple" => [":migration_lock", nil]},
}) %{"tuple" => [":key1", 150]},
%{"tuple" => [":key2", "string"]}
])
assert binary ==
:erlang.term_to_binary(
types: Pleroma.PostgresTypes,
telemetry_event: [Pleroma.Repo.Instrumenter],
migration_lock: nil,
key1: 150,
key2: "string"
)
assert Config.from_binary(binary) == [
types: Pleroma.PostgresTypes,
telemetry_event: [Pleroma.Repo.Instrumenter],
migration_lock: nil,
key1: 150,
key2: "string"
]
end
test "complex keyword with nested mixed childs" do
binary =
Config.transform([
%{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
%{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
%{"tuple" => [":link_name", true]},
%{"tuple" => [":proxy_remote", false]},
%{"tuple" => [":common_map", %{":key" => "value"}]},
%{
"tuple" => [
":proxy_opts",
[
%{"tuple" => [":redirect_on_failure", false]},
%{"tuple" => [":max_body_length", 1_048_576]},
%{
"tuple" => [
":http",
[%{"tuple" => [":follow_redirect", true]}, %{"tuple" => [":pool", ":upload"]}]
]
}
]
]
}
])
assert binary ==
:erlang.term_to_binary(
uploader: Pleroma.Uploaders.Local,
filters: [Pleroma.Upload.Filter.Dedupe],
link_name: true,
proxy_remote: false,
common_map: %{key: "value"},
proxy_opts: [
redirect_on_failure: false,
max_body_length: 1_048_576,
http: [
follow_redirect: true,
pool: :upload
]
]
)
assert Config.from_binary(binary) ==
[
uploader: Pleroma.Uploaders.Local,
filters: [Pleroma.Upload.Filter.Dedupe],
link_name: true,
proxy_remote: false,
common_map: %{key: "value"},
proxy_opts: [
redirect_on_failure: false,
max_body_length: 1_048_576,
http: [
follow_redirect: true,
pool: :upload
]
]
]
end
test "common keyword" do
binary =
Config.transform([
%{"tuple" => [":level", ":warn"]},
%{"tuple" => [":meta", [":all"]]},
%{"tuple" => [":path", ""]},
%{"tuple" => [":val", nil]},
%{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
])
assert binary == assert binary ==
:erlang.term_to_binary( :erlang.term_to_binary(
level: :warn, level: :warn,
meta: [:all], meta: [:all],
path: "",
val: nil,
webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE" webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
) )
assert Config.from_binary(binary) == [ assert Config.from_binary(binary) == [
level: :warn, level: :warn,
meta: [:all], meta: [:all],
path: "",
val: nil,
webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE" webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
] ]
end end
test "complex map with sigil" do test "complex keyword with sigil" do
binary = binary =
Config.transform(%{ Config.transform([
federated_timeline_removal: [], %{"tuple" => [":federated_timeline_removal", []]},
reject: [~r/comp[lL][aA][iI][nN]er/], %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
replace: [] %{"tuple" => [":replace", []]}
}) ])
assert binary == assert binary ==
:erlang.term_to_binary( :erlang.term_to_binary(
@ -184,54 +373,68 @@ test "complex map with sigil" do
[federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []] [federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
end end
test "complex map with tuples with more than 2 values" do test "complex keyword with tuples with more than 2 values" do
binary = binary =
Config.transform(%{ Config.transform([
"http" => %{ %{
"dispatch" => [ "tuple" => [
%{ ":http",
"tuple" => [ [
":_", %{
[ "tuple" => [
%{ ":key1",
"tuple" => [ [
"/api/v1/streaming", %{
"Pleroma.Web.MastodonAPI.WebsocketHandler", "tuple" => [
[] ":_",
] [
}, %{
%{ "tuple" => [
"tuple" => [ "/api/v1/streaming",
"/websocket", "Pleroma.Web.MastodonAPI.WebsocketHandler",
"Phoenix.Endpoint.CowboyWebSocket", []
%{ ]
"tuple" => [ },
"Phoenix.Transports.WebSocket", %{
%{"tuple" => ["Pleroma.Web.Endpoint", "Pleroma.Web.UserSocket", []]} "tuple" => [
"/websocket",
"Phoenix.Endpoint.CowboyWebSocket",
%{
"tuple" => [
"Phoenix.Transports.WebSocket",
%{
"tuple" => [
"Pleroma.Web.Endpoint",
"Pleroma.Web.UserSocket",
[]
]
}
]
}
]
},
%{
"tuple" => [
":_",
"Phoenix.Endpoint.Cowboy2Handler",
%{"tuple" => ["Pleroma.Web.Endpoint", []]}
]
}
] ]
} ]
] }
}, ]
%{
"tuple" => [
":_",
"Phoenix.Endpoint.Cowboy2Handler",
%{
"tuple" => ["Pleroma.Web.Endpoint", []]
}
]
}
] ]
] }
} ]
] ]
} }
}) ])
assert binary == assert binary ==
:erlang.term_to_binary( :erlang.term_to_binary(
http: [ http: [
dispatch: [ key1: [
_: [ _: [
{"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []}, {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{"/websocket", Phoenix.Endpoint.CowboyWebSocket, {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
@ -245,7 +448,7 @@ test "complex map with tuples with more than 2 values" do
assert Config.from_binary(binary) == [ assert Config.from_binary(binary) == [
http: [ http: [
dispatch: [ key1: [
{:_, {:_,
[ [
{"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []}, {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},