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