Projet

Général

Profil

0002-Fix-hobo-tests-under-django-3.2.patch

A. Berriot, 13 octobre 2022 15:25

Télécharger (8,29 ko)

Voir les différences:

Subject: [PATCH 2/5] Fix hobo tests under django 3.2

 hobo/environment/models.py     | 36 ++++++++++++++++++++--------------
 hobo/environment/validators.py |  7 +++----
 tests/test_cook.py             | 17 ++++++++--------
 tests/test_environment.py      |  5 +++--
 tests/test_theme.py            |  2 +-
 5 files changed, 37 insertions(+), 30 deletions(-)
hobo/environment/models.py
97 97
        self._parse_value_as_json()
98 98

  
99 99

  
100
def is_resolvable(url):
101
    try:
102
        netloc = urllib.parse.urlparse(url).netloc
103
        if netloc and socket.gethostbyname(netloc):
104
            return True
105
    except socket.gaierror:
106
        return False
107

  
108

  
109
def has_valid_certificate(url):
110
    try:
111
        requests.get(url, timeout=5, verify=True, allow_redirects=False)
112
        return True
113
    except requests.exceptions.SSLError:
114
        return False
115
    except requests.exceptions.ConnectionError:
116
        return False
117

  
118

  
100 119
class ServiceBase(models.Model):
101 120
    class Meta:
102 121
        abstract = True
......
224 243
        return self.get_base_url_path() + '__provision__/'
225 244

  
226 245
    def is_resolvable(self):
227
        try:
228
            netloc = urllib.parse.urlparse(self.base_url).netloc
229
            if netloc and socket.gethostbyname(netloc):
230
                return True
231
        except socket.gaierror:
232
            return False
246
        return is_resolvable(self.base_url)
233 247

  
234 248
    def has_valid_certificate(self):
235
        if not self.is_resolvable():
236
            return False
237
        try:
238
            requests.get(self.base_url, timeout=5, verify=True, allow_redirects=False)
239
            return True
240
        except requests.exceptions.SSLError:
241
            return False
242
        except requests.exceptions.ConnectionError:
243
            return False
249
        return has_valid_certificate(self.base_url)
244 250

  
245 251
    def is_running(self):
246 252
        if not self.is_resolvable():
hobo/environment/validators.py
3 3
from django.core.exceptions import ValidationError
4 4
from django.utils.translation import gettext_lazy as _
5 5

  
6
from hobo.environment.models import ServiceBase
6
from hobo.environment import models
7 7

  
8 8

  
9 9
def validate_service_url(url):
10
    service = ServiceBase(title='dummy', base_url=url)
11
    if not service.is_resolvable():
10
    if not models.is_resolvable(url):
12 11
        raise ValidationError(
13 12
            _('Error: %(netloc)s is not resolvable in URL %(url)s'),
14 13
            code='not-resolvable',
15 14
            params={'netloc': urllib.parse.urlsplit(url).netloc, 'url': url},
16 15
        )
17
    if not service.has_valid_certificate():
16
    if not models.has_valid_certificate(url):
18 17
        raise ValidationError(
19 18
            _('Error: no valid certificate for %(url)s'), code='invalid-certificate', params={'url': url}
20 19
        )
tests/test_cook.py
8 8
from django.contrib.contenttypes.models import ContentType
9 9
from django.core.management.base import CommandError
10 10

  
11
from hobo.environment import models as environment_models
11 12
from hobo.environment.management.commands.cook import Command
12 13
from hobo.environment.models import (
13 14
    Authentic,
......
32 33
    command.server_action = 'mock a server_action handler (ex: hobo-create)'
33 34
    action, action_args = 'server-action', {'url': 'https://test.org/'}
34 35

  
35
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
36
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
36
    monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True)
37
    monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: True)
37 38
    command.check_action(action, action_args)
38 39
    assert True
39 40

  
......
44 45
    command.server_action = 'mock a server_action handler (ex: hobo-create)'
45 46
    action, action_args = 'not-a-server-action', {'url': 'https://test.org/'}
46 47

  
47
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
48
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
48
    monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True)
49
    monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: True)
49 50
    with pytest.raises(CommandError) as e_info:
50 51
        command.check_action(action, action_args)
51 52
    assert 'Unknown action not-a-server-action' in str(e_info.value)
......
57 58
    command.server_action = 'mock a server_action handler (ex: hobo-create)'
58 59
    action, action_args = 'server-action', {'url': 'https://test.org/'}
59 60

  
60
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: False)
61
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
61
    monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: False)
62
    monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: True)
62 63
    with pytest.raises(CommandError) as e_info:
63 64
        command.check_action(action, action_args)
64 65
    assert 'test.org is not resolvable in URL https://test.org/' in str(e_info.value)
......
70 71
    command.server_action = 'mock a server_action handler (ex: hobo-create)'
71 72
    action, action_args = 'server-action', {'url': 'https://test.org/'}
72 73

  
73
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
74
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: False)
74
    monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True)
75
    monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: False)
75 76
    with pytest.raises(CommandError) as e_info:
76 77
        command.check_action(action, action_args)
77 78
    assert 'no valid certificate for https://test.org/' in str(e_info.value)
tests/test_environment.py
8 8
from test_manager import login
9 9
from webtest import Upload
10 10

  
11
from hobo.environment import models as environment_models
11 12
from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle, ServiceBase, Variable
12 13
from hobo.environment.utils import get_installed_services_dict
13 14
from hobo.profile.models import AttributeDefinition
......
143 144
    response = form.submit()
144 145
    assert 'not resolvable' in response
145 146

  
146
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
147
    monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True)
147 148
    form = response.form
148 149
    response = form.submit()
149 150
    assert 'no valid certificate' in response
150 151

  
151 152
    assert not Combo.objects.exists()
152
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
153
    monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: True)
153 154
    form = response.form
154 155
    response = form.submit()
155 156
    assert Combo.objects.exists()
tests/test_theme.py
19 19
    assert Variable.objects.filter(name='theme')[0].value == 'alfortville'
20 20
    assert Variable.objects.filter(name='foo')[0].value == 'bar'
21 21
    assert resp.location == '/theme/'
22
    assert "The theme has been changed" in dict(resp.headers)['Set-Cookie']
23 22
    resp = resp.follow()
23
    assert "The theme has been changed" in str(resp.html)
24 24
    assert resp.form['theme'].value == 'alfortville'
25 25

  
26 26
    resp.form['theme'].value = 'publik'
27
-