diff options
Diffstat (limited to 'test/pleroma/application')
-rw-r--r-- | test/pleroma/application/config_dependent_deps_test.exs | 149 | ||||
-rw-r--r-- | test/pleroma/application/environment_test.exs | 248 | ||||
-rw-r--r-- | test/pleroma/application/requirements_test.exs | 165 |
3 files changed, 562 insertions, 0 deletions
diff --git a/test/pleroma/application/config_dependent_deps_test.exs b/test/pleroma/application/config_dependent_deps_test.exs new file mode 100644 index 000000000..620da16d9 --- /dev/null +++ b/test/pleroma/application/config_dependent_deps_test.exs @@ -0,0 +1,149 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Application.ConfigDependentDepsTest do + use ExUnit.Case + + alias Pleroma.Application.ConfigDependentDeps + + setup do + {:ok, _} = + DynamicSupervisor.start_link( + strategy: :one_for_one, + name: Pleroma.Application.DynamicSupervisorTest + ) + + {:ok, pid} = + Pleroma.Application.ConfigDependentDeps.start_link( + dynamic_supervisor: Pleroma.Application.DynamicSupervisorTest, + name: Pleroma.Application.ConfigDependentDepsTesting, + relations: [ + {{:pleroma, :dummy_module1}, Pleroma.DummyModule1}, + {{:pleroma, :dummy_module2}, Pleroma.DummyModule2}, + {:dummy_group1, :dummy_group1}, + {:ex_aws, :ex_aws}, + {:not_started_app, :not_started_app} + ] + ) + + [pid: pid] + end + + test "start_dependency/2", %{pid: pid} do + {:ok, pid} = ConfigDependentDeps.start_dependency(Pleroma.DummyModule1, pid) + assert Process.alive?(pid) + end + + describe "need_reboot?/1" do + test "apps and paths", %{pid: pid} do + changes = [ + %Pleroma.ConfigDB{group: :dummy_group1}, + %Pleroma.ConfigDB{group: :pleroma, key: :dummy_module1} + ] + + assert ConfigDependentDeps.save_config_paths_for_restart(changes, pid) == [ + {:pleroma, :dummy_module1}, + :dummy_group1 + ] + + assert ConfigDependentDeps.need_reboot?(pid) + end + + test "app and path are not duplicated", %{pid: pid} do + changes = [ + %Pleroma.ConfigDB{group: :dummy_group1}, + %Pleroma.ConfigDB{group: :dummy_group1}, + %Pleroma.ConfigDB{group: :pleroma, key: :dummy_module1}, + %Pleroma.ConfigDB{group: :pleroma, key: :dummy_module1} + ] + + assert ConfigDependentDeps.save_config_paths_for_restart(changes, pid) == [ + {:pleroma, :dummy_module1}, + :dummy_group1 + ] + + assert ConfigDependentDeps.need_reboot?(pid) + end + end + + describe "restart_dependencies/1" do + test "started dependency", %{pid: pid} do + {:ok, dummy_pid} = ConfigDependentDeps.start_dependency(Pleroma.DummyModule1, pid) + + changes = [ + %Pleroma.ConfigDB{group: :ex_aws}, + %Pleroma.ConfigDB{group: :pleroma, key: :dummy_module1} + ] + + assert ConfigDependentDeps.save_config_paths_for_restart(changes, pid) == [ + {:pleroma, :dummy_module1}, + :ex_aws + ] + + assert :ok == ConfigDependentDeps.restart_dependencies(pid) + + restarted = Process.whereis(Pleroma.DummyModule1) + + refute dummy_pid == restarted + end + + test "not started process and app", %{pid: pid} do + changes = [ + %Pleroma.ConfigDB{group: :pleroma, key: :dummy_module1}, + %Pleroma.ConfigDB{group: :not_started_app} + ] + + assert ConfigDependentDeps.save_config_paths_for_restart(changes, pid) == [ + :not_started_app, + {:pleroma, :dummy_module1} + ] + + assert :ok == ConfigDependentDeps.restart_dependencies(pid) + + started = Process.whereis(Pleroma.DummyModule1) + + assert Process.alive?(started) + end + + test "ignored dependency", %{pid: pid} do + changes = [ + %Pleroma.ConfigDB{group: :pleroma, key: :dummy_module2} + ] + + assert ConfigDependentDeps.save_config_paths_for_restart(changes, pid) == [ + {:pleroma, :dummy_module2} + ] + + assert :ok == ConfigDependentDeps.restart_dependencies(pid) + + refute Process.whereis(Pleroma.DummyModule2) + end + end + + test "process goes down", %{pid: pid} do + {:ok, dummy_pid} = ConfigDependentDeps.start_dependency(Pleroma.DummyModule1, pid) + + Process.exit(dummy_pid, :kill) + + Process.sleep(10) + restarted = Process.whereis(Pleroma.DummyModule1) + refute restarted == dummy_pid + end +end + +defmodule Pleroma.DummyModule1 do + use Agent + + def start_link(_) do + Agent.start_link(fn -> nil end, name: __MODULE__) + end +end + +defmodule Pleroma.DummyModule2 do + use Agent + + def start_link(_) do + :ignore + end +end diff --git a/test/pleroma/application/environment_test.exs b/test/pleroma/application/environment_test.exs new file mode 100644 index 000000000..2ad27ebd8 --- /dev/null +++ b/test/pleroma/application/environment_test.exs @@ -0,0 +1,248 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Application.EnvironmentTest do + use Pleroma.DataCase + + import Pleroma.Factory + + alias Pleroma.Application.Environment + + setup do: clear_config(:configurable_from_database, true) + + describe "load_from_db_and_update/0" do + test "transfer config values from db to env" do + refute Application.get_env(:pleroma, :test_key) + refute Application.get_env(:idna, :test_key) + refute Application.get_env(:quack, :test_key) + refute Application.get_env(:postgrex, :test_key) + initial = Application.get_env(:logger, :level) + + insert(:config, key: :test_key, value: [live: 2, com: 3]) + insert(:config, group: :idna, key: :test_key, value: [live: 15, com: 35]) + + insert(:config, + group: :quack, + key: nil, + value: [test_key: [key1: :test_value1, key2: :test_value2]] + ) + + insert(:config, group: :logger, key: nil, value: [level: :debug]) + + Environment.load_from_db_and_update() + + assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3] + assert Application.get_env(:idna, :test_key) == [live: 15, com: 35] + assert Application.get_env(:quack, :test_key) == [key1: :test_value1, key2: :test_value2] + assert Application.get_env(:logger, :level) == :debug + + on_exit(fn -> + Application.delete_env(:pleroma, :test_key) + Application.delete_env(:idna, :test_key) + Application.delete_env(:quack, :test_key) + Application.delete_env(:postgrex, :test_key) + Application.put_env(:logger, :level, initial) + end) + end + + test "transfer config values for 1 group and some keys" do + quack_env = Application.get_all_env(:quack) + + insert(:config, group: :quack, key: nil, value: [level: :info, meta: [:none]]) + + Environment.load_from_db_and_update() + + assert Application.get_env(:quack, :level) == :info + assert Application.get_env(:quack, :meta) == [:none] + default = Pleroma.Config.Holder.default_config(:quack, :webhook_url) + assert Application.get_env(:quack, :webhook_url) == default + + on_exit(fn -> + Application.put_all_env(quack: quack_env) + end) + end + + test "transfer config values with full subkey update" do + clear_config(:emoji) + clear_config(:assets) + + insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]]) + insert(:config, key: :assets, value: [mascots: [a: 1, b: 2]]) + + Environment.load_from_db_and_update() + + emoji_env = Application.get_env(:pleroma, :emoji) + assert emoji_env[:groups] == [a: 1, b: 2] + assets_env = Application.get_env(:pleroma, :assets) + assert assets_env[:mascots] == [a: 1, b: 2] + end + end + + describe "update/2 :ex_syslogger" do + setup do + initial = Application.get_env(:logger, :ex_syslogger) + + config = + insert(:config, + group: :logger, + key: nil, + value: [ + ex_syslogger: [ + level: :warn, + ident: "pleroma", + format: "$metadata[$level] $message", + metadata: [:request_id, :key] + ] + ] + ) + + on_exit(fn -> Application.put_env(:logger, :ex_syslogger, initial) end) + [config: config, initial: initial] + end + + test "changing", %{config: config} do + assert Environment.update([config]) == :ok + + env = Application.get_env(:logger, :ex_syslogger) + assert env[:level] == :warn + assert env[:metadata] == [:request_id, :key] + end + + test "deletion", %{config: config, initial: initial} do + assert Environment.update([config]) == :ok + + {:ok, config} = Pleroma.ConfigDB.delete(config) + assert Environment.update([config]) == :ok + + env = Application.get_env(:logger, :ex_syslogger) + + assert env == initial + end + end + + describe "update/2 :console" do + setup do + initial = Application.get_env(:logger, :console) + + config = + insert(:config, + group: :logger, + key: nil, + value: [ + console: [ + level: :info, + format: "$time $metadata[$level]", + metadata: [:request_id, :key] + ] + ] + ) + + on_exit(fn -> Application.put_env(:logger, :console, initial) end) + [config: config, initial: initial] + end + + test "change", %{config: config} do + assert Environment.update([config]) == :ok + env = Application.get_env(:logger, :console) + assert env[:level] == :info + assert env[:format] == "$time $metadata[$level]" + assert env[:metadata] == [:request_id, :key] + end + + test "deletion", %{config: config, initial: initial} do + assert Environment.update([config]) == :ok + {:ok, config} = Pleroma.ConfigDB.delete(config) + assert Environment.update([config]) == :ok + + env = Application.get_env(:logger, :console) + assert env == initial + end + end + + describe "update/2 :backends" do + setup do + initial = Application.get_all_env(:logger) + + config = + insert(:config, group: :logger, key: nil, value: [backends: [:console, :ex_syslogger]]) + + on_exit(fn -> Application.put_all_env(logger: initial) end) + + [config: config, initial: initial] + end + + test "change", %{config: config} do + assert Environment.update([config]) == :ok + env = Application.get_all_env(:logger) + assert env[:backends] == [:console, :ex_syslogger] + end + + test "deletion", %{config: config, initial: initial} do + assert Environment.update([config]) == :ok + {:ok, config} = Pleroma.ConfigDB.delete(config) + assert Environment.update([config]) + + env = Application.get_all_env(:logger) + assert env == initial + end + end + + describe "update/2 logger settings" do + setup do + initial = Application.get_all_env(:logger) + + config = + insert(:config, + group: :logger, + key: nil, + value: [ + console: [ + level: :info, + format: "$time $metadata[$level]", + metadata: [:request_id, :key] + ], + ex_syslogger: [ + level: :warn, + ident: "pleroma", + format: "$metadata[$level] $message", + metadata: [:request_id, :key] + ], + backends: [:console, :ex_syslogger] + ] + ) + + on_exit(fn -> Application.put_all_env(logger: initial) end) + [config: config] + end + + test "change", %{config: config} do + assert Environment.update([config]) == :ok + + env = + :logger + |> Application.get_all_env() + |> Keyword.take([:backends, :console, :ex_syslogger]) + + assert env[:console] == config.value[:console] + assert env[:ex_syslogger] == config.value[:ex_syslogger] + assert env[:backends] == config.value[:backends] + end + end + + test "update/2 for change without key :cors_plug" do + config = + insert(:config, + group: :cors_plug, + key: nil, + value: [max_age: 300, methods: ["GET"]] + ) + + assert Environment.update([config]) == :ok + + env = Application.get_all_env(:cors_plug) + + assert env[:max_age] == 300 + assert env[:methods] == ["GET"] + end +end diff --git a/test/pleroma/application/requirements_test.exs b/test/pleroma/application/requirements_test.exs new file mode 100644 index 000000000..c4ce045cc --- /dev/null +++ b/test/pleroma/application/requirements_test.exs @@ -0,0 +1,165 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Application.RequirementsTest do + use Pleroma.DataCase + + import ExUnit.CaptureLog + import Mock + + alias Pleroma.Application.Requirements + alias Pleroma.Emails.Mailer + + describe "check_repo_pool_size!/1" do + test "raises if the pool size is unexpected" do + clear_config([Pleroma.Repo, :pool_size], 11) + clear_config([:dangerzone, :override_repo_pool_size], false) + + assert_raise Requirements.VerifyError, + "Repo.pool_size different than recommended value.", + fn -> + capture_log(&Requirements.verify!/0) + end + end + + test "doesn't raise if the pool size is unexpected but the respective flag is set" do + clear_config([Pleroma.Repo, :pool_size], 11) + clear_config([:dangerzone, :override_repo_pool_size], true) + + assert Requirements.verify!() == :ok + end + end + + describe "check_welcome_message_config!/1" do + setup do: clear_config([:welcome]) + setup do: clear_config([Mailer]) + + test "warns if welcome email enabled but mail disabled" do + clear_config([:welcome, :email, :enabled], true) + clear_config([Mailer, :enabled], false) + + assert capture_log(fn -> + assert Requirements.verify!() == :ok + end) =~ "Welcome emails will NOT be sent" + end + end + + describe "check_confirmation_accounts!" do + setup_with_mocks([ + {Requirements, [:passthrough], + [ + check_migrations_applied!: fn _ -> :ok end + ]} + ]) do + :ok + end + + setup do: clear_config([:instance, :account_activation_required]) + + test "warns if account confirmation is required but mailer isn't enabled" do + clear_config([:instance, :account_activation_required], true) + clear_config([Mailer, :enabled], false) + + assert capture_log(fn -> + assert Requirements.verify!() == :ok + end) =~ "Users will NOT be able to confirm their accounts" + end + + test "doesn't do anything if account confirmation is disabled" do + clear_config([:instance, :account_activation_required], false) + clear_config([Mailer, :enabled], false) + assert Requirements.verify!() == :ok + end + + test "doesn't do anything if account confirmation is required and mailer is enabled" do + clear_config([:instance, :account_activation_required], true) + clear_config([Mailer, :enabled], true) + assert Requirements.verify!() == :ok + end + end + + describe "check_rum!" do + setup_with_mocks([ + {Requirements, [:passthrough], [check_migrations_applied!: fn _ -> :ok end]} + ]) do + :ok + end + + setup do: clear_config([:database, :rum_enabled]) + + test "raises if rum is enabled and detects unapplied rum migrations" do + clear_config([:database, :rum_enabled], true) + + with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> false end]}]) do + assert_raise Requirements.VerifyError, + "Unapplied RUM Migrations detected", + fn -> + capture_log(&Requirements.verify!/0) + end + end + end + + test "raises if rum is disabled and detects rum migrations" do + clear_config([:database, :rum_enabled], false) + + with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> true end]}]) do + assert_raise Requirements.VerifyError, + "RUM Migrations detected", + fn -> + capture_log(&Requirements.verify!/0) + end + end + end + + test "doesn't do anything if rum enabled and applied migrations" do + clear_config([:database, :rum_enabled], true) + + with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> true end]}]) do + assert Requirements.verify!() == :ok + end + end + + test "doesn't do anything if rum disabled" do + clear_config([:database, :rum_enabled], false) + + with_mocks([{Repo, [:passthrough], [exists?: fn _, _ -> false end]}]) do + assert Requirements.verify!() == :ok + end + end + end + + describe "check_migrations_applied" do + setup_with_mocks([ + {Ecto.Migrator, [], + [ + with_repo: fn repo, fun -> passthrough([repo, fun]) end, + migrations: fn Repo -> + [ + {:up, 20_191_128_153_944, "fix_missing_following_count"}, + {:up, 20_191_203_043_610, "create_report_notes"}, + {:down, 20_191_220_174_645, "add_scopes_to_pleroma_feo_auth_records"} + ] + end + ]} + ]) do + :ok + end + + setup do: clear_config([:i_am_aware_this_may_cause_data_loss, :disable_migration_check]) + + test "raises if it detects unapplied migrations" do + assert_raise Requirements.VerifyError, + "Unapplied Migrations detected", + fn -> + capture_log(&Requirements.verify!/0) + end + end + + test "doesn't do anything if disabled" do + clear_config([:i_am_aware_this_may_cause_data_loss, :disable_migration_check], true) + + assert :ok == Requirements.verify!() + end + end +end |