0d328f0c32b66f994303001bcce9667c20fdfece
[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 assert capture_log(fn -> TransferTask.start_link([]) end) =~ ""
123 end
124
125 test "restart pleroma on reboot time key" do
126 chat = Application.get_env(:pleroma, :chat)
127 on_exit(fn -> Application.put_env(:pleroma, :chat, chat) end)
128
129 ConfigDB.create(%{
130 group: ":pleroma",
131 key: ":chat",
132 value: [enabled: false]
133 })
134
135 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
136 end
137
138 test "restart pleroma on reboot time subkey" do
139 captcha = Application.get_env(:pleroma, Pleroma.Captcha)
140 on_exit(fn -> Application.put_env(:pleroma, Pleroma.Captcha, captcha) end)
141
142 ConfigDB.create(%{
143 group: ":pleroma",
144 key: "Pleroma.Captcha",
145 value: [seconds_valid: 60]
146 })
147
148 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
149 end
150
151 test "don't restart pleroma on reboot time key and subkey if there is false flag" do
152 chat = Application.get_env(:pleroma, :chat)
153 captcha = Application.get_env(:pleroma, Pleroma.Captcha)
154
155 on_exit(fn ->
156 Application.put_env(:pleroma, :chat, chat)
157 Application.put_env(:pleroma, Pleroma.Captcha, captcha)
158 end)
159
160 ConfigDB.create(%{
161 group: ":pleroma",
162 key: ":chat",
163 value: [enabled: false]
164 })
165
166 ConfigDB.create(%{
167 group: ":pleroma",
168 key: "Pleroma.Captcha",
169 value: [seconds_valid: 60]
170 })
171
172 assert capture_log(fn -> TransferTask.load_and_update_env([], false) end) =~ ""
173 end
174 end
175 end