logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

config_test.exs (8008B)


  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. defmodule Mix.Tasks.Pleroma.ConfigTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.ConfigDB
  8. alias Pleroma.Repo
  9. setup_all do
  10. Mix.shell(Mix.Shell.Process)
  11. on_exit(fn ->
  12. Mix.shell(Mix.Shell.IO)
  13. Application.delete_env(:pleroma, :first_setting)
  14. Application.delete_env(:pleroma, :second_setting)
  15. end)
  16. :ok
  17. end
  18. setup_all do: clear_config(:configurable_from_database, true)
  19. test "error if file with custom settings doesn't exist" do
  20. Mix.Tasks.Pleroma.Config.migrate_to_db("config/not_existance_config_file.exs")
  21. assert_receive {:mix_shell, :info,
  22. [
  23. "To migrate settings, you must define custom settings in config/not_existance_config_file.exs."
  24. ]},
  25. 15
  26. end
  27. describe "migrate_to_db/1" do
  28. setup do
  29. initial = Application.get_env(:quack, :level)
  30. on_exit(fn -> Application.put_env(:quack, :level, initial) end)
  31. end
  32. @tag capture_log: true
  33. test "config migration refused when deprecated settings are found" do
  34. clear_config([:media_proxy, :whitelist], ["domain_without_scheme.com"])
  35. assert Repo.all(ConfigDB) == []
  36. Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
  37. assert_received {:mix_shell, :error, [message]}
  38. assert message =~
  39. "Migration is not allowed until all deprecation warnings have been resolved."
  40. end
  41. test "filtered settings are migrated to db" do
  42. assert Repo.all(ConfigDB) == []
  43. Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
  44. config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
  45. config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})
  46. config3 = ConfigDB.get_by_params(%{group: ":quack", key: ":level"})
  47. refute ConfigDB.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
  48. refute ConfigDB.get_by_params(%{group: ":postgrex", key: ":json_library"})
  49. refute ConfigDB.get_by_params(%{group: ":pleroma", key: ":database"})
  50. assert config1.value == [key: "value", key2: [Repo]]
  51. assert config2.value == [key: "value2", key2: ["Activity"]]
  52. assert config3.value == :info
  53. end
  54. test "config table is truncated before migration" do
  55. insert(:config, key: :first_setting, value: [key: "value", key2: ["Activity"]])
  56. assert Repo.aggregate(ConfigDB, :count, :id) == 1
  57. Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
  58. config = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
  59. assert config.value == [key: "value", key2: [Repo]]
  60. end
  61. end
  62. describe "with deletion temp file" do
  63. setup do
  64. temp_file = "config/temp.exported_from_db.secret.exs"
  65. on_exit(fn ->
  66. :ok = File.rm(temp_file)
  67. end)
  68. {:ok, temp_file: temp_file}
  69. end
  70. test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
  71. insert(:config, key: :setting_first, value: [key: "value", key2: ["Activity"]])
  72. insert(:config, key: :setting_second, value: [key: "value2", key2: [Repo]])
  73. insert(:config, group: :quack, key: :level, value: :info)
  74. Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", "temp", "-d"])
  75. assert Repo.all(ConfigDB) == []
  76. file = File.read!(temp_file)
  77. assert file =~ "config :pleroma, :setting_first,"
  78. assert file =~ "config :pleroma, :setting_second,"
  79. assert file =~ "config :quack, :level, :info"
  80. end
  81. test "load a settings with large values and pass to file", %{temp_file: temp_file} do
  82. insert(:config,
  83. key: :instance,
  84. value: [
  85. name: "Pleroma",
  86. email: "example@example.com",
  87. notify_email: "noreply@example.com",
  88. description: "A Pleroma instance, an alternative fediverse server",
  89. limit: 5_000,
  90. chat_limit: 5_000,
  91. remote_limit: 100_000,
  92. upload_limit: 16_000_000,
  93. avatar_upload_limit: 2_000_000,
  94. background_upload_limit: 4_000_000,
  95. banner_upload_limit: 4_000_000,
  96. poll_limits: %{
  97. max_options: 20,
  98. max_option_chars: 200,
  99. min_expiration: 0,
  100. max_expiration: 365 * 24 * 60 * 60
  101. },
  102. registrations_open: true,
  103. federating: true,
  104. federation_incoming_replies_max_depth: 100,
  105. federation_reachability_timeout_days: 7,
  106. federation_publisher_modules: [Pleroma.Web.ActivityPub.Publisher],
  107. allow_relay: true,
  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. autofollowed_nicknames: [],
  114. max_pinned_statuses: 1,
  115. attachment_links: false,
  116. max_report_comment_size: 1000,
  117. safe_dm_mentions: false,
  118. healthcheck: false,
  119. remote_post_retention_days: 90,
  120. skip_thread_containment: true,
  121. limit_to_local_content: :unauthenticated,
  122. user_bio_length: 5000,
  123. user_name_length: 100,
  124. max_account_fields: 10,
  125. max_remote_account_fields: 20,
  126. account_field_name_length: 512,
  127. account_field_value_length: 2048,
  128. external_user_synchronization: true,
  129. extended_nickname_format: true,
  130. multi_factor_authentication: [
  131. totp: [
  132. digits: 6,
  133. period: 30
  134. ],
  135. backup_codes: [
  136. number: 2,
  137. length: 6
  138. ]
  139. ]
  140. ]
  141. )
  142. Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", "temp", "-d"])
  143. assert Repo.all(ConfigDB) == []
  144. assert File.exists?(temp_file)
  145. {:ok, file} = File.read(temp_file)
  146. header =
  147. if Code.ensure_loaded?(Config.Reader) do
  148. "import Config"
  149. else
  150. "use Mix.Config"
  151. end
  152. assert file ==
  153. "#{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"
  154. end
  155. end
  156. end