config editing through database
[akkoma] / test / tasks / config_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 Mix.Tasks.Pleroma.ConfigTest do
6 use Pleroma.DataCase
7 alias Pleroma.Repo
8 alias Pleroma.Web.AdminAPI.Config
9
10 setup_all do
11 Mix.shell(Mix.Shell.Process)
12
13 on_exit(fn ->
14 Mix.shell(Mix.Shell.IO)
15 Application.delete_env(:pleroma, :first_setting)
16 Application.delete_env(:pleroma, :second_setting)
17 end)
18
19 :ok
20 end
21
22 clear_config_all([:instance, :dynamic_configuration]) do
23 Pleroma.Config.put([:instance, :dynamic_configuration], true)
24 end
25
26 test "settings are migrated to db" do
27 assert Repo.all(Config) == []
28
29 Application.put_env(:pleroma, :first_setting, key: "value", key2: [Repo])
30 Application.put_env(:pleroma, :second_setting, key: "value2", key2: ["Activity"])
31
32 Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
33
34 config1 = Config.get_by_params(%{group: ":pleroma", key: ":first_setting"})
35 config2 = Config.get_by_params(%{group: ":pleroma", key: ":second_setting"})
36 refute Config.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
37
38 assert Config.from_binary(config1.value) == [key: "value", key2: [Repo]]
39 assert Config.from_binary(config2.value) == [key: "value2", key2: ["Activity"]]
40 end
41
42 test "settings are migrated to file and deleted from db" do
43 env = "temp"
44 config_file = "config/#{env}.exported_from_db.secret.exs"
45
46 on_exit(fn ->
47 :ok = File.rm(config_file)
48 end)
49
50 Config.create(%{
51 group: ":pleroma",
52 key: ":setting_first",
53 value: [key: "value", key2: ["Activity"]]
54 })
55
56 Config.create(%{
57 group: ":pleroma",
58 key: ":setting_second",
59 value: [key: "value2", key2: [Repo]]
60 })
61
62 Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", env, "-d"])
63
64 assert Repo.all(Config) == []
65
66 file = File.read!(config_file)
67 assert file =~ "config :pleroma, :setting_first,"
68 assert file =~ "config :pleroma, :setting_second,"
69 end
70
71 test "load a settings with large values and pass to file", %{temp_file: temp_file} do
72 Config.create(%{
73 group: "pleroma",
74 key: ":instance",
75 value: [
76 name: "Pleroma",
77 email: "example@example.com",
78 notify_email: "noreply@example.com",
79 description: "A Pleroma instance, an alternative fediverse server",
80 limit: 5_000,
81 chat_limit: 5_000,
82 remote_limit: 100_000,
83 upload_limit: 16_000_000,
84 avatar_upload_limit: 2_000_000,
85 background_upload_limit: 4_000_000,
86 banner_upload_limit: 4_000_000,
87 poll_limits: %{
88 max_options: 20,
89 max_option_chars: 200,
90 min_expiration: 0,
91 max_expiration: 365 * 24 * 60 * 60
92 },
93 registrations_open: true,
94 federating: true,
95 federation_incoming_replies_max_depth: 100,
96 federation_reachability_timeout_days: 7,
97 federation_publisher_modules: [Pleroma.Web.ActivityPub.Publisher],
98 allow_relay: true,
99 rewrite_policy: Pleroma.Web.ActivityPub.MRF.NoOpPolicy,
100 public: true,
101 quarantined_instances: [],
102 managed_config: true,
103 static_dir: "instance/static/",
104 allowed_post_formats: ["text/plain", "text/html", "text/markdown", "text/bbcode"],
105 mrf_transparency: true,
106 mrf_transparency_exclusions: [],
107 autofollowed_nicknames: [],
108 max_pinned_statuses: 1,
109 no_attachment_links: true,
110 welcome_user_nickname: nil,
111 welcome_message: nil,
112 max_report_comment_size: 1000,
113 safe_dm_mentions: false,
114 healthcheck: false,
115 remote_post_retention_days: 90,
116 skip_thread_containment: true,
117 limit_to_local_content: :unauthenticated,
118 dynamic_configuration: false,
119 user_bio_length: 5000,
120 user_name_length: 100,
121 max_account_fields: 10,
122 max_remote_account_fields: 20,
123 account_field_name_length: 512,
124 account_field_value_length: 2048,
125 external_user_synchronization: true,
126 extended_nickname_format: true,
127 multi_factor_authentication: [
128 totp: [
129 # digits 6 or 8
130 digits: 6,
131 period: 30
132 ],
133 backup_codes: [
134 number: 2,
135 length: 6
136 ]
137 ]
138 ]
139 })
140
141 Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "temp", "true"])
142
143 assert Repo.all(Config) == []
144 assert File.exists?(temp_file)
145 {:ok, file} = File.read(temp_file)
146
147 assert file ==
148 "use Mix.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 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 rewrite_policy: Pleroma.Web.ActivityPub.MRF.NoOpPolicy,\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 mrf_transparency: true,\n mrf_transparency_exclusions: [],\n autofollowed_nicknames: [],\n max_pinned_statuses: 1,\n no_attachment_links: true,\n welcome_user_nickname: nil,\n welcome_message: nil,\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 dynamic_configuration: false,\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"
149 end
150 end