Projet

Général

Profil

Development #16599 » test_cook.py

Nicolas Roche, 16 avril 2019 14:50

 
1
import pytest
2

    
3
from django.core.management.base import CommandError
4

    
5
from hobo.environment.models import ServiceBase
6
from hobo.environment.management.commands.cook import Command
7

    
8

    
9
def test_check_action(monkeypatch):
10
    """no exception raised if url are available"""
11
    command = Command()
12
    command.server_action = 'mock a server_action handler (ex: hobo-create)'
13
    action, action_args = 'server-action', {u'url': u'https://test.org/'}
14

    
15
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
16
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
17
    command.check_action(action, action_args)
18
    assert True
19

    
20
def test_check_action_unknown_action(monkeypatch):
21
    """raise CommandError"""
22
    command = Command()
23
    command.server_action = 'mock a server_action handler (ex: hobo-create)'
24
    action, action_args = 'not-a-server-action', {u'url': u'https://test.org/'}
25

    
26
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
27
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
28
    with pytest.raises(CommandError) as e_info:
29
        command.check_action(action, action_args)
30
        assert 'Unknown action Bobo' in str(e_info.value)
31

    
32
def test_check_action_unresolvable(monkeypatch):
33
    """raise CommandError"""
34
    command = Command()
35
    command.server_action = 'mock a server_action handler (ex: hobo-create)'
36
    action, action_args = 'server-action', {u'url': u'https://test.org/'}
37

    
38
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: False)
39
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
40
    with pytest.raises(CommandError) as e_info:
41
        command.check_action(action, action_args)
42
        assert 'is not resolvable' in str(e_info.value)
43

    
44
def test_check_action_invalid_certificat(monkeypatch):
45
    """raise CommandError"""
46
    command = Command()
47
    command.server_action = 'mock a server_action handler (ex: hobo-create)'
48
    action, action_args = 'server-action', {u'url': u'https://test.org/'}
49

    
50
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
51
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: False)
52
    with pytest.raises(CommandError) as e_info:
53
        command.check_action(action, action_args)
54
        assert 'has no valid certificate' in str(e_info.value)