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