Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
7.5KB

  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. test "filtered settings are migrated to db" do
  33. assert Repo.all(ConfigDB) == []
  34. Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
  35. config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
  36. config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})
  37. config3 = ConfigDB.get_by_params(%{group: ":quack", key: ":level"})
  38. refute ConfigDB.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
  39. refute ConfigDB.get_by_params(%{group: ":postgrex", key: ":json_library"})
  40. refute ConfigDB.get_by_params(%{group: ":pleroma", key: ":database"})
  41. assert config1.value == [key: "value", key2: [Repo]]
  42. assert config2.value == [key: "value2", key2: ["Activity"]]
  43. assert config3.value == :info
  44. end
  45. test "config table is truncated before migration" do
  46. insert(:config, key: :first_setting, value: [key: "value", key2: ["Activity"]])
  47. assert Repo.aggregate(ConfigDB, :count, :id) == 1
  48. Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
  49. config = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
  50. assert config.value == [key: "value", key2: [Repo]]
  51. end
  52. end
  53. describe "with deletion temp file" do
  54. setup do
  55. temp_file = "config/temp.exported_from_db.secret.exs"
  56. on_exit(fn ->
  57. :ok = File.rm(temp_file)
  58. end)
  59. {:ok, temp_file: temp_file}
  60. end
  61. test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
  62. insert(:config, key: :setting_first, value: [key: "value", key2: ["Activity"]])
  63. insert(:config, key: :setting_second, value: [key: "value2", key2: [Repo]])
  64. insert(:config, group: :quack, key: :level, value: :info)
  65. Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", "temp", "-d"])
  66. assert Repo.all(ConfigDB) == []
  67. file = File.read!(temp_file)
  68. assert file =~ "config :pleroma, :setting_first,"
  69. assert file =~ "config :pleroma, :setting_second,"
  70. assert file =~ "config :quack, :level, :info"
  71. end
  72. test "load a settings with large values and pass to file", %{temp_file: temp_file} do
  73. insert(:config,
  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. public: true,
  100. quarantined_instances: [],
  101. managed_config: true,
  102. static_dir: "instance/static/",
  103. allowed_post_formats: ["text/plain", "text/html", "text/markdown", "text/bbcode"],
  104. autofollowed_nicknames: [],
  105. max_pinned_statuses: 1,
  106. attachment_links: false,
  107. welcome_user_nickname: nil,
  108. welcome_message: nil,
  109. max_report_comment_size: 1000,
  110. safe_dm_mentions: false,
  111. healthcheck: false,
  112. remote_post_retention_days: 90,
  113. skip_thread_containment: true,
  114. limit_to_local_content: :unauthenticated,
  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,
  126. period: 30
  127. ],
  128. backup_codes: [
  129. number: 2,
  130. length: 6
  131. ]
  132. ]
  133. ]
  134. )
  135. Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", "temp", "-d"])
  136. assert Repo.all(ConfigDB) == []
  137. assert File.exists?(temp_file)
  138. {:ok, file} = File.read(temp_file)
  139. header =
  140. if Code.ensure_loaded?(Config.Reader) do
  141. "import Config"
  142. else
  143. "use Mix.Config"
  144. end
  145. assert file ==
  146. "#{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 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 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"
  147. end
  148. end
  149. end