First
[anni] / test / pleroma / config / transfer_task_test.exs
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
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: clear_config(:configurable_from_database, true)
14
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)
20
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)
25
26     TransferTask.start_link([])
27
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
32
33     on_exit(fn ->
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)
38     end)
39   end
40
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)
44
45     insert(:config, group: :somegroup, key: :level, value: :info)
46     insert(:config, group: :somegroup, key: :meta, value: [:none])
47
48     TransferTask.start_link([])
49
50     assert Application.get_env(:somegroup, :level) == :info
51     assert Application.get_env(:somegroup, :meta) == [:none]
52
53     on_exit(fn ->
54       Application.put_env(:somegroup, :level, level)
55       Application.put_env(:somegroup, :meta, meta)
56     end)
57   end
58
59   test "transfer config values with full subkey update" do
60     clear_config(:emoji)
61     clear_config(:assets)
62
63     insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
64     insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]])
65
66     TransferTask.start_link([])
67
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]
72   end
73
74   describe "pleroma restart" do
75     setup do
76       on_exit(fn ->
77         Restarter.Pleroma.refresh()
78
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?()
85       end)
86     end
87
88     test "don't restart if no reboot time settings were changed" do
89       clear_config(:emoji)
90       insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
91
92       refute String.contains?(
93                capture_log(fn ->
94                  TransferTask.start_link([])
95
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?()
101                end),
102                "pleroma restarted"
103              )
104     end
105
106     test "on reboot time key" do
107       clear_config(:shout)
108       insert(:config, key: :shout, value: [enabled: false])
109
110       # Note that we don't actually restart Pleroma.
111       # See module Restarter.Pleroma
112       assert capture_log(fn ->
113                TransferTask.start_link([])
114
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"
121     end
122
123     test "on reboot time subkey" do
124       clear_config(Pleroma.Captcha)
125       insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
126
127       # Note that we don't actually restart Pleroma.
128       # See module Restarter.Pleroma
129       assert capture_log(fn ->
130                TransferTask.start_link([])
131
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"
138     end
139
140     test "don't restart pleroma on reboot time key and subkey if there is false flag" do
141       clear_config(:shout)
142       clear_config(Pleroma.Captcha)
143
144       insert(:config, key: :shout, value: [enabled: false])
145       insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
146
147       refute String.contains?(
148                capture_log(fn ->
149                  TransferTask.load_and_update_env([], false)
150
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?()
156                end),
157                "pleroma restarted"
158              )
159     end
160   end
161 end