Merge branch 'instance-docs' into 'develop'
[akkoma] / test / tasks / 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 setup_all do: clear_config(:configurable_from_database, true)
26
27 test "error if file with custom settings doesn't exist" do
28 Mix.Tasks.Pleroma.Config.migrate_to_db("config/not_existance_config_file.exs")
29
30 assert_receive {:mix_shell, :info,
31 [
32 "To migrate settings, you must define custom settings in config/not_existance_config_file.exs."
33 ]},
34 15
35 end
36
37 describe "migrate_to_db/1" do
38 setup do
39 initial = Application.get_env(:quack, :level)
40 on_exit(fn -> Application.put_env(:quack, :level, initial) end)
41 end
42
43 @tag capture_log: true
44 test "config migration refused when deprecated settings are found" do
45 clear_config([:media_proxy, :whitelist], ["domain_without_scheme.com"])
46 assert Repo.all(ConfigDB) == []
47
48 Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
49
50 assert_received {:mix_shell, :error, [message]}
51
52 assert message =~
53 "Migration is not allowed until all deprecation warnings have been resolved."
54 end
55
56 test "filtered settings are migrated to db" do
57 assert Repo.all(ConfigDB) == []
58
59 Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
60
61 config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
62 config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})
63 config3 = ConfigDB.get_by_params(%{group: ":quack", key: ":level"})
64 refute ConfigDB.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
65 refute ConfigDB.get_by_params(%{group: ":postgrex", key: ":json_library"})
66 refute ConfigDB.get_by_params(%{group: ":pleroma", key: ":database"})
67
68 assert config1.value == [key: "value", key2: [Repo]]
69 assert config2.value == [key: "value2", key2: ["Activity"]]
70 assert config3.value == :info
71 end
72
73 test "config table is truncated before migration" do
74 insert(:config, key: :first_setting, value: [key: "value", key2: ["Activity"]])
75 assert Repo.aggregate(ConfigDB, :count, :id) == 1
76
77 Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
78
79 config = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
80 assert config.value == [key: "value", key2: [Repo]]
81 end
82 end
83
84 describe "with deletion temp file" do
85 setup do
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 end