1
|
import pytest
|
2
|
import mock
|
3
|
|
4
|
from django.conf import settings
|
5
|
from django.test import override_settings
|
6
|
|
7
|
from hobo.agent.common.management.commands.import_template import Command
|
8
|
|
9
|
|
10
|
@pytest.fixture
|
11
|
def template():
|
12
|
# build a template file to import
|
13
|
basepath = '/tmp'
|
14
|
template_name = 'import_me'
|
15
|
with open('/%s/%s.json' % (basepath, template_name),'w') as handler:
|
16
|
handler.write('some content to import')
|
17
|
return template_name
|
18
|
|
19
|
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
|
20
|
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
|
21
|
def test_import_template_ok(mocked_get_commands, mocked_call_command, template):
|
22
|
"""do import a template"""
|
23
|
expected = [mock.call('import_site', '/tmp/import_me.json')]
|
24
|
|
25
|
mocked_get_commands.return_value = 'import_site'
|
26
|
mocked_call_command.return_value = mock.MagicMock()
|
27
|
|
28
|
# simulate
|
29
|
# $ XXX-manage import-template import_me --basepath /tmp
|
30
|
command = Command()
|
31
|
with override_settings(PROJECT_NAME='bobo'):
|
32
|
command.handle(template_name=template, basepath='/tmp')
|
33
|
|
34
|
# assert 'import_site' command is run
|
35
|
assert mocked_call_command.mock_calls == expected
|
36
|
|
37
|
def test_import_template_default_path():
|
38
|
"""check default path where template should be found"""
|
39
|
expected = '/var/lib/bobo/skeletons/import_me.json'
|
40
|
|
41
|
# simulate
|
42
|
# $ XXX-manage import-template import_me
|
43
|
command = Command()
|
44
|
with override_settings(PROJECT_NAME='bobo'):
|
45
|
command.handle(template_name='import_me')
|
46
|
|
47
|
# should raise error about the path where template was expected
|
48
|
# should return the expected path in order to check it
|
49
|
# in fact this path is wrong: /var/lib/%s/skeletons/import_me.json
|
50
|
assert '/var/lib/%s/skeletons/import_me.json' == expected
|