1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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_key/1" do
11 config = insert(:config)
14 assert config == ConfigDB.get_by_params(%{group: config.group, key: config.key})
18 {:ok, config} = ConfigDB.create(%{group: ":pleroma", key: ":some_key", value: "some_value"})
19 assert config == ConfigDB.get_by_params(%{group: ":pleroma", key: ":some_key"})
23 config = insert(:config)
24 {:ok, updated} = ConfigDB.update(config, %{value: "some_value"})
25 loaded = ConfigDB.get_by_params(%{group: config.group, key: config.key})
26 assert loaded == updated
29 test "get_all_as_keyword/0" do
30 saved = insert(:config)
31 insert(:config, group: ":quack", key: ":level", value: ConfigDB.to_binary(:info))
32 insert(:config, group: ":quack", key: ":meta", value: ConfigDB.to_binary([:none]))
37 value: ConfigDB.to_binary("https://hooks.slack.com/services/KEY/some_val")
40 config = ConfigDB.get_all_as_keyword()
42 assert config[:pleroma] == [
43 {ConfigDB.from_string(saved.key), ConfigDB.from_binary(saved.value)}
46 assert config[:quack] == [
49 webhook_url: "https://hooks.slack.com/services/KEY/some_val"
53 describe "update_or_create/1" do
55 config = insert(:config)
59 %{group: "pleroma", key: key2, value: "another_value"},
60 %{group: config.group, key: config.key, value: "new_value"}
63 assert Repo.all(ConfigDB) |> length() == 1
65 Enum.each(params, &ConfigDB.update_or_create(&1))
67 assert Repo.all(ConfigDB) |> length() == 2
69 config1 = ConfigDB.get_by_params(%{group: config.group, key: config.key})
70 config2 = ConfigDB.get_by_params(%{group: "pleroma", key: key2})
72 assert config1.value == ConfigDB.transform("new_value")
73 assert config2.value == ConfigDB.transform("another_value")
76 test "partial update" do
77 config = insert(:config, value: ConfigDB.to_binary(key1: "val1", key2: :val2))
80 ConfigDB.update_or_create(%{
83 value: [key1: :val1, key3: :val3]
86 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
88 value = ConfigDB.from_binary(updated.value)
89 assert length(value) == 3
90 assert value[:key1] == :val1
91 assert value[:key2] == :val2
92 assert value[:key3] == :val3
96 config = insert(:config, value: ConfigDB.to_binary(key1: "val1", key2: [k1: :v1, k2: "v2"]))
99 ConfigDB.update_or_create(%{
102 value: [key1: :val1, key2: [k2: :v2, k3: :v3], key3: :val3]
105 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
107 assert config.value == updated.value
109 value = ConfigDB.from_binary(updated.value)
110 assert value[:key1] == :val1
111 assert value[:key2] == [k1: :v1, k2: :v2, k3: :v3]
112 assert value[:key3] == :val3
115 test "only full update for some keys" do
116 config1 = insert(:config, key: ":ecto_repos", value: ConfigDB.to_binary(repo: Pleroma.Repo))
119 insert(:config, group: ":cors_plug", key: ":max_age", value: ConfigDB.to_binary(18))
122 ConfigDB.update_or_create(%{
123 group: config1.group,
125 value: [another_repo: [Pleroma.Repo]]
129 ConfigDB.update_or_create(%{
130 group: config2.group,
135 updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
136 updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})
138 assert ConfigDB.from_binary(updated1.value) == [another_repo: [Pleroma.Repo]]
139 assert ConfigDB.from_binary(updated2.value) == 777
142 test "full update if value is not keyword" do
147 value: ConfigDB.to_binary(Tesla.Adapter.Hackney)
151 ConfigDB.update_or_create(%{
154 value: Tesla.Adapter.Httpc
157 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
159 assert ConfigDB.from_binary(updated.value) == Tesla.Adapter.Httpc
162 test "only full update for some subkeys" do
166 value: ConfigDB.to_binary(groups: [a: 1, b: 2], key: [a: 1])
172 value: ConfigDB.to_binary(mascots: [a: 1, b: 2], key: [a: 1])
176 ConfigDB.update_or_create(%{
177 group: config1.group,
179 value: [groups: [c: 3, d: 4], key: [b: 2]]
183 ConfigDB.update_or_create(%{
184 group: config2.group,
186 value: [mascots: [c: 3, d: 4], key: [b: 2]]
189 updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
190 updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})
192 assert ConfigDB.from_binary(updated1.value) == [groups: [c: 3, d: 4], key: [a: 1, b: 2]]
193 assert ConfigDB.from_binary(updated2.value) == [mascots: [c: 3, d: 4], key: [a: 1, b: 2]]
197 describe "delete/1" do
198 test "error on deleting non existing setting" do
199 {:error, error} = ConfigDB.delete(%{group: ":pleroma", key: ":key"})
200 assert error =~ "Config with params %{group: \":pleroma\", key: \":key\"} not found"
203 test "full delete" do
204 config = insert(:config)
205 {:ok, deleted} = ConfigDB.delete(%{group: config.group, key: config.key})
206 assert Ecto.get_meta(deleted, :state) == :deleted
207 refute ConfigDB.get_by_params(%{group: config.group, key: config.key})
210 test "partial subkeys delete" do
211 config = insert(:config, value: ConfigDB.to_binary(groups: [a: 1, b: 2], key: [a: 1]))
214 ConfigDB.delete(%{group: config.group, key: config.key, subkeys: [":groups"]})
216 assert Ecto.get_meta(deleted, :state) == :loaded
218 assert deleted.value == ConfigDB.to_binary(key: [a: 1])
220 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
222 assert updated.value == deleted.value
225 test "full delete if remaining value after subkeys deletion is empty list" do
226 config = insert(:config, value: ConfigDB.to_binary(groups: [a: 1, b: 2]))
229 ConfigDB.delete(%{group: config.group, key: config.key, subkeys: [":groups"]})
231 assert Ecto.get_meta(deleted, :state) == :deleted
233 refute ConfigDB.get_by_params(%{group: config.group, key: config.key})
237 describe "transform/1" do
239 binary = ConfigDB.transform("value as string")
240 assert binary == :erlang.term_to_binary("value as string")
241 assert ConfigDB.from_binary(binary) == "value as string"
245 binary = ConfigDB.transform(false)
246 assert binary == :erlang.term_to_binary(false)
247 assert ConfigDB.from_binary(binary) == false
251 binary = ConfigDB.transform(nil)
252 assert binary == :erlang.term_to_binary(nil)
253 assert ConfigDB.from_binary(binary) == nil
257 binary = ConfigDB.transform(150)
258 assert binary == :erlang.term_to_binary(150)
259 assert ConfigDB.from_binary(binary) == 150
263 binary = ConfigDB.transform(":atom")
264 assert binary == :erlang.term_to_binary(:atom)
265 assert ConfigDB.from_binary(binary) == :atom
268 test "ssl options" do
269 binary = ConfigDB.transform([":tlsv1", ":tlsv1.1", ":tlsv1.2"])
270 assert binary == :erlang.term_to_binary([:tlsv1, :"tlsv1.1", :"tlsv1.2"])
271 assert ConfigDB.from_binary(binary) == [:tlsv1, :"tlsv1.1", :"tlsv1.2"]
274 test "pleroma module" do
275 binary = ConfigDB.transform("Pleroma.Bookmark")
276 assert binary == :erlang.term_to_binary(Pleroma.Bookmark)
277 assert ConfigDB.from_binary(binary) == Pleroma.Bookmark
280 test "pleroma string" do
281 binary = ConfigDB.transform("Pleroma")
282 assert binary == :erlang.term_to_binary("Pleroma")
283 assert ConfigDB.from_binary(binary) == "Pleroma"
286 test "phoenix module" do
287 binary = ConfigDB.transform("Phoenix.Socket.V1.JSONSerializer")
288 assert binary == :erlang.term_to_binary(Phoenix.Socket.V1.JSONSerializer)
289 assert ConfigDB.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
292 test "tesla module" do
293 binary = ConfigDB.transform("Tesla.Adapter.Hackney")
294 assert binary == :erlang.term_to_binary(Tesla.Adapter.Hackney)
295 assert ConfigDB.from_binary(binary) == Tesla.Adapter.Hackney
298 test "ExSyslogger module" do
299 binary = ConfigDB.transform("ExSyslogger")
300 assert binary == :erlang.term_to_binary(ExSyslogger)
301 assert ConfigDB.from_binary(binary) == ExSyslogger
304 test "Quack.Logger module" do
305 binary = ConfigDB.transform("Quack.Logger")
306 assert binary == :erlang.term_to_binary(Quack.Logger)
307 assert ConfigDB.from_binary(binary) == Quack.Logger
310 test "Swoosh.Adapters modules" do
311 binary = ConfigDB.transform("Swoosh.Adapters.SMTP")
312 assert binary == :erlang.term_to_binary(Swoosh.Adapters.SMTP)
313 assert ConfigDB.from_binary(binary) == Swoosh.Adapters.SMTP
314 binary = ConfigDB.transform("Swoosh.Adapters.AmazonSES")
315 assert binary == :erlang.term_to_binary(Swoosh.Adapters.AmazonSES)
316 assert ConfigDB.from_binary(binary) == Swoosh.Adapters.AmazonSES
320 binary = ConfigDB.transform("~r[comp[lL][aA][iI][nN]er]")
321 assert binary == :erlang.term_to_binary(~r/comp[lL][aA][iI][nN]er/)
322 assert ConfigDB.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
326 binary = ConfigDB.transform("~r/https:\/\/example.com/")
327 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/)
328 assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/
331 test "link sigil with um modifiers" do
332 binary = ConfigDB.transform("~r/https:\/\/example.com/um")
333 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/um)
334 assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/um
337 test "link sigil with i modifier" do
338 binary = ConfigDB.transform("~r/https:\/\/example.com/i")
339 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i)
340 assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/i
343 test "link sigil with s modifier" do
344 binary = ConfigDB.transform("~r/https:\/\/example.com/s")
345 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s)
346 assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/s
349 test "raise if valid delimiter not found" do
350 assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
351 ConfigDB.transform("~r/https://[]{}<>\"'()|example.com/s")
355 test "2 child tuple" do
356 binary = ConfigDB.transform(%{"tuple" => ["v1", ":v2"]})
357 assert binary == :erlang.term_to_binary({"v1", :v2})
358 assert ConfigDB.from_binary(binary) == {"v1", :v2}
361 test "proxy tuple with localhost" do
363 ConfigDB.transform(%{
364 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
367 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, :localhost, 1234}})
368 assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, :localhost, 1234}}
371 test "proxy tuple with domain" do
373 ConfigDB.transform(%{
374 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
377 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, 'domain.com', 1234}})
378 assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, 'domain.com', 1234}}
381 test "proxy tuple with ip" do
383 ConfigDB.transform(%{
384 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
387 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}})
388 assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
391 test "tuple with n childs" do
393 ConfigDB.transform(%{
400 "Phoenix.Socket.V1.JSONSerializer"
405 :erlang.term_to_binary(
406 {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
409 assert ConfigDB.from_binary(binary) ==
410 {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
413 test "map with string key" do
414 binary = ConfigDB.transform(%{"key" => "value"})
415 assert binary == :erlang.term_to_binary(%{"key" => "value"})
416 assert ConfigDB.from_binary(binary) == %{"key" => "value"}
419 test "map with atom key" do
420 binary = ConfigDB.transform(%{":key" => "value"})
421 assert binary == :erlang.term_to_binary(%{key: "value"})
422 assert ConfigDB.from_binary(binary) == %{key: "value"}
425 test "list of strings" do
426 binary = ConfigDB.transform(["v1", "v2", "v3"])
427 assert binary == :erlang.term_to_binary(["v1", "v2", "v3"])
428 assert ConfigDB.from_binary(binary) == ["v1", "v2", "v3"]
431 test "list of modules" do
432 binary = ConfigDB.transform(["Pleroma.Repo", "Pleroma.Activity"])
433 assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
434 assert ConfigDB.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
437 test "list of atoms" do
438 binary = ConfigDB.transform([":v1", ":v2", ":v3"])
439 assert binary == :erlang.term_to_binary([:v1, :v2, :v3])
440 assert ConfigDB.from_binary(binary) == [:v1, :v2, :v3]
443 test "list of mixed values" do
449 "Phoenix.Socket.V1.JSONSerializer",
455 :erlang.term_to_binary([
459 Phoenix.Socket.V1.JSONSerializer,
464 assert ConfigDB.from_binary(binary) == [
468 Phoenix.Socket.V1.JSONSerializer,
474 test "simple keyword" do
475 binary = ConfigDB.transform([%{"tuple" => [":key", "value"]}])
476 assert binary == :erlang.term_to_binary([{:key, "value"}])
477 assert ConfigDB.from_binary(binary) == [{:key, "value"}]
478 assert ConfigDB.from_binary(binary) == [key: "value"]
481 test "keyword with partial_chain key" do
483 ConfigDB.transform([%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}])
485 assert binary == :erlang.term_to_binary(partial_chain: &:hackney_connect.partial_chain/1)
486 assert ConfigDB.from_binary(binary) == [partial_chain: &:hackney_connect.partial_chain/1]
492 %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
493 %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
494 %{"tuple" => [":migration_lock", nil]},
495 %{"tuple" => [":key1", 150]},
496 %{"tuple" => [":key2", "string"]}
500 :erlang.term_to_binary(
501 types: Pleroma.PostgresTypes,
502 telemetry_event: [Pleroma.Repo.Instrumenter],
508 assert ConfigDB.from_binary(binary) == [
509 types: Pleroma.PostgresTypes,
510 telemetry_event: [Pleroma.Repo.Instrumenter],
517 test "complex keyword with nested mixed childs" do
520 %{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
521 %{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
522 %{"tuple" => [":link_name", true]},
523 %{"tuple" => [":proxy_remote", false]},
524 %{"tuple" => [":common_map", %{":key" => "value"}]},
529 %{"tuple" => [":redirect_on_failure", false]},
530 %{"tuple" => [":max_body_length", 1_048_576]},
534 [%{"tuple" => [":follow_redirect", true]}, %{"tuple" => [":pool", ":upload"]}]
543 :erlang.term_to_binary(
544 uploader: Pleroma.Uploaders.Local,
545 filters: [Pleroma.Upload.Filter.Dedupe],
548 common_map: %{key: "value"},
550 redirect_on_failure: false,
551 max_body_length: 1_048_576,
553 follow_redirect: true,
559 assert ConfigDB.from_binary(binary) ==
561 uploader: Pleroma.Uploaders.Local,
562 filters: [Pleroma.Upload.Filter.Dedupe],
565 common_map: %{key: "value"},
567 redirect_on_failure: false,
568 max_body_length: 1_048_576,
570 follow_redirect: true,
577 test "common keyword" do
580 %{"tuple" => [":level", ":warn"]},
581 %{"tuple" => [":meta", [":all"]]},
582 %{"tuple" => [":path", ""]},
583 %{"tuple" => [":val", nil]},
584 %{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
588 :erlang.term_to_binary(
593 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
596 assert ConfigDB.from_binary(binary) == [
601 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
605 test "complex keyword with sigil" do
608 %{"tuple" => [":federated_timeline_removal", []]},
609 %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
610 %{"tuple" => [":replace", []]}
614 :erlang.term_to_binary(
615 federated_timeline_removal: [],
616 reject: [~r/comp[lL][aA][iI][nN]er/],
620 assert ConfigDB.from_binary(binary) ==
621 [federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
624 test "complex keyword with tuples with more than 2 values" do
642 "Pleroma.Web.MastodonAPI.WebsocketHandler",
649 "Phoenix.Endpoint.CowboyWebSocket",
652 "Phoenix.Transports.WebSocket",
655 "Pleroma.Web.Endpoint",
656 "Pleroma.Web.UserSocket",
667 "Phoenix.Endpoint.Cowboy2Handler",
668 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
683 :erlang.term_to_binary(
687 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
688 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
689 {Phoenix.Transports.WebSocket,
690 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
691 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
697 assert ConfigDB.from_binary(binary) == [
702 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
703 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
704 {Phoenix.Transports.WebSocket,
705 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
706 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}