Merge remote-tracking branch 'upstream/develop' into simplepolicy-announce-leak
[akkoma] / test / mix / tasks / pleroma / config_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 Mix.Tasks.Pleroma.ConfigTest do
6 use Pleroma.DataCase
7
8 import Pleroma.Factory
9
10 alias Mix.Tasks.Pleroma.Config, as: MixTask
11 alias Pleroma.ConfigDB
12 alias Pleroma.Repo
13
14 setup_all do
15 Mix.shell(Mix.Shell.Process)
16
17 on_exit(fn ->
18 Mix.shell(Mix.Shell.IO)
19 Application.delete_env(:pleroma, :first_setting)
20 Application.delete_env(:pleroma, :second_setting)
21 end)
22
23 :ok
24 end
25
26 defp config_records do
27 ConfigDB
28 |> Repo.all()
29 |> Enum.sort()
30 end
31
32 defp insert_config_record(group, key, value) do
33 insert(:config,
34 group: group,
35 key: key,
36 value: value
37 )
38 end
39
40 test "error if file with custom settings doesn't exist" do
41 MixTask.migrate_to_db("config/non_existent_config_file.exs")
42
43 msg =
44 "To migrate settings, you must define custom settings in config/non_existent_config_file.exs."
45
46 assert_receive {:mix_shell, :info, [^msg]}, 15
47 end
48
49 describe "migrate_to_db/1" do
50 setup do
51 clear_config(:configurable_from_database, true)
52 clear_config([:quack, :level])
53 end
54
55 @tag capture_log: true
56 test "config migration refused when deprecated settings are found" do
57 clear_config([:media_proxy, :whitelist], ["domain_without_scheme.com"])
58 assert config_records() == []
59
60 MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
61
62 assert_received {:mix_shell, :error, [message]}
63
64 assert message =~
65 "Migration is not allowed until all deprecation warnings have been resolved."
66 end
67
68 test "filtered settings are migrated to db" do
69 assert config_records() == []
70
71 MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
72
73 config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
74 config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})
75 config3 = ConfigDB.get_by_params(%{group: ":quack", key: ":level"})
76 refute ConfigDB.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
77 refute ConfigDB.get_by_params(%{group: ":postgrex", key: ":json_library"})
78 refute ConfigDB.get_by_params(%{group: ":pleroma", key: ":database"})
79
80 assert config1.value == [key: "value", key2: [Repo]]
81 assert config2.value == [key: "value2", key2: ["Activity"]]
82 assert config3.value == :info
83 end
84
85 test "config table is truncated before migration" do
86 insert_config_record(:pleroma, :first_setting, key: "value", key2: ["Activity"])
87 assert length(config_records()) == 1
88
89 MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
90
91 config = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
92 assert config.value == [key: "value", key2: [Repo]]
93 end
94 end
95
96 describe "with deletion of temp file" do
97 setup do
98 clear_config(:configurable_from_database, true)
99 temp_file = "config/temp.exported_from_db.secret.exs"
100
101 on_exit(fn ->
102 :ok = File.rm(temp_file)
103 end)
104
105 {:ok, temp_file: temp_file}
106 end
107
108 test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
109 insert_config_record(:pleroma, :setting_first, key: "value", key2: ["Activity"])
110 insert_config_record(:pleroma, :setting_second, key: "value2", key2: [Repo])
111 insert_config_record(:quack, :level, :info)
112
113 MixTask.run(["migrate_from_db", "--env", "temp", "-d"])
114
115 assert config_records() == []
116
117 file = File.read!(temp_file)
118 assert file =~ "config :pleroma, :setting_first,"
119 assert file =~ "config :pleroma, :setting_second,"
120 assert file =~ "config :quack, :level, :info"
121 end
122
123 test "load a settings with large values and pass to file", %{temp_file: temp_file} do
124 insert(:config,
125 key: :instance,
126 value: [
127 name: "Pleroma",
128 email: "example@example.com",
129 notify_email: "noreply@example.com",
130 description: "A Pleroma instance, an alternative fediverse server",
131 limit: 5_000,
132 chat_limit: 5_000,
133 remote_limit: 100_000,
134 upload_limit: 16_000_000,
135 avatar_upload_limit: 2_000_000,
136 background_upload_limit: 4_000_000,
137 banner_upload_limit: 4_000_000,
138 poll_limits: %{
139 max_options: 20,
140 max_option_chars: 200,
141 min_expiration: 0,
142 max_expiration: 365 * 24 * 60 * 60
143 },
144 registrations_open: true,
145 federating: true,
146 federation_incoming_replies_max_depth: 100,
147 federation_reachability_timeout_days: 7,
148 federation_publisher_modules: [Pleroma.Web.ActivityPub.Publisher],
149 allow_relay: true,
150 public: true,
151 quarantined_instances: [],
152 managed_config: true,
153 static_dir: "instance/static/",
154 allowed_post_formats: ["text/plain", "text/html", "text/markdown", "text/bbcode"],
155 autofollowed_nicknames: [],
156 max_pinned_statuses: 1,
157 attachment_links: false,
158 max_report_comment_size: 1000,
159 safe_dm_mentions: false,
160 healthcheck: false,
161 remote_post_retention_days: 90,
162 skip_thread_containment: true,
163 limit_to_local_content: :unauthenticated,
164 user_bio_length: 5000,
165 user_name_length: 100,
166 max_account_fields: 10,
167 max_remote_account_fields: 20,
168 account_field_name_length: 512,
169 account_field_value_length: 2048,
170 external_user_synchronization: true,
171 extended_nickname_format: true,
172 multi_factor_authentication: [
173 totp: [
174 digits: 6,
175 period: 30
176 ],
177 backup_codes: [
178 number: 2,
179 length: 6
180 ]
181 ]
182 ]
183 )
184
185 MixTask.run(["migrate_from_db", "--env", "temp", "-d"])
186
187 assert config_records() == []
188 assert File.exists?(temp_file)
189 {:ok, file} = File.read(temp_file)
190
191 header =
192 if Code.ensure_loaded?(Config.Reader) do
193 "import Config"
194 else
195 "use Mix.Config"
196 end
197
198 assert file ==
199 "#{header}\n\nconfig :pleroma, :instance,\n name: \"Pleroma\",\n email: \"example@example.com\",\n notify_email: \"noreply@example.com\",\n description: \"A Pleroma instance, an alternative fediverse server\",\n limit: 5000,\n chat_limit: 5000,\n remote_limit: 100_000,\n upload_limit: 16_000_000,\n avatar_upload_limit: 2_000_000,\n background_upload_limit: 4_000_000,\n banner_upload_limit: 4_000_000,\n poll_limits: %{\n max_expiration: 31_536_000,\n max_option_chars: 200,\n max_options: 20,\n min_expiration: 0\n },\n registrations_open: true,\n federating: true,\n federation_incoming_replies_max_depth: 100,\n federation_reachability_timeout_days: 7,\n federation_publisher_modules: [Pleroma.Web.ActivityPub.Publisher],\n allow_relay: true,\n public: true,\n quarantined_instances: [],\n managed_config: true,\n static_dir: \"instance/static/\",\n allowed_post_formats: [\"text/plain\", \"text/html\", \"text/markdown\", \"text/bbcode\"],\n autofollowed_nicknames: [],\n max_pinned_statuses: 1,\n attachment_links: false,\n max_report_comment_size: 1000,\n safe_dm_mentions: false,\n healthcheck: false,\n remote_post_retention_days: 90,\n skip_thread_containment: true,\n limit_to_local_content: :unauthenticated,\n user_bio_length: 5000,\n user_name_length: 100,\n max_account_fields: 10,\n max_remote_account_fields: 20,\n account_field_name_length: 512,\n account_field_value_length: 2048,\n external_user_synchronization: true,\n extended_nickname_format: true,\n multi_factor_authentication: [\n totp: [digits: 6, period: 30],\n backup_codes: [number: 2, length: 6]\n ]\n"
200 end
201 end
202
203 describe "migrate_from_db/1" do
204 setup do: clear_config(:configurable_from_database, true)
205
206 setup do
207 insert_config_record(:pleroma, :setting_first, key: "value", key2: ["Activity"])
208 insert_config_record(:pleroma, :setting_second, key: "value2", key2: [Repo])
209 insert_config_record(:quack, :level, :info)
210
211 path = "test/instance_static"
212 file_path = Path.join(path, "temp.exported_from_db.secret.exs")
213
214 on_exit(fn -> File.rm!(file_path) end)
215
216 [file_path: file_path]
217 end
218
219 test "with path parameter", %{file_path: file_path} do
220 MixTask.run(["migrate_from_db", "--env", "temp", "--path", Path.dirname(file_path)])
221
222 file = File.read!(file_path)
223 assert file =~ "config :pleroma, :setting_first,"
224 assert file =~ "config :pleroma, :setting_second,"
225 assert file =~ "config :quack, :level, :info"
226 end
227
228 test "release", %{file_path: file_path} do
229 clear_config(:release, true)
230 clear_config(:config_path, file_path)
231
232 MixTask.run(["migrate_from_db", "--env", "temp"])
233
234 file = File.read!(file_path)
235 assert file =~ "config :pleroma, :setting_first,"
236 assert file =~ "config :pleroma, :setting_second,"
237 assert file =~ "config :quack, :level, :info"
238 end
239 end
240
241 describe "operations on database config" do
242 setup do: clear_config(:configurable_from_database, true)
243
244 test "dumping a specific group" do
245 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
246
247 insert_config_record(:web_push_encryption, :vapid_details,
248 subject: "mailto:administrator@example.com",
249 public_key:
250 "BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI",
251 private_key: "Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4"
252 )
253
254 MixTask.run(["dump", "pleroma"])
255
256 assert_receive {:mix_shell, :info,
257 ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
258
259 refute_receive {
260 :mix_shell,
261 :info,
262 [
263 "config :web_push_encryption, :vapid_details, [subject: \"mailto:administrator@example.com\", public_key: \"BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI\", private_key: \"Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4\"]\r\n\r\n"
264 ]
265 }
266
267 # Ensure operations work when using atom syntax
268 MixTask.run(["dump", ":pleroma"])
269
270 assert_receive {:mix_shell, :info,
271 ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
272 end
273
274 test "dumping a specific key in a group" do
275 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
276 insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
277
278 MixTask.run(["dump", "pleroma", "Pleroma.Captcha"])
279
280 refute_receive {:mix_shell, :info,
281 ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
282
283 assert_receive {:mix_shell, :info,
284 ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]}
285 end
286
287 test "dumps all configuration successfully" do
288 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
289 insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
290
291 MixTask.run(["dump"])
292
293 assert_receive {:mix_shell, :info,
294 ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
295
296 assert_receive {:mix_shell, :info,
297 ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]}
298 end
299 end
300
301 describe "when configdb disabled" do
302 test "refuses to dump" do
303 clear_config(:configurable_from_database, false)
304
305 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
306
307 MixTask.run(["dump"])
308
309 msg =
310 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
311
312 assert_receive {:mix_shell, :error, [^msg]}
313 end
314 end
315
316 describe "destructive operations" do
317 setup do: clear_config(:configurable_from_database, true)
318
319 setup do
320 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
321 insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
322 insert_config_record(:pleroma2, :key2, z: 1)
323
324 assert length(config_records()) == 3
325
326 :ok
327 end
328
329 test "deletes group of settings" do
330 MixTask.run(["delete", "--force", "pleroma"])
331
332 assert [%ConfigDB{group: :pleroma2, key: :key2}] = config_records()
333 end
334
335 test "deletes specified key" do
336 MixTask.run(["delete", "--force", "pleroma", "Pleroma.Captcha"])
337
338 assert [
339 %ConfigDB{group: :pleroma, key: :instance},
340 %ConfigDB{group: :pleroma2, key: :key2}
341 ] = config_records()
342 end
343
344 test "resets entire config" do
345 MixTask.run(["reset", "--force"])
346
347 assert config_records() == []
348 end
349 end
350 end