b59a9988e68facae024c666c6a751e4a12a9a0ac
[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_rum!" do
27 setup_with_mocks([
28 {Pleroma.ApplicationRequirements, [:passthrough],
29 [check_migrations_applied!: fn _ -> :ok end]}
30 ]) do
31 :ok
32 end
33
34 setup do: clear_config([:database, :rum_enabled])
35
36 test "raises if rum is enabled and detects unapplied rum migrations" do
37 Pleroma.Config.put([:database, :rum_enabled], true)
38
39 with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> false end]}]) do
40 assert_raise Pleroma.ApplicationRequirements.VerifyError,
41 "Unapplied RUM Migrations detected",
42 fn ->
43 capture_log(&Pleroma.ApplicationRequirements.verify!/0)
44 end
45 end
46 end
47
48 test "raises if rum is disabled and detects rum migrations" do
49 Pleroma.Config.put([:database, :rum_enabled], false)
50
51 with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> true end]}]) do
52 assert_raise Pleroma.ApplicationRequirements.VerifyError,
53 "RUM Migrations detected",
54 fn ->
55 capture_log(&Pleroma.ApplicationRequirements.verify!/0)
56 end
57 end
58 end
59
60 test "doesn't do anything if rum enabled and applied migrations" do
61 Pleroma.Config.put([:database, :rum_enabled], true)
62
63 with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> true end]}]) do
64 assert Pleroma.ApplicationRequirements.verify!() == :ok
65 end
66 end
67
68 test "doesn't do anything if rum disabled" do
69 Pleroma.Config.put([:database, :rum_enabled], false)
70
71 with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> false end]}]) do
72 assert Pleroma.ApplicationRequirements.verify!() == :ok
73 end
74 end
75 end
76
77 describe "check_migrations_applied!" do
78 setup_with_mocks([
79 {Ecto.Migrator, [],
80 [
81 with_repo: fn repo, fun -> passthrough([repo, fun]) end,
82 migrations: fn Repo ->
83 [
84 {:up, 20_191_128_153_944, "fix_missing_following_count"},
85 {:up, 20_191_203_043_610, "create_report_notes"},
86 {:down, 20_191_220_174_645, "add_scopes_to_pleroma_feo_auth_records"}
87 ]
88 end
89 ]}
90 ]) do
91 :ok
92 end
93
94 setup do: clear_config([:i_am_aware_this_may_cause_data_loss, :disable_migration_check])
95
96 test "raises if it detects unapplied migrations" do
97 assert_raise Pleroma.ApplicationRequirements.VerifyError,
98 "Unapplied Migrations detected",
99 fn ->
100 capture_log(&Pleroma.ApplicationRequirements.verify!/0)
101 end
102 end
103
104 test "doesn't do anything if disabled" do
105 Pleroma.Config.put([:i_am_aware_this_may_cause_data_loss, :disable_migration_check], true)
106
107 assert :ok == Pleroma.ApplicationRequirements.verify!()
108 end
109 end
110 end