ejabberd: tests: Use common fixtures for testing actions module

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2021-09-22 14:40:32 -07:00 committed by Veiko Aasa
parent cf32de2839
commit 757d24c2be
No known key found for this signature in database
GPG Key ID: 478539CAE680674E
2 changed files with 49 additions and 80 deletions

View File

@ -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

View File

@ -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):