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