1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Config.TransferTaskTest do
8 import ExUnit.CaptureLog
11 alias Pleroma.Config.TransferTask
13 setup do: clear_config(:configurable_from_database, true)
15 test "transfer config values from db to env" do
16 refute Application.get_env(:pleroma, :test_key)
17 refute Application.get_env(:idna, :test_key)
18 refute Application.get_env(:postgrex, :test_key)
19 initial = Application.get_env(:logger, :level)
21 insert(:config, key: :test_key, value: [live: 2, com: 3])
22 insert(:config, group: :idna, key: :test_key, value: [live: 15, com: 35])
23 insert(:config, group: :postgrex, key: :test_key, value: :value)
24 insert(:config, group: :logger, key: :level, value: :debug)
26 TransferTask.start_link([])
28 assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
29 assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
30 assert Application.get_env(:logger, :level) == :debug
31 assert Application.get_env(:postgrex, :test_key) == :value
34 Application.delete_env(:pleroma, :test_key)
35 Application.delete_env(:idna, :test_key)
36 Application.delete_env(:postgrex, :test_key)
37 Application.put_env(:logger, :level, initial)
41 test "transfer config values for 1 group and some keys" do
42 level = Application.get_env(:somegroup, :level)
43 meta = Application.get_env(:somegroup, :meta)
45 insert(:config, group: :somegroup, key: :level, value: :info)
46 insert(:config, group: :somegroup, key: :meta, value: [:none])
48 TransferTask.start_link([])
50 assert Application.get_env(:somegroup, :level) == :info
51 assert Application.get_env(:somegroup, :meta) == [:none]
54 Application.put_env(:somegroup, :level, level)
55 Application.put_env(:somegroup, :meta, meta)
59 test "transfer config values with full subkey update" do
63 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
64 insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]])
66 TransferTask.start_link([])
68 emoji_env = Application.get_env(:pleroma, :emoji)
69 assert emoji_env[:groups] == [a: 1, b: 2]
70 assets_env = Application.get_env(:pleroma, :assets)
71 assert assets_env[:mascots] == [a: 1, b: 2]
74 describe "pleroma restart" do
77 Restarter.Pleroma.refresh()
79 # Restarter.Pleroma.refresh/0 is an asynchronous call.
80 # A GenServer will first finish the previous call before starting a new one.
81 # Here we do a synchronous call.
82 # That way we are sure that the previous call has finished before we continue.
83 # See https://stackoverflow.com/questions/51361856/how-to-use-task-await-with-genserver
84 Restarter.Pleroma.rebooted?()
88 test "don't restart if no reboot time settings were changed" do
90 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
92 refute String.contains?(
94 TransferTask.start_link([])
96 # TransferTask.start_link/1 is an asynchronous call.
97 # A GenServer will first finish the previous call before starting a new one.
98 # Here we do a synchronous call.
99 # That way we are sure that the previous call has finished before we continue.
100 Restarter.Pleroma.rebooted?()
106 test "on reboot time key" do
108 insert(:config, key: :shout, value: [enabled: false])
110 # Note that we don't actually restart Pleroma.
111 # See module Restarter.Pleroma
112 assert capture_log(fn ->
113 TransferTask.start_link([])
115 # TransferTask.start_link/1 is an asynchronous call.
116 # A GenServer will first finish the previous call before starting a new one.
117 # Here we do a synchronous call.
118 # That way we are sure that the previous call has finished before we continue.
119 Restarter.Pleroma.rebooted?()
120 end) =~ "pleroma restarted"
123 test "on reboot time subkey" do
124 clear_config(Pleroma.Captcha)
125 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
127 # Note that we don't actually restart Pleroma.
128 # See module Restarter.Pleroma
129 assert capture_log(fn ->
130 TransferTask.start_link([])
132 # TransferTask.start_link/1 is an asynchronous call.
133 # A GenServer will first finish the previous call before starting a new one.
134 # Here we do a synchronous call.
135 # That way we are sure that the previous call has finished before we continue.
136 Restarter.Pleroma.rebooted?()
137 end) =~ "pleroma restarted"
140 test "don't restart pleroma on reboot time key and subkey if there is false flag" do
142 clear_config(Pleroma.Captcha)
144 insert(:config, key: :shout, value: [enabled: false])
145 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
147 refute String.contains?(
149 TransferTask.load_and_update_env([], false)
151 # TransferTask.start_link/1 is an asynchronous call.
152 # A GenServer will first finish the previous call before starting a new one.
153 # Here we do a synchronous call.
154 # That way we are sure that the previous call has finished before we continue.
155 Restarter.Pleroma.rebooted?()