From e9be841fc3371ae1699df4017d5179e15655de9d Mon Sep 17 00:00:00 2001 From: Agate Berriot Date: Thu, 28 Jul 2022 10:10:49 +0200 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(-) diff --git a/hobo/environment/models.py b/hobo/environment/models.py index 0b1ed9b..18e71be 100644 --- a/hobo/environment/models.py +++ b/hobo/environment/models.py @@ -97,6 +97,25 @@ class Variable(models.Model): self._parse_value_as_json() +def is_resolvable(url): + try: + netloc = urllib.parse.urlparse(url).netloc + if netloc and socket.gethostbyname(netloc): + return True + except socket.gaierror: + return False + + +def has_valid_certificate(url): + try: + requests.get(url, timeout=5, verify=True, allow_redirects=False) + return True + except requests.exceptions.SSLError: + return False + except requests.exceptions.ConnectionError: + return False + + class ServiceBase(models.Model): class Meta: abstract = True @@ -224,23 +243,10 @@ class ServiceBase(models.Model): return self.get_base_url_path() + '__provision__/' def is_resolvable(self): - try: - netloc = urllib.parse.urlparse(self.base_url).netloc - if netloc and socket.gethostbyname(netloc): - return True - except socket.gaierror: - return False + return is_resolvable(self.base_url) def has_valid_certificate(self): - if not self.is_resolvable(): - return False - try: - requests.get(self.base_url, timeout=5, verify=True, allow_redirects=False) - return True - except requests.exceptions.SSLError: - return False - except requests.exceptions.ConnectionError: - return False + return has_valid_certificate(self.base_url) def is_running(self): if not self.is_resolvable(): diff --git a/hobo/environment/validators.py b/hobo/environment/validators.py index 047e186..fa20c8c 100644 --- a/hobo/environment/validators.py +++ b/hobo/environment/validators.py @@ -3,18 +3,17 @@ import urllib.parse from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ -from hobo.environment.models import ServiceBase +from hobo.environment import models def validate_service_url(url): - service = ServiceBase(title='dummy', base_url=url) - if not service.is_resolvable(): + if not models.is_resolvable(url): raise ValidationError( _('Error: %(netloc)s is not resolvable in URL %(url)s'), code='not-resolvable', params={'netloc': urllib.parse.urlsplit(url).netloc, 'url': url}, ) - if not service.has_valid_certificate(): + if not models.has_valid_certificate(url): raise ValidationError( _('Error: no valid certificate for %(url)s'), code='invalid-certificate', params={'url': url} ) diff --git a/tests/test_cook.py b/tests/test_cook.py index 496bd32..8af9c63 100644 --- a/tests/test_cook.py +++ b/tests/test_cook.py @@ -8,6 +8,7 @@ from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.core.management.base import CommandError +from hobo.environment import models as environment_models from hobo.environment.management.commands.cook import Command from hobo.environment.models import ( Authentic, @@ -32,8 +33,8 @@ def test_check_action(monkeypatch): command.server_action = 'mock a server_action handler (ex: hobo-create)' action, action_args = 'server-action', {'url': 'https://test.org/'} - monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True) - monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True) + monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True) + monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: True) command.check_action(action, action_args) assert True @@ -44,8 +45,8 @@ def test_check_action_unknown_action(monkeypatch): command.server_action = 'mock a server_action handler (ex: hobo-create)' action, action_args = 'not-a-server-action', {'url': 'https://test.org/'} - monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True) - monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True) + monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True) + monkeypatch.setattr(environment_models, '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) @@ -57,8 +58,8 @@ def test_check_action_unresolvable(monkeypatch): command.server_action = 'mock a server_action handler (ex: hobo-create)' action, action_args = 'server-action', {'url': 'https://test.org/'} - monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: False) - monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True) + monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: False) + monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: True) with pytest.raises(CommandError) as e_info: command.check_action(action, action_args) assert 'test.org is not resolvable in URL https://test.org/' in str(e_info.value) @@ -70,8 +71,8 @@ def test_check_action_invalid_certificat(monkeypatch): command.server_action = 'mock a server_action handler (ex: hobo-create)' action, action_args = 'server-action', {'url': 'https://test.org/'} - monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True) - monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: False) + monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True) + monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: False) with pytest.raises(CommandError) as e_info: command.check_action(action, action_args) assert 'no valid certificate for https://test.org/' in str(e_info.value) diff --git a/tests/test_environment.py b/tests/test_environment.py index caeb6b8..b28d190 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -8,6 +8,7 @@ from django.utils import timezone from test_manager import login from webtest import Upload +from hobo.environment import models as environment_models from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle, ServiceBase, Variable from hobo.environment.utils import get_installed_services_dict from hobo.profile.models import AttributeDefinition @@ -143,13 +144,13 @@ def test_service_creation_url_validation(app, admin_user, monkeypatch): response = form.submit() assert 'not resolvable' in response - monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True) + monkeypatch.setattr(environment_models, 'is_resolvable', lambda x: True) form = response.form response = form.submit() assert 'no valid certificate' in response assert not Combo.objects.exists() - monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True) + monkeypatch.setattr(environment_models, 'has_valid_certificate', lambda x: True) form = response.form response = form.submit() assert Combo.objects.exists() diff --git a/tests/test_theme.py b/tests/test_theme.py index 23f9b5a..31de80f 100644 --- a/tests/test_theme.py +++ b/tests/test_theme.py @@ -19,8 +19,8 @@ def test_theme_view(mocked_random, app, admin_user, fake_themes): assert Variable.objects.filter(name='theme')[0].value == 'alfortville' assert Variable.objects.filter(name='foo')[0].value == 'bar' assert resp.location == '/theme/' - assert "The theme has been changed" in dict(resp.headers)['Set-Cookie'] resp = resp.follow() + assert "The theme has been changed" in str(resp.html) assert resp.form['theme'].value == 'alfortville' resp.form['theme'].value = 'publik' -- 2.37.2