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