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.Web.AdminAPI.ConfigTest do
6 use Pleroma.DataCase, async: true
8 alias Pleroma.Web.AdminAPI.Config
10 test "get_by_key/1" do
11 config = insert(:config)
14 assert config == Config.get_by_params(%{group: config.group, key: config.key})
18 {:ok, config} = Config.create(%{group: "pleroma", key: "some_key", value: "some_value"})
19 assert config == Config.get_by_params(%{group: "pleroma", key: "some_key"})
23 config = insert(:config)
24 {:ok, updated} = Config.update(config, %{value: "some_value"})
25 loaded = Config.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(Config) |> length() == 1
41 Enum.each(params, &Config.update_or_create(&1))
43 assert Repo.all(Config) |> length() == 2
45 config1 = Config.get_by_params(%{group: config.group, key: config.key})
46 config2 = Config.get_by_params(%{group: "pleroma", key: key2})
48 assert config1.value == Config.transform("new_value")
49 assert config2.value == Config.transform("another_value")
52 test "partial update" do
53 config = insert(:config, value: Config.to_binary(key1: "val1", key2: :val2))
56 Config.update_or_create(%{
59 value: [key1: :val1, key3: :val3]
62 updated = Config.get_by_params(%{group: config.group, key: config.key})
64 value = Config.from_binary(updated.value)
65 assert length(value) == 3
66 assert value[:key1] == :val1
67 assert value[:key2] == :val2
68 assert value[:key3] == :val3
71 test "only full update for some keys" do
72 config1 = insert(:config, key: ":ecto_repos", value: Config.to_binary(repo: Pleroma.Repo))
73 config2 = insert(:config, group: ":cors_plug", key: ":max_age", value: Config.to_binary(18))
76 Config.update_or_create(%{
79 value: [another_repo: [Pleroma.Repo]]
83 Config.update_or_create(%{
89 updated1 = Config.get_by_params(%{group: config1.group, key: config1.key})
90 updated2 = Config.get_by_params(%{group: config2.group, key: config2.key})
92 assert Config.from_binary(updated1.value) == [another_repo: [Pleroma.Repo]]
93 assert Config.from_binary(updated2.value) == 777
96 test "full update if value is not keyword" do
101 value: Config.to_binary(Tesla.Adapter.Hackney)
105 Config.update_or_create(%{
108 value: Tesla.Adapter.Httpc
111 updated = Config.get_by_params(%{group: config.group, key: config.key})
113 assert Config.from_binary(updated.value) == Tesla.Adapter.Httpc
118 config = insert(:config)
119 {:ok, _} = Config.delete(%{key: config.key, group: config.group})
120 refute Config.get_by_params(%{key: config.key, group: config.group})
123 describe "transform/1" do
125 binary = Config.transform("value as string")
126 assert binary == :erlang.term_to_binary("value as string")
127 assert Config.from_binary(binary) == "value as string"
131 binary = Config.transform(false)
132 assert binary == :erlang.term_to_binary(false)
133 assert Config.from_binary(binary) == false
137 binary = Config.transform(nil)
138 assert binary == :erlang.term_to_binary(nil)
139 assert Config.from_binary(binary) == nil
143 binary = Config.transform(150)
144 assert binary == :erlang.term_to_binary(150)
145 assert Config.from_binary(binary) == 150
149 binary = Config.transform(":atom")
150 assert binary == :erlang.term_to_binary(:atom)
151 assert Config.from_binary(binary) == :atom
154 test "pleroma module" do
155 binary = Config.transform("Pleroma.Bookmark")
156 assert binary == :erlang.term_to_binary(Pleroma.Bookmark)
157 assert Config.from_binary(binary) == Pleroma.Bookmark
160 test "pleroma string" do
161 binary = Config.transform("Pleroma")
162 assert binary == :erlang.term_to_binary("Pleroma")
163 assert Config.from_binary(binary) == "Pleroma"
166 test "phoenix module" do
167 binary = Config.transform("Phoenix.Socket.V1.JSONSerializer")
168 assert binary == :erlang.term_to_binary(Phoenix.Socket.V1.JSONSerializer)
169 assert Config.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
172 test "tesla module" do
173 binary = Config.transform("Tesla.Adapter.Hackney")
174 assert binary == :erlang.term_to_binary(Tesla.Adapter.Hackney)
175 assert Config.from_binary(binary) == Tesla.Adapter.Hackney
179 binary = Config.transform("~r[comp[lL][aA][iI][nN]er]")
180 assert binary == :erlang.term_to_binary(~r/comp[lL][aA][iI][nN]er/)
181 assert Config.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
185 binary = Config.transform("~r/https:\/\/example.com/")
186 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/)
187 assert Config.from_binary(binary) == ~r/https:\/\/example.com/
190 test "link sigil with um modifiers" do
191 binary = Config.transform("~r/https:\/\/example.com/um")
192 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/um)
193 assert Config.from_binary(binary) == ~r/https:\/\/example.com/um
196 test "link sigil with i modifier" do
197 binary = Config.transform("~r/https:\/\/example.com/i")
198 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i)
199 assert Config.from_binary(binary) == ~r/https:\/\/example.com/i
202 test "link sigil with s modifier" do
203 binary = Config.transform("~r/https:\/\/example.com/s")
204 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s)
205 assert Config.from_binary(binary) == ~r/https:\/\/example.com/s
208 test "raise if valid delimiter not found" do
209 assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
210 Config.transform("~r/https://[]{}<>\"'()|example.com/s")
214 test "2 child tuple" do
215 binary = Config.transform(%{"tuple" => ["v1", ":v2"]})
216 assert binary == :erlang.term_to_binary({"v1", :v2})
217 assert Config.from_binary(binary) == {"v1", :v2}
220 test "proxy tuple with localhost" do
223 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
226 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, :localhost, 1234}})
227 assert Config.from_binary(binary) == {:proxy_url, {:socks5, :localhost, 1234}}
230 test "proxy tuple with domain" do
233 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
236 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, 'domain.com', 1234}})
237 assert Config.from_binary(binary) == {:proxy_url, {:socks5, 'domain.com', 1234}}
240 test "proxy tuple with ip" do
243 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
246 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}})
247 assert Config.from_binary(binary) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
250 test "tuple with n childs" do
259 "Phoenix.Socket.V1.JSONSerializer"
264 :erlang.term_to_binary(
265 {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
268 assert Config.from_binary(binary) ==
269 {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
272 test "tuple with dispatch key" do
273 binary = Config.transform(%{"tuple" => [":dispatch", ["{:_,
275 {\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
276 {\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
277 {Phoenix.Transports.WebSocket,
278 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
279 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
283 :erlang.term_to_binary(
288 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
289 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
290 {Phoenix.Transports.WebSocket,
291 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
292 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
297 assert Config.from_binary(binary) ==
302 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
303 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
304 {Phoenix.Transports.WebSocket,
305 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
306 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
311 test "map with string key" do
312 binary = Config.transform(%{"key" => "value"})
313 assert binary == :erlang.term_to_binary(%{"key" => "value"})
314 assert Config.from_binary(binary) == %{"key" => "value"}
317 test "map with atom key" do
318 binary = Config.transform(%{":key" => "value"})
319 assert binary == :erlang.term_to_binary(%{key: "value"})
320 assert Config.from_binary(binary) == %{key: "value"}
323 test "list of strings" do
324 binary = Config.transform(["v1", "v2", "v3"])
325 assert binary == :erlang.term_to_binary(["v1", "v2", "v3"])
326 assert Config.from_binary(binary) == ["v1", "v2", "v3"]
329 test "list of modules" do
330 binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"])
331 assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
332 assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
335 test "list of atoms" do
336 binary = Config.transform([":v1", ":v2", ":v3"])
337 assert binary == :erlang.term_to_binary([:v1, :v2, :v3])
338 assert Config.from_binary(binary) == [:v1, :v2, :v3]
341 test "list of mixed values" do
347 "Phoenix.Socket.V1.JSONSerializer",
353 :erlang.term_to_binary([
357 Phoenix.Socket.V1.JSONSerializer,
362 assert Config.from_binary(binary) == [
366 Phoenix.Socket.V1.JSONSerializer,
372 test "simple keyword" do
373 binary = Config.transform([%{"tuple" => [":key", "value"]}])
374 assert binary == :erlang.term_to_binary([{:key, "value"}])
375 assert Config.from_binary(binary) == [{:key, "value"}]
376 assert Config.from_binary(binary) == [key: "value"]
379 test "keyword with partial_chain key" do
381 Config.transform([%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}])
383 assert binary == :erlang.term_to_binary(partial_chain: &:hackney_connect.partial_chain/1)
384 assert Config.from_binary(binary) == [partial_chain: &:hackney_connect.partial_chain/1]
390 %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
391 %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
392 %{"tuple" => [":migration_lock", nil]},
393 %{"tuple" => [":key1", 150]},
394 %{"tuple" => [":key2", "string"]}
398 :erlang.term_to_binary(
399 types: Pleroma.PostgresTypes,
400 telemetry_event: [Pleroma.Repo.Instrumenter],
406 assert Config.from_binary(binary) == [
407 types: Pleroma.PostgresTypes,
408 telemetry_event: [Pleroma.Repo.Instrumenter],
415 test "complex keyword with nested mixed childs" do
418 %{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
419 %{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
420 %{"tuple" => [":link_name", true]},
421 %{"tuple" => [":proxy_remote", false]},
422 %{"tuple" => [":common_map", %{":key" => "value"}]},
427 %{"tuple" => [":redirect_on_failure", false]},
428 %{"tuple" => [":max_body_length", 1_048_576]},
432 [%{"tuple" => [":follow_redirect", true]}, %{"tuple" => [":pool", ":upload"]}]
441 :erlang.term_to_binary(
442 uploader: Pleroma.Uploaders.Local,
443 filters: [Pleroma.Upload.Filter.Dedupe],
446 common_map: %{key: "value"},
448 redirect_on_failure: false,
449 max_body_length: 1_048_576,
451 follow_redirect: true,
457 assert Config.from_binary(binary) ==
459 uploader: Pleroma.Uploaders.Local,
460 filters: [Pleroma.Upload.Filter.Dedupe],
463 common_map: %{key: "value"},
465 redirect_on_failure: false,
466 max_body_length: 1_048_576,
468 follow_redirect: true,
475 test "common keyword" do
478 %{"tuple" => [":level", ":warn"]},
479 %{"tuple" => [":meta", [":all"]]},
480 %{"tuple" => [":path", ""]},
481 %{"tuple" => [":val", nil]},
482 %{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
486 :erlang.term_to_binary(
491 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
494 assert Config.from_binary(binary) == [
499 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
503 test "complex keyword with sigil" do
506 %{"tuple" => [":federated_timeline_removal", []]},
507 %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
508 %{"tuple" => [":replace", []]}
512 :erlang.term_to_binary(
513 federated_timeline_removal: [],
514 reject: [~r/comp[lL][aA][iI][nN]er/],
518 assert Config.from_binary(binary) ==
519 [federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
522 test "complex keyword with tuples with more than 2 values" do
540 "Pleroma.Web.MastodonAPI.WebsocketHandler",
547 "Phoenix.Endpoint.CowboyWebSocket",
550 "Phoenix.Transports.WebSocket",
553 "Pleroma.Web.Endpoint",
554 "Pleroma.Web.UserSocket",
565 "Phoenix.Endpoint.Cowboy2Handler",
566 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
581 :erlang.term_to_binary(
585 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
586 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
587 {Phoenix.Transports.WebSocket,
588 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
589 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
595 assert Config.from_binary(binary) == [
600 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
601 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
602 {Phoenix.Transports.WebSocket,
603 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
604 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}