From 757d24c2bede9dc9b08035287e3be9ca2264e494 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 22 Sep 2021 14:40:32 -0700 Subject: [PATCH] ejabberd: tests: Use common fixtures for testing actions module Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/modules/ejabberd/tests/conftest.py | 79 ------------------- .../ejabberd/tests/test_turn_config.py | 50 +++++++++++- 2 files changed, 49 insertions(+), 80 deletions(-) delete mode 100644 plinth/modules/ejabberd/tests/conftest.py diff --git a/plinth/modules/ejabberd/tests/conftest.py b/plinth/modules/ejabberd/tests/conftest.py deleted file mode 100644 index ce5488342..000000000 --- a/plinth/modules/ejabberd/tests/conftest.py +++ /dev/null @@ -1,79 +0,0 @@ -# SPDX-License-Identifier: AGPL-3.0-or-later -""" -Common test fixtures for ejabberd. -""" - -import importlib -import pathlib -import shutil -import types -from unittest.mock import MagicMock, patch - -import pytest - -from plinth.modules import ejabberd - -current_directory = pathlib.Path(__file__).parent - - -def _load_actions_module(): - actions_file_path = str(current_directory / '..' / '..' / '..' / '..' / - 'actions' / 'ejabberd') - loader = importlib.machinery.SourceFileLoader('ejabberd', - actions_file_path) - module = types.ModuleType(loader.name) - loader.exec_module(module) - return module - - -actions = _load_actions_module() - - -@pytest.fixture(name='conf_file') -def fixture_conf_file(tmp_path): - """Uses a dummy configuration file.""" - settings_file_name = 'ejabberd.yml' - conf_file = tmp_path / settings_file_name - conf_file.touch() - shutil.copyfile(str(current_directory / 'data' / 'ejabberd.yml.example'), - str(conf_file)) - return str(conf_file) - - -@pytest.fixture(name='managed_file') -def fixture_managed_file(tmp_path): - """Uses a dummy managed file.""" - file_name = 'freedombox_managed_coturn' - fil = tmp_path / file_name - return str(fil) - - -@pytest.fixture(name='call_action') -def fixture_call_action(capsys, conf_file, managed_file): - """Run actions with custom root path.""" - - def _call_action(module_name, args, **kwargs): - actions.EJABBERD_CONFIG = conf_file - actions.EJABBERD_MANAGED_COTURN = managed_file - with patch('argparse._sys.argv', [module_name] + args): - actions.main() - captured = capsys.readouterr() - return captured.out - - return _call_action - - -@pytest.fixture(name='test_configuration', autouse=True) -def fixture_test_configuration(call_action, conf_file): - """Use a separate ejabberd configuration for tests. - - Patches actions.superuser_run with the fixture call_action. - The module state is patched to be 'up-to-date'. - """ - with patch('plinth.actions.superuser_run', call_action): - - helper = MagicMock() - helper.get_state.return_value = 'up-to-date' - ejabberd.setup_helper = helper - - yield diff --git a/plinth/modules/ejabberd/tests/test_turn_config.py b/plinth/modules/ejabberd/tests/test_turn_config.py index a27171388..21de15441 100644 --- a/plinth/modules/ejabberd/tests/test_turn_config.py +++ b/plinth/modules/ejabberd/tests/test_turn_config.py @@ -3,7 +3,11 @@ Test module for ejabberd STUN/TURN configuration. """ -from unittest.mock import patch +import pathlib +import shutil +from unittest.mock import MagicMock, patch + +import pytest from plinth.modules import ejabberd from plinth.modules.coturn.components import TurnConfiguration @@ -16,6 +20,50 @@ overridden_configuration = TurnConfiguration( 'public.coturn.site', [], 'BUeKvKzhAsTZ8MEwMd3yTwpr2uvbOxgWe51aiP02OAGyOlj6WGuCyqj7iaOsbkC7') +actions_name = 'ejabberd' + +current_directory = pathlib.Path(__file__).parent + + +@pytest.fixture(name='conf_file') +def fixture_conf_file(tmp_path): + """Uses a dummy configuration file.""" + settings_file_name = 'ejabberd.yml' + conf_file = tmp_path / settings_file_name + conf_file.touch() + shutil.copyfile(str(current_directory / 'data' / 'ejabberd.yml.example'), + str(conf_file)) + return str(conf_file) + + +@pytest.fixture(name='managed_file') +def fixture_managed_file(tmp_path): + """Uses a dummy managed file.""" + file_name = 'freedombox_managed_coturn' + fil = tmp_path / file_name + return str(fil) + + +@pytest.fixture(autouse=True) +def fixture_set_configuration_paths(actions_module, conf_file, managed_file): + """Setup configuration file paths in action module.""" + actions_module.EJABBERD_CONFIG = conf_file + actions_module.EJABBERD_MANAGED_COTURN = managed_file + + +@pytest.fixture(name='test_configuration', autouse=True) +def fixture_test_configuration(call_action, conf_file): + """Use a separate ejabberd configuration for tests. + + Patches actions.superuser_run with the fixture call_action. + The module state is patched to be 'up-to-date'. + """ + with patch('plinth.actions.superuser_run', call_action): + helper = MagicMock() + helper.get_state.return_value = 'up-to-date' + ejabberd.setup_helper = helper + yield + def _set_turn_configuration(monkeypatch, config=managed_configuration, managed=True):