total rebase
[anni] / test / pleroma / repo / migrations / move_welcome_settings_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.Repo.Migrations.MoveWelcomeSettingsTest do
6   use Pleroma.DataCase, async: true
7   import Pleroma.Factory
8   import Pleroma.Tests.Helpers
9   alias Pleroma.ConfigDB
10
11   setup_all do: require_migration("20200724133313_move_welcome_settings")
12
13   describe "up/0" do
14     test "converts welcome settings", %{migration: migration} do
15       insert(:config,
16         group: :pleroma,
17         key: :instance,
18         value: [
19           welcome_message: "Test message",
20           welcome_user_nickname: "jimm",
21           name: "Pleroma"
22         ]
23       )
24
25       migration.up()
26       instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
27       welcome_config = ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
28
29       assert instance_config.value == [name: "Pleroma"]
30
31       assert welcome_config.value == [
32                direct_message: %{
33                  enabled: true,
34                  message: "Test message",
35                  sender_nickname: "jimm"
36                },
37                email: %{
38                  enabled: false,
39                  html: "Welcome to <%= instance_name %>",
40                  sender: nil,
41                  subject: "Welcome to <%= instance_name %>",
42                  text: "Welcome to <%= instance_name %>"
43                }
44              ]
45     end
46
47     test "does nothing when message empty", %{migration: migration} do
48       insert(:config,
49         group: :pleroma,
50         key: :instance,
51         value: [
52           welcome_message: "",
53           welcome_user_nickname: "jimm",
54           name: "Pleroma"
55         ]
56       )
57
58       migration.up()
59       instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
60       refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
61       assert instance_config.value == [name: "Pleroma"]
62     end
63
64     test "does nothing when welcome_message not set", %{migration: migration} do
65       insert(:config,
66         group: :pleroma,
67         key: :instance,
68         value: [welcome_user_nickname: "jimm", name: "Pleroma"]
69       )
70
71       migration.up()
72       instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
73       refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
74       assert instance_config.value == [name: "Pleroma"]
75     end
76   end
77
78   describe "down/0" do
79     test "revert new settings to old when instance setting not exists", %{migration: migration} do
80       insert(:config,
81         group: :pleroma,
82         key: :welcome,
83         value: [
84           direct_message: %{
85             enabled: true,
86             message: "Test message",
87             sender_nickname: "jimm"
88           },
89           email: %{
90             enabled: false,
91             html: "Welcome to <%= instance_name %>",
92             sender: nil,
93             subject: "Welcome to <%= instance_name %>",
94             text: "Welcome to <%= instance_name %>"
95           }
96         ]
97       )
98
99       migration.down()
100
101       refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
102       instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
103
104       assert instance_config.value == [
105                welcome_user_nickname: "jimm",
106                welcome_message: "Test message"
107              ]
108     end
109
110     test "revert new settings to old when instance setting exists", %{migration: migration} do
111       insert(:config, group: :pleroma, key: :instance, value: [name: "Pleroma App"])
112
113       insert(:config,
114         group: :pleroma,
115         key: :welcome,
116         value: [
117           direct_message: %{
118             enabled: true,
119             message: "Test message",
120             sender_nickname: "jimm"
121           },
122           email: %{
123             enabled: false,
124             html: "Welcome to <%= instance_name %>",
125             sender: nil,
126             subject: "Welcome to <%= instance_name %>",
127             text: "Welcome to <%= instance_name %>"
128           }
129         ]
130       )
131
132       migration.down()
133
134       refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
135       instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
136
137       assert instance_config.value == [
138                name: "Pleroma App",
139                welcome_user_nickname: "jimm",
140                welcome_message: "Test message"
141              ]
142     end
143   end
144 end