Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

351 lignes
13KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 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 Mix.Tasks.Pleroma.Config, as: MixTask
  8. alias Pleroma.ConfigDB
  9. alias Pleroma.Repo
  10. setup_all do
  11. Mix.shell(Mix.Shell.Process)
  12. on_exit(fn ->
  13. Mix.shell(Mix.Shell.IO)
  14. Application.delete_env(:pleroma, :first_setting)
  15. Application.delete_env(:pleroma, :second_setting)
  16. end)
  17. :ok
  18. end
  19. defp config_records do
  20. ConfigDB
  21. |> Repo.all()
  22. |> Enum.sort()
  23. end
  24. defp insert_config_record(group, key, value) do
  25. insert(:config,
  26. group: group,
  27. key: key,
  28. value: value
  29. )
  30. end
  31. test "error if file with custom settings doesn't exist" do
  32. MixTask.migrate_to_db("config/non_existent_config_file.exs")
  33. msg =
  34. "To migrate settings, you must define custom settings in config/non_existent_config_file.exs."
  35. assert_receive {:mix_shell, :info, [^msg]}, 15
  36. end
  37. describe "migrate_to_db/1" do
  38. setup do
  39. clear_config(:configurable_from_database, true)
  40. clear_config([:quack, :level])
  41. end
  42. @tag capture_log: true
  43. test "config migration refused when deprecated settings are found" do
  44. clear_config([:media_proxy, :whitelist], ["domain_without_scheme.com"])
  45. assert config_records() == []
  46. MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
  47. assert_received {:mix_shell, :error, [message]}
  48. assert message =~
  49. "Migration is not allowed until all deprecation warnings have been resolved."
  50. end
  51. test "filtered settings are migrated to db" do
  52. assert config_records() == []
  53. MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
  54. config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
  55. config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})
  56. config3 = ConfigDB.get_by_params(%{group: ":quack", key: ":level"})
  57. refute ConfigDB.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
  58. refute ConfigDB.get_by_params(%{group: ":postgrex", key: ":json_library"})
  59. refute ConfigDB.get_by_params(%{group: ":pleroma", key: ":database"})
  60. assert config1.value == [key: "value", key2: [Repo]]
  61. assert config2.value == [key: "value2", key2: ["Activity"]]
  62. assert config3.value == :info
  63. end
  64. test "config table is truncated before migration" do
  65. insert_config_record(:pleroma, :first_setting, key: "value", key2: ["Activity"])
  66. assert length(config_records()) == 1
  67. MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs")
  68. config = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
  69. assert config.value == [key: "value", key2: [Repo]]
  70. end
  71. end
  72. describe "with deletion of temp file" do
  73. setup do
  74. clear_config(:configurable_from_database, true)
  75. temp_file = "config/temp.exported_from_db.secret.exs"
  76. on_exit(fn ->
  77. :ok = File.rm(temp_file)
  78. end)
  79. {:ok, temp_file: temp_file}
  80. end
  81. test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
  82. insert_config_record(:pleroma, :setting_first, key: "value", key2: ["Activity"])
  83. insert_config_record(:pleroma, :setting_second, key: "value2", key2: [Repo])
  84. insert_config_record(:quack, :level, :info)
  85. MixTask.run(["migrate_from_db", "--env", "temp", "-d"])
  86. assert config_records() == []
  87. file = File.read!(temp_file)
  88. assert file =~ "config :pleroma, :setting_first,"
  89. assert file =~ "config :pleroma, :setting_second,"
  90. assert file =~ "config :quack, :level, :info"
  91. end
  92. test "load a settings with large values and pass to file", %{temp_file: temp_file} do
  93. insert(:config,
  94. key: :instance,
  95. value: [
  96. name: "Pleroma",
  97. email: "example@example.com",
  98. notify_email: "noreply@example.com",
  99. description: "A Pleroma instance, an alternative fediverse server",
  100. limit: 5_000,
  101. chat_limit: 5_000,
  102. remote_limit: 100_000,
  103. upload_limit: 16_000_000,
  104. avatar_upload_limit: 2_000_000,
  105. background_upload_limit: 4_000_000,
  106. banner_upload_limit: 4_000_000,
  107. poll_limits: %{
  108. max_options: 20,
  109. max_option_chars: 200,
  110. min_expiration: 0,
  111. max_expiration: 365 * 24 * 60 * 60
  112. },
  113. registrations_open: true,
  114. federating: true,
  115. federation_incoming_replies_max_depth: 100,
  116. federation_reachability_timeout_days: 7,
  117. federation_publisher_modules: [Pleroma.Web.ActivityPub.Publisher],
  118. allow_relay: true,
  119. public: true,
  120. quarantined_instances: [],
  121. managed_config: true,
  122. static_dir: "instance/static/",
  123. allowed_post_formats: ["text/plain", "text/html", "text/markdown", "text/bbcode"],
  124. autofollowed_nicknames: [],
  125. max_pinned_statuses: 1,
  126. attachment_links: false,
  127. max_report_comment_size: 1000,
  128. safe_dm_mentions: false,
  129. healthcheck: false,
  130. remote_post_retention_days: 90,
  131. skip_thread_containment: true,
  132. limit_to_local_content: :unauthenticated,
  133. user_bio_length: 5000,
  134. user_name_length: 100,
  135. max_account_fields: 10,
  136. max_remote_account_fields: 20,
  137. account_field_name_length: 512,
  138. account_field_value_length: 2048,
  139. external_user_synchronization: true,
  140. extended_nickname_format: true,
  141. multi_factor_authentication: [
  142. totp: [
  143. digits: 6,
  144. period: 30
  145. ],
  146. backup_codes: [
  147. number: 2,
  148. length: 6
  149. ]
  150. ]
  151. ]
  152. )
  153. MixTask.run(["migrate_from_db", "--env", "temp", "-d"])
  154. assert config_records() == []
  155. assert File.exists?(temp_file)
  156. {:ok, file} = File.read(temp_file)
  157. header =
  158. if Code.ensure_loaded?(Config.Reader) do
  159. "import Config"
  160. else
  161. "use Mix.Config"
  162. end
  163. assert file ==
  164. "#{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"
  165. end
  166. end
  167. describe "migrate_from_db/1" do
  168. setup do: clear_config(:configurable_from_database, true)
  169. setup do
  170. insert_config_record(:pleroma, :setting_first, key: "value", key2: ["Activity"])
  171. insert_config_record(:pleroma, :setting_second, key: "value2", key2: [Repo])
  172. insert_config_record(:quack, :level, :info)
  173. path = "test/instance_static"
  174. file_path = Path.join(path, "temp.exported_from_db.secret.exs")
  175. on_exit(fn -> File.rm!(file_path) end)
  176. [file_path: file_path]
  177. end
  178. test "with path parameter", %{file_path: file_path} do
  179. MixTask.run(["migrate_from_db", "--env", "temp", "--path", Path.dirname(file_path)])
  180. file = File.read!(file_path)
  181. assert file =~ "config :pleroma, :setting_first,"
  182. assert file =~ "config :pleroma, :setting_second,"
  183. assert file =~ "config :quack, :level, :info"
  184. end
  185. test "release", %{file_path: file_path} do
  186. clear_config(:release, true)
  187. clear_config(:config_path, file_path)
  188. MixTask.run(["migrate_from_db", "--env", "temp"])
  189. file = File.read!(file_path)
  190. assert file =~ "config :pleroma, :setting_first,"
  191. assert file =~ "config :pleroma, :setting_second,"
  192. assert file =~ "config :quack, :level, :info"
  193. end
  194. end
  195. describe "operations on database config" do
  196. setup do: clear_config(:configurable_from_database, true)
  197. test "dumping a specific group" do
  198. insert_config_record(:pleroma, :instance, name: "Pleroma Test")
  199. insert_config_record(:web_push_encryption, :vapid_details,
  200. subject: "mailto:administrator@example.com",
  201. public_key:
  202. "BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI",
  203. private_key: "Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4"
  204. )
  205. MixTask.run(["dump", "pleroma"])
  206. assert_receive {:mix_shell, :info,
  207. ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
  208. refute_receive {
  209. :mix_shell,
  210. :info,
  211. [
  212. "config :web_push_encryption, :vapid_details, [subject: \"mailto:administrator@example.com\", public_key: \"BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI\", private_key: \"Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4\"]\r\n\r\n"
  213. ]
  214. }
  215. # Ensure operations work when using atom syntax
  216. MixTask.run(["dump", ":pleroma"])
  217. assert_receive {:mix_shell, :info,
  218. ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
  219. end
  220. test "dumping a specific key in a group" do
  221. insert_config_record(:pleroma, :instance, name: "Pleroma Test")
  222. insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
  223. MixTask.run(["dump", "pleroma", "Pleroma.Captcha"])
  224. refute_receive {:mix_shell, :info,
  225. ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
  226. assert_receive {:mix_shell, :info,
  227. ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]}
  228. end
  229. test "dumps all configuration successfully" do
  230. insert_config_record(:pleroma, :instance, name: "Pleroma Test")
  231. insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
  232. MixTask.run(["dump"])
  233. assert_receive {:mix_shell, :info,
  234. ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]}
  235. assert_receive {:mix_shell, :info,
  236. ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]}
  237. end
  238. end
  239. describe "when configdb disabled" do
  240. test "refuses to dump" do
  241. clear_config(:configurable_from_database, false)
  242. insert_config_record(:pleroma, :instance, name: "Pleroma Test")
  243. MixTask.run(["dump"])
  244. msg =
  245. "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
  246. assert_receive {:mix_shell, :error, [^msg]}
  247. end
  248. end
  249. describe "destructive operations" do
  250. setup do: clear_config(:configurable_from_database, true)
  251. setup do
  252. insert_config_record(:pleroma, :instance, name: "Pleroma Test")
  253. insert_config_record(:pleroma, Pleroma.Captcha, enabled: false)
  254. insert_config_record(:pleroma2, :key2, z: 1)
  255. assert length(config_records()) == 3
  256. :ok
  257. end
  258. test "deletes group of settings" do
  259. MixTask.run(["delete", "--force", "pleroma"])
  260. assert [%ConfigDB{group: :pleroma2, key: :key2}] = config_records()
  261. end
  262. test "deletes specified key" do
  263. MixTask.run(["delete", "--force", "pleroma", "Pleroma.Captcha"])
  264. assert [
  265. %ConfigDB{group: :pleroma, key: :instance},
  266. %ConfigDB{group: :pleroma2, key: :key2}
  267. ] = config_records()
  268. end
  269. test "resets entire config" do
  270. MixTask.run(["reset", "--force"])
  271. assert config_records() == []
  272. end
  273. end
  274. end