Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / pleroma / application_requirements_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.ApplicationRequirementsTest do
6 use Pleroma.DataCase
7
8 import ExUnit.CaptureLog
9 import Mock
10
11 alias Pleroma.ApplicationRequirements
12 alias Pleroma.Config
13 alias Pleroma.Repo
14
15 describe "check_repo_pool_size!/1" do
16 test "raises if the pool size is unexpected" do
17 clear_config([Pleroma.Repo, :pool_size], 11)
18 clear_config([:dangerzone, :override_repo_pool_size], false)
19
20 assert_raise Pleroma.ApplicationRequirements.VerifyError,
21 "Repo.pool_size different than recommended value.",
22 fn ->
23 capture_log(&Pleroma.ApplicationRequirements.verify!/0)
24 end
25 end
26
27 test "doesn't raise if the pool size is unexpected but the respective flag is set" do
28 clear_config([Pleroma.Repo, :pool_size], 11)
29 clear_config([:dangerzone, :override_repo_pool_size], true)
30
31 assert Pleroma.ApplicationRequirements.verify!() == :ok
32 end
33 end
34
35 describe "check_welcome_message_config!/1" do
36 setup do: clear_config([:welcome])
37 setup do: clear_config([Pleroma.Emails.Mailer])
38
39 test "raises if welcome email enabled but mail disabled" do
40 Pleroma.Config.put([:welcome, :email, :enabled], true)
41 Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
42
43 assert_raise Pleroma.ApplicationRequirements.VerifyError, "The mail disabled.", fn ->
44 capture_log(&Pleroma.ApplicationRequirements.verify!/0)
45 end
46 end
47 end
48
49 describe "check_confirmation_accounts!" do
50 setup_with_mocks([
51 {Pleroma.ApplicationRequirements, [:passthrough],
52 [
53 check_migrations_applied!: fn _ -> :ok end
54 ]}
55 ]) do
56 :ok
57 end
58
59 setup do: clear_config([:instance, :account_activation_required])
60
61 test "raises if account confirmation is required but mailer isn't enable" do
62 Pleroma.Config.put([:instance, :account_activation_required], true)
63 Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
64
65 assert_raise Pleroma.ApplicationRequirements.VerifyError,
66 "Account activation enabled, but Mailer is disabled. Cannot send confirmation emails.",
67 fn ->
68 capture_log(&Pleroma.ApplicationRequirements.verify!/0)
69 end
70 end
71
72 test "doesn't do anything if account confirmation is disabled" do
73 Pleroma.Config.put([:instance, :account_activation_required], false)
74 Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
75 assert Pleroma.ApplicationRequirements.verify!() == :ok
76 end
77
78 test "doesn't do anything if account confirmation is required and mailer is enabled" do
79 Pleroma.Config.put([:instance, :account_activation_required], true)
80 Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], true)
81 assert Pleroma.ApplicationRequirements.verify!() == :ok
82 end
83 end
84
85 describe "check_rum!" do
86 setup_with_mocks([
87 {Pleroma.ApplicationRequirements, [:passthrough],
88 [check_migrations_applied!: fn _ -> :ok end]}
89 ]) do
90 :ok
91 end
92
93 setup do: clear_config([:database, :rum_enabled])
94
95 test "raises if rum is enabled and detects unapplied rum migrations" do
96 Config.put([:database, :rum_enabled], true)
97
98 with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> false end]}]) do
99 assert_raise ApplicationRequirements.VerifyError,
100 "Unapplied RUM Migrations detected",
101 fn ->
102 capture_log(&ApplicationRequirements.verify!/0)
103 end
104 end
105 end
106
107 test "raises if rum is disabled and detects rum migrations" do
108 Config.put([:database, :rum_enabled], false)
109
110 with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> true end]}]) do
111 assert_raise ApplicationRequirements.VerifyError,
112 "RUM Migrations detected",
113 fn ->
114 capture_log(&ApplicationRequirements.verify!/0)
115 end
116 end
117 end
118
119 test "doesn't do anything if rum enabled and applied migrations" do
120 Config.put([:database, :rum_enabled], true)
121
122 with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> true end]}]) do
123 assert ApplicationRequirements.verify!() == :ok
124 end
125 end
126
127 test "doesn't do anything if rum disabled" do
128 Config.put([:database, :rum_enabled], false)
129
130 with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> false end]}]) do
131 assert ApplicationRequirements.verify!() == :ok
132 end
133 end
134 end
135
136 describe "check_migrations_applied!" do
137 setup_with_mocks([
138 {Ecto.Migrator, [],
139 [
140 with_repo: fn repo, fun -> passthrough([repo, fun]) end,
141 migrations: fn Repo ->
142 [
143 {:up, 20_191_128_153_944, "fix_missing_following_count"},
144 {:up, 20_191_203_043_610, "create_report_notes"},
145 {:down, 20_191_220_174_645, "add_scopes_to_pleroma_feo_auth_records"}
146 ]
147 end
148 ]}
149 ]) do
150 :ok
151 end
152
153 setup do: clear_config([:i_am_aware_this_may_cause_data_loss, :disable_migration_check])
154
155 test "raises if it detects unapplied migrations" do
156 assert_raise ApplicationRequirements.VerifyError,
157 "Unapplied Migrations detected",
158 fn ->
159 capture_log(&ApplicationRequirements.verify!/0)
160 end
161 end
162
163 test "doesn't do anything if disabled" do
164 Config.put([:i_am_aware_this_may_cause_data_loss, :disable_migration_check], true)
165
166 assert :ok == ApplicationRequirements.verify!()
167 end
168 end
169 end