Merge branch 'develop' into openapi/account
[akkoma] / test / config / transfer_task_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.Config.TransferTaskTest do
6 use Pleroma.DataCase
7
8 import ExUnit.CaptureLog
9
10 alias Pleroma.Config.TransferTask
11 alias Pleroma.ConfigDB
12
13 setup do: clear_config(:configurable_from_database, true)
14
15 test "transfer config values from db to env" do
16 refute Application.get_env(:pleroma, :test_key)
17 refute Application.get_env(:idna, :test_key)
18 refute Application.get_env(:quack, :test_key)
19 refute Application.get_env(:postgrex, :test_key)
20 initial = Application.get_env(:logger, :level)
21
22 ConfigDB.create(%{
23 group: ":pleroma",
24 key: ":test_key",
25 value: [live: 2, com: 3]
26 })
27
28 ConfigDB.create(%{
29 group: ":idna",
30 key: ":test_key",
31 value: [live: 15, com: 35]
32 })
33
34 ConfigDB.create(%{
35 group: ":quack",
36 key: ":test_key",
37 value: [:test_value1, :test_value2]
38 })
39
40 ConfigDB.create(%{
41 group: ":postgrex",
42 key: ":test_key",
43 value: :value
44 })
45
46 ConfigDB.create(%{group: ":logger", key: ":level", value: :debug})
47
48 TransferTask.start_link([])
49
50 assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
51 assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
52 assert Application.get_env(:quack, :test_key) == [:test_value1, :test_value2]
53 assert Application.get_env(:logger, :level) == :debug
54 assert Application.get_env(:postgrex, :test_key) == :value
55
56 on_exit(fn ->
57 Application.delete_env(:pleroma, :test_key)
58 Application.delete_env(:idna, :test_key)
59 Application.delete_env(:quack, :test_key)
60 Application.delete_env(:postgrex, :test_key)
61 Application.put_env(:logger, :level, initial)
62 end)
63 end
64
65 test "transfer config values for 1 group and some keys" do
66 level = Application.get_env(:quack, :level)
67 meta = Application.get_env(:quack, :meta)
68
69 ConfigDB.create(%{
70 group: ":quack",
71 key: ":level",
72 value: :info
73 })
74
75 ConfigDB.create(%{
76 group: ":quack",
77 key: ":meta",
78 value: [:none]
79 })
80
81 TransferTask.start_link([])
82
83 assert Application.get_env(:quack, :level) == :info
84 assert Application.get_env(:quack, :meta) == [:none]
85 default = Pleroma.Config.Holder.default_config(:quack, :webhook_url)
86 assert Application.get_env(:quack, :webhook_url) == default
87
88 on_exit(fn ->
89 Application.put_env(:quack, :level, level)
90 Application.put_env(:quack, :meta, meta)
91 end)
92 end
93
94 test "transfer config values with full subkey update" do
95 clear_config(:emoji)
96 clear_config(:assets)
97
98 ConfigDB.create(%{
99 group: ":pleroma",
100 key: ":emoji",
101 value: [groups: [a: 1, b: 2]]
102 })
103
104 ConfigDB.create(%{
105 group: ":pleroma",
106 key: ":assets",
107 value: [mascots: [a: 1, b: 2]]
108 })
109
110 TransferTask.start_link([])
111
112 emoji_env = Application.get_env(:pleroma, :emoji)
113 assert emoji_env[:groups] == [a: 1, b: 2]
114 assets_env = Application.get_env(:pleroma, :assets)
115 assert assets_env[:mascots] == [a: 1, b: 2]
116 end
117
118 describe "pleroma restart" do
119 setup do
120 on_exit(fn -> Restarter.Pleroma.refresh() end)
121 end
122
123 test "don't restart if no reboot time settings were changed" do
124 clear_config(:emoji)
125
126 ConfigDB.create(%{
127 group: ":pleroma",
128 key: ":emoji",
129 value: [groups: [a: 1, b: 2]]
130 })
131
132 refute String.contains?(
133 capture_log(fn -> TransferTask.start_link([]) end),
134 "pleroma restarted"
135 )
136 end
137
138 test "on reboot time key" do
139 clear_config(:chat)
140
141 ConfigDB.create(%{
142 group: ":pleroma",
143 key: ":chat",
144 value: [enabled: false]
145 })
146
147 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
148 end
149
150 test "on reboot time subkey" do
151 clear_config(Pleroma.Captcha)
152
153 ConfigDB.create(%{
154 group: ":pleroma",
155 key: "Pleroma.Captcha",
156 value: [seconds_valid: 60]
157 })
158
159 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
160 end
161
162 test "don't restart pleroma on reboot time key and subkey if there is false flag" do
163 clear_config(:chat)
164 clear_config(Pleroma.Captcha)
165
166 ConfigDB.create(%{
167 group: ":pleroma",
168 key: ":chat",
169 value: [enabled: false]
170 })
171
172 ConfigDB.create(%{
173 group: ":pleroma",
174 key: "Pleroma.Captcha",
175 value: [seconds_valid: 60]
176 })
177
178 refute String.contains?(
179 capture_log(fn -> TransferTask.load_and_update_env([], false) end),
180 "pleroma restarted"
181 )
182 end
183 end
184 end