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