1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
14 clear_config(:configurable_from_database, true)
17 test "transfer config values from db to env" do
18 refute Application.get_env(:pleroma, :test_key)
19 refute Application.get_env(:idna, :test_key)
20 refute Application.get_env(:postgrex, :test_key)
22 initial = Application.get_env(:logger, :level)
24 insert(:config, key: :test_key, value: [live: 2, com: 3])
25 insert(:config, group: :idna, key: :test_key, value: [live: 15, com: 35])
26 insert(:config, group: :postgrex, key: :test_key, value: :value)
27 insert(:config, group: :logger, key: :level, value: :debug)
28 insert(:config, group: :pleroma, key: :instance, value: [static_dir: "static_dir_from_db"])
29 TransferTask.start_link([])
31 assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
32 assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
33 assert Application.get_env(:logger, :level) == :debug
34 assert Application.get_env(:postgrex, :test_key) == :value
35 assert Application.get_env(:pleroma, :instance)[:static_dir] == "static_dir_from_db"
38 Application.delete_env(:pleroma, :test_key)
39 Application.delete_env(:idna, :test_key)
40 Application.delete_env(:postgrex, :test_key)
41 Application.put_env(:logger, :level, initial)
42 System.delete_env("RELEASE_NAME")
46 test "transfer task falls back to env before default" do
47 instance = Application.get_env(:pleroma, :instance)
49 insert(:config, key: :instance, value: [name: "wow"])
50 clear_config([:instance, :static_dir], "static_dir_from_env")
51 TransferTask.start_link([])
53 assert Application.get_env(:pleroma, :instance)[:name] == "wow"
54 assert Application.get_env(:pleroma, :instance)[:static_dir] == "static_dir_from_env"
57 Application.put_env(:pleroma, :instance, instance)
61 test "transfer task falls back to release defaults if no other values found" do
62 instance = Application.get_env(:pleroma, :instance)
64 System.put_env("RELEASE_NAME", "akkoma")
65 Pleroma.Config.Holder.save_default()
66 insert(:config, key: :instance, value: [name: "wow"])
67 Application.delete_env(:pleroma, :instance)
69 TransferTask.start_link([])
71 assert Application.get_env(:pleroma, :instance)[:name] == "wow"
72 assert Application.get_env(:pleroma, :instance)[:static_dir] == "/var/lib/akkoma/static"
75 System.delete_env("RELEASE_NAME")
76 Pleroma.Config.Holder.save_default()
77 Application.put_env(:pleroma, :instance, instance)
81 test "transfer config values with full subkey update" do
85 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
86 insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]])
88 TransferTask.start_link([])
90 emoji_env = Application.get_env(:pleroma, :emoji)
91 assert emoji_env[:groups] == [a: 1, b: 2]
92 assets_env = Application.get_env(:pleroma, :assets)
93 assert assets_env[:mascots] == [a: 1, b: 2]
96 describe "pleroma restart" do
99 Restarter.Pleroma.refresh()
101 # Restarter.Pleroma.refresh/0 is an asynchronous call.
102 # A GenServer will first finish the previous call before starting a new one.
103 # Here we do a synchronous call.
104 # That way we are sure that the previous call has finished before we continue.
105 # See https://stackoverflow.com/questions/51361856/how-to-use-task-await-with-genserver
106 Restarter.Pleroma.rebooted?()
110 test "don't restart if no reboot time settings were changed" do
112 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
114 refute String.contains?(
116 TransferTask.start_link([])
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?()
128 test "on reboot time key" do
129 clear_config(:rate_limit)
130 insert(:config, key: :rate_limit, value: [enabled: false])
132 # Note that we don't actually restart Pleroma.
133 # See module Restarter.Pleroma
134 assert capture_log(fn ->
135 TransferTask.start_link([])
137 # TransferTask.start_link/1 is an asynchronous call.
138 # A GenServer will first finish the previous call before starting a new one.
139 # Here we do a synchronous call.
140 # That way we are sure that the previous call has finished before we continue.
141 Restarter.Pleroma.rebooted?()
142 end) =~ "pleroma restarted"
145 test "on reboot time subkey" do
146 clear_config(Pleroma.Captcha)
147 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
149 # Note that we don't actually restart Pleroma.
150 # See module Restarter.Pleroma
151 assert capture_log(fn ->
152 TransferTask.start_link([])
154 # TransferTask.start_link/1 is an asynchronous call.
155 # A GenServer will first finish the previous call before starting a new one.
156 # Here we do a synchronous call.
157 # That way we are sure that the previous call has finished before we continue.
158 Restarter.Pleroma.rebooted?()
159 end) =~ "pleroma restarted"
162 test "don't restart pleroma on reboot time key and subkey if there is false flag" do
163 clear_config(:rate_limit)
164 clear_config(Pleroma.Captcha)
166 insert(:config, key: :rate_limit, value: [enabled: false])
167 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
169 refute String.contains?(
171 TransferTask.load_and_update_env([], false)
173 # TransferTask.start_link/1 is an asynchronous call.
174 # A GenServer will first finish the previous call before starting a new one.
175 # Here we do a synchronous call.
176 # That way we are sure that the previous call has finished before we continue.
177 Restarter.Pleroma.rebooted?()