ensure .exs config is used before default (#197)
[akkoma] / test / pleroma / config / transfer_task_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Config.TransferTaskTest do
6 use Pleroma.DataCase
7
8 import ExUnit.CaptureLog
9 import Pleroma.Factory
10
11 alias Pleroma.Config.TransferTask
12
13 setup do
14 clear_config(:configurable_from_database, true)
15 end
16
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)
22
23 initial = Application.get_env(:logger, :level)
24
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([])
32
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"
39
40 on_exit(fn ->
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")
47 end)
48 end
49
50 test "transfer task falls back to env before default" do
51 instance = Application.get_env(:pleroma, :instance)
52
53 insert(:config, key: :instance, value: [name: "wow"])
54 clear_config([:instance, :static_dir], "static_dir_from_env")
55 TransferTask.start_link([])
56
57 assert Application.get_env(:pleroma, :instance)[:name] == "wow"
58 assert Application.get_env(:pleroma, :instance)[:static_dir] == "static_dir_from_env"
59
60 on_exit(fn ->
61 Application.put_env(:pleroma, :instance, instance)
62 end)
63 end
64
65 test "transfer task falls back to release defaults if no other values found" do
66 instance = Application.get_env(:pleroma, :instance)
67
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)
72
73 TransferTask.start_link([])
74
75 assert Application.get_env(:pleroma, :instance)[:name] == "wow"
76 assert Application.get_env(:pleroma, :instance)[:static_dir] == "/var/lib/akkoma/static"
77
78 on_exit(fn ->
79 System.delete_env("RELEASE_NAME")
80 Pleroma.Config.Holder.save_default()
81 Application.put_env(:pleroma, :instance, instance)
82 end)
83 end
84
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)
88
89 insert(:config, group: :quack, key: :level, value: :info)
90 insert(:config, group: :quack, key: :meta, value: [:none])
91
92 TransferTask.start_link([])
93
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
98
99 on_exit(fn ->
100 Application.put_env(:quack, :level, level)
101 Application.put_env(:quack, :meta, meta)
102 end)
103 end
104
105 test "transfer config values with full subkey update" do
106 clear_config(:emoji)
107 clear_config(:assets)
108
109 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
110 insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]])
111
112 TransferTask.start_link([])
113
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]
118 end
119
120 describe "pleroma restart" do
121 setup do
122 on_exit(fn -> Restarter.Pleroma.refresh() end)
123 end
124
125 @tag :erratic
126 test "don't restart if no reboot time settings were changed" do
127 clear_config(:emoji)
128 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
129
130 refute String.contains?(
131 capture_log(fn -> TransferTask.start_link([]) end),
132 "pleroma restarted"
133 )
134 end
135
136 @tag :erratic
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"
141 end
142
143 @tag :erratic
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"
148 end
149
150 @tag :erratic
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)
154
155 insert(:config, key: {:pleroma, :rate_limit}, value: [enabled: false])
156 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
157
158 refute String.contains?(
159 capture_log(fn -> TransferTask.load_and_update_env([], false) end),
160 "pleroma restarted"
161 )
162 end
163 end
164 end