78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
import pytest
|
|
import mock
|
|
import StringIO
|
|
|
|
from django.core.management.base import CommandError
|
|
|
|
from hobo.environment.models import ServiceBase
|
|
from hobo.environment.management.commands.cook import Command
|
|
|
|
|
|
def test_check_action(monkeypatch):
|
|
"""no exception raised if url are available"""
|
|
command = Command()
|
|
command.server_action = 'mock a server_action handler (ex: hobo-create)'
|
|
action, action_args = 'server-action', {u'url': u'https://test.org/'}
|
|
|
|
monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
|
|
monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
|
|
command.check_action(action, action_args)
|
|
assert True
|
|
|
|
def test_check_action_unknown_action(monkeypatch):
|
|
"""raise CommandError"""
|
|
command = Command()
|
|
command.server_action = 'mock a server_action handler (ex: hobo-create)'
|
|
action, action_args = 'not-a-server-action', {u'url': u'https://test.org/'}
|
|
|
|
monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
|
|
monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
|
|
with pytest.raises(CommandError) as e_info:
|
|
command.check_action(action, action_args)
|
|
assert 'Unknown action not-a-server-action' in str(e_info.value)
|
|
|
|
def test_check_action_unresolvable(monkeypatch):
|
|
"""raise CommandError"""
|
|
command = Command()
|
|
command.server_action = 'mock a server_action handler (ex: hobo-create)'
|
|
action, action_args = 'server-action', {u'url': u'https://test.org/'}
|
|
|
|
monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: False)
|
|
monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
|
|
with pytest.raises(CommandError) as e_info:
|
|
command.check_action(action, action_args)
|
|
assert 'is not resolvable' in str(e_info.value)
|
|
|
|
def test_check_action_invalid_certificat(monkeypatch):
|
|
"""raise CommandError"""
|
|
command = Command()
|
|
command.server_action = 'mock a server_action handler (ex: hobo-create)'
|
|
action, action_args = 'server-action', {u'url': u'https://test.org/'}
|
|
|
|
monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
|
|
monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: False)
|
|
with pytest.raises(CommandError) as e_info:
|
|
command.check_action(action, action_args)
|
|
assert 'has no valid certificate' in str(e_info.value)
|
|
|
|
@mock.patch('hobo.environment.management.commands.cook.open')
|
|
def test_having_several_action_args(mocked_open):
|
|
"""load variables from a file"""
|
|
receipe = """{
|
|
"steps": [
|
|
{"create-authentic": {
|
|
"url": "https://entrouvert.org/",
|
|
"title": "Connexion"
|
|
}}
|
|
]
|
|
}"""
|
|
expected_a2 = "[call(title=u'Connexion', url=u'https://entrouvert.org/')]"
|
|
|
|
command = Command()
|
|
command.permissive = True
|
|
command.create_authentic = mock.MagicMock()
|
|
|
|
mocked_open.side_effect = [StringIO.StringIO(receipe)]
|
|
command.run_cook('recipe_me.json')
|
|
assert str(command.create_authentic.mock_calls) == expected_a2
|