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