2018-12-23 20:11:29 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 11:11:47 +00:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:11:29 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-06 15:00:48 +00:00
|
|
|
defmodule Pleroma.ConfigTest do
|
2020-12-21 14:05:56 +00:00
|
|
|
use Pleroma.DataCase
|
2018-11-06 15:00:48 +00:00
|
|
|
|
|
|
|
test "get/1 with an atom" do
|
|
|
|
assert Pleroma.Config.get(:instance) == Application.get_env(:pleroma, :instance)
|
|
|
|
assert Pleroma.Config.get(:azertyuiop) == nil
|
2018-11-06 18:34:57 +00:00
|
|
|
assert Pleroma.Config.get(:azertyuiop, true) == true
|
2018-11-06 15:00:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "get/1 with a list of keys" do
|
|
|
|
assert Pleroma.Config.get([:instance, :public]) ==
|
|
|
|
Keyword.get(Application.get_env(:pleroma, :instance), :public)
|
|
|
|
|
|
|
|
assert Pleroma.Config.get([Pleroma.Web.Endpoint, :render_errors, :view]) ==
|
|
|
|
get_in(
|
|
|
|
Application.get_env(
|
|
|
|
:pleroma,
|
|
|
|
Pleroma.Web.Endpoint
|
|
|
|
),
|
|
|
|
[:render_errors, :view]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Pleroma.Config.get([:azerty, :uiop]) == nil
|
2018-11-06 18:34:57 +00:00
|
|
|
assert Pleroma.Config.get([:azerty, :uiop], true) == true
|
|
|
|
end
|
|
|
|
|
2020-08-04 11:35:47 +00:00
|
|
|
describe "nil values" do
|
|
|
|
setup do
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config(:lorem, nil)
|
|
|
|
clear_config(:ipsum, %{dolor: [sit: nil]})
|
|
|
|
clear_config(:dolor, sit: %{amet: nil})
|
2020-08-04 11:35:47 +00:00
|
|
|
|
|
|
|
on_exit(fn -> Enum.each(~w(lorem ipsum dolor)a, &Pleroma.Config.delete/1) end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get/1 with an atom for nil value" do
|
|
|
|
assert Pleroma.Config.get(:lorem) == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get/2 with an atom for nil value" do
|
|
|
|
assert Pleroma.Config.get(:lorem, true) == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get/1 with a list of keys for nil value" do
|
|
|
|
assert Pleroma.Config.get([:ipsum, :dolor, :sit]) == nil
|
|
|
|
assert Pleroma.Config.get([:dolor, :sit, :amet]) == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get/2 with a list of keys for nil value" do
|
|
|
|
assert Pleroma.Config.get([:ipsum, :dolor, :sit], true) == nil
|
|
|
|
assert Pleroma.Config.get([:dolor, :sit, :amet], true) == nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-13 22:40:59 +00:00
|
|
|
test "get/1 when value is false" do
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:instance, :false_test], false)
|
|
|
|
clear_config([:instance, :nested], [])
|
|
|
|
clear_config([:instance, :nested, :false_test], false)
|
2019-05-13 22:40:59 +00:00
|
|
|
|
|
|
|
assert Pleroma.Config.get([:instance, :false_test]) == false
|
|
|
|
assert Pleroma.Config.get([:instance, :nested, :false_test]) == false
|
|
|
|
end
|
|
|
|
|
2018-11-06 18:34:57 +00:00
|
|
|
test "get!/1" do
|
|
|
|
assert Pleroma.Config.get!(:instance) == Application.get_env(:pleroma, :instance)
|
|
|
|
|
|
|
|
assert Pleroma.Config.get!([:instance, :public]) ==
|
|
|
|
Keyword.get(Application.get_env(:pleroma, :instance), :public)
|
|
|
|
|
|
|
|
assert_raise(Pleroma.Config.Error, fn ->
|
|
|
|
Pleroma.Config.get!(:azertyuiop)
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert_raise(Pleroma.Config.Error, fn ->
|
|
|
|
Pleroma.Config.get!([:azerty, :uiop])
|
|
|
|
end)
|
2018-11-06 15:00:48 +00:00
|
|
|
end
|
|
|
|
|
2019-05-13 22:40:59 +00:00
|
|
|
test "get!/1 when value is false" do
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:instance, :false_test], false)
|
|
|
|
clear_config([:instance, :nested], [])
|
|
|
|
clear_config([:instance, :nested, :false_test], false)
|
2019-05-13 22:40:59 +00:00
|
|
|
|
|
|
|
assert Pleroma.Config.get!([:instance, :false_test]) == false
|
|
|
|
assert Pleroma.Config.get!([:instance, :nested, :false_test]) == false
|
|
|
|
end
|
|
|
|
|
2018-11-06 15:00:48 +00:00
|
|
|
test "put/2 with a key" do
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config(:config_test, true)
|
2018-11-06 15:00:48 +00:00
|
|
|
|
|
|
|
assert Pleroma.Config.get(:config_test) == true
|
|
|
|
end
|
|
|
|
|
|
|
|
test "put/2 with a list of keys" do
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:instance, :config_test], true)
|
|
|
|
clear_config([:instance, :config_nested_test], [])
|
|
|
|
clear_config([:instance, :config_nested_test, :x], true)
|
2018-11-06 15:00:48 +00:00
|
|
|
|
|
|
|
assert Pleroma.Config.get([:instance, :config_test]) == true
|
|
|
|
assert Pleroma.Config.get([:instance, :config_nested_test, :x]) == true
|
|
|
|
end
|
2018-11-30 17:19:22 +00:00
|
|
|
|
|
|
|
test "delete/1 with a key" do
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:delete_me], :delete_me)
|
2018-11-30 17:19:22 +00:00
|
|
|
Pleroma.Config.delete([:delete_me])
|
|
|
|
assert Pleroma.Config.get([:delete_me]) == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete/2 with a list of keys" do
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:delete_me], hello: "world", world: "Hello")
|
2018-11-30 17:19:22 +00:00
|
|
|
Pleroma.Config.delete([:delete_me, :world])
|
|
|
|
assert Pleroma.Config.get([:delete_me]) == [hello: "world"]
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:delete_me, :delete_me], hello: "world", world: "Hello")
|
2018-11-30 17:19:22 +00:00
|
|
|
Pleroma.Config.delete([:delete_me, :delete_me, :world])
|
|
|
|
assert Pleroma.Config.get([:delete_me, :delete_me]) == [hello: "world"]
|
2020-08-05 10:00:49 +00:00
|
|
|
|
|
|
|
assert Pleroma.Config.delete([:this_key_does_not_exist])
|
|
|
|
assert Pleroma.Config.delete([:non, :existing, :key])
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fetch/1" do
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:lorem], :ipsum)
|
|
|
|
clear_config([:ipsum], dolor: :sit)
|
2020-08-05 10:00:49 +00:00
|
|
|
|
|
|
|
assert Pleroma.Config.fetch([:lorem]) == {:ok, :ipsum}
|
2020-08-05 15:23:12 +00:00
|
|
|
assert Pleroma.Config.fetch(:lorem) == {:ok, :ipsum}
|
2020-08-05 10:00:49 +00:00
|
|
|
assert Pleroma.Config.fetch([:ipsum, :dolor]) == {:ok, :sit}
|
|
|
|
assert Pleroma.Config.fetch([:lorem, :ipsum]) == :error
|
|
|
|
assert Pleroma.Config.fetch([:loremipsum]) == :error
|
2020-08-05 15:23:12 +00:00
|
|
|
assert Pleroma.Config.fetch(:loremipsum) == :error
|
2020-08-05 10:00:49 +00:00
|
|
|
|
|
|
|
Pleroma.Config.delete([:lorem])
|
|
|
|
Pleroma.Config.delete([:ipsum])
|
2018-11-30 17:19:22 +00:00
|
|
|
end
|
2018-11-06 15:00:48 +00:00
|
|
|
end
|