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.ConfigTest do
8 test "get/1 with an atom" do
9 assert Pleroma.Config.get(:instance) == Application.get_env(:pleroma, :instance)
10 assert Pleroma.Config.get(:azertyuiop) == nil
11 assert Pleroma.Config.get(:azertyuiop, true) == true
14 test "get/1 with a list of keys" do
15 assert Pleroma.Config.get([:instance, :public]) ==
16 Keyword.get(Application.get_env(:pleroma, :instance), :public)
18 assert Pleroma.Config.get([Pleroma.Web.Endpoint, :render_errors, :view]) ==
24 [:render_errors, :view]
27 assert Pleroma.Config.get([:azerty, :uiop]) == nil
28 assert Pleroma.Config.get([:azerty, :uiop], true) == true
31 describe "nil values" do
33 clear_config(:lorem, nil)
34 clear_config(:ipsum, %{dolor: [sit: nil]})
35 clear_config(:dolor, sit: %{amet: nil})
37 on_exit(fn -> Enum.each(~w(lorem ipsum dolor)a, &Pleroma.Config.delete/1) end)
40 test "get/1 with an atom for nil value" do
41 assert Pleroma.Config.get(:lorem) == nil
44 test "get/2 with an atom for nil value" do
45 assert Pleroma.Config.get(:lorem, true) == nil
48 test "get/1 with a list of keys for nil value" do
49 assert Pleroma.Config.get([:ipsum, :dolor, :sit]) == nil
50 assert Pleroma.Config.get([:dolor, :sit, :amet]) == nil
53 test "get/2 with a list of keys for nil value" do
54 assert Pleroma.Config.get([:ipsum, :dolor, :sit], true) == nil
55 assert Pleroma.Config.get([:dolor, :sit, :amet], true) == nil
59 test "get/1 when value is false" do
60 clear_config([:instance, :false_test], false)
61 clear_config([:instance, :nested], [])
62 clear_config([:instance, :nested, :false_test], false)
64 assert Pleroma.Config.get([:instance, :false_test]) == false
65 assert Pleroma.Config.get([:instance, :nested, :false_test]) == false
69 assert Pleroma.Config.get!(:instance) == Application.get_env(:pleroma, :instance)
71 assert Pleroma.Config.get!([:instance, :public]) ==
72 Keyword.get(Application.get_env(:pleroma, :instance), :public)
74 assert_raise(Pleroma.Config.Error, fn ->
75 Pleroma.Config.get!(:azertyuiop)
78 assert_raise(Pleroma.Config.Error, fn ->
79 Pleroma.Config.get!([:azerty, :uiop])
83 test "get!/1 when value is false" do
84 clear_config([:instance, :false_test], false)
85 clear_config([:instance, :nested], [])
86 clear_config([:instance, :nested, :false_test], false)
88 assert Pleroma.Config.get!([:instance, :false_test]) == false
89 assert Pleroma.Config.get!([:instance, :nested, :false_test]) == false
92 test "put/2 with a key" do
93 clear_config(:config_test, true)
95 assert Pleroma.Config.get(:config_test) == true
98 test "put/2 with a list of keys" do
99 clear_config([:instance, :config_test], true)
100 clear_config([:instance, :config_nested_test], [])
101 clear_config([:instance, :config_nested_test, :x], true)
103 assert Pleroma.Config.get([:instance, :config_test]) == true
104 assert Pleroma.Config.get([:instance, :config_nested_test, :x]) == true
107 test "delete/1 with a key" do
108 clear_config([:delete_me], :delete_me)
109 Pleroma.Config.delete([:delete_me])
110 assert Pleroma.Config.get([:delete_me]) == nil
113 test "delete/2 with a list of keys" do
114 clear_config([:delete_me], hello: "world", world: "Hello")
115 Pleroma.Config.delete([:delete_me, :world])
116 assert Pleroma.Config.get([:delete_me]) == [hello: "world"]
117 clear_config([:delete_me, :delete_me], hello: "world", world: "Hello")
118 Pleroma.Config.delete([:delete_me, :delete_me, :world])
119 assert Pleroma.Config.get([:delete_me, :delete_me]) == [hello: "world"]
121 assert Pleroma.Config.delete([:this_key_does_not_exist])
122 assert Pleroma.Config.delete([:non, :existing, :key])
126 clear_config([:lorem], :ipsum)
127 clear_config([:ipsum], dolor: :sit)
129 assert Pleroma.Config.fetch([:lorem]) == {:ok, :ipsum}
130 assert Pleroma.Config.fetch(:lorem) == {:ok, :ipsum}
131 assert Pleroma.Config.fetch([:ipsum, :dolor]) == {:ok, :sit}
132 assert Pleroma.Config.fetch([:lorem, :ipsum]) == :error
133 assert Pleroma.Config.fetch([:loremipsum]) == :error
134 assert Pleroma.Config.fetch(:loremipsum) == :error
136 Pleroma.Config.delete([:lorem])
137 Pleroma.Config.delete([:ipsum])