logo

pleroma

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

transfer_task_test.exs (6009B)


  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 Pleroma.Config.TransferTaskTest do
  5. use Pleroma.DataCase
  6. import ExUnit.CaptureLog
  7. import Pleroma.Factory
  8. alias Pleroma.Config.TransferTask
  9. setup do: clear_config(:configurable_from_database, true)
  10. test "transfer config values from db to env" do
  11. refute Application.get_env(:pleroma, :test_key)
  12. refute Application.get_env(:idna, :test_key)
  13. refute Application.get_env(:postgrex, :test_key)
  14. initial = Application.get_env(:logger, :level)
  15. insert(:config, key: :test_key, value: [live: 2, com: 3])
  16. insert(:config, group: :idna, key: :test_key, value: [live: 15, com: 35])
  17. insert(:config, group: :postgrex, key: :test_key, value: :value)
  18. insert(:config, group: :logger, key: :level, value: :debug)
  19. TransferTask.start_link([])
  20. assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
  21. assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
  22. assert Application.get_env(:logger, :level) == :debug
  23. assert Application.get_env(:postgrex, :test_key) == :value
  24. on_exit(fn ->
  25. Application.delete_env(:pleroma, :test_key)
  26. Application.delete_env(:idna, :test_key)
  27. Application.delete_env(:postgrex, :test_key)
  28. Application.put_env(:logger, :level, initial)
  29. end)
  30. end
  31. test "transfer config values for 1 group and some keys" do
  32. level = Application.get_env(:somegroup, :level)
  33. meta = Application.get_env(:somegroup, :meta)
  34. insert(:config, group: :somegroup, key: :level, value: :info)
  35. insert(:config, group: :somegroup, key: :meta, value: [:none])
  36. TransferTask.start_link([])
  37. assert Application.get_env(:somegroup, :level) == :info
  38. assert Application.get_env(:somegroup, :meta) == [:none]
  39. on_exit(fn ->
  40. Application.put_env(:somegroup, :level, level)
  41. Application.put_env(:somegroup, :meta, meta)
  42. end)
  43. end
  44. test "transfer config values with full subkey update" do
  45. clear_config(:emoji)
  46. clear_config(:assets)
  47. insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
  48. insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]])
  49. TransferTask.start_link([])
  50. emoji_env = Application.get_env(:pleroma, :emoji)
  51. assert emoji_env[:groups] == [a: 1, b: 2]
  52. assets_env = Application.get_env(:pleroma, :assets)
  53. assert assets_env[:mascots] == [a: 1, b: 2]
  54. end
  55. describe "pleroma restart" do
  56. setup do
  57. on_exit(fn ->
  58. Restarter.Pleroma.refresh()
  59. # Restarter.Pleroma.refresh/0 is an asynchronous call.
  60. # A GenServer will first finish the previous call before starting a new one.
  61. # Here we do a synchronous call.
  62. # That way we are sure that the previous call has finished before we continue.
  63. # See https://stackoverflow.com/questions/51361856/how-to-use-task-await-with-genserver
  64. Restarter.Pleroma.rebooted?()
  65. end)
  66. end
  67. test "don't restart if no reboot time settings were changed" do
  68. clear_config(:emoji)
  69. insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
  70. refute String.contains?(
  71. capture_log(fn ->
  72. TransferTask.start_link([])
  73. # TransferTask.start_link/1 is an asynchronous call.
  74. # A GenServer will first finish the previous call before starting a new one.
  75. # Here we do a synchronous call.
  76. # That way we are sure that the previous call has finished before we continue.
  77. Restarter.Pleroma.rebooted?()
  78. end),
  79. "pleroma restarted"
  80. )
  81. end
  82. test "on reboot time key" do
  83. clear_config(:shout)
  84. insert(:config, key: :shout, value: [enabled: false])
  85. # Note that we don't actually restart Pleroma.
  86. # See module Restarter.Pleroma
  87. assert capture_log(fn ->
  88. TransferTask.start_link([])
  89. # TransferTask.start_link/1 is an asynchronous call.
  90. # A GenServer will first finish the previous call before starting a new one.
  91. # Here we do a synchronous call.
  92. # That way we are sure that the previous call has finished before we continue.
  93. Restarter.Pleroma.rebooted?()
  94. end) =~ "pleroma restarted"
  95. end
  96. test "on reboot time subkey" do
  97. clear_config(Pleroma.Captcha)
  98. insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
  99. # Note that we don't actually restart Pleroma.
  100. # See module Restarter.Pleroma
  101. assert capture_log(fn ->
  102. TransferTask.start_link([])
  103. # TransferTask.start_link/1 is an asynchronous call.
  104. # A GenServer will first finish the previous call before starting a new one.
  105. # Here we do a synchronous call.
  106. # That way we are sure that the previous call has finished before we continue.
  107. Restarter.Pleroma.rebooted?()
  108. end) =~ "pleroma restarted"
  109. end
  110. test "don't restart pleroma on reboot time key and subkey if there is false flag" do
  111. clear_config(:shout)
  112. clear_config(Pleroma.Captcha)
  113. insert(:config, key: :shout, value: [enabled: false])
  114. insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
  115. refute String.contains?(
  116. capture_log(fn ->
  117. TransferTask.load_and_update_env([], false)
  118. # TransferTask.start_link/1 is an asynchronous call.
  119. # A GenServer will first finish the previous call before starting a new one.
  120. # Here we do a synchronous call.
  121. # That way we are sure that the previous call has finished before we continue.
  122. Restarter.Pleroma.rebooted?()
  123. end),
  124. "pleroma restarted"
  125. )
  126. end
  127. end
  128. end