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(:quack, :test_key)
21 refute Application.get_env(:postgrex, :test_key)
23 initial = Application.get_env(:logger, :level)
25 insert(:config, key: :test_key, value: [live: 2, com: 3])
26 insert(:config, group: :idna, key: :test_key, value: [live: 15, com: 35])
27 insert(:config, group: :quack, key: :test_key, value: [:test_value1, :test_value2])
28 insert(:config, group: :postgrex, key: :test_key, value: :value)
29 insert(:config, group: :logger, key: :level, value: :debug)
30 insert(:config, group: :pleroma, key: :instance, value: [static_dir: "static_dir_from_db"])
31 TransferTask.start_link([])
33 assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
34 assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
35 assert Application.get_env(:quack, :test_key) == [:test_value1, :test_value2]
36 assert Application.get_env(:logger, :level) == :debug
37 assert Application.get_env(:postgrex, :test_key) == :value
38 assert Application.get_env(:pleroma, :instance)[:static_dir] == "static_dir_from_db"
41 Application.delete_env(:pleroma, :test_key)
42 Application.delete_env(:idna, :test_key)
43 Application.delete_env(:quack, :test_key)
44 Application.delete_env(:postgrex, :test_key)
45 Application.put_env(:logger, :level, initial)
46 System.delete_env("RELEASE_NAME")
50 test "transfer task falls back to env before default" do
51 instance = Application.get_env(:pleroma, :instance)
53 insert(:config, key: :instance, value: [name: "wow"])
54 clear_config([:instance, :static_dir], "static_dir_from_env")
55 TransferTask.start_link([])
57 assert Application.get_env(:pleroma, :instance)[:name] == "wow"
58 assert Application.get_env(:pleroma, :instance)[:static_dir] == "static_dir_from_env"
61 Application.put_env(:pleroma, :instance, instance)
65 test "transfer task falls back to release defaults if no other values found" do
66 instance = Application.get_env(:pleroma, :instance)
68 System.put_env("RELEASE_NAME", "akkoma")
69 Pleroma.Config.Holder.save_default()
70 insert(:config, key: :instance, value: [name: "wow"])
71 Application.delete_env(:pleroma, :instance)
73 TransferTask.start_link([])
75 assert Application.get_env(:pleroma, :instance)[:name] == "wow"
76 assert Application.get_env(:pleroma, :instance)[:static_dir] == "/var/lib/akkoma/static"
79 System.delete_env("RELEASE_NAME")
80 Pleroma.Config.Holder.save_default()
81 Application.put_env(:pleroma, :instance, instance)
85 test "transfer config values for 1 group and some keys" do
86 level = Application.get_env(:quack, :level)
87 meta = Application.get_env(:quack, :meta)
89 insert(:config, group: :quack, key: :level, value: :info)
90 insert(:config, group: :quack, key: :meta, value: [:none])
92 TransferTask.start_link([])
94 assert Application.get_env(:quack, :level) == :info
95 assert Application.get_env(:quack, :meta) == [:none]
96 default = Pleroma.Config.Holder.default_config(:quack, :webhook_url)
97 assert Application.get_env(:quack, :webhook_url) == default
100 Application.put_env(:quack, :level, level)
101 Application.put_env(:quack, :meta, meta)
105 test "transfer config values with full subkey update" do
107 clear_config(:assets)
109 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
110 insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]])
112 TransferTask.start_link([])
114 emoji_env = Application.get_env(:pleroma, :emoji)
115 assert emoji_env[:groups] == [a: 1, b: 2]
116 assets_env = Application.get_env(:pleroma, :assets)
117 assert assets_env[:mascots] == [a: 1, b: 2]
120 describe "pleroma restart" do
122 on_exit(fn -> Restarter.Pleroma.refresh() end)
126 test "don't restart if no reboot time settings were changed" do
128 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
130 refute String.contains?(
131 capture_log(fn -> TransferTask.start_link([]) end),
137 test "on reboot time key" do
138 clear_config([:pleroma, :rate_limit])
139 insert(:config, key: {:pleroma, :rate_limit}, value: [enabled: false])
140 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
144 test "on reboot time subkey" do
145 clear_config(Pleroma.Captcha)
146 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
147 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
151 test "don't restart pleroma on reboot time key and subkey if there is false flag" do
152 clear_config([:pleroma, :rate_limit])
153 clear_config(Pleroma.Captcha)
155 insert(:config, key: {:pleroma, :rate_limit}, value: [enabled: false])
156 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
158 refute String.contains?(
159 capture_log(fn -> TransferTask.load_and_update_env([], false) end),