1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ConfigDBTest do
6 use Pleroma.DataCase, async: true
10 test "get_by_params/1" do
11 config = insert(:config)
14 assert config == ConfigDB.get_by_params(%{group: config.group, key: config.key})
17 describe "update_or_create/1" do
19 config = insert(:config)
23 %{group: :pleroma, key: key2, value: "another_value"},
24 %{group: :pleroma, key: config.key, value: [a: 1, b: 2, c: "new_value"]}
27 assert Repo.all(ConfigDB) |> length() == 1
29 Enum.each(params, &ConfigDB.update_or_create(&1))
31 assert Repo.all(ConfigDB) |> length() == 2
33 config1 = ConfigDB.get_by_params(%{group: config.group, key: config.key})
34 config2 = ConfigDB.get_by_params(%{group: :pleroma, key: key2})
36 assert config1.value == [a: 1, b: 2, c: "new_value"]
37 assert config2.value == "another_value"
40 test "partial update" do
41 config = insert(:config, value: [key1: "val1", key2: :val2])
44 ConfigDB.update_or_create(%{
47 value: [key1: :val1, key3: :val3]
50 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
52 assert config.value == updated.value
53 assert updated.value[:key1] == :val1
54 assert updated.value[:key2] == :val2
55 assert updated.value[:key3] == :val3
59 config = insert(:config, value: [key1: "val1", key2: [k1: :v1, k2: "v2"]])
62 ConfigDB.update_or_create(%{
65 value: [key1: :val1, key2: [k2: :v2, k3: :v3], key3: :val3]
68 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
70 assert config.value == updated.value
71 assert updated.value[:key1] == :val1
72 assert updated.value[:key2] == [k1: :v1, k2: :v2, k3: :v3]
73 assert updated.value[:key3] == :val3
76 test "only full update for some keys" do
77 config1 = insert(:config, key: :ecto_repos, value: [repo: Pleroma.Repo])
79 config2 = insert(:config, group: :cors_plug, key: :max_age, value: 18)
82 ConfigDB.update_or_create(%{
85 value: [another_repo: [Pleroma.Repo]]
89 ConfigDB.update_or_create(%{
95 updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
96 updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})
98 assert updated1.value == [another_repo: [Pleroma.Repo]]
99 assert updated2.value == 777
102 test "full update if value is not keyword" do
107 value: Tesla.Adapter.Hackney
111 ConfigDB.update_or_create(%{
114 value: Tesla.Adapter.Httpc
117 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
119 assert updated.value == Tesla.Adapter.Httpc
122 test "only full update for some subkeys" do
126 value: [groups: [a: 1, b: 2], key: [a: 1]]
132 value: [mascots: [a: 1, b: 2], key: [a: 1]]
136 ConfigDB.update_or_create(%{
137 group: config1.group,
139 value: [groups: [c: 3, d: 4], key: [b: 2]]
143 ConfigDB.update_or_create(%{
144 group: config2.group,
146 value: [mascots: [c: 3, d: 4], key: [b: 2]]
149 updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
150 updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})
152 assert updated1.value == [groups: [c: 3, d: 4], key: [a: 1, b: 2]]
153 assert updated2.value == [mascots: [c: 3, d: 4], key: [a: 1, b: 2]]
157 describe "delete/1" do
158 test "error on deleting non existing setting" do
159 {:error, error} = ConfigDB.delete(%{group: ":pleroma", key: ":key"})
160 assert error =~ "Config with params %{group: \":pleroma\", key: \":key\"} not found"
163 test "full delete" do
164 config = insert(:config)
165 {:ok, deleted} = ConfigDB.delete(%{group: config.group, key: config.key})
166 assert Ecto.get_meta(deleted, :state) == :deleted
167 refute ConfigDB.get_by_params(%{group: config.group, key: config.key})
170 test "partial subkeys delete" do
171 config = insert(:config, value: [groups: [a: 1, b: 2], key: [a: 1]])
174 ConfigDB.delete(%{group: config.group, key: config.key, subkeys: [":groups"]})
176 assert Ecto.get_meta(deleted, :state) == :loaded
178 assert deleted.value == [key: [a: 1]]
180 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
182 assert updated.value == deleted.value
185 test "full delete if remaining value after subkeys deletion is empty list" do
186 config = insert(:config, value: [groups: [a: 1, b: 2]])
189 ConfigDB.delete(%{group: config.group, key: config.key, subkeys: [":groups"]})
191 assert Ecto.get_meta(deleted, :state) == :deleted
193 refute ConfigDB.get_by_params(%{group: config.group, key: config.key})
197 describe "to_elixir_types/1" do
199 assert ConfigDB.to_elixir_types("value as string") == "value as string"
203 assert ConfigDB.to_elixir_types(false) == false
207 assert ConfigDB.to_elixir_types(nil) == nil
211 assert ConfigDB.to_elixir_types(150) == 150
215 assert ConfigDB.to_elixir_types(":atom") == :atom
218 test "ssl options" do
219 assert ConfigDB.to_elixir_types([":tlsv1", ":tlsv1.1", ":tlsv1.2"]) == [
226 test "pleroma module" do
227 assert ConfigDB.to_elixir_types("Pleroma.Bookmark") == Pleroma.Bookmark
230 test "removed module" do
231 assert ConfigDB.to_elixir_types("Pleroma.Nowhere") == :invalid_atom
234 test "pleroma string" do
235 assert ConfigDB.to_elixir_types("Pleroma") == "Pleroma"
238 test "phoenix module" do
239 assert ConfigDB.to_elixir_types("Phoenix.Socket.V1.JSONSerializer") ==
240 Phoenix.Socket.V1.JSONSerializer
243 test "tesla module" do
244 assert ConfigDB.to_elixir_types("Tesla.Adapter.Hackney") == Tesla.Adapter.Hackney
247 test "ExSyslogger module" do
248 assert ConfigDB.to_elixir_types("ExSyslogger") == ExSyslogger
251 test "Quack.Logger module" do
252 assert ConfigDB.to_elixir_types("Quack.Logger") == Quack.Logger
255 test "Swoosh.Adapters modules" do
256 assert ConfigDB.to_elixir_types("Swoosh.Adapters.SMTP") == Swoosh.Adapters.SMTP
257 assert ConfigDB.to_elixir_types("Swoosh.Adapters.AmazonSES") == Swoosh.Adapters.AmazonSES
261 assert ConfigDB.to_elixir_types("~r[comp[lL][aA][iI][nN]er]") == ~r/comp[lL][aA][iI][nN]er/
265 assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/") == ~r/https:\/\/example.com/
268 test "link sigil with um modifiers" do
269 assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/um") ==
270 ~r/https:\/\/example.com/um
273 test "link sigil with i modifier" do
274 assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/i") == ~r/https:\/\/example.com/i
277 test "link sigil with s modifier" do
278 assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/s") == ~r/https:\/\/example.com/s
281 test "raise if valid delimiter not found" do
282 assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
283 ConfigDB.to_elixir_types("~r/https://[]{}<>\"'()|example.com/s")
287 test "2 child tuple" do
288 assert ConfigDB.to_elixir_types(%{"tuple" => ["v1", ":v2"]}) == {"v1", :v2}
291 test "proxy tuple with localhost" do
292 assert ConfigDB.to_elixir_types(%{
293 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
294 }) == {:proxy_url, {:socks5, :localhost, 1234}}
297 test "proxy tuple with domain" do
298 assert ConfigDB.to_elixir_types(%{
299 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
300 }) == {:proxy_url, {:socks5, 'domain.com', 1234}}
303 test "proxy tuple with ip" do
304 assert ConfigDB.to_elixir_types(%{
305 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
306 }) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
309 test "tuple with n childs" do
310 assert ConfigDB.to_elixir_types(%{
317 "Phoenix.Socket.V1.JSONSerializer"
319 }) == {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
322 test "map with string key" do
323 assert ConfigDB.to_elixir_types(%{"key" => "value"}) == %{"key" => "value"}
326 test "map with atom key" do
327 assert ConfigDB.to_elixir_types(%{":key" => "value"}) == %{key: "value"}
330 test "list of strings" do
331 assert ConfigDB.to_elixir_types(["v1", "v2", "v3"]) == ["v1", "v2", "v3"]
334 test "list of modules" do
335 assert ConfigDB.to_elixir_types(["Pleroma.Repo", "Pleroma.Activity"]) == [
341 test "list of atoms" do
342 assert ConfigDB.to_elixir_types([":v1", ":v2", ":v3"]) == [:v1, :v2, :v3]
345 test "list of mixed values" do
346 assert ConfigDB.to_elixir_types([
350 "Phoenix.Socket.V1.JSONSerializer",
357 Phoenix.Socket.V1.JSONSerializer,
363 test "simple keyword" do
364 assert ConfigDB.to_elixir_types([%{"tuple" => [":key", "value"]}]) == [key: "value"]
368 assert ConfigDB.to_elixir_types([
369 %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
370 %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
371 %{"tuple" => [":migration_lock", nil]},
372 %{"tuple" => [":key1", 150]},
373 %{"tuple" => [":key2", "string"]}
375 types: Pleroma.PostgresTypes,
376 telemetry_event: [Pleroma.Repo.Instrumenter],
383 test "trandformed keyword" do
384 assert ConfigDB.to_elixir_types(a: 1, b: 2, c: "string") == [a: 1, b: 2, c: "string"]
387 test "complex keyword with nested mixed childs" do
388 assert ConfigDB.to_elixir_types([
389 %{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
390 %{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
391 %{"tuple" => [":link_name", true]},
392 %{"tuple" => [":proxy_remote", false]},
393 %{"tuple" => [":common_map", %{":key" => "value"}]},
398 %{"tuple" => [":redirect_on_failure", false]},
399 %{"tuple" => [":max_body_length", 1_048_576]},
404 %{"tuple" => [":follow_redirect", true]},
405 %{"tuple" => [":pool", ":upload"]}
413 uploader: Pleroma.Uploaders.Local,
414 filters: [Pleroma.Upload.Filter.Dedupe],
417 common_map: %{key: "value"},
419 redirect_on_failure: false,
420 max_body_length: 1_048_576,
422 follow_redirect: true,
429 test "common keyword" do
430 assert ConfigDB.to_elixir_types([
431 %{"tuple" => [":level", ":warn"]},
432 %{"tuple" => [":meta", [":all"]]},
433 %{"tuple" => [":path", ""]},
434 %{"tuple" => [":val", nil]},
435 %{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
441 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
445 test "complex keyword with sigil" do
446 assert ConfigDB.to_elixir_types([
447 %{"tuple" => [":federated_timeline_removal", []]},
448 %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
449 %{"tuple" => [":replace", []]}
451 federated_timeline_removal: [],
452 reject: [~r/comp[lL][aA][iI][nN]er/],
457 test "complex keyword with tuples with more than 2 values" do
458 assert ConfigDB.to_elixir_types([
474 "Pleroma.Web.MastodonAPI.WebsocketHandler",
481 "Phoenix.Endpoint.CowboyWebSocket",
484 "Phoenix.Transports.WebSocket",
487 "Pleroma.Web.Endpoint",
488 "Pleroma.Web.UserSocket",
499 "Phoenix.Endpoint.Cowboy2Handler",
500 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
517 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
518 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
519 {Phoenix.Transports.WebSocket,
520 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
521 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}