0002-Fix-hobo-tests-under-django-3.2.patch
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.core.management.base import CommandError |
9 | 9 |
from mock import Mock, call, mock_open, patch |
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', {u'url': u'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', {u'url': u'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', {u'url': u'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', {u'url': u'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 | ||
---|---|---|
9 | 9 |
from test_manager import login |
10 | 10 |
from webtest import Upload |
11 | 11 | |
12 |
from hobo.environment import models as environment_models |
|
12 | 13 |
from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle, ServiceBase, Variable |
13 | 14 |
from hobo.environment.utils import get_installed_services_dict |
14 | 15 |
from hobo.profile.models import AttributeDefinition |
... | ... | |
144 | 145 |
response = form.submit() |
145 | 146 |
assert 'not resolvable' in response |
146 | 147 | |
147 |
monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
|
|
148 |
monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True)
|
|
148 | 149 |
form = response.form |
149 | 150 |
response = form.submit() |
150 | 151 |
assert 'no valid certificate' in response |
151 | 152 | |
152 | 153 |
assert not Combo.objects.exists() |
153 |
monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
|
|
154 |
monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: True)
|
|
154 | 155 |
form = response.form |
155 | 156 |
response = form.submit() |
156 | 157 |
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 |
- |