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