2019-07-10 01:13:23 -04:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
defmodule Pleroma.ConfigDBTest do
|
2019-06-14 11:45:05 -04:00
|
|
|
use Pleroma.DataCase, async: true
|
|
|
|
import Pleroma.Factory
|
2020-01-16 00:50:27 -05:00
|
|
|
alias Pleroma.ConfigDB
|
2019-06-14 11:45:05 -04:00
|
|
|
|
|
|
|
test "get_by_key/1" do
|
|
|
|
config = insert(:config)
|
|
|
|
insert(:config)
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert config == ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "create/1" do
|
2020-01-17 03:45:44 -05:00
|
|
|
{:ok, config} = ConfigDB.create(%{group: ":pleroma", key: ":some_key", value: "some_value"})
|
|
|
|
assert config == ConfigDB.get_by_params(%{group: ":pleroma", key: ":some_key"})
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "update/1" do
|
|
|
|
config = insert(:config)
|
2020-01-16 00:50:27 -05:00
|
|
|
{:ok, updated} = ConfigDB.update(config, %{value: "some_value"})
|
|
|
|
loaded = ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
2019-06-14 11:45:05 -04:00
|
|
|
assert loaded == updated
|
|
|
|
end
|
|
|
|
|
2020-01-17 03:45:44 -05:00
|
|
|
test "get_all_as_keyword/0" do
|
2020-01-22 07:14:11 -05:00
|
|
|
saved = insert(:config)
|
2020-01-17 03:45:44 -05:00
|
|
|
insert(:config, group: ":quack", key: ":level", value: ConfigDB.to_binary(:info))
|
|
|
|
insert(:config, group: ":quack", key: ":meta", value: ConfigDB.to_binary([:none]))
|
|
|
|
|
|
|
|
insert(:config,
|
|
|
|
group: ":quack",
|
|
|
|
key: ":webhook_url",
|
|
|
|
value: ConfigDB.to_binary("https://hooks.slack.com/services/KEY/some_val")
|
|
|
|
)
|
|
|
|
|
2020-01-22 07:14:11 -05:00
|
|
|
config = ConfigDB.get_all_as_keyword()
|
|
|
|
|
|
|
|
assert config[:pleroma] == [
|
|
|
|
{ConfigDB.from_string(saved.key), ConfigDB.from_binary(saved.value)}
|
|
|
|
]
|
|
|
|
|
|
|
|
assert config[:quack] == [
|
|
|
|
level: :info,
|
|
|
|
meta: [:none],
|
|
|
|
webhook_url: "https://hooks.slack.com/services/KEY/some_val"
|
|
|
|
]
|
2020-01-17 03:45:44 -05:00
|
|
|
end
|
|
|
|
|
2019-12-06 07:12:56 -05:00
|
|
|
describe "update_or_create/1" do
|
|
|
|
test "common" do
|
|
|
|
config = insert(:config)
|
|
|
|
key2 = "another_key"
|
|
|
|
|
|
|
|
params = [
|
|
|
|
%{group: "pleroma", key: key2, value: "another_value"},
|
|
|
|
%{group: config.group, key: config.key, value: "new_value"}
|
|
|
|
]
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert Repo.all(ConfigDB) |> length() == 1
|
2019-12-06 07:12:56 -05:00
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
Enum.each(params, &ConfigDB.update_or_create(&1))
|
2019-12-06 07:12:56 -05:00
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert Repo.all(ConfigDB) |> length() == 2
|
2019-12-06 07:12:56 -05:00
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
config1 = ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
|
|
|
config2 = ConfigDB.get_by_params(%{group: "pleroma", key: key2})
|
2019-12-06 07:12:56 -05:00
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert config1.value == ConfigDB.transform("new_value")
|
|
|
|
assert config2.value == ConfigDB.transform("another_value")
|
2019-12-06 07:12:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "partial update" do
|
2020-01-16 00:50:27 -05:00
|
|
|
config = insert(:config, value: ConfigDB.to_binary(key1: "val1", key2: :val2))
|
2019-06-14 11:45:05 -04:00
|
|
|
|
2019-12-06 07:12:56 -05:00
|
|
|
{:ok, _config} =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.update_or_create(%{
|
2019-12-06 07:12:56 -05:00
|
|
|
group: config.group,
|
|
|
|
key: config.key,
|
|
|
|
value: [key1: :val1, key3: :val3]
|
|
|
|
})
|
2019-06-14 11:45:05 -04:00
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
2019-06-14 11:45:05 -04:00
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
value = ConfigDB.from_binary(updated.value)
|
2019-12-06 07:12:56 -05:00
|
|
|
assert length(value) == 3
|
|
|
|
assert value[:key1] == :val1
|
|
|
|
assert value[:key2] == :val2
|
|
|
|
assert value[:key3] == :val3
|
|
|
|
end
|
2019-06-14 11:45:05 -04:00
|
|
|
|
2020-01-06 06:05:32 -05:00
|
|
|
test "deep merge" do
|
2020-01-16 00:50:27 -05:00
|
|
|
config = insert(:config, value: ConfigDB.to_binary(key1: "val1", key2: [k1: :v1, k2: "v2"]))
|
2020-01-06 06:05:32 -05:00
|
|
|
|
|
|
|
{:ok, config} =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.update_or_create(%{
|
2020-01-06 06:05:32 -05:00
|
|
|
group: config.group,
|
|
|
|
key: config.key,
|
|
|
|
value: [key1: :val1, key2: [k2: :v2, k3: :v3], key3: :val3]
|
|
|
|
})
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
2020-01-06 06:05:32 -05:00
|
|
|
|
|
|
|
assert config.value == updated.value
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
value = ConfigDB.from_binary(updated.value)
|
2020-01-06 06:05:32 -05:00
|
|
|
assert value[:key1] == :val1
|
|
|
|
assert value[:key2] == [k1: :v1, k2: :v2, k3: :v3]
|
|
|
|
assert value[:key3] == :val3
|
|
|
|
end
|
|
|
|
|
2019-12-06 07:12:56 -05:00
|
|
|
test "only full update for some keys" do
|
2020-01-16 00:50:27 -05:00
|
|
|
config1 = insert(:config, key: ":ecto_repos", value: ConfigDB.to_binary(repo: Pleroma.Repo))
|
|
|
|
|
|
|
|
config2 =
|
|
|
|
insert(:config, group: ":cors_plug", key: ":max_age", value: ConfigDB.to_binary(18))
|
2019-12-06 07:12:56 -05:00
|
|
|
|
|
|
|
{:ok, _config} =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.update_or_create(%{
|
2019-12-06 07:12:56 -05:00
|
|
|
group: config1.group,
|
|
|
|
key: config1.key,
|
|
|
|
value: [another_repo: [Pleroma.Repo]]
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, _config} =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.update_or_create(%{
|
2019-12-06 07:12:56 -05:00
|
|
|
group: config2.group,
|
|
|
|
key: config2.key,
|
|
|
|
value: 777
|
|
|
|
})
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
|
|
|
|
updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})
|
2019-12-06 07:12:56 -05:00
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(updated1.value) == [another_repo: [Pleroma.Repo]]
|
|
|
|
assert ConfigDB.from_binary(updated2.value) == 777
|
2019-12-06 07:12:56 -05:00
|
|
|
end
|
2019-06-14 11:45:05 -04:00
|
|
|
|
2019-12-06 07:12:56 -05:00
|
|
|
test "full update if value is not keyword" do
|
|
|
|
config =
|
|
|
|
insert(:config,
|
|
|
|
group: ":tesla",
|
|
|
|
key: ":adapter",
|
2020-01-16 00:50:27 -05:00
|
|
|
value: ConfigDB.to_binary(Tesla.Adapter.Hackney)
|
2019-12-06 07:12:56 -05:00
|
|
|
)
|
2019-06-14 11:45:05 -04:00
|
|
|
|
2019-12-06 07:12:56 -05:00
|
|
|
{:ok, _config} =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.update_or_create(%{
|
2019-12-06 07:12:56 -05:00
|
|
|
group: config.group,
|
|
|
|
key: config.key,
|
|
|
|
value: Tesla.Adapter.Httpc
|
|
|
|
})
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
2019-12-06 07:12:56 -05:00
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(updated.value) == Tesla.Adapter.Httpc
|
2019-12-06 07:12:56 -05:00
|
|
|
end
|
2020-01-18 04:25:56 -05:00
|
|
|
|
|
|
|
test "only full update for some subkeys" do
|
|
|
|
config1 =
|
|
|
|
insert(:config,
|
|
|
|
key: ":emoji",
|
|
|
|
value: ConfigDB.to_binary(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])
|
|
|
|
)
|
|
|
|
|
|
|
|
{:ok, _config} =
|
|
|
|
ConfigDB.update_or_create(%{
|
|
|
|
group: config1.group,
|
|
|
|
key: config1.key,
|
|
|
|
value: [groups: [c: 3, d: 4], key: [b: 2]]
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, _config} =
|
|
|
|
ConfigDB.update_or_create(%{
|
|
|
|
group: config2.group,
|
|
|
|
key: config2.key,
|
|
|
|
value: [mascots: [c: 3, d: 4], key: [b: 2]]
|
|
|
|
})
|
|
|
|
|
|
|
|
updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
|
|
|
|
updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})
|
|
|
|
|
|
|
|
assert ConfigDB.from_binary(updated1.value) == [groups: [c: 3, d: 4], key: [a: 1, b: 2]]
|
|
|
|
assert ConfigDB.from_binary(updated2.value) == [mascots: [c: 3, d: 4], key: [a: 1, b: 2]]
|
|
|
|
end
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
|
2020-01-22 07:14:11 -05:00
|
|
|
describe "delete/1" do
|
|
|
|
test "error on deleting non existing setting" do
|
|
|
|
{:error, error} = ConfigDB.delete(%{group: ":pleroma", key: ":key"})
|
|
|
|
assert error =~ "Config with params %{group: \":pleroma\", key: \":key\"} not found"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "full delete" do
|
|
|
|
config = insert(:config)
|
|
|
|
{:ok, deleted} = ConfigDB.delete(%{group: config.group, key: config.key})
|
|
|
|
assert Ecto.get_meta(deleted, :state) == :deleted
|
|
|
|
refute ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "partial subkeys delete" do
|
|
|
|
config = insert(:config, value: ConfigDB.to_binary(groups: [a: 1, b: 2], key: [a: 1]))
|
|
|
|
|
|
|
|
{:ok, deleted} =
|
|
|
|
ConfigDB.delete(%{group: config.group, key: config.key, subkeys: [":groups"]})
|
|
|
|
|
|
|
|
assert Ecto.get_meta(deleted, :state) == :loaded
|
|
|
|
|
|
|
|
assert deleted.value == ConfigDB.to_binary(key: [a: 1])
|
|
|
|
|
|
|
|
updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
|
|
|
|
|
|
|
assert updated.value == deleted.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test "full delete if remaining value after subkeys deletion is empty list" do
|
|
|
|
config = insert(:config, value: ConfigDB.to_binary(groups: [a: 1, b: 2]))
|
|
|
|
|
|
|
|
{:ok, deleted} =
|
|
|
|
ConfigDB.delete(%{group: config.group, key: config.key, subkeys: [":groups"]})
|
|
|
|
|
|
|
|
assert Ecto.get_meta(deleted, :state) == :deleted
|
|
|
|
|
|
|
|
refute ConfigDB.get_by_params(%{group: config.group, key: config.key})
|
|
|
|
end
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "transform/1" do
|
|
|
|
test "string" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("value as string")
|
2019-06-14 11:45:05 -04:00
|
|
|
assert binary == :erlang.term_to_binary("value as string")
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == "value as string"
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "boolean" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(false)
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(false)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == false
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "nil" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(nil)
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(nil)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == nil
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "integer" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(150)
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(150)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == 150
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "atom" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(":atom")
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(:atom)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == :atom
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
2019-12-25 07:31:51 -05:00
|
|
|
test "ssl options" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform([":tlsv1", ":tlsv1.1", ":tlsv1.2"])
|
2019-12-25 07:31:51 -05:00
|
|
|
assert binary == :erlang.term_to_binary([:tlsv1, :"tlsv1.1", :"tlsv1.2"])
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == [:tlsv1, :"tlsv1.1", :"tlsv1.2"]
|
2019-12-25 07:31:51 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "pleroma module" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("Pleroma.Bookmark")
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(Pleroma.Bookmark)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == Pleroma.Bookmark
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
2019-09-29 04:17:38 -04:00
|
|
|
test "pleroma string" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("Pleroma")
|
2019-09-29 04:17:38 -04:00
|
|
|
assert binary == :erlang.term_to_binary("Pleroma")
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == "Pleroma"
|
2019-09-29 04:17:38 -04:00
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "phoenix module" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("Phoenix.Socket.V1.JSONSerializer")
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(Phoenix.Socket.V1.JSONSerializer)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
2019-09-29 04:17:38 -04:00
|
|
|
test "tesla module" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("Tesla.Adapter.Hackney")
|
2019-09-29 04:17:38 -04:00
|
|
|
assert binary == :erlang.term_to_binary(Tesla.Adapter.Hackney)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == Tesla.Adapter.Hackney
|
2019-09-29 04:17:38 -04:00
|
|
|
end
|
|
|
|
|
2019-12-21 05:54:22 -05:00
|
|
|
test "ExSyslogger module" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("ExSyslogger")
|
2019-12-21 05:54:22 -05:00
|
|
|
assert binary == :erlang.term_to_binary(ExSyslogger)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == ExSyslogger
|
2019-12-21 05:54:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Quack.Logger module" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("Quack.Logger")
|
2019-12-21 05:54:22 -05:00
|
|
|
assert binary == :erlang.term_to_binary(Quack.Logger)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == Quack.Logger
|
2019-12-21 05:54:22 -05:00
|
|
|
end
|
|
|
|
|
2020-01-23 10:21:29 -05:00
|
|
|
test "Swoosh.Adapters modules" do
|
|
|
|
binary = ConfigDB.transform("Swoosh.Adapters.SMTP")
|
|
|
|
assert binary == :erlang.term_to_binary(Swoosh.Adapters.SMTP)
|
|
|
|
assert ConfigDB.from_binary(binary) == Swoosh.Adapters.SMTP
|
|
|
|
binary = ConfigDB.transform("Swoosh.Adapters.AmazonSES")
|
|
|
|
assert binary == :erlang.term_to_binary(Swoosh.Adapters.AmazonSES)
|
|
|
|
assert ConfigDB.from_binary(binary) == Swoosh.Adapters.AmazonSES
|
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "sigil" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("~r[comp[lL][aA][iI][nN]er]")
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(~r/comp[lL][aA][iI][nN]er/)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
2019-08-30 07:04:21 -04:00
|
|
|
test "link sigil" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("~r/https:\/\/example.com/")
|
2019-08-30 07:04:21 -04:00
|
|
|
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/
|
2019-08-30 07:04:21 -04:00
|
|
|
end
|
|
|
|
|
2019-09-29 04:17:38 -04:00
|
|
|
test "link sigil with um modifiers" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("~r/https:\/\/example.com/um")
|
2019-09-29 04:17:38 -04:00
|
|
|
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/um)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/um
|
2019-08-30 07:04:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "link sigil with i modifier" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("~r/https:\/\/example.com/i")
|
2019-08-30 07:04:21 -04:00
|
|
|
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/i
|
2019-08-30 07:04:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "link sigil with s modifier" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform("~r/https:\/\/example.com/s")
|
2019-08-30 07:04:21 -04:00
|
|
|
assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s)
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/s
|
2019-08-30 07:04:21 -04:00
|
|
|
end
|
|
|
|
|
2019-09-29 04:17:38 -04:00
|
|
|
test "raise if valid delimiter not found" do
|
|
|
|
assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform("~r/https://[]{}<>\"'()|example.com/s")
|
2019-09-29 04:17:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "2 child tuple" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(%{"tuple" => ["v1", ":v2"]})
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary({"v1", :v2})
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == {"v1", :v2}
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
2019-12-17 11:51:01 -05:00
|
|
|
test "proxy tuple with localhost" do
|
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform(%{
|
2019-12-17 11:51:01 -05:00
|
|
|
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
|
|
|
|
})
|
|
|
|
|
|
|
|
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, :localhost, 1234}})
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, :localhost, 1234}}
|
2019-12-17 11:51:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "proxy tuple with domain" do
|
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform(%{
|
2019-12-17 11:51:01 -05:00
|
|
|
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
|
|
|
|
})
|
|
|
|
|
|
|
|
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, 'domain.com', 1234}})
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, 'domain.com', 1234}}
|
2019-12-17 11:51:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "proxy tuple with ip" do
|
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform(%{
|
2019-12-17 11:51:01 -05:00
|
|
|
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
|
|
|
|
})
|
|
|
|
|
|
|
|
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}})
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
|
2019-12-17 11:51:01 -05:00
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "tuple with n childs" do
|
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform(%{
|
2019-07-11 09:02:13 -04:00
|
|
|
"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}
|
|
|
|
)
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) ==
|
2019-07-11 09:02:13 -04:00
|
|
|
{"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "map with string key" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(%{"key" => "value"})
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(%{"key" => "value"})
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == %{"key" => "value"}
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "map with atom key" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(%{":key" => "value"})
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(%{key: "value"})
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == %{key: "value"}
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "list of strings" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(["v1", "v2", "v3"])
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary(["v1", "v2", "v3"])
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == ["v1", "v2", "v3"]
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
2019-06-14 11:45:05 -04:00
|
|
|
test "list of modules" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform(["Pleroma.Repo", "Pleroma.Activity"])
|
2019-06-14 11:45:05 -04:00
|
|
|
assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "list of atoms" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform([":v1", ":v2", ":v3"])
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary([:v1, :v2, :v3])
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == [:v1, :v2, :v3]
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "list of mixed values" do
|
2019-06-14 11:45:05 -04:00
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform([
|
2019-07-11 09:02:13 -04:00
|
|
|
"v1",
|
|
|
|
":v2",
|
|
|
|
"Pleroma.Repo",
|
|
|
|
"Phoenix.Socket.V1.JSONSerializer",
|
|
|
|
15,
|
|
|
|
false
|
|
|
|
])
|
|
|
|
|
|
|
|
assert binary ==
|
|
|
|
:erlang.term_to_binary([
|
|
|
|
"v1",
|
|
|
|
:v2,
|
|
|
|
Pleroma.Repo,
|
|
|
|
Phoenix.Socket.V1.JSONSerializer,
|
|
|
|
15,
|
|
|
|
false
|
|
|
|
])
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == [
|
2019-07-11 09:02:13 -04:00
|
|
|
"v1",
|
|
|
|
:v2,
|
|
|
|
Pleroma.Repo,
|
|
|
|
Phoenix.Socket.V1.JSONSerializer,
|
|
|
|
15,
|
|
|
|
false
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "simple keyword" do
|
2020-01-16 00:50:27 -05:00
|
|
|
binary = ConfigDB.transform([%{"tuple" => [":key", "value"]}])
|
2019-07-11 09:02:13 -04:00
|
|
|
assert binary == :erlang.term_to_binary([{:key, "value"}])
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == [{:key, "value"}]
|
|
|
|
assert ConfigDB.from_binary(binary) == [key: "value"]
|
2019-07-11 09:02:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "keyword" do
|
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform([
|
2019-07-11 09:02:13 -04:00
|
|
|
%{"tuple" => [":types", "Pleroma.PostgresTypes"]},
|
|
|
|
%{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
|
|
|
|
%{"tuple" => [":migration_lock", nil]},
|
|
|
|
%{"tuple" => [":key1", 150]},
|
|
|
|
%{"tuple" => [":key2", "string"]}
|
|
|
|
])
|
2019-06-14 11:45:05 -04:00
|
|
|
|
|
|
|
assert binary ==
|
|
|
|
:erlang.term_to_binary(
|
2019-07-11 09:02:13 -04:00
|
|
|
types: Pleroma.PostgresTypes,
|
2019-06-14 11:45:05 -04:00
|
|
|
telemetry_event: [Pleroma.Repo.Instrumenter],
|
2019-07-11 09:02:13 -04:00
|
|
|
migration_lock: nil,
|
|
|
|
key1: 150,
|
|
|
|
key2: "string"
|
2019-06-14 11:45:05 -04:00
|
|
|
)
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == [
|
2019-07-11 09:02:13 -04:00
|
|
|
types: Pleroma.PostgresTypes,
|
2019-06-14 11:45:05 -04:00
|
|
|
telemetry_event: [Pleroma.Repo.Instrumenter],
|
2019-07-11 09:02:13 -04:00
|
|
|
migration_lock: nil,
|
|
|
|
key1: 150,
|
|
|
|
key2: "string"
|
2019-06-14 11:45:05 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "complex keyword with nested mixed childs" do
|
2019-06-14 11:45:05 -04:00
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform([
|
2019-07-11 09:02:13 -04:00
|
|
|
%{"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"]}]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
2019-06-14 11:45:05 -04:00
|
|
|
}
|
2019-07-11 09:02:13 -04:00
|
|
|
])
|
2019-06-14 11:45:05 -04:00
|
|
|
|
|
|
|
assert binary ==
|
|
|
|
:erlang.term_to_binary(
|
2019-07-11 09:02:13 -04:00
|
|
|
uploader: Pleroma.Uploaders.Local,
|
2019-06-14 11:45:05 -04:00
|
|
|
filters: [Pleroma.Upload.Filter.Dedupe],
|
|
|
|
link_name: true,
|
2019-07-11 09:02:13 -04:00
|
|
|
proxy_remote: false,
|
|
|
|
common_map: %{key: "value"},
|
2019-06-14 11:45:05 -04:00
|
|
|
proxy_opts: [
|
2019-07-11 09:02:13 -04:00
|
|
|
redirect_on_failure: false,
|
|
|
|
max_body_length: 1_048_576,
|
2019-06-14 11:45:05 -04:00
|
|
|
http: [
|
|
|
|
follow_redirect: true,
|
|
|
|
pool: :upload
|
2019-07-11 09:02:13 -04:00
|
|
|
]
|
|
|
|
]
|
2019-06-14 11:45:05 -04:00
|
|
|
)
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) ==
|
2019-06-14 11:45:05 -04:00
|
|
|
[
|
2019-07-11 09:02:13 -04:00
|
|
|
uploader: Pleroma.Uploaders.Local,
|
2019-06-14 11:45:05 -04:00
|
|
|
filters: [Pleroma.Upload.Filter.Dedupe],
|
|
|
|
link_name: true,
|
2019-07-11 09:02:13 -04:00
|
|
|
proxy_remote: false,
|
|
|
|
common_map: %{key: "value"},
|
2019-06-14 11:45:05 -04:00
|
|
|
proxy_opts: [
|
2019-07-11 09:02:13 -04:00
|
|
|
redirect_on_failure: false,
|
|
|
|
max_body_length: 1_048_576,
|
2019-06-14 11:45:05 -04:00
|
|
|
http: [
|
|
|
|
follow_redirect: true,
|
|
|
|
pool: :upload
|
2019-07-11 09:02:13 -04:00
|
|
|
]
|
|
|
|
]
|
2019-06-14 11:45:05 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "common keyword" do
|
2019-06-14 11:45:05 -04:00
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform([
|
2019-07-11 09:02:13 -04:00
|
|
|
%{"tuple" => [":level", ":warn"]},
|
|
|
|
%{"tuple" => [":meta", [":all"]]},
|
|
|
|
%{"tuple" => [":path", ""]},
|
|
|
|
%{"tuple" => [":val", nil]},
|
|
|
|
%{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
|
|
|
|
])
|
2019-06-14 11:45:05 -04:00
|
|
|
|
|
|
|
assert binary ==
|
|
|
|
:erlang.term_to_binary(
|
|
|
|
level: :warn,
|
|
|
|
meta: [:all],
|
2019-07-11 09:02:13 -04:00
|
|
|
path: "",
|
|
|
|
val: nil,
|
2019-06-14 11:45:05 -04:00
|
|
|
webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
|
|
|
|
)
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == [
|
2019-06-14 11:45:05 -04:00
|
|
|
level: :warn,
|
|
|
|
meta: [:all],
|
2019-07-11 09:02:13 -04:00
|
|
|
path: "",
|
|
|
|
val: nil,
|
2019-06-14 11:45:05 -04:00
|
|
|
webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "complex keyword with sigil" do
|
2019-06-14 11:45:05 -04:00
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform([
|
2019-07-11 09:02:13 -04:00
|
|
|
%{"tuple" => [":federated_timeline_removal", []]},
|
|
|
|
%{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
|
|
|
|
%{"tuple" => [":replace", []]}
|
|
|
|
])
|
2019-06-14 11:45:05 -04:00
|
|
|
|
|
|
|
assert binary ==
|
|
|
|
:erlang.term_to_binary(
|
|
|
|
federated_timeline_removal: [],
|
|
|
|
reject: [~r/comp[lL][aA][iI][nN]er/],
|
|
|
|
replace: []
|
|
|
|
)
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) ==
|
2019-06-14 11:45:05 -04:00
|
|
|
[federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
|
|
|
|
end
|
2019-06-22 10:30:53 -04:00
|
|
|
|
2019-07-11 09:02:13 -04:00
|
|
|
test "complex keyword with tuples with more than 2 values" do
|
2019-06-22 10:30:53 -04:00
|
|
|
binary =
|
2020-01-16 00:50:27 -05:00
|
|
|
ConfigDB.transform([
|
2019-07-11 09:02:13 -04:00
|
|
|
%{
|
|
|
|
"tuple" => [
|
|
|
|
":http",
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"tuple" => [
|
|
|
|
":key1",
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"tuple" => [
|
|
|
|
":_",
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"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", []]}
|
|
|
|
]
|
|
|
|
}
|
2019-06-22 10:30:53 -04:00
|
|
|
]
|
2019-07-11 09:02:13 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2019-06-22 10:30:53 -04:00
|
|
|
]
|
2019-07-11 09:02:13 -04:00
|
|
|
}
|
|
|
|
]
|
2019-06-22 10:30:53 -04:00
|
|
|
]
|
|
|
|
}
|
2019-07-11 09:02:13 -04:00
|
|
|
])
|
2019-06-22 10:30:53 -04:00
|
|
|
|
|
|
|
assert binary ==
|
|
|
|
:erlang.term_to_binary(
|
|
|
|
http: [
|
2019-07-11 09:02:13 -04:00
|
|
|
key1: [
|
2019-06-22 10:30:53 -04:00
|
|
|
_: [
|
|
|
|
{"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
|
|
|
|
{"/websocket", Phoenix.Endpoint.CowboyWebSocket,
|
|
|
|
{Phoenix.Transports.WebSocket,
|
|
|
|
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
|
|
|
|
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-01-16 00:50:27 -05:00
|
|
|
assert ConfigDB.from_binary(binary) == [
|
2019-06-22 10:30:53 -04:00
|
|
|
http: [
|
2019-07-11 09:02:13 -04:00
|
|
|
key1: [
|
2019-06-22 10:30:53 -04:00
|
|
|
{:_,
|
|
|
|
[
|
|
|
|
{"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
|
|
|
|
{"/websocket", Phoenix.Endpoint.CowboyWebSocket,
|
|
|
|
{Phoenix.Transports.WebSocket,
|
|
|
|
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
|
|
|
|
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
|
|
|
|
]}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
end
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
end
|