988214eb158ddced9f7a7bbd62208c74706dbfbd
[akkoma] / test / pleroma / config / transfer_task_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 import Pleroma.Factory
10
11 alias Pleroma.Config.TransferTask
12
13 setup do
14 clear_config(:configurable_from_database, true)
15 end
16
17 test "transfer config values from db to env" do
18 refute Application.get_env(:pleroma, :test_key)
19 refute Application.get_env(:idna, :test_key)
20 refute Application.get_env(:quack, :test_key)
21 refute Application.get_env(:postgrex, :test_key)
22
23 initial = Application.get_env(:logger, :level)
24
25 insert(:config, key: :test_key, value: [live: 2, com: 3])
26 insert(:config, group: :idna, key: :test_key, value: [live: 15, com: 35])
27 insert(:config, group: :quack, key: :test_key, value: [:test_value1, :test_value2])
28 insert(:config, group: :postgrex, key: :test_key, value: :value)
29 insert(:config, group: :logger, key: :level, value: :debug)
30 insert(:config, group: :pleroma, key: :instance, value: [static_dir: "static_dir_from_db"])
31 TransferTask.start_link([])
32
33 assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
34 assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
35 assert Application.get_env(:quack, :test_key) == [:test_value1, :test_value2]
36 assert Application.get_env(:logger, :level) == :debug
37 assert Application.get_env(:postgrex, :test_key) == :value
38 assert Application.get_env(:pleroma, :instance)[:static_dir] == "static_dir_from_db"
39
40 on_exit(fn ->
41 Application.delete_env(:pleroma, :test_key)
42 Application.delete_env(:idna, :test_key)
43 Application.delete_env(:quack, :test_key)
44 Application.delete_env(:postgrex, :test_key)
45 Application.put_env(:logger, :level, initial)
46 System.delete_env("RELEASE_NAME")
47 end)
48 end
49
50 test "transfer task falls back to env before default" do
51 instance = Application.get_env(:pleroma, :instance)
52
53 insert(:config, key: :instance, value: [name: "wow"])
54 clear_config([:instance, :static_dir], "static_dir_from_env")
55 TransferTask.start_link([])
56
57 assert Application.get_env(:pleroma, :instance)[:name] == "wow"
58 assert Application.get_env(:pleroma, :instance)[:static_dir] == "static_dir_from_env"
59
60 on_exit(fn ->
61 Application.put_env(:pleroma, :instance, instance)
62 end)
63 end
64
65 test "transfer task falls back to release defaults if no other values found" do
66 instance = Application.get_env(:pleroma, :instance)
67
68 System.put_env("RELEASE_NAME", "akkoma")
69 Pleroma.Config.Holder.save_default()
70 insert(:config, key: :instance, value: [name: "wow"])
71 Application.delete_env(:pleroma, :instance)
72
73 TransferTask.start_link([])
74
75 assert Application.get_env(:pleroma, :instance)[:name] == "wow"
76 assert Application.get_env(:pleroma, :instance)[:static_dir] == "/var/lib/akkoma/static"
77
78 on_exit(fn ->
79 System.delete_env("RELEASE_NAME")
80 Pleroma.Config.Holder.save_default()
81 Application.put_env(:pleroma, :instance, instance)
82 end)
83 end
84
85 test "transfer config values for 1 group and some keys" do
86 level = Application.get_env(:quack, :level)
87 meta = Application.get_env(:quack, :meta)
88
89 insert(:config, group: :quack, key: :level, value: :info)
90 insert(:config, group: :quack, key: :meta, value: [:none])
91
92 TransferTask.start_link([])
93
94 assert Application.get_env(:quack, :level) == :info
95 assert Application.get_env(:quack, :meta) == [:none]
96 default = Pleroma.Config.Holder.default_config(:quack, :webhook_url)
97 assert Application.get_env(:quack, :webhook_url) == default
98
99 on_exit(fn ->
100 Application.put_env(:quack, :level, level)
101 Application.put_env(:quack, :meta, meta)
102 end)
103 end
104
105 test "transfer config values with full subkey update" do
106 clear_config(:emoji)
107 clear_config(:assets)
108
109 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
110 insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]])
111
112 TransferTask.start_link([])
113
114 emoji_env = Application.get_env(:pleroma, :emoji)
115 assert emoji_env[:groups] == [a: 1, b: 2]
116 assets_env = Application.get_env(:pleroma, :assets)
117 assert assets_env[:mascots] == [a: 1, b: 2]
118 end
119
120 describe "pleroma restart" do
121 setup do
122 on_exit(fn ->
123 Restarter.Pleroma.refresh()
124
125 # Restarter.Pleroma.refresh/0 is an asynchronous call.
126 # A GenServer will first finish the previous call before starting a new one.
127 # Here we do a synchronous call.
128 # That way we are sure that the previous call has finished before we continue.
129 # See https://stackoverflow.com/questions/51361856/how-to-use-task-await-with-genserver
130 Restarter.Pleroma.rebooted?()
131 end)
132 end
133
134 test "don't restart if no reboot time settings were changed" do
135 clear_config(:emoji)
136 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
137
138 refute String.contains?(
139 capture_log(fn ->
140 TransferTask.start_link([])
141
142 # TransferTask.start_link/1 is an asynchronous call.
143 # A GenServer will first finish the previous call before starting a new one.
144 # Here we do a synchronous call.
145 # That way we are sure that the previous call has finished before we continue.
146 Restarter.Pleroma.rebooted?()
147 end),
148 "pleroma restarted"
149 )
150 end
151
152 test "on reboot time key" do
153 clear_config(:rate_limit)
154 insert(:config, key: :rate_limit, value: [enabled: false])
155
156 # Note that we don't actually restart Pleroma.
157 # See module Restarter.Pleroma
158 assert capture_log(fn ->
159 TransferTask.start_link([])
160
161 # TransferTask.start_link/1 is an asynchronous call.
162 # A GenServer will first finish the previous call before starting a new one.
163 # Here we do a synchronous call.
164 # That way we are sure that the previous call has finished before we continue.
165 Restarter.Pleroma.rebooted?()
166 end) =~ "pleroma restarted"
167 end
168
169 test "on reboot time subkey" do
170 clear_config(Pleroma.Captcha)
171 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
172
173 # Note that we don't actually restart Pleroma.
174 # See module Restarter.Pleroma
175 assert capture_log(fn ->
176 TransferTask.start_link([])
177
178 # TransferTask.start_link/1 is an asynchronous call.
179 # A GenServer will first finish the previous call before starting a new one.
180 # Here we do a synchronous call.
181 # That way we are sure that the previous call has finished before we continue.
182 Restarter.Pleroma.rebooted?()
183 end) =~ "pleroma restarted"
184 end
185
186 test "don't restart pleroma on reboot time key and subkey if there is false flag" do
187 clear_config(:rate_limit)
188 clear_config(Pleroma.Captcha)
189
190 insert(:config, key: :rate_limit, value: [enabled: false])
191 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
192
193 refute String.contains?(
194 capture_log(fn ->
195 TransferTask.load_and_update_env([], false)
196
197 # TransferTask.start_link/1 is an asynchronous call.
198 # A GenServer will first finish the previous call before starting a new one.
199 # Here we do a synchronous call.
200 # That way we are sure that the previous call has finished before we continue.
201 Restarter.Pleroma.rebooted?()
202 end),
203 "pleroma restarted"
204 )
205 end
206 end
207 end