From 76ce7c38f3b40e542d1550b461f34280b247185c Mon Sep 17 00:00:00 2001 From: Nicolas ROCHE Date: Fri, 24 May 2019 18:15:30 +0200 Subject: [PATCH] tests: add unit tests for import_template.py (#33333) --- tests/test_import_template.py | 57 +++++++++++++++++---------- tests_schemas/test_import_template.py | 42 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 tests_schemas/test_import_template.py diff --git a/tests/test_import_template.py b/tests/test_import_template.py index b90cb29..425246c 100644 --- a/tests/test_import_template.py +++ b/tests/test_import_template.py @@ -6,18 +6,17 @@ from django.core.management.base import CommandError from django.test import override_settings from hobo.agent.common.management.commands.import_template import Command, UnknownTemplateError +from django.core.management import load_command_class @mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists') @mock.patch('hobo.agent.common.management.commands.import_template.call_command') @mock.patch('hobo.agent.common.management.commands.import_template.get_commands') -def test_import_template_default(get_commands, call_command, os_path_exists): +def test_import_template_default(mocked_get_commands, mocked_call_command, mocked_exists): """do import a template from default path""" - expected = [mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')] - - get_commands.return_value = 'import_site' - call_command.return_value = mock.MagicMock() - os_path_exists.return_value = True + mocked_get_commands.return_value = 'import_site' + mocked_call_command.return_value = mock.MagicMock() + mocked_exists.return_value = True # simulate: # $ XXX-manage import-template import_me @@ -26,18 +25,17 @@ def test_import_template_default(get_commands, call_command, os_path_exists): command.handle(template_name='import_me') # assert 'import_site' command is run - assert call_command.mock_calls == expected + assert mocked_call_command.mock_calls == [ + mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')] @mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists') @mock.patch('hobo.agent.common.management.commands.import_template.call_command') @mock.patch('hobo.agent.common.management.commands.import_template.get_commands') -def test_import_template_basepath(get_commands, call_command, os_path_exists): +def test_import_template_basepath(mocked_get_commands, mocked_call_command, mocked_exists): """do import a template having basepath parameter provided""" - expected = [mock.call('import_site', '/tmp/import_me.json')] - - get_commands.return_value = 'import_site' - call_command.return_value = mock.MagicMock() - os_path_exists.return_value = True + mocked_get_commands.return_value = 'import_site' + mocked_call_command.return_value = mock.MagicMock() + mocked_exists.return_value = True # simulate: # $ XXX-manage import-template import_me --basepath /tmp @@ -46,18 +44,18 @@ def test_import_template_basepath(get_commands, call_command, os_path_exists): command.handle(template_name='import_me', basepath='/tmp') # assert 'import_site' command is run - assert call_command.mock_calls == expected + assert mocked_call_command.mock_calls == [ + mock.call('import_site', '/tmp/import_me.json')] @mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists') @mock.patch('hobo.agent.common.management.commands.import_template.call_command') @mock.patch('hobo.agent.common.management.commands.import_template.get_commands') -def test_import_template_warning(get_commands, call_command, os_path_exists): +def test_import_template_warning(mocked_get_commands, mocked_call_command, mocked_exists): """returns if template is not found""" - - get_commands.return_value = 'import_site' - call_command.return_value = mock.MagicMock() - os_path_exists.return_value = False + mocked_get_commands.return_value = 'import_site' + mocked_call_command.return_value = mock.MagicMock() + mocked_exists.return_value = False # simulate: # $ XXX-manage import-template import_me @@ -67,4 +65,23 @@ def test_import_template_warning(get_commands, call_command, os_path_exists): command.handle(template_name='import_me') # assert 'import_site' command is not run - assert call_command.mock_calls == [] + assert mocked_call_command.mock_calls == [] + + +@mock.patch('hobo.agent.common.management.commands.import_template.Command.handle') +def test_import_template_on_combo_agent(mocked_super): + """overloaded import-template for combo""" + command = load_command_class('hobo.agent.combo', 'import_template') + + # template specified and found + command.handle(template_name='my-template') + assert mocked_super.mock_calls == [mock.call(template_name='my-template')] + + # do nothing on internal templates + mocked_super.side_effect = UnknownTemplateError + for template in 'portal-user', 'portal-agent': + command.handle(template_name=template) + + # template no found + with pytest.raises(UnknownTemplateError): + command.handle(template_name='unknown-template') diff --git a/tests_schemas/test_import_template.py b/tests_schemas/test_import_template.py new file mode 100644 index 0000000..7fdc8fa --- /dev/null +++ b/tests_schemas/test_import_template.py @@ -0,0 +1,42 @@ +import os + +import mock +import pytest + +from django.core.management import call_command, load_command_class +from hobo.agent.common.management.commands.import_template import UnknownTemplateError + +@mock.patch('hobo.agent.common.management.commands.import_template.call_command') +@mock.patch('hobo.agent.common.management.commands.import_template.get_commands') +def test_import_template_on_common_agent(mocked_get_commands, mocked_call_command, tmpdir): + """import-template basic case (for passerelle here): + $ passerelle-manage import_template my-template # simulate this bash query + check this call result in '$ passerelle-manage import_site my-template' + """ + command = load_command_class('hobo.agent.common', 'import_template') + + mocked_get_commands.return_value = ['import_site'] + template_path = os.path.join(str(tmpdir), 'my-template.json') + with open(template_path, 'w') as handler: + handler.write('...') + + command.handle(basepath=str(tmpdir), template_name='my-template') + assert mocked_call_command.mock_calls == [mock.call('import_site', template_path)] + + +@mock.patch('hobo.agent.common.management.commands.import_template.call_command') +@mock.patch('hobo.agent.common.management.commands.import_template.get_commands') +def test_import_template_on_combo_agent(mocked_get_commands, mocked_call_command, tmpdir): + """overloaded case for combo + $ combo-manage import_template my-template # simulate this bash query + only check we reach the hobo.agent.common's code here + """ + command = load_command_class('hobo.agent.combo', 'import_template') + + mocked_get_commands.return_value = ['import_site'] + template_path = os.path.join(str(tmpdir), 'my-template.json') + with open(template_path, 'w') as handler: + handler.write('...') + + command.handle(basepath=str(tmpdir), template_name='my-template') + assert mocked_call_command.mock_calls == [mock.call('import_site', template_path)]