logo

pleroma

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

config_test.exs (10776B)


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