Merge branch 'stable' into stable-sync/2.1.1
[akkoma] / test / config / transfer_task_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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(:quack, :test_key)
19 refute Application.get_env(:postgrex, :test_key)
20 initial = Application.get_env(:logger, :level)
21
22 insert(:config, key: :test_key, value: [live: 2, com: 3])
23 insert(:config, group: :idna, key: :test_key, value: [live: 15, com: 35])
24 insert(:config, group: :quack, key: :test_key, value: [:test_value1, :test_value2])
25 insert(:config, group: :postgrex, key: :test_key, value: :value)
26 insert(:config, group: :logger, key: :level, value: :debug)
27
28 TransferTask.start_link([])
29
30 assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
31 assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
32 assert Application.get_env(:quack, :test_key) == [:test_value1, :test_value2]
33 assert Application.get_env(:logger, :level) == :debug
34 assert Application.get_env(:postgrex, :test_key) == :value
35
36 on_exit(fn ->
37 Application.delete_env(:pleroma, :test_key)
38 Application.delete_env(:idna, :test_key)
39 Application.delete_env(:quack, :test_key)
40 Application.delete_env(:postgrex, :test_key)
41 Application.put_env(:logger, :level, initial)
42 end)
43 end
44
45 test "transfer config values for 1 group and some keys" do
46 level = Application.get_env(:quack, :level)
47 meta = Application.get_env(:quack, :meta)
48
49 insert(:config, group: :quack, key: :level, value: :info)
50 insert(:config, group: :quack, key: :meta, value: [:none])
51
52 TransferTask.start_link([])
53
54 assert Application.get_env(:quack, :level) == :info
55 assert Application.get_env(:quack, :meta) == [:none]
56 default = Pleroma.Config.Holder.default_config(:quack, :webhook_url)
57 assert Application.get_env(:quack, :webhook_url) == default
58
59 on_exit(fn ->
60 Application.put_env(:quack, :level, level)
61 Application.put_env(:quack, :meta, meta)
62 end)
63 end
64
65 test "transfer config values with full subkey update" do
66 clear_config(:emoji)
67 clear_config(:assets)
68
69 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
70 insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]])
71
72 TransferTask.start_link([])
73
74 emoji_env = Application.get_env(:pleroma, :emoji)
75 assert emoji_env[:groups] == [a: 1, b: 2]
76 assets_env = Application.get_env(:pleroma, :assets)
77 assert assets_env[:mascots] == [a: 1, b: 2]
78 end
79
80 describe "pleroma restart" do
81 setup do
82 on_exit(fn -> Restarter.Pleroma.refresh() end)
83 end
84
85 test "don't restart if no reboot time settings were changed" do
86 clear_config(:emoji)
87 insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]])
88
89 refute String.contains?(
90 capture_log(fn -> TransferTask.start_link([]) end),
91 "pleroma restarted"
92 )
93 end
94
95 test "on reboot time key" do
96 clear_config(:chat)
97 insert(:config, key: :chat, value: [enabled: false])
98 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
99 end
100
101 test "on reboot time subkey" do
102 clear_config(Pleroma.Captcha)
103 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
104 assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"
105 end
106
107 test "don't restart pleroma on reboot time key and subkey if there is false flag" do
108 clear_config(:chat)
109 clear_config(Pleroma.Captcha)
110
111 insert(:config, key: :chat, value: [enabled: false])
112 insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])
113
114 refute String.contains?(
115 capture_log(fn -> TransferTask.load_and_update_env([], false) end),
116 "pleroma restarted"
117 )
118 end
119 end
120 end