Merge branch 'docs/kyclos' into 'develop'
[akkoma] / test / config / transfer_task_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.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 test "don't restart if no reboot time settings were changed" do
113 emoji = Application.get_env(:pleroma, :emoji)
114 on_exit(fn -> Application.put_env(:pleroma, :emoji, emoji) end)
115
116 ConfigDB.create(%{
117 group: ":pleroma",
118 key: ":emoji",
119 value: [groups: [a: 1, b: 2]]
120 })
121
122 refute String.contains?(
123 capture_log(fn -> TransferTask.start_link([]) end),
124 "pleroma restarted"
125 )
126 end
127
128 test "restart pleroma on reboot time key" do
129 chat = Application.get_env(:pleroma, :chat)
130 on_exit(fn -> Application.put_env(:pleroma, :chat, chat) end)
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 "restart pleroma on reboot time subkey" do
142 captcha = Application.get_env(:pleroma, Pleroma.Captcha)
143 on_exit(fn -> Application.put_env(:pleroma, Pleroma.Captcha, captcha) end)
144
145 ConfigDB.create(%{
146 group: ":pleroma",
147 key: "Pleroma.Captcha",
148 value: [seconds_valid: 60]
149 })
150
151 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
152 end
153
154 test "don't restart pleroma on reboot time key and subkey if there is false flag" do
155 chat = Application.get_env(:pleroma, :chat)
156 captcha = Application.get_env(:pleroma, Pleroma.Captcha)
157
158 on_exit(fn ->
159 Application.put_env(:pleroma, :chat, chat)
160 Application.put_env(:pleroma, Pleroma.Captcha, captcha)
161 end)
162
163 ConfigDB.create(%{
164 group: ":pleroma",
165 key: ":chat",
166 value: [enabled: false]
167 })
168
169 ConfigDB.create(%{
170 group: ":pleroma",
171 key: "Pleroma.Captcha",
172 value: [seconds_valid: 60]
173 })
174
175 refute String.contains?(
176 capture_log(fn -> TransferTask.load_and_update_env([], false) end),
177 "pleroma restarted"
178 )
179 end
180 end
181 end