logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

config_test.exs (10803B)


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