akkoma/test/pleroma/config_db_test.exs

527 lines
17 KiB
Elixir
Raw Normal View History

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