Unify Config.get behaviour for atom/list key param
[akkoma] / test / config_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ConfigTest do
6 use ExUnit.Case
7
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
12 end
13
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)
17
18 assert Pleroma.Config.get([Pleroma.Web.Endpoint, :render_errors, :view]) ==
19 get_in(
20 Application.get_env(
21 :pleroma,
22 Pleroma.Web.Endpoint
23 ),
24 [:render_errors, :view]
25 )
26
27 assert Pleroma.Config.get([:azerty, :uiop]) == nil
28 assert Pleroma.Config.get([:azerty, :uiop], true) == true
29 end
30
31 describe "nil values" do
32 setup do
33 Pleroma.Config.put(:lorem, nil)
34 Pleroma.Config.put(:ipsum, %{dolor: [sit: nil]})
35 Pleroma.Config.put(:dolor, sit: %{amet: nil})
36
37 on_exit(fn -> Enum.each(~w(lorem ipsum dolor)a, &Pleroma.Config.delete/1) end)
38 end
39
40 test "get/1 with an atom for nil value" do
41 assert Pleroma.Config.get(:lorem) == nil
42 end
43
44 test "get/2 with an atom for nil value" do
45 assert Pleroma.Config.get(:lorem, true) == nil
46 end
47
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
51 end
52
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
56 end
57 end
58
59 test "get/1 when value is false" do
60 Pleroma.Config.put([:instance, :false_test], false)
61 Pleroma.Config.put([:instance, :nested], [])
62 Pleroma.Config.put([:instance, :nested, :false_test], false)
63
64 assert Pleroma.Config.get([:instance, :false_test]) == false
65 assert Pleroma.Config.get([:instance, :nested, :false_test]) == false
66 end
67
68 test "get!/1" do
69 assert Pleroma.Config.get!(:instance) == Application.get_env(:pleroma, :instance)
70
71 assert Pleroma.Config.get!([:instance, :public]) ==
72 Keyword.get(Application.get_env(:pleroma, :instance), :public)
73
74 assert_raise(Pleroma.Config.Error, fn ->
75 Pleroma.Config.get!(:azertyuiop)
76 end)
77
78 assert_raise(Pleroma.Config.Error, fn ->
79 Pleroma.Config.get!([:azerty, :uiop])
80 end)
81 end
82
83 test "get!/1 when value is false" do
84 Pleroma.Config.put([:instance, :false_test], false)
85 Pleroma.Config.put([:instance, :nested], [])
86 Pleroma.Config.put([:instance, :nested, :false_test], false)
87
88 assert Pleroma.Config.get!([:instance, :false_test]) == false
89 assert Pleroma.Config.get!([:instance, :nested, :false_test]) == false
90 end
91
92 test "put/2 with a key" do
93 Pleroma.Config.put(:config_test, true)
94
95 assert Pleroma.Config.get(:config_test) == true
96 end
97
98 test "put/2 with a list of keys" do
99 Pleroma.Config.put([:instance, :config_test], true)
100 Pleroma.Config.put([:instance, :config_nested_test], [])
101 Pleroma.Config.put([:instance, :config_nested_test, :x], true)
102
103 assert Pleroma.Config.get([:instance, :config_test]) == true
104 assert Pleroma.Config.get([:instance, :config_nested_test, :x]) == true
105 end
106
107 test "delete/1 with a key" do
108 Pleroma.Config.put([:delete_me], :delete_me)
109 Pleroma.Config.delete([:delete_me])
110 assert Pleroma.Config.get([:delete_me]) == nil
111 end
112
113 test "delete/2 with a list of keys" do
114 Pleroma.Config.put([:delete_me], hello: "world", world: "Hello")
115 Pleroma.Config.delete([:delete_me, :world])
116 assert Pleroma.Config.get([:delete_me]) == [hello: "world"]
117 Pleroma.Config.put([: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"]
120 end
121 end