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