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