1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.ConfigTest do
10 alias Mix.Tasks.Pleroma.Config, as: MixTask
11 alias Pleroma.ConfigDB
15 Mix.shell(Mix.Shell.Process)
18 Mix.shell(Mix.Shell.IO)
19 Application.delete_env(:pleroma, :first_setting)
20 Application.delete_env(:pleroma, :second_setting)
26 defp config_records do
32 defp insert_config_record(group, key, value) do
40 test "error if file with custom settings doesn't exist" do
41 MixTask.migrate_to_db("config/non_existent_config_file.exs")
44 "To migrate settings, you must define custom settings in config/non_existent_config_file.exs."
46 assert_receive {:mix_shell, :info, [^msg]}, 15
49 describe "migrate_to_db/1" do
51 clear_config(:configurable_from_database, true)
52 clear_config([:quack, :level])
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() == []
60 MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
62 assert_received {:mix_shell, :error, [message]}
65 "Migration is not allowed until all deprecation warnings have been resolved."
68 test "filtered settings are migrated to db" do
69 assert config_records() == []
71 MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
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"})
80 assert config1.value == [key: "value", key2: [Repo]]
81 assert config2.value == [key: "value2", key2: ["Activity"]]
82 assert config3.value == :info
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
89 MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
91 config = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
92 assert config.value == [key: "value", key2: [Repo]]
96 describe "with deletion of temp file" do
98 clear_config(:configurable_from_database, true)
99 temp_file = "config/temp.exported_from_db.secret.exs"
102 :ok = File.rm(temp_file)
105 {:ok, temp_file: temp_file}
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)
113 MixTask.run(["migrate_from_db", "--env", "temp", "-d"])
115 assert config_records() == []
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"
123 test "load a settings with large values and pass to file", %{temp_file: temp_file} do
128 email: "example@example.com",
129 notify_email: "noreply@example.com",
130 description: "A Pleroma instance, an alternative fediverse server",
132 remote_limit: 100_000,
133 upload_limit: 16_000_000,
134 avatar_upload_limit: 2_000_000,
135 background_upload_limit: 4_000_000,
136 banner_upload_limit: 4_000_000,
139 max_option_chars: 200,
141 max_expiration: 365 * 24 * 60 * 60
143 registrations_open: true,
145 federation_incoming_replies_max_depth: 100,
146 federation_reachability_timeout_days: 7,
147 federation_publisher_modules: [Pleroma.Web.ActivityPub.Publisher],
150 quarantined_instances: [],
151 managed_config: true,
152 static_dir: "instance/static/",
153 allowed_post_formats: ["text/plain", "text/html", "text/markdown", "text/bbcode"],
154 autofollowed_nicknames: [],
155 max_pinned_statuses: 1,
156 attachment_links: false,
157 max_report_comment_size: 1000,
158 safe_dm_mentions: false,
160 remote_post_retention_days: 90,
161 skip_thread_containment: true,
162 limit_to_local_content: :unauthenticated,
163 user_bio_length: 5000,
164 user_name_length: 100,
165 max_account_fields: 10,
166 max_remote_account_fields: 20,
167 account_field_name_length: 512,
168 account_field_value_length: 2048,
169 external_user_synchronization: true,
170 extended_nickname_format: true,
171 multi_factor_authentication: [
184 MixTask.run(["migrate_from_db", "--env", "temp", "-d"])
186 assert config_records() == []
187 assert File.exists?(temp_file)
188 {:ok, file} = File.read(temp_file)
191 "import Config\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 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"
195 describe "migrate_from_db/1" do
196 setup do: clear_config(:configurable_from_database, true)
199 insert_config_record(:pleroma, :setting_first, key: "value", key2: ["Activity"])
200 insert_config_record(:pleroma, :setting_second, key: "value2", key2: [Repo])
201 insert_config_record(:quack, :level, :info)
203 path = "test/instance_static"
204 file_path = Path.join(path, "temp.exported_from_db.secret.exs")
206 on_exit(fn -> File.rm!(file_path) end)
208 [file_path: file_path]
211 test "with path parameter", %{file_path: file_path} do
212 MixTask.run(["migrate_from_db", "--env", "temp", "--path", Path.dirname(file_path)])
214 file = File.read!(file_path)
215 assert file =~ "config :pleroma, :setting_first,"
216 assert file =~ "config :pleroma, :setting_second,"
217 assert file =~ "config :quack, :level, :info"
220 test "release", %{file_path: file_path} do
221 clear_config(:release, true)
222 clear_config(:config_path, file_path)
224 MixTask.run(["migrate_from_db", "--env", "temp"])
226 file = File.read!(file_path)
227 assert file =~ "config :pleroma, :setting_first,"
228 assert file =~ "config :pleroma, :setting_second,"
229 assert file =~ "config :quack, :level, :info"
233 describe "operations on database config" do
234 setup do: clear_config(:configurable_from_database, true)
236 test "dumping a specific group" do
237 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
239 insert_config_record(:web_push_encryption, :vapid_details,
240 subject: "mailto:administrator@example.com",
242 "BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI",
243 private_key: "Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4"
246 MixTask.run(["dump", "pleroma"])
248 assert_receive {:mix_shell, :info,
249 ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
255 "config :web_push_encryption, :vapid_details, [subject: \"mailto:administrator@example.com\", public_key: \"BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI\", private_key: \"Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4\"]\r\n\r\n"
259 # Ensure operations work when using atom syntax
260 MixTask.run(["dump", ":pleroma"])
262 assert_receive {:mix_shell, :info,
263 ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
266 test "dumping a specific key in a group" do
267 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
268 insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
270 MixTask.run(["dump", "pleroma", "Pleroma.Captcha"])
272 refute_receive {:mix_shell, :info,
273 ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
275 assert_receive {:mix_shell, :info,
276 ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]}
279 test "dumps all configuration successfully" do
280 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
281 insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
283 MixTask.run(["dump"])
285 assert_receive {:mix_shell, :info,
286 ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
288 assert_receive {:mix_shell, :info,
289 ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]}
293 describe "when configdb disabled" do
294 test "refuses to dump" do
295 clear_config(:configurable_from_database, false)
297 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
299 MixTask.run(["dump"])
302 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
304 assert_receive {:mix_shell, :error, [^msg]}
308 describe "destructive operations" do
309 setup do: clear_config(:configurable_from_database, true)
312 insert_config_record(:pleroma, :instance, name: "Pleroma Test")
313 insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
314 insert_config_record(:pleroma2, :key2, z: 1)
316 assert length(config_records()) == 3
321 test "deletes group of settings" do
322 MixTask.run(["delete", "--force", "pleroma"])
324 assert [%ConfigDB{group: :pleroma2, key: :key2}] = config_records()
327 test "deletes specified key" do
328 MixTask.run(["delete", "--force", "pleroma", "Pleroma.Captcha"])
331 %ConfigDB{group: :pleroma, key: :instance},
332 %ConfigDB{group: :pleroma2, key: :key2}
336 test "resets entire config" do
337 MixTask.run(["reset", "--force"])
339 assert config_records() == []