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 "pleroma string" do
231 assert ConfigDB.to_elixir_types("Pleroma") == "Pleroma"
234 test "phoenix module" do
235 assert ConfigDB.to_elixir_types("Phoenix.Socket.V1.JSONSerializer") ==
236 Phoenix.Socket.V1.JSONSerializer
239 test "tesla module" do
240 assert ConfigDB.to_elixir_types("Tesla.Adapter.Hackney") == Tesla.Adapter.Hackney
243 test "ExSyslogger module" do
244 assert ConfigDB.to_elixir_types("ExSyslogger") == ExSyslogger
247 test "Quack.Logger module" do
248 assert ConfigDB.to_elixir_types("Quack.Logger") == Quack.Logger
251 test "Swoosh.Adapters modules" do
252 assert ConfigDB.to_elixir_types("Swoosh.Adapters.SMTP") == Swoosh.Adapters.SMTP
253 assert ConfigDB.to_elixir_types("Swoosh.Adapters.AmazonSES") == Swoosh.Adapters.AmazonSES
257 assert ConfigDB.to_elixir_types("~r[comp[lL][aA][iI][nN]er]") == ~r/comp[lL][aA][iI][nN]er/
261 assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/") == ~r/https:\/\/example.com/
264 test "link sigil with um modifiers" do
265 assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/um") ==
266 ~r/https:\/\/example.com/um
269 test "link sigil with i modifier" do
270 assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/i") == ~r/https:\/\/example.com/i
273 test "link sigil with s modifier" do
274 assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/s") == ~r/https:\/\/example.com/s
277 test "raise if valid delimiter not found" do
278 assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
279 ConfigDB.to_elixir_types("~r/https://[]{}<>\"'()|example.com/s")
283 test "2 child tuple" do
284 assert ConfigDB.to_elixir_types(%{"tuple" => ["v1", ":v2"]}) == {"v1", :v2}
287 test "proxy tuple with localhost" do
288 assert ConfigDB.to_elixir_types(%{
289 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
290 }) == {:proxy_url, {:socks5, :localhost, 1234}}
293 test "proxy tuple with domain" do
294 assert ConfigDB.to_elixir_types(%{
295 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
296 }) == {:proxy_url, {:socks5, 'domain.com', 1234}}
299 test "proxy tuple with ip" do
300 assert ConfigDB.to_elixir_types(%{
301 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
302 }) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
305 test "tuple with n childs" do
306 assert ConfigDB.to_elixir_types(%{
313 "Phoenix.Socket.V1.JSONSerializer"
315 }) == {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
318 test "map with string key" do
319 assert ConfigDB.to_elixir_types(%{"key" => "value"}) == %{"key" => "value"}
322 test "map with atom key" do
323 assert ConfigDB.to_elixir_types(%{":key" => "value"}) == %{key: "value"}
326 test "list of strings" do
327 assert ConfigDB.to_elixir_types(["v1", "v2", "v3"]) == ["v1", "v2", "v3"]
330 test "list of modules" do
331 assert ConfigDB.to_elixir_types(["Pleroma.Repo", "Pleroma.Activity"]) == [
337 test "list of atoms" do
338 assert ConfigDB.to_elixir_types([":v1", ":v2", ":v3"]) == [:v1, :v2, :v3]
341 test "list of mixed values" do
342 assert ConfigDB.to_elixir_types([
346 "Phoenix.Socket.V1.JSONSerializer",
353 Phoenix.Socket.V1.JSONSerializer,
359 test "simple keyword" do
360 assert ConfigDB.to_elixir_types([%{"tuple" => [":key", "value"]}]) == [key: "value"]
364 assert ConfigDB.to_elixir_types([
365 %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
366 %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
367 %{"tuple" => [":migration_lock", nil]},
368 %{"tuple" => [":key1", 150]},
369 %{"tuple" => [":key2", "string"]}
371 types: Pleroma.PostgresTypes,
372 telemetry_event: [Pleroma.Repo.Instrumenter],
379 test "trandformed keyword" do
380 assert ConfigDB.to_elixir_types(a: 1, b: 2, c: "string") == [a: 1, b: 2, c: "string"]
383 test "complex keyword with nested mixed childs" do
384 assert ConfigDB.to_elixir_types([
385 %{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
386 %{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
387 %{"tuple" => [":link_name", true]},
388 %{"tuple" => [":proxy_remote", false]},
389 %{"tuple" => [":common_map", %{":key" => "value"}]},
394 %{"tuple" => [":redirect_on_failure", false]},
395 %{"tuple" => [":max_body_length", 1_048_576]},
400 %{"tuple" => [":follow_redirect", true]},
401 %{"tuple" => [":pool", ":upload"]}
409 uploader: Pleroma.Uploaders.Local,
410 filters: [Pleroma.Upload.Filter.Dedupe],
413 common_map: %{key: "value"},
415 redirect_on_failure: false,
416 max_body_length: 1_048_576,
418 follow_redirect: true,
425 test "common keyword" do
426 assert ConfigDB.to_elixir_types([
427 %{"tuple" => [":level", ":warn"]},
428 %{"tuple" => [":meta", [":all"]]},
429 %{"tuple" => [":path", ""]},
430 %{"tuple" => [":val", nil]},
431 %{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
437 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
441 test "complex keyword with sigil" do
442 assert ConfigDB.to_elixir_types([
443 %{"tuple" => [":federated_timeline_removal", []]},
444 %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
445 %{"tuple" => [":replace", []]}
447 federated_timeline_removal: [],
448 reject: [~r/comp[lL][aA][iI][nN]er/],
453 test "complex keyword with tuples with more than 2 values" do
454 assert ConfigDB.to_elixir_types([
470 "Pleroma.Web.MastodonAPI.WebsocketHandler",
477 "Phoenix.Endpoint.CowboyWebSocket",
480 "Phoenix.Transports.WebSocket",
483 "Pleroma.Web.Endpoint",
484 "Pleroma.Web.UserSocket",
495 "Phoenix.Endpoint.Cowboy2Handler",
496 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
513 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
514 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
515 {Phoenix.Transports.WebSocket,
516 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
517 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}