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