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 describe "update_or_create/1" do
31 config = insert(:config)
35 %{group: "pleroma", key: key2, value: "another_value"},
36 %{group: config.group, key: config.key, value: "new_value"}
39 assert Repo.all(ConfigDB) |> length() == 1
41 Enum.each(params, &ConfigDB.update_or_create(&1))
43 assert Repo.all(ConfigDB) |> length() == 2
45 config1 = ConfigDB.get_by_params(%{group: config.group, key: config.key})
46 config2 = ConfigDB.get_by_params(%{group: "pleroma", key: key2})
48 assert config1.value == ConfigDB.transform("new_value")
49 assert config2.value == ConfigDB.transform("another_value")
52 test "partial update" do
53 config = insert(:config, value: ConfigDB.to_binary(key1: "val1", key2: :val2))
56 ConfigDB.update_or_create(%{
59 value: [key1: :val1, key3: :val3]
62 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
64 value = ConfigDB.from_binary(updated.value)
65 assert length(value) == 3
66 assert value[:key1] == :val1
67 assert value[:key2] == :val2
68 assert value[:key3] == :val3
72 config = insert(:config, value: ConfigDB.to_binary(key1: "val1", key2: [k1: :v1, k2: "v2"]))
75 ConfigDB.update_or_create(%{
78 value: [key1: :val1, key2: [k2: :v2, k3: :v3], key3: :val3]
81 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
83 assert config.value == updated.value
85 value = ConfigDB.from_binary(updated.value)
86 assert value[:key1] == :val1
87 assert value[:key2] == [k1: :v1, k2: :v2, k3: :v3]
88 assert value[:key3] == :val3
91 test "only full update for some keys" do
92 config1 = insert(:config, key: ":ecto_repos", value: ConfigDB.to_binary(repo: Pleroma.Repo))
95 insert(:config, group: ":cors_plug", key: ":max_age", value: ConfigDB.to_binary(18))
98 ConfigDB.update_or_create(%{
101 value: [another_repo: [Pleroma.Repo]]
105 ConfigDB.update_or_create(%{
106 group: config2.group,
111 updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
112 updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})
114 assert ConfigDB.from_binary(updated1.value) == [another_repo: [Pleroma.Repo]]
115 assert ConfigDB.from_binary(updated2.value) == 777
118 test "full update if value is not keyword" do
123 value: ConfigDB.to_binary(Tesla.Adapter.Hackney)
127 ConfigDB.update_or_create(%{
130 value: Tesla.Adapter.Httpc
133 updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})
135 assert ConfigDB.from_binary(updated.value) == Tesla.Adapter.Httpc
140 config = insert(:config)
141 {:ok, _} = ConfigDB.delete(%{key: config.key, group: config.group})
142 refute ConfigDB.get_by_params(%{key: config.key, group: config.group})
145 describe "transform/1" do
147 binary = ConfigDB.transform("value as string")
148 assert binary == :erlang.term_to_binary("value as string")
149 assert ConfigDB.from_binary(binary) == "value as string"
153 binary = ConfigDB.transform(false)
154 assert binary == :erlang.term_to_binary(false)
155 assert ConfigDB.from_binary(binary) == false
159 binary = ConfigDB.transform(nil)
160 assert binary == :erlang.term_to_binary(nil)
161 assert ConfigDB.from_binary(binary) == nil
165 binary = ConfigDB.transform(150)
166 assert binary == :erlang.term_to_binary(150)
167 assert ConfigDB.from_binary(binary) == 150
171 binary = ConfigDB.transform(":atom")
172 assert binary == :erlang.term_to_binary(:atom)
173 assert ConfigDB.from_binary(binary) == :atom
176 test "ssl options" do
177 binary = ConfigDB.transform([":tlsv1", ":tlsv1.1", ":tlsv1.2"])
178 assert binary == :erlang.term_to_binary([:tlsv1, :"tlsv1.1", :"tlsv1.2"])
179 assert ConfigDB.from_binary(binary) == [:tlsv1, :"tlsv1.1", :"tlsv1.2"]
182 test "pleroma module" do
183 binary = ConfigDB.transform("Pleroma.Bookmark")
184 assert binary == :erlang.term_to_binary(Pleroma.Bookmark)
185 assert ConfigDB.from_binary(binary) == Pleroma.Bookmark
188 test "pleroma string" do
189 binary = ConfigDB.transform("Pleroma")
190 assert binary == :erlang.term_to_binary("Pleroma")
191 assert ConfigDB.from_binary(binary) == "Pleroma"
194 test "phoenix module" do
195 binary = ConfigDB.transform("Phoenix.Socket.V1.JSONSerializer")
196 assert binary == :erlang.term_to_binary(Phoenix.Socket.V1.JSONSerializer)
197 assert ConfigDB.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
200 test "tesla module" do
201 binary = ConfigDB.transform("Tesla.Adapter.Hackney")
202 assert binary == :erlang.term_to_binary(Tesla.Adapter.Hackney)
203 assert ConfigDB.from_binary(binary) == Tesla.Adapter.Hackney
206 test "ExSyslogger module" do
207 binary = ConfigDB.transform("ExSyslogger")
208 assert binary == :erlang.term_to_binary(ExSyslogger)
209 assert ConfigDB.from_binary(binary) == ExSyslogger
212 test "Quack.Logger module" do
213 binary = ConfigDB.transform("Quack.Logger")
214 assert binary == :erlang.term_to_binary(Quack.Logger)
215 assert ConfigDB.from_binary(binary) == Quack.Logger
219 binary = ConfigDB.transform("~r[comp[lL][aA][iI][nN]er]")
220 assert binary == :erlang.term_to_binary(~r/comp[lL][aA][iI][nN]er/)
221 assert ConfigDB.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
225 binary = ConfigDB.transform("~r/https:\/\/example.com/")
226 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/)
227 assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/
230 test "link sigil with um modifiers" do
231 binary = ConfigDB.transform("~r/https:\/\/example.com/um")
232 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/um)
233 assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/um
236 test "link sigil with i modifier" do
237 binary = ConfigDB.transform("~r/https:\/\/example.com/i")
238 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i)
239 assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/i
242 test "link sigil with s modifier" do
243 binary = ConfigDB.transform("~r/https:\/\/example.com/s")
244 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s)
245 assert ConfigDB.from_binary(binary) == ~r/https:\/\/example.com/s
248 test "raise if valid delimiter not found" do
249 assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
250 ConfigDB.transform("~r/https://[]{}<>\"'()|example.com/s")
254 test "2 child tuple" do
255 binary = ConfigDB.transform(%{"tuple" => ["v1", ":v2"]})
256 assert binary == :erlang.term_to_binary({"v1", :v2})
257 assert ConfigDB.from_binary(binary) == {"v1", :v2}
260 test "proxy tuple with localhost" do
262 ConfigDB.transform(%{
263 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
266 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, :localhost, 1234}})
267 assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, :localhost, 1234}}
270 test "proxy tuple with domain" do
272 ConfigDB.transform(%{
273 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
276 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, 'domain.com', 1234}})
277 assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, 'domain.com', 1234}}
280 test "proxy tuple with ip" do
282 ConfigDB.transform(%{
283 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
286 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}})
287 assert ConfigDB.from_binary(binary) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
290 test "tuple with n childs" do
292 ConfigDB.transform(%{
299 "Phoenix.Socket.V1.JSONSerializer"
304 :erlang.term_to_binary(
305 {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
308 assert ConfigDB.from_binary(binary) ==
309 {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
312 test "tuple with dispatch key" do
313 binary = ConfigDB.transform(%{"tuple" => [":dispatch", ["{:_,
315 {\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
316 {\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
317 {Phoenix.Transports.WebSocket,
318 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
319 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
323 :erlang.term_to_binary(
328 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
329 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
330 {Phoenix.Transports.WebSocket,
331 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
332 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
337 assert ConfigDB.from_binary(binary) ==
342 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
343 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
344 {Phoenix.Transports.WebSocket,
345 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
346 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
351 test "map with string key" do
352 binary = ConfigDB.transform(%{"key" => "value"})
353 assert binary == :erlang.term_to_binary(%{"key" => "value"})
354 assert ConfigDB.from_binary(binary) == %{"key" => "value"}
357 test "map with atom key" do
358 binary = ConfigDB.transform(%{":key" => "value"})
359 assert binary == :erlang.term_to_binary(%{key: "value"})
360 assert ConfigDB.from_binary(binary) == %{key: "value"}
363 test "list of strings" do
364 binary = ConfigDB.transform(["v1", "v2", "v3"])
365 assert binary == :erlang.term_to_binary(["v1", "v2", "v3"])
366 assert ConfigDB.from_binary(binary) == ["v1", "v2", "v3"]
369 test "list of modules" do
370 binary = ConfigDB.transform(["Pleroma.Repo", "Pleroma.Activity"])
371 assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
372 assert ConfigDB.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
375 test "list of atoms" do
376 binary = ConfigDB.transform([":v1", ":v2", ":v3"])
377 assert binary == :erlang.term_to_binary([:v1, :v2, :v3])
378 assert ConfigDB.from_binary(binary) == [:v1, :v2, :v3]
381 test "list of mixed values" do
387 "Phoenix.Socket.V1.JSONSerializer",
393 :erlang.term_to_binary([
397 Phoenix.Socket.V1.JSONSerializer,
402 assert ConfigDB.from_binary(binary) == [
406 Phoenix.Socket.V1.JSONSerializer,
412 test "simple keyword" do
413 binary = ConfigDB.transform([%{"tuple" => [":key", "value"]}])
414 assert binary == :erlang.term_to_binary([{:key, "value"}])
415 assert ConfigDB.from_binary(binary) == [{:key, "value"}]
416 assert ConfigDB.from_binary(binary) == [key: "value"]
419 test "keyword with partial_chain key" do
421 ConfigDB.transform([%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}])
423 assert binary == :erlang.term_to_binary(partial_chain: &:hackney_connect.partial_chain/1)
424 assert ConfigDB.from_binary(binary) == [partial_chain: &:hackney_connect.partial_chain/1]
430 %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
431 %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
432 %{"tuple" => [":migration_lock", nil]},
433 %{"tuple" => [":key1", 150]},
434 %{"tuple" => [":key2", "string"]}
438 :erlang.term_to_binary(
439 types: Pleroma.PostgresTypes,
440 telemetry_event: [Pleroma.Repo.Instrumenter],
446 assert ConfigDB.from_binary(binary) == [
447 types: Pleroma.PostgresTypes,
448 telemetry_event: [Pleroma.Repo.Instrumenter],
455 test "complex keyword with nested mixed childs" do
458 %{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
459 %{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
460 %{"tuple" => [":link_name", true]},
461 %{"tuple" => [":proxy_remote", false]},
462 %{"tuple" => [":common_map", %{":key" => "value"}]},
467 %{"tuple" => [":redirect_on_failure", false]},
468 %{"tuple" => [":max_body_length", 1_048_576]},
472 [%{"tuple" => [":follow_redirect", true]}, %{"tuple" => [":pool", ":upload"]}]
481 :erlang.term_to_binary(
482 uploader: Pleroma.Uploaders.Local,
483 filters: [Pleroma.Upload.Filter.Dedupe],
486 common_map: %{key: "value"},
488 redirect_on_failure: false,
489 max_body_length: 1_048_576,
491 follow_redirect: true,
497 assert ConfigDB.from_binary(binary) ==
499 uploader: Pleroma.Uploaders.Local,
500 filters: [Pleroma.Upload.Filter.Dedupe],
503 common_map: %{key: "value"},
505 redirect_on_failure: false,
506 max_body_length: 1_048_576,
508 follow_redirect: true,
515 test "common keyword" do
518 %{"tuple" => [":level", ":warn"]},
519 %{"tuple" => [":meta", [":all"]]},
520 %{"tuple" => [":path", ""]},
521 %{"tuple" => [":val", nil]},
522 %{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
526 :erlang.term_to_binary(
531 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
534 assert ConfigDB.from_binary(binary) == [
539 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
543 test "complex keyword with sigil" do
546 %{"tuple" => [":federated_timeline_removal", []]},
547 %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
548 %{"tuple" => [":replace", []]}
552 :erlang.term_to_binary(
553 federated_timeline_removal: [],
554 reject: [~r/comp[lL][aA][iI][nN]er/],
558 assert ConfigDB.from_binary(binary) ==
559 [federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
562 test "complex keyword with tuples with more than 2 values" do
580 "Pleroma.Web.MastodonAPI.WebsocketHandler",
587 "Phoenix.Endpoint.CowboyWebSocket",
590 "Phoenix.Transports.WebSocket",
593 "Pleroma.Web.Endpoint",
594 "Pleroma.Web.UserSocket",
605 "Phoenix.Endpoint.Cowboy2Handler",
606 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
621 :erlang.term_to_binary(
625 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
626 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
627 {Phoenix.Transports.WebSocket,
628 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
629 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
635 assert ConfigDB.from_binary(binary) == [
640 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
641 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
642 {Phoenix.Transports.WebSocket,
643 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
644 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}