From a107b7ef0e847882dcd6d5fc8c39e8feccc5705e Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 29 Sep 2022 15:23:21 +0200 Subject: [PATCH] misc: apply pyupgrade (#69708) --- debian/debian_config_common.py | 2 +- .../management/commands/hobo_deploy.py | 2 +- .../management/commands/hobo_provision.py | 6 +- hobo/agent/authentic2/provisionning.py | 24 +- hobo/agent/authentic2/role_forms.py | 10 +- .../management/commands/import_template.py | 2 +- .../common/management/commands/hobo_deploy.py | 4 +- hobo/agent/common/migrations/0001_initial.py | 3 - .../migrations/0002_auto_20160105_1702.py | 3 - .../migrations/0003_auto_20200707_1656.py | 2 - hobo/agent/common/models.py | 4 +- .../hobo/management/commands/hobo_deploy.py | 2 +- hobo/agent/worker/celery.py | 1 - hobo/agent/worker/services.py | 3 +- hobo/applications/migrations/0001_initial.py | 2 - hobo/context_processors.py | 2 +- hobo/debug/views.py | 6 +- hobo/emails/forms.py | 2 +- hobo/emails/validators.py | 2 +- hobo/environment/forms.py | 16 +- .../management/commands/check_operational.py | 2 - hobo/environment/management/commands/cook.py | 1 - hobo/environment/migrations/0001_initial.py | 3 - .../0002_authentic_use_as_idp_for_self.py | 3 - .../migrations/0003_auto_20150309_0811.py | 3 - hobo/environment/migrations/0004_fargo.py | 3 - .../migrations/0005_variable_label.py | 3 - .../migrations/0006_auto_20150708_0830.py | 3 - .../migrations/0007_auto_20151008_1406.py | 3 - .../migrations/0008_auto_20151021_1414.py | 3 - hobo/environment/migrations/0009_mandayejs.py | 3 - .../migrations/0010_variable_auto.py | 3 - hobo/environment/migrations/0011_chrono.py | 3 - .../migrations/0012_mandayejs_site_app.py | 3 - .../migrations/0013_auto_20160226_1633.py | 3 - hobo/environment/migrations/0014_piwik.py | 3 - hobo/environment/migrations/0015_corbo.py | 3 - hobo/environment/migrations/0016_bijoe.py | 3 - hobo/environment/migrations/0017_hobo.py | 3 - .../migrations/0018_auto_20161102_1929.py | 3 - .../migrations/0019_delete_piwik.py | 2 - .../migrations/0020_delete_corbo_mandaye.py | 2 - hobo/environment/models.py | 8 +- hobo/environment/views.py | 24 +- hobo/forms.py | 4 +- hobo/journal.py | 5 +- hobo/logger.py | 19 +- hobo/matomo/utils.py | 2 +- hobo/matomo/views.py | 14 +- hobo/middleware/debug.py | 2 +- hobo/middleware/stats.py | 2 +- hobo/multitenant/cache.py | 2 +- hobo/multitenant/haystack.py | 7 +- hobo/multitenant/log.py | 2 +- .../management/commands/__init__.py | 10 +- .../management/commands/create_hobo_tenant.py | 2 +- .../management/commands/create_schemas.py | 2 - .../management/commands/list_tenants.py | 2 +- .../management/commands/migrate.py | 2 +- .../management/commands/migrate_schemas.py | 12 +- .../management/commands/runserver.py | 2 +- hobo/multitenant/management/commands/shell.py | 2 +- .../commands/showmigrations_schemas.py | 6 +- .../multitenant/management/commands/syncdb.py | 4 +- .../management/commands/tenant_command.py | 3 +- hobo/multitenant/mellon.py | 2 - hobo/multitenant/models.py | 2 +- hobo/multitenant/settings.py | 2 +- hobo/multitenant/settings_loaders.py | 8 +- hobo/multitenant/template_loader.py | 10 +- hobo/profile/migrations/0001_initial.py | 3 - hobo/profile/migrations/0002_add_data.py | 25 +- .../0003_attributedefinition_searchable.py | 2 - .../migrations/0004_auto_20200518_1810.py | 2 - hobo/profile/models.py | 4 +- hobo/rest_authentication.py | 2 +- hobo/seo/views.py | 10 +- hobo/theme/views.py | 4 +- hobo/urls_utils.py | 4 +- hobo/views.py | 2 +- setup.py | 3 +- tests/test_cook.py | 24 +- tests/test_emails.py | 2 +- tests/test_environment.py | 5 +- tests/test_health_api.py | 2 +- tests/test_hobo_deploy.py | 24 +- tests/test_home_views.py | 3 +- tests/test_import_template.py | 3 +- tests/test_maintenance.py | 3 +- tests/test_matomo_utils.py | 10 +- tests/test_matomo_views.py | 4 +- tests/test_settings_loaders.py | 64 ++-- tests/test_signature.py | 4 +- tests/test_theme.py | 4 +- tests/test_version.py | 2 +- tests_authentic/settings.py | 3 +- tests_authentic/test_hobo_deploy.py | 37 ++- tests_authentic/test_provisionning.py | 198 +++++++----- tests_multipublik/settings.py | 3 +- tests_multitenant/settings.py | 3 +- tests_multitenant/test_agent_worker.py | 69 +++-- tests_multitenant/test_create_tenant.py | 3 +- tests_multitenant/test_hobo_notify.py | 284 +++++++++--------- tests_multitenant/test_middleware.py | 2 - tests_multitenant/test_settings.py | 31 +- tests_multitenant/test_tenant_command.py | 25 +- tests_multitenant/test_uwsgidecorators.py | 2 +- tests_multitenant/utilities.py | 2 +- tests_passerelle/settings.py | 3 +- tests_schemas/test_cook.py | 2 +- tests_schemas/test_hobo_deploy.py | 2 +- tests_schemas/test_import_template.py | 2 +- tests_schemas/test_rename_hobo_service.py | 3 +- tests_schemas/test_rename_service.py | 3 +- 114 files changed, 573 insertions(+), 640 deletions(-) diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py index 40bba1a..31b8ee8 100644 --- a/debian/debian_config_common.py +++ b/debian/debian_config_common.py @@ -391,7 +391,7 @@ A2_MANAGER_ROLE_FORM_CLASS = 'hobo.agent.authentic2.role_forms.RoleForm' # Locale and timezone LANGUAGE_CODE = 'fr-fr' TIME_ZONE = 'Europe/Paris' -LANGUAGES = (('fr', u'Fran\xe7ais'),) +LANGUAGES = (('fr', 'Fran\xe7ais'),) USE_L10N = True USE_TZ = True diff --git a/hobo/agent/authentic2/management/commands/hobo_deploy.py b/hobo/agent/authentic2/management/commands/hobo_deploy.py index c0e7f9d..7d9d17f 100644 --- a/hobo/agent/authentic2/management/commands/hobo_deploy.py +++ b/hobo/agent/authentic2/management/commands/hobo_deploy.py @@ -31,7 +31,7 @@ class Command(hobo_deploy.Command): def __init__(self, *args, **kwargs): self.logger = logging.getLogger(__name__) - super(Command, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) def deploy_specifics(self, hobo_environment, tenant): # generate SAML keys diff --git a/hobo/agent/authentic2/management/commands/hobo_provision.py b/hobo/agent/authentic2/management/commands/hobo_provision.py index 2d490ce..60269b8 100644 --- a/hobo/agent/authentic2/management/commands/hobo_provision.py +++ b/hobo/agent/authentic2/management/commands/hobo_provision.py @@ -38,7 +38,7 @@ class Command(BaseCommand): def provision_roles(self, engine, ous): roles = get_role_model().objects.all() if self.verbosity > 0: - self.stdout.write('Provisionning {} roles.'.format(roles.count())) + self.stdout.write(f'Provisionning {roles.count()} roles.') engine.notify_roles(ous, roles, full=True) def provision_users(self, engine, ous, batch_size=512, batch_sleep=30, verbosity=1): @@ -66,11 +66,11 @@ class Command(BaseCommand): normal_users = qs.exclude(roles__in=roles_with_attributes) if self.verbosity > 0: - self.stdout.write('Provisionning {} normal users.'.format(normal_users.count())) + self.stdout.write(f'Provisionning {normal_users.count()} normal users.') do_provision(normal_users) # then those with an admin attribute, use distinct to prevent # duplicates caused by join on a m2m relation admin_users = qs.filter(roles__in=roles_with_attributes).distinct() if self.verbosity > 0: - self.stdout.write('Provisionning {} admin users.'.format(admin_users.count())) + self.stdout.write(f'Provisionning {admin_users.count()} admin users.') do_provision(admin_users) diff --git a/hobo/agent/authentic2/provisionning.py b/hobo/agent/authentic2/provisionning.py index c7c404a..87128c2 100644 --- a/hobo/agent/authentic2/provisionning.py +++ b/hobo/agent/authentic2/provisionning.py @@ -202,7 +202,7 @@ class Provisionning(threading.local): ) all_roles = Role.objects.all().prefetch_related('attributes') - roles = dict((r.id, r) for r in all_roles) + roles = {r.id: r for r in all_roles} user_roles = {} parents = {} for rp in RoleParenting.objects.filter(deleted__isnull=True): @@ -249,9 +249,9 @@ class Provisionning(threading.local): if not audience: continue logger.info( - u'provisionning users %s to %s', - u', '.join(map(force_text, users)), - u', '.join(audience), + 'provisionning users %s to %s', + ', '.join(map(force_text, users)), + ', '.join(audience), ) self.notify_agents( { @@ -269,7 +269,7 @@ class Provisionning(threading.local): elif users: audience = [audience for ou in ous.keys() for s, audience in self.get_audience(ou)] logger.info( - u'deprovisionning users %s from %s', u', '.join(map(force_text, users)), u', '.join(audience) + 'deprovisionning users %s from %s', ', '.join(map(force_text, users)), ', '.join(audience) ) self.notify_agents( { @@ -298,7 +298,7 @@ class Provisionning(threading.local): tuple(allowed_technical_roles_prefixes) ) - roles = set([role for role in roles if not is_forbidden_technical_role(role)]) + roles = {role for role in roles if not is_forbidden_technical_role(role)} if mode == 'provision': self.complete_roles(roles) @@ -333,7 +333,7 @@ class Provisionning(threading.local): ] audience = [entity_id for service, entity_id in self.get_audience(ou)] - logger.info(u'%sning roles %s to %s', mode, roles, audience) + logger.info('%sning roles %s to %s', mode, roles, audience) self.notify_agents( { '@type': mode, @@ -381,7 +381,7 @@ class Provisionning(threading.local): self.notify_users(ous, deleted.get(User, []), mode='deprovision') except Exception: # last step, clear everything - logger.exception(u'error in provisionning thread') + logger.exception('error in provisionning thread') finally: self.threads.discard(threading.current_thread()) @@ -408,7 +408,7 @@ class Provisionning(threading.local): for role in roles: role.emails = [] role.emails_to_members = True - role.details = u'' + role.details = '' for attribute in role.attributes.all(): if attribute.name in ('emails', 'emails_to_members', 'details') and attribute.kind == 'json': setattr(role, attribute.name, json.loads(attribute.value)) @@ -428,7 +428,7 @@ class Provisionning(threading.local): if not isinstance(instance, (User, Role, RoleAttribute, AttributeValue)): return # ignore last_login update on login - if isinstance(instance, User) and (update_fields and set(update_fields) == set(['last_login'])): + if isinstance(instance, User) and (update_fields and set(update_fields) == {'last_login'}): return if isinstance(instance, RoleAttribute): instance = instance.role @@ -506,7 +506,7 @@ class Provisionning(threading.local): f.write('%s %s ' % (datetime.datetime.now().isoformat(), connection.tenant.domain_url)) json.dump(data, f, indent=2) f.write('\n') - except IOError: + except OSError: pass if getattr(settings, 'HOBO_HTTP_PROVISIONNING', True): @@ -543,7 +543,7 @@ class Provisionning(threading.local): response = requests.put(sign_url(url, service['secret']), json=data) response.raise_for_status() except requests.RequestException as e: - logger.error(u'error provisionning to %s (%s)', audience, e) + logger.error('error provisionning to %s (%s)', audience, e) else: leftover_audience.remove(audience) return leftover_audience diff --git a/hobo/agent/authentic2/role_forms.py b/hobo/agent/authentic2/role_forms.py index 4da4366..c37f66f 100644 --- a/hobo/agent/authentic2/role_forms.py +++ b/hobo/agent/authentic2/role_forms.py @@ -25,7 +25,7 @@ from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ -class ListValidator(object): +class ListValidator: def __init__(self, item_validator): self.item_validator = item_validator @@ -42,7 +42,7 @@ class CommaSeparatedInput(forms.TextInput): if not value: return '' if not isinstance(value, str): - return u', '.join(value) + return ', '.join(value) return value @@ -54,7 +54,7 @@ class CommaSeparatedCharField(forms.Field): self.max_length = max_length self.min_length = min_length item_validators = kwargs.pop('item_validators', []) - super(CommaSeparatedCharField, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) for item_validator in item_validators: self.validators.append(ListValidator(item_validator)) @@ -97,12 +97,12 @@ class RoleForm(RoleEditForm): if role_attribute.name in fields: continue initial[role_attribute.name] = json.loads(role_attribute.value) - super(RoleForm, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) def save(self, commit=True): fields = [x.name for x in Role._meta.get_fields()] assert commit - instance = super(RoleForm, self).save(commit=commit) + instance = super().save(commit=commit) for field in self.cleaned_data: if field in fields: continue diff --git a/hobo/agent/combo/management/commands/import_template.py b/hobo/agent/combo/management/commands/import_template.py index 92e6dba..33fbd41 100644 --- a/hobo/agent/combo/management/commands/import_template.py +++ b/hobo/agent/combo/management/commands/import_template.py @@ -21,7 +21,7 @@ from hobo.agent.common.management.commands import import_template class Command(import_template.Command): def handle(self, *args, **kwargs): try: - return super(Command, self).handle(*args, **kwargs) + return super().handle(*args, **kwargs) except import_template.UnknownTemplateError: # ignore errors if template name contains portal-user or portal-agent as # those names do not actually require an existing file to work. diff --git a/hobo/agent/common/management/commands/hobo_deploy.py b/hobo/agent/common/management/commands/hobo_deploy.py index 0dc97ea..8ff04fd 100644 --- a/hobo/agent/common/management/commands/hobo_deploy.py +++ b/hobo/agent/common/management/commands/hobo_deploy.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import json import os import subprocess @@ -51,7 +49,7 @@ class Command(BaseCommand): for tenant in TenantMiddleware.get_tenants(): try: hobo_environment = tenant.get_hobo_json() - except IOError: + except OSError: continue try: me = [x for x in hobo_environment.get('services') if x.get('this') is True][0] diff --git a/hobo/agent/common/migrations/0001_initial.py b/hobo/agent/common/migrations/0001_initial.py index 4c5dae5..a10a8dd 100644 --- a/hobo/agent/common/migrations/0001_initial.py +++ b/hobo/agent/common/migrations/0001_initial.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/agent/common/migrations/0002_auto_20160105_1702.py b/hobo/agent/common/migrations/0002_auto_20160105_1702.py index 074ed39..11b293d 100644 --- a/hobo/agent/common/migrations/0002_auto_20160105_1702.py +++ b/hobo/agent/common/migrations/0002_auto_20160105_1702.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/agent/common/migrations/0003_auto_20200707_1656.py b/hobo/agent/common/migrations/0003_auto_20200707_1656.py index 4174272..5911dcd 100644 --- a/hobo/agent/common/migrations/0003_auto_20200707_1656.py +++ b/hobo/agent/common/migrations/0003_auto_20200707_1656.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.18 on 2020-07-07 14:56 -from __future__ import unicode_literals import django.contrib.postgres.fields from django.db import migrations, models diff --git a/hobo/agent/common/models.py b/hobo/agent/common/models.py index c7c8568..132d036 100644 --- a/hobo/agent/common/models.py +++ b/hobo/agent/common/models.py @@ -5,8 +5,8 @@ from django.db import models class Role(Group): uuid = models.CharField(max_length=32, db_index=True) - description = models.TextField(default=u'') - details = models.TextField(default=u'') + description = models.TextField(default='') + details = models.TextField(default='') emails = ArrayField(models.CharField(max_length=128), default=list) emails_to_members = models.BooleanField(default=True) diff --git a/hobo/agent/hobo/management/commands/hobo_deploy.py b/hobo/agent/hobo/management/commands/hobo_deploy.py index fc3402e..25e8582 100644 --- a/hobo/agent/hobo/management/commands/hobo_deploy.py +++ b/hobo/agent/hobo/management/commands/hobo_deploy.py @@ -17,7 +17,7 @@ class Command(hobo_deploy.Command): def deploy_specifics(self, hobo_environment, tenant): me = [x for x in hobo_environment.get('services') if x.get('this')][0] if not me.get('secondary'): - super(Command, self).deploy_specifics(hobo_environment, tenant) + super().deploy_specifics(hobo_environment, tenant) with tenant_context(tenant): services = hobo_environment.get('services') diff --git a/hobo/agent/worker/celery.py b/hobo/agent/worker/celery.py index 23ded82..58acc53 100644 --- a/hobo/agent/worker/celery.py +++ b/hobo/agent/worker/celery.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from __future__ import absolute_import from celery import Celery from kombu.common import Broadcast diff --git a/hobo/agent/worker/services.py b/hobo/agent/worker/services.py index c4a1e0a..f29f67d 100644 --- a/hobo/agent/worker/services.py +++ b/hobo/agent/worker/services.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from __future__ import print_function import fnmatch import json @@ -30,7 +29,7 @@ from django.utils.encoding import force_bytes from . import settings -class BaseService(object): +class BaseService: tenants_dirs = None def __init__(self, base_url, title, secret_key, **kwargs): diff --git a/hobo/applications/migrations/0001_initial.py b/hobo/applications/migrations/0001_initial.py index b279a9a..fb24181 100644 --- a/hobo/applications/migrations/0001_initial.py +++ b/hobo/applications/migrations/0001_initial.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.29 on 2022-01-09 13:16 -from __future__ import unicode_literals import django.contrib.postgres.fields.jsonb import django.db.models.deletion diff --git a/hobo/context_processors.py b/hobo/context_processors.py index 288f952..71519be 100644 --- a/hobo/context_processors.py +++ b/hobo/context_processors.py @@ -39,7 +39,7 @@ def template_vars(request): return template_vars -class RemoteTemplate(object): +class RemoteTemplate: PAGE_CACHE_KEY = 'page-cache' def __init__(self, source): diff --git a/hobo/debug/views.py b/hobo/debug/views.py index df5a544..ff1d336 100644 --- a/hobo/debug/views.py +++ b/hobo/debug/views.py @@ -37,7 +37,7 @@ class HomeView(FormView): return get_setting_variable('INTERNAL_IPS.extend') def get_initial(self): - initial = super(HomeView, self).get_initial() + initial = super().get_initial() initial['debug_log'] = bool(self.debug_log_variable.json) initial['debug_ips'] = self.debug_ips_variable.json return initial @@ -47,7 +47,7 @@ class HomeView(FormView): return self.request.META.get('REMOTE_ADDR') or None def get_context_data(self, **kwargs): - ctx = super(HomeView, self).get_context_data(**kwargs) + ctx = super().get_context_data(**kwargs) ctx['current_ip_debug'] = self.current_ip in self.debug_ips_variable.json return ctx @@ -67,7 +67,7 @@ class HomeView(FormView): debug_ips = self.toggle_value(debug_ips, self.current_ip) self.debug_ips_variable.json = debug_ips self.debug_ips_variable.save() - return super(HomeView, self).form_valid(form) + return super().form_valid(form) home = HomeView.as_view() diff --git a/hobo/emails/forms.py b/hobo/emails/forms.py index 29be99d..4c838a4 100644 --- a/hobo/emails/forms.py +++ b/hobo/emails/forms.py @@ -50,5 +50,5 @@ class EmailsForm(forms.Form): ) def __init__(self, *args, **kwargs): - super(EmailsForm, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.fields['email_signature'].widget.attrs = {'rows': 4, 'cols': 80} diff --git a/hobo/emails/validators.py b/hobo/emails/validators.py index ae8bb2c..d2ec203 100644 --- a/hobo/emails/validators.py +++ b/hobo/emails/validators.py @@ -45,7 +45,7 @@ def validate_email_address(value): smtp.quit() finally: smtp.close() - except (socket.error, IOError, OSError) as e: + except OSError as e: raise ValidationError( _('Error while connecting to %(server)s: %(msg)s') % {'server': mx_server, 'msg': e} ) diff --git a/hobo/environment/forms.py b/hobo/environment/forms.py index 44b1582..31d21e0 100644 --- a/hobo/environment/forms.py +++ b/hobo/environment/forms.py @@ -37,7 +37,7 @@ EXCLUDED_FIELDS = ( class BaseForm(forms.ModelForm): def __init__(self, *args, **kwargs): choices = self.get_template_choices() - super(BaseForm, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) if len(choices) < 2 or self.instance.id: del self.fields['template_name'] else: @@ -84,7 +84,7 @@ class BaseForm(forms.ModelForm): choices = self.get_template_choices() if not self.instance.id and len(choices) == 1: self.instance.template_name = choices[0][0] - return super(BaseForm, self).save(commit=commit) + return super().save(commit=commit) class AuthenticForm(BaseForm): @@ -93,7 +93,7 @@ class AuthenticForm(BaseForm): exclude = EXCLUDED_FIELDS def __init__(self, *args, **kwargs): - super(AuthenticForm, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) if not self.instance.is_operational() and not self.initial.get('use_as_idp_for_self'): del self.fields['use_as_idp_for_self'] @@ -102,7 +102,7 @@ class AuthenticForm(BaseForm): if self.cleaned_data.get('use_as_idp_for_self'): # this idp was just marked as the idp to use, unmark all others Authentic.objects.update(use_as_idp_for_self=False) - return super(AuthenticForm, self).save(commit=commit) + return super().save(commit=commit) class WcsForm(BaseForm): @@ -166,21 +166,21 @@ class VariableForm(forms.ModelForm): def __init__(self, service=None, **kwargs): self.service = service - super(VariableForm, self).__init__(**kwargs) + super().__init__(**kwargs) def save(self, commit=True): if self.service: self.instance.service = self.service - return super(VariableForm, self).save(commit=commit) + return super().save(commit=commit) -class VariablesFormMixin(object): +class VariablesFormMixin: form_class = None success_message = None variables = [] def get_context_data(self, **kwargs): - context = super(VariablesFormMixin, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) if self.request.POST: form_data = self.request.POST else: diff --git a/hobo/environment/management/commands/check_operational.py b/hobo/environment/management/commands/check_operational.py index 0099f63..113db7c 100644 --- a/hobo/environment/management/commands/check_operational.py +++ b/hobo/environment/management/commands/check_operational.py @@ -1,5 +1,3 @@ -from __future__ import print_function - from optparse import make_option from django.core.management.base import BaseCommand diff --git a/hobo/environment/management/commands/cook.py b/hobo/environment/management/commands/cook.py index b5235a1..5959339 100644 --- a/hobo/environment/management/commands/cook.py +++ b/hobo/environment/management/commands/cook.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from __future__ import print_function import json import os diff --git a/hobo/environment/migrations/0001_initial.py b/hobo/environment/migrations/0001_initial.py index 0f4d8fa..8e57679 100644 --- a/hobo/environment/migrations/0001_initial.py +++ b/hobo/environment/migrations/0001_initial.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0002_authentic_use_as_idp_for_self.py b/hobo/environment/migrations/0002_authentic_use_as_idp_for_self.py index b84bd80..1c9eb51 100644 --- a/hobo/environment/migrations/0002_authentic_use_as_idp_for_self.py +++ b/hobo/environment/migrations/0002_authentic_use_as_idp_for_self.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0003_auto_20150309_0811.py b/hobo/environment/migrations/0003_auto_20150309_0811.py index f311570..7492424 100644 --- a/hobo/environment/migrations/0003_auto_20150309_0811.py +++ b/hobo/environment/migrations/0003_auto_20150309_0811.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0004_fargo.py b/hobo/environment/migrations/0004_fargo.py index 1b53ec7..34c57c7 100644 --- a/hobo/environment/migrations/0004_fargo.py +++ b/hobo/environment/migrations/0004_fargo.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0005_variable_label.py b/hobo/environment/migrations/0005_variable_label.py index 754369c..c58294c 100644 --- a/hobo/environment/migrations/0005_variable_label.py +++ b/hobo/environment/migrations/0005_variable_label.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0006_auto_20150708_0830.py b/hobo/environment/migrations/0006_auto_20150708_0830.py index 84a40ca..14cc203 100644 --- a/hobo/environment/migrations/0006_auto_20150708_0830.py +++ b/hobo/environment/migrations/0006_auto_20150708_0830.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0007_auto_20151008_1406.py b/hobo/environment/migrations/0007_auto_20151008_1406.py index 858a913..7a25033 100644 --- a/hobo/environment/migrations/0007_auto_20151008_1406.py +++ b/hobo/environment/migrations/0007_auto_20151008_1406.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0008_auto_20151021_1414.py b/hobo/environment/migrations/0008_auto_20151021_1414.py index 8d38b28..97bd71b 100644 --- a/hobo/environment/migrations/0008_auto_20151021_1414.py +++ b/hobo/environment/migrations/0008_auto_20151021_1414.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0009_mandayejs.py b/hobo/environment/migrations/0009_mandayejs.py index 53d0600..87de17f 100644 --- a/hobo/environment/migrations/0009_mandayejs.py +++ b/hobo/environment/migrations/0009_mandayejs.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0010_variable_auto.py b/hobo/environment/migrations/0010_variable_auto.py index 7a10fba..cc63e6d 100644 --- a/hobo/environment/migrations/0010_variable_auto.py +++ b/hobo/environment/migrations/0010_variable_auto.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0011_chrono.py b/hobo/environment/migrations/0011_chrono.py index 28c42f3..49139fd 100644 --- a/hobo/environment/migrations/0011_chrono.py +++ b/hobo/environment/migrations/0011_chrono.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0012_mandayejs_site_app.py b/hobo/environment/migrations/0012_mandayejs_site_app.py index 598e256..5426f6e 100644 --- a/hobo/environment/migrations/0012_mandayejs_site_app.py +++ b/hobo/environment/migrations/0012_mandayejs_site_app.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0013_auto_20160226_1633.py b/hobo/environment/migrations/0013_auto_20160226_1633.py index 73fdab9..d71154a 100644 --- a/hobo/environment/migrations/0013_auto_20160226_1633.py +++ b/hobo/environment/migrations/0013_auto_20160226_1633.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0014_piwik.py b/hobo/environment/migrations/0014_piwik.py index 6ef923e..314bab7 100644 --- a/hobo/environment/migrations/0014_piwik.py +++ b/hobo/environment/migrations/0014_piwik.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0015_corbo.py b/hobo/environment/migrations/0015_corbo.py index b9b8579..2c04ea4 100644 --- a/hobo/environment/migrations/0015_corbo.py +++ b/hobo/environment/migrations/0015_corbo.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0016_bijoe.py b/hobo/environment/migrations/0016_bijoe.py index 04fa68b..d40879a 100644 --- a/hobo/environment/migrations/0016_bijoe.py +++ b/hobo/environment/migrations/0016_bijoe.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0017_hobo.py b/hobo/environment/migrations/0017_hobo.py index ddde847..4997240 100644 --- a/hobo/environment/migrations/0017_hobo.py +++ b/hobo/environment/migrations/0017_hobo.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0018_auto_20161102_1929.py b/hobo/environment/migrations/0018_auto_20161102_1929.py index a83be7c..3cda901 100644 --- a/hobo/environment/migrations/0018_auto_20161102_1929.py +++ b/hobo/environment/migrations/0018_auto_20161102_1929.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/hobo/environment/migrations/0019_delete_piwik.py b/hobo/environment/migrations/0019_delete_piwik.py index 206b1b2..77e47a3 100644 --- a/hobo/environment/migrations/0019_delete_piwik.py +++ b/hobo/environment/migrations/0019_delete_piwik.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.12 on 2019-02-28 10:28 -from __future__ import unicode_literals from django.db import migrations diff --git a/hobo/environment/migrations/0020_delete_corbo_mandaye.py b/hobo/environment/migrations/0020_delete_corbo_mandaye.py index 99801d9..2616555 100644 --- a/hobo/environment/migrations/0020_delete_corbo_mandaye.py +++ b/hobo/environment/migrations/0020_delete_corbo_mandaye.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.29 on 2021-02-28 09:57 -from __future__ import unicode_literals import django.core.validators from django.db import migrations, models diff --git a/hobo/environment/models.py b/hobo/environment/models.py index 2662db0..0b1ed9b 100644 --- a/hobo/environment/models.py +++ b/hobo/environment/models.py @@ -152,13 +152,13 @@ class ServiceBase(models.Model): return (self.last_operational_check_timestamp - self.last_update_timestamp) < two_minutes def as_dict(self): - as_dict = dict([(x, y) for (x, y) in self.__dict__.items() if isinstance(y, (int, str))]) + as_dict = {x: y for (x, y) in self.__dict__.items() if isinstance(y, (int, str))} as_dict['base_url'] = self.get_base_url_path() if self.legacy_urls: as_dict['legacy_urls'] = self.legacy_urls as_dict['service-id'] = self.Extra.service_id as_dict['service-label'] = force_text(self.Extra.service_label) - as_dict['variables'] = dict(((v.name, v.json) for v in self.variables.all())) + as_dict['variables'] = {v.name: v.json for v in self.variables.all()} as_dict['secondary'] = self.secondary if self.get_saml_sp_metadata_url(): as_dict['saml-sp-metadata-url'] = self.get_saml_sp_metadata_url() @@ -182,7 +182,7 @@ class ServiceBase(models.Model): raise ValidationError(_('This slug is already used. It must be unique.')) if service.title == self.title and service.secondary is False and self.secondary is False: raise ValidationError(_('This title is already used. It must be unique.')) - return super(ServiceBase, self).clean(*args, **kwargs) + return super().clean(*args, **kwargs) def save(self, *args, **kwargs): self.base_url = self.base_url.strip().lower() @@ -192,7 +192,7 @@ class ServiceBase(models.Model): self.secret_key = get_random_string(50, SECRET_CHARS) is_new = self.id is None - super(ServiceBase, self).save(*args, **kwargs) + super().save(*args, **kwargs) if is_new and settings.SERVICE_EXTRA_VARIABLES: for variable in settings.SERVICE_EXTRA_VARIABLES.get(self.Extra.service_id, []): diff --git a/hobo/environment/views.py b/hobo/environment/views.py index 78be0bc..713c6b2 100644 --- a/hobo/environment/views.py +++ b/hobo/environment/views.py @@ -33,7 +33,7 @@ from . import forms, utils from .models import AVAILABLE_SERVICES, Variable -class AvailableService(object): +class AvailableService: def __init__(self, klass): self.id = klass.Extra.service_id self.label = klass._meta.verbose_name @@ -43,7 +43,7 @@ class HomeView(TemplateView): template_name = 'environment/home.html' def get_context_data(self, **kwargs): - context = super(HomeView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) context['available_services'] = [AvailableService(x) for x in AVAILABLE_SERVICES if x.is_enabled()] installed_services = [x for x in utils.get_installed_services() if not x.secondary] for service in installed_services: @@ -62,7 +62,7 @@ class VariablesView(TemplateView): template_name = 'environment/variables.html' def get_context_data(self, **kwargs): - context = super(VariablesView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) context['variables'] = Variable.objects.filter(auto=False, service_pk__isnull=True).order_by('label') return context @@ -72,7 +72,7 @@ class VariableCreateView(CreateView): form_class = forms.VariableForm def get_form_kwargs(self): - kwargs = super(VariableCreateView, self).get_form_kwargs() + kwargs = super().get_form_kwargs() if 'service' in self.kwargs: service_id = self.kwargs.pop('service') service_slug = self.kwargs.pop('slug') @@ -130,12 +130,12 @@ class ServiceCreateView(CreateView): success_url = reverse_lazy('environment-home') def get_context_data(self, **kwargs): - context = super(ServiceCreateView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) context['model_name'] = self.model._meta.verbose_name return context def get_initial(self): - initial = super(ServiceCreateView, self).get_initial() + initial = super().get_initial() initial['base_url'] = utils.create_base_url( self.request.build_absolute_uri(), self.model.Extra.service_default_slug ) @@ -147,11 +147,11 @@ class ServiceCreateView(CreateView): def get(self, request, *args, **kwargs): self.service_id = kwargs.pop('service') - return super(ServiceCreateView, self).get(request, *args, **kwargs) + return super().get(request, *args, **kwargs) def post(self, request, *args, **kwargs): self.service_id = kwargs.pop('service') - return super(ServiceCreateView, self).post(request, *args, **kwargs) + return super().post(request, *args, **kwargs) def get_form_class(self): for service in AVAILABLE_SERVICES: @@ -166,7 +166,7 @@ class ServiceUpdateView(UpdateView): success_url = reverse_lazy('environment-home') def get_context_data(self, **kwargs): - context = super(ServiceUpdateView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) context['model_name'] = self.model._meta.verbose_name return context @@ -176,12 +176,12 @@ class ServiceUpdateView(UpdateView): def get(self, request, *args, **kwargs): self.service_id = kwargs.pop('service') self.get_form_class() - return super(ServiceUpdateView, self).get(request, *args, **kwargs) + return super().get(request, *args, **kwargs) def post(self, request, *args, **kwargs): self.service_id = kwargs.pop('service') self.get_form_class() - return super(ServiceUpdateView, self).post(request, *args, **kwargs) + return super().post(request, *args, **kwargs) def get_form_class(self): for service in AVAILABLE_SERVICES: @@ -219,7 +219,7 @@ class ImportView(FormView): return self.form_invalid(form) utils.import_parameters(parameters_json) - return super(ImportView, self).form_valid(form) + return super().form_valid(form) class ExportView(View): diff --git a/hobo/forms.py b/hobo/forms.py index 2981898..3f39ad6 100644 --- a/hobo/forms.py +++ b/hobo/forms.py @@ -12,13 +12,13 @@ class HoboForm(ModelForm): required_css_class = 'required' def __init__(self, *args, **kwargs): - super(HoboForm, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.fields['schema_name'].required = False def clean(self): from django.template.defaultfilters import slugify - cleaned_data = super(HoboForm, self).clean() + cleaned_data = super().clean() if not cleaned_data.get('schema_name') and cleaned_data.get('domain_url'): cleaned_data['schema_name'] = slugify(cleaned_data['domain_url']) return cleaned_data diff --git a/hobo/journal.py b/hobo/journal.py index 84271fe..a411579 100644 --- a/hobo/journal.py +++ b/hobo/journal.py @@ -1,4 +1,3 @@ -# -*- Mode: python; coding:utf-8; indent-tabs-mode: nil -*- */ # # # Copyright 2012 David Strauss @@ -79,7 +78,7 @@ def send(MESSAGE, MESSAGE_ID=None, CODE_FILE=None, CODE_LINE=None, CODE_FUNC=Non if CODE_FILE is not None: args.append('CODE_FILE=' + CODE_FILE) if CODE_LINE is not None: - args.append('CODE_LINE={:d}'.format(CODE_LINE)) + args.append(f'CODE_LINE={CODE_LINE:d}') if CODE_FUNC is not None: args.append('CODE_FUNC=' + CODE_FUNC) @@ -140,7 +139,7 @@ class JournalHandler(_logging.Handler): """ def __init__(self, level=_logging.NOTSET, sender_function=send, **kwargs): - super(JournalHandler, self).__init__(level) + super().__init__(level) for name in kwargs: if not _valid_field_name(name): diff --git a/hobo/logger.py b/hobo/logger.py index 5bc8229..2c7924f 100644 --- a/hobo/logger.py +++ b/hobo/logger.py @@ -34,7 +34,7 @@ class SettingsLogLevel(str): warnings.warn( 'SettingsLogLevel is deprecated, use DEBUG_LOG instead.', DeprecationWarning, stacklevel=2 ) - return super(SettingsLogLevel, cls).__new__(value) + return super().__new__(value) class RequestContextFilter(logging.Filter): @@ -120,14 +120,14 @@ class ForceDebugFilter(logging.Filter): def filter(self, record): record.levelno = logging.DEBUG record.levelname = 'DEBUG' - return super(ForceDebugFilter, self).filter(record) + return super().filter(record) class LogRecord(logging.LogRecord): '''Subclass LogRecord to make multiline log parseable''' def getMessage(self): - return super(LogRecord, self).getMessage().replace('\n', '\n ') + return super().getMessage().replace('\n', '\n ') class TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler): @@ -135,12 +135,12 @@ class TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler): old_class = record.__class__ record.__class__ = LogRecord try: - return super(TimedRotatingFileHandler, self).format(record) + return super().format(record) finally: record.__class__ = old_class -class DebugLogFilter(object): +class DebugLogFilter: '''Filter debug log records based on the DEBUG_LOG setting''' def filter(self, record): @@ -163,7 +163,7 @@ class DebugLogFilter(object): return bool(debug_log) -class DebugLog(object): +class DebugLog: def __init__(self, path): self.path = path @@ -228,18 +228,17 @@ class DebugLog(object): return if not os.path.exists(debug_log_path): return - for record in cls(debug_log_path)._parse(cursor=cursor): - yield record + yield from cls(debug_log_path)._parse(cursor=cursor) class ClampLogLevel(logging.Filter): def __init__(self, level): self.levelname = level.upper() self.levelno = getattr(logging, self.levelname) - super(ClampLogLevel, self).__init__() + super().__init__() def filter(self, record): if record.levelno > self.levelno: record.levelno = self.levelno record.levelname = self.levelname - return super(ClampLogLevel, self).filter(record) + return super().filter(record) diff --git a/hobo/matomo/utils.py b/hobo/matomo/utils.py index e305147..4d4acd5 100644 --- a/hobo/matomo/utils.py +++ b/hobo/matomo/utils.py @@ -109,7 +109,7 @@ class MatomoError(MatomoException): """expected Matomo error responses""" -class MatomoWS(object): +class MatomoWS: """api for matomo webservices""" def __init__(self): diff --git a/hobo/matomo/views.py b/hobo/matomo/views.py index a971daf..c2683a2 100644 --- a/hobo/matomo/views.py +++ b/hobo/matomo/views.py @@ -39,17 +39,17 @@ class HomeView(FormView): success_url = reverse_lazy('matomo-home') def get_initial(self): - initial = super(HomeView, self).get_initial() + initial = super().get_initial() initial['tracking_js'] = get_tracking_js() return initial def form_valid(self, form): tracking_js = form.cleaned_data['tracking_js'] put_tracking_js(tracking_js) - return super(HomeView, self).form_valid(form) + return super().form_valid(form) def get_context_data(self, **kwargs): - context = super(HomeView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) tracking_js = get_tracking_js() logme_url = get_variable_value('matomo_logme_url') context['logme_url'] = logme_url @@ -80,7 +80,7 @@ class EnableManualView(FormView): success_url = reverse_lazy('matomo-home') def get_initial(self): - initial = super(EnableManualView, self).get_initial() + initial = super().get_initial() initial['tracking_js'] = get_tracking_js() return initial @@ -89,7 +89,7 @@ class EnableManualView(FormView): put_tracking_js(tracking_js) logme_url = get_variable('matomo_logme_url') logme_url.delete() - return super(EnableManualView, self).form_valid(form) + return super().form_valid(form) enable_manual = EnableManualView.as_view() @@ -111,7 +111,7 @@ class EnableAutoView(FormView): matomo.create_fake_first_tracking_visit(id_site) except MatomoException as exc: messages.warning(self.request, 'ping: ' + str(exc)) - return super(EnableAutoView, self).form_valid(form) + return super().form_valid(form) enable_auto = EnableAutoView.as_view() @@ -126,7 +126,7 @@ class DisableView(FormView): put_tracking_js('') variable = get_variable('matomo_logme_url') variable.delete() - return super(DisableView, self).form_valid(form) + return super().form_valid(form) disable = DisableView.as_view() diff --git a/hobo/middleware/debug.py b/hobo/middleware/debug.py index d73e826..71d72a0 100644 --- a/hobo/middleware/debug.py +++ b/hobo/middleware/debug.py @@ -17,7 +17,7 @@ from django.conf import settings -class InternalIPMiddleware(object): +class InternalIPMiddleware: def __init__(self, get_response=None): self.get_response = get_response diff --git a/hobo/middleware/stats.py b/hobo/middleware/stats.py index 30acd52..931e3b0 100644 --- a/hobo/middleware/stats.py +++ b/hobo/middleware/stats.py @@ -97,7 +97,7 @@ class PrometheusStatsMiddleware(MiddlewareMixin): if connection.queries_logged: sql_queries_count = len(connection.queries) - sql_queries_time = sum([float(x['time']) for x in connection.queries]) + sql_queries_time = sum(float(x['time']) for x in connection.queries) requests_queries_count_by_host_view_status_method.labels( host_name, view_name, status_code, http_method ).observe(sql_queries_count) diff --git a/hobo/multitenant/cache.py b/hobo/multitenant/cache.py index 5a72ef2..cb607e5 100644 --- a/hobo/multitenant/cache.py +++ b/hobo/multitenant/cache.py @@ -4,7 +4,7 @@ from django.db import connection from django.utils.module_loading import import_string -class TenantBaseCache(object): +class TenantBaseCache: '''Prepend the tenant schema name to the cache prefix''' def set_key_prefix(self, prefix): diff --git a/hobo/multitenant/haystack.py b/hobo/multitenant/haystack.py index 5c03d80..fa08f22 100644 --- a/hobo/multitenant/haystack.py +++ b/hobo/multitenant/haystack.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from __future__ import absolute_import import os import shutil @@ -43,13 +42,13 @@ class WhooshSearchBackend(haystack.backends.whoosh_backend.WhooshSearchBackend): # when there are opened files. renamed_path = self.path + '.deleted-%s' % time.time() os.rename(self.path, renamed_path) - super(WhooshSearchBackend, self).delete_index() + super().delete_index() if renamed_path: # remove afterwards and ignore errors (residual directories will # have to be cleaned manually) try: shutil.rmtree(renamed_path) - except (IOError, OSError): + except OSError: pass @property @@ -62,7 +61,7 @@ class WhooshSearchBackend(haystack.backends.whoosh_backend.WhooshSearchBackend): pass def setup(self): - super(WhooshSearchBackend, self).setup() + super().setup() # make it be always reinitialized self.setup_complete = False diff --git a/hobo/multitenant/log.py b/hobo/multitenant/log.py index db23976..620963d 100644 --- a/hobo/multitenant/log.py +++ b/hobo/multitenant/log.py @@ -22,7 +22,7 @@ class AdminEmailHandler(django.utils.log.AdminEmailHandler): def format_subject(self, subject): from .models import Tenant - subject = super(AdminEmailHandler, self).format_subject(subject) + subject = super().format_subject(subject) try: subject = '[%s] %s' % (connection.get_tenant().domain_url, subject) except AttributeError: diff --git a/hobo/multitenant/management/commands/__init__.py b/hobo/multitenant/management/commands/__init__.py index e7ad746..be9cf14 100644 --- a/hobo/multitenant/management/commands/__init__.py +++ b/hobo/multitenant/management/commands/__init__.py @@ -26,7 +26,7 @@ class BaseTenantCommand(BaseCommand): """ Sets option_list and help dynamically. """ - obj = super(BaseTenantCommand, cls).__new__(cls, *args, **kwargs) + obj = super().__new__(cls, *args, **kwargs) app_name = get_commands()[obj.COMMAND_NAME] if isinstance(app_name, BaseCommand): @@ -48,7 +48,7 @@ class BaseTenantCommand(BaseCommand): return obj def add_arguments(self, parser): - super(BaseTenantCommand, self).add_arguments(parser) + super().add_arguments(parser) parser.add_argument("-d", "--domain", dest="domain") # use the privately held reference to the underlying command to invoke # the add_arguments path on this parser instance @@ -88,7 +88,7 @@ class BaseTenantCommand(BaseCommand): self.execute_command(tenant, self.COMMAND_NAME, *args, **options) -class InteractiveTenantOption(object): +class InteractiveTenantOption: def add_arguments(self, parser): parser.add_argument("-d", "--domain", dest="domain", help='specify tenant domain') @@ -135,12 +135,12 @@ class TenantWrappedCommand(InteractiveTenantOption, BaseCommand): """ def __new__(cls, *args, **kwargs): - obj = super(TenantWrappedCommand, cls).__new__(cls, *args, **kwargs) + obj = super().__new__(cls, *args, **kwargs) obj.command_instance = obj.COMMAND() return obj def add_arguments(self, parser): - super(TenantWrappedCommand, self).add_arguments(parser) + super().add_arguments(parser) self.command_instance.add_arguments(parser) def handle(self, *args, **options): diff --git a/hobo/multitenant/management/commands/create_hobo_tenant.py b/hobo/multitenant/management/commands/create_hobo_tenant.py index 74b8125..96abe7d 100644 --- a/hobo/multitenant/management/commands/create_hobo_tenant.py +++ b/hobo/multitenant/management/commands/create_hobo_tenant.py @@ -22,7 +22,7 @@ class Command(CreateTenantCommand): hostnames.remove('-') hostnames.extend([x.strip() for x in sys.stdin.readlines()]) - super(Command, self).handle(hostnames, **options) + super().handle(hostnames, **options) # create SP keys for each tenant for hostname in hostnames: diff --git a/hobo/multitenant/management/commands/create_schemas.py b/hobo/multitenant/management/commands/create_schemas.py index d3e0399..f09aaa5 100644 --- a/hobo/multitenant/management/commands/create_schemas.py +++ b/hobo/multitenant/management/commands/create_schemas.py @@ -1,5 +1,3 @@ -from __future__ import print_function - from django.core.management.base import BaseCommand from django.db import connection diff --git a/hobo/multitenant/management/commands/list_tenants.py b/hobo/multitenant/management/commands/list_tenants.py index 0854ffc..9b14a3c 100644 --- a/hobo/multitenant/management/commands/list_tenants.py +++ b/hobo/multitenant/management/commands/list_tenants.py @@ -11,4 +11,4 @@ class Command(BaseCommand): all_tenants = TenantMiddleware.get_tenants() for tenant in all_tenants: - print("{0} {1}".format(tenant.schema_name, tenant.domain_url)) + print(f"{tenant.schema_name} {tenant.domain_url}") diff --git a/hobo/multitenant/management/commands/migrate.py b/hobo/multitenant/management/commands/migrate.py index 98fb04e..22c020d 100644 --- a/hobo/multitenant/management/commands/migrate.py +++ b/hobo/multitenant/management/commands/migrate.py @@ -21,7 +21,7 @@ class Command(BaseCommand): "instead. Please read the documentation if you don't know why you " "shouldn't call migrate directly!".format(database) ) - super(Command, self).handle(*args, **options) + super().handle(*args, **options) if django_is_in_test_mode(): diff --git a/hobo/multitenant/management/commands/migrate_schemas.py b/hobo/multitenant/management/commands/migrate_schemas.py index 9c34f54..0c40b7f 100644 --- a/hobo/multitenant/management/commands/migrate_schemas.py +++ b/hobo/multitenant/management/commands/migrate_schemas.py @@ -33,18 +33,18 @@ class MigrateSchemasCommand(SyncCommon): requires_system_checks = [] def add_arguments(self, parser): - super(MigrateSchemasCommand, self).add_arguments(parser) + super().add_arguments(parser) command = MigrateCommand() command.add_arguments(parser) parser.set_defaults(verbosity=0) def handle(self, *args, **options): - super(MigrateSchemasCommand, self).handle(*args, **options) + super().handle(*args, **options) if self.domain: try: tenant = TenantMiddleware.get_tenant_by_hostname(self.domain) except TenantNotFound: - raise RuntimeError('Tenant "{}" does not exist'.format(self.domain)) + raise RuntimeError(f'Tenant "{self.domain}" does not exist') else: self.run_migrations(tenant, settings.TENANT_APPS) elif self.schema_name: @@ -56,9 +56,9 @@ class MigrateSchemasCommand(SyncCommon): app_labels.append(app.label) loader = MigrationLoader(None) loader.load_disk() - all_migrations = set( - [(app, migration) for app, migration in loader.disk_migrations if app in app_labels] - ) + all_migrations = { + (app, migration) for app, migration in loader.disk_migrations if app in app_labels + } for tenant in TenantMiddleware.get_tenants(): connection.set_tenant(tenant, include_public=False) applied_migrations = self.get_applied_migrations(app_labels) diff --git a/hobo/multitenant/management/commands/runserver.py b/hobo/multitenant/management/commands/runserver.py index 89d6439..c99f002 100644 --- a/hobo/multitenant/management/commands/runserver.py +++ b/hobo/multitenant/management/commands/runserver.py @@ -40,7 +40,7 @@ class Command(StaticRunserverCommand): handler. """ - handler = super(Command, self).get_handler(*args, **options) + handler = super().get_handler(*args, **options) use_static_handler = options.get('use_static_handler', True) insecure_serving = options.get('insecure_serving', False) if use_static_handler and (settings.DEBUG or insecure_serving): diff --git a/hobo/multitenant/management/commands/shell.py b/hobo/multitenant/management/commands/shell.py index cbc01f8..b4b242f 100644 --- a/hobo/multitenant/management/commands/shell.py +++ b/hobo/multitenant/management/commands/shell.py @@ -8,4 +8,4 @@ class Command(TenantWrappedCommand): def handle(self, *args, **kwargs): disable_global_logging() - super(Command, self).handle(*args, **kwargs) + super().handle(*args, **kwargs) diff --git a/hobo/multitenant/management/commands/showmigrations_schemas.py b/hobo/multitenant/management/commands/showmigrations_schemas.py index c61a255..5379d2b 100644 --- a/hobo/multitenant/management/commands/showmigrations_schemas.py +++ b/hobo/multitenant/management/commands/showmigrations_schemas.py @@ -26,17 +26,17 @@ class ShowMigrationsSchemasCommand(SyncCommon): help = "Show database schema migrations." def add_arguments(self, parser): - super(ShowMigrationsSchemasCommand, self).add_arguments(parser) + super().add_arguments(parser) command = ShowMigrationsCommand() command.add_arguments(parser) def handle(self, *args, **options): - super(ShowMigrationsSchemasCommand, self).handle(*args, **options) + super().handle(*args, **options) if self.domain: try: tenant = TenantMiddleware.get_tenant_by_hostname(self.domain) except TenantNotFound: - raise RuntimeError('Schema "{}" does not exist'.format(self.schema_name)) + raise RuntimeError(f'Schema "{self.schema_name}" does not exist') self.run_showmigrations(tenant, settings.TENANT_APPS) else: for tenant in TenantMiddleware.get_tenants(): diff --git a/hobo/multitenant/management/commands/syncdb.py b/hobo/multitenant/management/commands/syncdb.py index 8e23595..42b2f7d 100644 --- a/hobo/multitenant/management/commands/syncdb.py +++ b/hobo/multitenant/management/commands/syncdb.py @@ -21,9 +21,9 @@ class Command(syncdb.Command): and not django_is_in_test_mode() ): raise CommandError( - "syncdb has been disabled, for database '{0}'. " + "syncdb has been disabled, for database '{}'. " "Use sync_schemas instead. Please read the " "documentation if you don't know why " "you shouldn't call syncdb directly!".format(database) ) - super(Command, self).handle(*args, **options) + super().handle(*args, **options) diff --git a/hobo/multitenant/management/commands/tenant_command.py b/hobo/multitenant/management/commands/tenant_command.py index fb88265..ea59b62 100644 --- a/hobo/multitenant/management/commands/tenant_command.py +++ b/hobo/multitenant/management/commands/tenant_command.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # this file derive from django-tenant-schemas # Author: Bernardo Pires Carneiro # Email: carneiro.be@gmail.com @@ -141,7 +140,7 @@ class Command(InteractiveTenantOption, BaseCommand): print(prefix + msg) continue if args_verbosity.verbosity > 1: - print(u'* Running command %s on tenant %s' % (command, tenant.domain_url)) + print('* Running command %s on tenant %s' % (command, tenant.domain_url)) error = run_command_from_argv(klass, args) if error: errors.append(error) diff --git a/hobo/multitenant/mellon.py b/hobo/multitenant/mellon.py index c72333e..3fda36a 100644 --- a/hobo/multitenant/mellon.py +++ b/hobo/multitenant/mellon.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import - import json import logging import os diff --git a/hobo/multitenant/models.py b/hobo/multitenant/models.py index 29200d3..1b69afa 100644 --- a/hobo/multitenant/models.py +++ b/hobo/multitenant/models.py @@ -20,7 +20,7 @@ class Tenant(TenantMixin): pass def __unicode__(self): - return u'Tenant %s (%s)' % (self.domain_url, self.schema_name) + return 'Tenant %s (%s)' % (self.domain_url, self.schema_name) def __str__(self): return 'Tenant %s (%s)' % (self.domain_url, self.schema_name) diff --git a/hobo/multitenant/settings.py b/hobo/multitenant/settings.py index 98ca30c..07d49d2 100644 --- a/hobo/multitenant/settings.py +++ b/hobo/multitenant/settings.py @@ -27,7 +27,7 @@ def import_class(klass): return getattr(import_module(module), attr) -class TenantSettingsWrapper(object): +class TenantSettingsWrapper: local = threading.local() def __init__(self, default_settings): diff --git a/hobo/multitenant/settings_loaders.py b/hobo/multitenant/settings_loaders.py index 4b45b47..4a64c28 100644 --- a/hobo/multitenant/settings_loaders.py +++ b/hobo/multitenant/settings_loaders.py @@ -10,7 +10,7 @@ from django.utils.http import urlencode from hobo.theme.utils import get_theme -class FileBaseSettingsLoader(object): +class FileBaseSettingsLoader: """ Base middleware class for loading settings from FILENAME. Child classes MUST override update_settings_from_path(). @@ -36,7 +36,7 @@ class FileBaseSettingsLoader(object): raise NotImplemented -class SettingsDictUpdateMixin(object): +class SettingsDictUpdateMixin: def do_update(self, tenant_settings, key, value): old_value = getattr(tenant_settings, key, {}) new_value = old_value.copy() @@ -306,7 +306,7 @@ class SharedThemeSettings(FileBaseSettingsLoader): break -class CookieNames(object): +class CookieNames: def get_new_time(self, tenant): return 0 @@ -395,7 +395,7 @@ class Mellon(FileBaseSettingsLoader): tenant_settings.MELLON_PRIVATE_KEY = saml_key -class SiteBaseUrl(object): +class SiteBaseUrl: def get_new_time(self, tenant): tenant_dir = os.path.join(settings.TENANT_BASE, tenant.domain_url) for filename in ['unsecure', 'base_url']: diff --git a/hobo/multitenant/template_loader.py b/hobo/multitenant/template_loader.py index db6afaa..c55068b 100644 --- a/hobo/multitenant/template_loader.py +++ b/hobo/multitenant/template_loader.py @@ -19,7 +19,7 @@ from tenant_schemas.utils import get_public_schema_name class CachedLoader(DjangoCachedLoader): def cache_key(self, template_name, template_dirs, skip=None): - key = super(CachedLoader, self).cache_key(template_name, template_dirs, skip=skip) + key = super().cache_key(template_name, template_dirs, skip=skip) if connection.tenant: return connection.tenant.domain_url + '-' + key return key @@ -51,13 +51,13 @@ class FilesystemLoader(DjangoFilesystemLoader): known_dirnames = ( list( itertools.chain( - *[ + *( ( '%s/variants/%s/portal-agent' % (x, theme_value), '%s/variants/%s' % (x, theme_value), ) for x in known_dirnames - ] + ) ) ) + known_dirnames @@ -66,13 +66,13 @@ class FilesystemLoader(DjangoFilesystemLoader): known_dirnames = ( list( itertools.chain( - *[ + *( ( '%s/variants/%s/portal-user' % (x, theme_value), '%s/variants/%s' % (x, theme_value), ) for x in known_dirnames - ] + ) ) ) + known_dirnames diff --git a/hobo/profile/migrations/0001_initial.py b/hobo/profile/migrations/0001_initial.py index b5b46c6..eb14541 100644 --- a/hobo/profile/migrations/0001_initial.py +++ b/hobo/profile/migrations/0001_initial.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - import django.core.validators from django.db import migrations, models diff --git a/hobo/profile/migrations/0002_add_data.py b/hobo/profile/migrations/0002_add_data.py index cffc1d5..7a3d902 100644 --- a/hobo/profile/migrations/0002_add_data.py +++ b/hobo/profile/migrations/0002_add_data.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations @@ -8,17 +5,17 @@ def add_initial_data(apps, schema_editor): AttributeDefinition = apps.get_model('profile', 'AttributeDefinition') attributes = [ - {'label': u'Civilité', 'name': 'title', 'kind': 'title', 'disabled': True}, - {'label': u'Prénom', 'name': 'first_name', 'required': True, 'asked_on_registration': True}, - {'label': u'Nom', 'name': 'last_name', 'required': True, 'asked_on_registration': True}, - {'label': u'Adresse électronique', 'name': 'email', 'kind': 'email', 'required': True}, - {'label': u'Adresse', 'name': 'address'}, - {'label': u'Code postal', 'name': 'zipcode'}, - {'label': u'Commune', 'name': 'city'}, - {'label': u'Pays', 'name': 'country', 'disabled': True}, - {'label': u'Date de naissance', 'name': 'birthdate', 'kind': 'birthdate', 'disabled': True}, - {'label': u'Téléphone', 'name': 'phone', 'kind': 'phone_number'}, - {'label': u'Mobile', 'name': 'mobile', 'kind': 'phone_number'}, + {'label': 'Civilité', 'name': 'title', 'kind': 'title', 'disabled': True}, + {'label': 'Prénom', 'name': 'first_name', 'required': True, 'asked_on_registration': True}, + {'label': 'Nom', 'name': 'last_name', 'required': True, 'asked_on_registration': True}, + {'label': 'Adresse électronique', 'name': 'email', 'kind': 'email', 'required': True}, + {'label': 'Adresse', 'name': 'address'}, + {'label': 'Code postal', 'name': 'zipcode'}, + {'label': 'Commune', 'name': 'city'}, + {'label': 'Pays', 'name': 'country', 'disabled': True}, + {'label': 'Date de naissance', 'name': 'birthdate', 'kind': 'birthdate', 'disabled': True}, + {'label': 'Téléphone', 'name': 'phone', 'kind': 'phone_number'}, + {'label': 'Mobile', 'name': 'mobile', 'kind': 'phone_number'}, ] for i, attribute_dict in enumerate(attributes): diff --git a/hobo/profile/migrations/0003_attributedefinition_searchable.py b/hobo/profile/migrations/0003_attributedefinition_searchable.py index 10bc31d..f9a53ac 100644 --- a/hobo/profile/migrations/0003_attributedefinition_searchable.py +++ b/hobo/profile/migrations/0003_attributedefinition_searchable.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.12 on 2019-01-01 09:46 -from __future__ import unicode_literals from django.db import migrations, models diff --git a/hobo/profile/migrations/0004_auto_20200518_1810.py b/hobo/profile/migrations/0004_auto_20200518_1810.py index 9fe6c8c..f468921 100644 --- a/hobo/profile/migrations/0004_auto_20200518_1810.py +++ b/hobo/profile/migrations/0004_auto_20200518_1810.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.17 on 2020-05-18 18:10 -from __future__ import unicode_literals import django.core.validators from django.db import migrations, models diff --git a/hobo/profile/models.py b/hobo/profile/models.py index b1bb583..38e7195 100644 --- a/hobo/profile/models.py +++ b/hobo/profile/models.py @@ -67,7 +67,7 @@ class AttributeDefinition(models.Model): ordering = ['order'] def as_dict(self): - as_dict = dict([(x, y) for (x, y) in self.__dict__.items() if type(y) in (str, bool)]) + as_dict = {x: y for (x, y) in self.__dict__.items() if type(y) in (str, bool)} return as_dict def get_real_kind_display(self): @@ -78,4 +78,4 @@ class AttributeDefinition(models.Model): def save(self, *args, **kwargs): if self.order is None: self.order = max([0] + [x.order for x in AttributeDefinition.objects.all()]) + 1 - super(AttributeDefinition, self).save(*args, **kwargs) + super().save(*args, **kwargs) diff --git a/hobo/rest_authentication.py b/hobo/rest_authentication.py index eb29277..b7476b1 100644 --- a/hobo/rest_authentication.py +++ b/hobo/rest_authentication.py @@ -90,7 +90,7 @@ class PublikAuthenticationFailed(exceptions.APIException): class PublikAuthentication(authentication.BaseAuthentication): def __init__(self, *args, **kwargs): self.logger = logging.getLogger(__name__) - super(PublikAuthentication, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) def resolve_user(self, request): User = get_user_model() diff --git a/hobo/seo/views.py b/hobo/seo/views.py index b86dc58..c9eafa4 100644 --- a/hobo/seo/views.py +++ b/hobo/seo/views.py @@ -42,7 +42,7 @@ class HomeView(FormView): success_url = reverse_lazy('seo-home') def get_initial(self): - initial = super(HomeView, self).get_initial() + initial = super().get_initial() initial['meta_description'] = get_variable('meta_description').value initial['meta_keywords'] = get_variable('meta_keywords').value return initial @@ -50,10 +50,10 @@ class HomeView(FormView): def form_valid(self, form): set_variable('meta_description', form.cleaned_data['meta_description']) set_variable('meta_keywords', form.cleaned_data['meta_keywords']) - return super(HomeView, self).form_valid(form) + return super().form_valid(form) def get_context_data(self, **kwargs): - context = super(HomeView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) context['robots_txt'] = get_variable('robots_txt').value context['mode'] = get_mode(context['robots_txt']) return context @@ -78,13 +78,13 @@ class CustomizeView(FormView): success_url = reverse_lazy('seo-home') def get_initial(self): - initial = super(CustomizeView, self).get_initial() + initial = super().get_initial() initial['content'] = get_variable('robots_txt').value return initial def form_valid(self, form): set_variable('robots_txt', form.cleaned_data['content']) - return super(CustomizeView, self).form_valid(form) + return super().form_valid(form) customize = CustomizeView.as_view() diff --git a/hobo/theme/views.py b/hobo/theme/views.py index c242cef..5f238b3 100644 --- a/hobo/theme/views.py +++ b/hobo/theme/views.py @@ -32,14 +32,14 @@ class HomeView(TemplateView): template_name = 'hobo/theme_home.html' def get_context_data(self, **kwargs): - context = super(HomeView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) def hsv(theme): theme_colour = theme.get('variables').get('theme_color') or '#FFFFFF' if theme_colour == '#FFFFFF': return (1, 1, 1) theme_rgb = [int(x, 16) for x in (theme_colour[1:3], theme_colour[3:5], theme_colour[5:])] - theme_hsv = colorsys.rgb_to_hsv(*[1.0 * x / 255 for x in theme_rgb]) + theme_hsv = colorsys.rgb_to_hsv(*(1.0 * x / 255 for x in theme_rgb)) return theme_hsv context['themes'] = get_themes() diff --git a/hobo/urls_utils.py b/hobo/urls_utils.py index cf23bce..43c26a8 100644 --- a/hobo/urls_utils.py +++ b/hobo/urls_utils.py @@ -10,7 +10,7 @@ except ImportError: class DecoratedURLPattern(URLPattern): def resolve(self, *args, **kwargs): - result = super(DecoratedURLPattern, self).resolve(*args, **kwargs) + result = super().resolve(*args, **kwargs) if result: result.func = self._decorate_with(result.func) return result @@ -18,7 +18,7 @@ class DecoratedURLPattern(URLPattern): class DecoratedRegexURLResolver(URLResolver): def resolve(self, *args, **kwargs): - result = super(DecoratedRegexURLResolver, self).resolve(*args, **kwargs) + result = super().resolve(*args, **kwargs) if result: result.func = self._decorate_with(result.func) return result diff --git a/hobo/views.py b/hobo/views.py index 07f8258..6817389 100644 --- a/hobo/views.py +++ b/hobo/views.py @@ -34,7 +34,7 @@ class Home(TemplateView): template_name = 'hobo/home.html' def get_context_data(self, **kwargs): - context = super(Home, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) context['services'] = [x for x in get_installed_services() if not x.secondary] context['has_authentic'] = bool(Authentic.objects.filter(secondary=False)) context['has_global_title'] = Variable.objects.filter(name='global_title').exists() diff --git a/setup.py b/setup.py index 080a783..f621bef 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,4 @@ #! /usr/bin/env python -# -*- coding: utf-8 -*- import glob import os @@ -31,7 +30,7 @@ class eo_sdist(sdist): def get_version(): if os.path.exists('VERSION'): - version_file = open('VERSION', 'r') + version_file = open('VERSION') version = version_file.read() version_file.close() return version diff --git a/tests/test_cook.py b/tests/test_cook.py index c2ba220..496bd32 100644 --- a/tests/test_cook.py +++ b/tests/test_cook.py @@ -1,12 +1,12 @@ import json import os from io import StringIO +from unittest.mock import Mock, call, mock_open, patch import pytest from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.core.management.base import CommandError -from mock import Mock, call, mock_open, patch from hobo.environment.management.commands.cook import Command from hobo.environment.models import ( @@ -30,7 +30,7 @@ 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/'} + 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) @@ -42,7 +42,7 @@ 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/'} + 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) @@ -55,7 +55,7 @@ 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/'} + 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) @@ -68,7 +68,7 @@ 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/'} + 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) @@ -106,8 +106,8 @@ def test_run_cook(mocked_notify_agents, tmpdir): handler.write(json.dumps(recipe)) command.run_cook(recipe_path) - assert command.check_action.mock_calls == [call(u'create-hobo', {u'url': u'https://entrouvert.org/'})] - assert command.create_hobo.mock_calls == [call(url=u'https://entrouvert.org/')] + assert command.check_action.mock_calls == [call('create-hobo', {'url': 'https://entrouvert.org/'})] + assert command.create_hobo.mock_calls == [call(url='https://entrouvert.org/')] assert mocked_notify_agents.mock_calls == [] assert command.wait_operationals.mock_calls == [call(timeout=42)] @@ -123,7 +123,7 @@ def test_having_several_action_args(tmpdir): handler.write(json.dumps(recipe)) command.run_cook(recipe_path) - assert command.create_authentic.mock_calls == [call(title=u'Connexion', url=u'https://entrouvert.org/')] + assert command.create_authentic.mock_calls == [call(title='Connexion', url='https://entrouvert.org/')] def test_load_variables_from(db, tmpdir): @@ -153,9 +153,9 @@ def test_load_variables_from(db, tmpdir): handler.write(json.dumps(recipe)) command.run_cook(recipe_path) - assert command.create_hobo.mock_calls == [call(url=u'https://entrouvert.org/')] - assert command.create_authentic.mock_calls == [call(url=u'https://foo1/')] - assert command.create_combo.mock_calls == [call(not_a_string=[], url=u'https://bar2/')] + assert command.create_hobo.mock_calls == [call(url='https://entrouvert.org/')] + assert command.create_authentic.mock_calls == [call(url='https://foo1/')] + assert command.create_combo.mock_calls == [call(not_a_string=[], url='https://bar2/')] def test_wait_operationals(db, monkeypatch): @@ -357,7 +357,7 @@ def test_create_hobo_not_primary(mocked_TenantMiddleware, mocked_call_command, m with patch('hobo.environment.management.commands.cook.open', mocked_open, create=True): command.create_hobo('http://entrouvert.org/and_much') assert command.create_site.mock_calls == [ - call(Hobo, 'http://entrouvert.org/and_much', None, u'hobo-none', template_name='', variables=None) + call(Hobo, 'http://entrouvert.org/and_much', None, 'hobo-none', template_name='', variables=None) ] assert mocked_call_command.mock_calls == [] assert len(mocked_connection.set_tenant.mock_calls) == 1 diff --git a/tests/test_emails.py b/tests/test_emails.py index 32bbade..47f601e 100644 --- a/tests/test_emails.py +++ b/tests/test_emails.py @@ -4,9 +4,9 @@ import smtpd import smtplib import socket import threading +from unittest import mock import dns.resolver -import mock import pytest from django.core.exceptions import ValidationError from django.utils.encoding import force_text diff --git a/tests/test_environment.py b/tests/test_environment.py index b2d7aee..caeb6b8 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import json import pytest @@ -44,7 +43,7 @@ def test_unique_slug(): with pytest.raises(ValidationError) as e: passerelle.clean() - assert e.value.messages[0] == u'This slug is already used. It must be unique.' + assert e.value.messages[0] == 'This slug is already used. It must be unique.' def test_unique_title(): @@ -69,7 +68,7 @@ def test_unique_title(): with pytest.raises(ValidationError) as e: combo.clean() - assert e.value.messages[0] == u'This title is already used. It must be unique.' + assert e.value.messages[0] == 'This title is already used. It must be unique.' # secondary services can be added combo.secondary = True diff --git a/tests/test_health_api.py b/tests/test_health_api.py index bbb1d8c..dabb7de 100644 --- a/tests/test_health_api.py +++ b/tests/test_health_api.py @@ -1,12 +1,12 @@ import json import socket +from unittest.mock import MagicMock import pytest import requests from django.core.cache import cache from django.utils import timezone from httmock import HTTMock, remember_called, urlmatch -from mock import MagicMock from hobo.environment.models import Authentic, Combo diff --git a/tests/test_hobo_deploy.py b/tests/test_hobo_deploy.py index 224bd6a..829b096 100644 --- a/tests/test_hobo_deploy.py +++ b/tests/test_hobo_deploy.py @@ -4,9 +4,9 @@ import json import os import sys from io import StringIO +from unittest.mock import Mock, call, patch import pytest -from mock import Mock, call, patch from requests import Response, exceptions from hobo.agent.common.management.commands.hobo_deploy import Command, CommandError, replace_file @@ -21,12 +21,12 @@ def test_replace_file(tmpdir): content = 'content of my new file' replace_file(path, content) - with open(path, 'r') as handler: + with open(path) as handler: assert handler.read() == content content = 'new content for my file' replace_file(path, content) - with open(path, 'r') as handler: + with open(path) as handler: assert handler.read() == content @@ -164,7 +164,7 @@ def test_deploy(mocked_get_tenant_by_hostname, tmpdir): tenant, ) ] - with open(tenant_hobo_json, 'r') as handler: # hobo.json file + with open(tenant_hobo_json) as handler: # hobo.json file content = json.load(handler) assert ENVIRONMENT['services'][0]['this'] is True # new entry added assert json.dumps(content, sort_keys=True), json.dumps(ENVIRONMENT, sort_keys=True) @@ -224,16 +224,16 @@ def test_generate_saml_keys(tmpdir): tenant.domain_url = 'combo.dev.publik.love' command.generate_saml_keys(tenant) - with open('%s/saml.key' % str(tmpdir), 'r') as handler: + with open('%s/saml.key' % str(tmpdir)) as handler: key = handler.read() - with open('%s/saml.crt' % str(tmpdir), 'r') as handler: + with open('%s/saml.crt' % str(tmpdir)) as handler: crt = handler.read() # if files exist don't regenerate them command.generate_saml_keys(tenant) - with open('%s/saml.key' % str(tmpdir), 'r') as handler: + with open('%s/saml.key' % str(tmpdir)) as handler: assert key == handler.read() - with open('%s/saml.crt' % str(tmpdir), 'r') as handler: + with open('%s/saml.crt' % str(tmpdir)) as handler: assert crt == handler.read() @@ -269,7 +269,7 @@ def test_configure_service_provider(mocked_get, tmpdir): # normal case (stop when configuration on a service success for this tenant) mocked_get.side_effect = [response1] command.configure_service_provider(ENVIRONMENT, tenant) - with open(tenant_idp_metadata, 'r') as handler: + with open(tenant_idp_metadata) as handler: assert handler.read() == 'my saml idp metadata (1)' # no 'idp_url' JSON entry @@ -277,19 +277,19 @@ def test_configure_service_provider(mocked_get, tmpdir): os.remove(tenant_idp_metadata) command.configure_service_provider(env, tenant) with pytest.raises(IOError, match='No such file or directory'): - open(tenant_idp_metadata, 'r') + open(tenant_idp_metadata) # idp not available response1.status_code = 500 mocked_get.side_effect = [exceptions.RequestException, response1] command.configure_service_provider(ENVIRONMENT, tenant) with pytest.raises(IOError, match='No such file or directory'): - open(tenant_idp_metadata, 'r') + open(tenant_idp_metadata) # case when idp is becoming available mocked_get.side_effect = [response1, response2] command.configure_service_provider(ENVIRONMENT, tenant) - with open(tenant_idp_metadata, 'r') as handler: + with open(tenant_idp_metadata) as handler: assert handler.read() == 'my saml idp metadata (2)' diff --git a/tests/test_home_views.py b/tests/test_home_views.py index 29adc5e..29557b1 100644 --- a/tests/test_home_views.py +++ b/tests/test_home_views.py @@ -1,8 +1,7 @@ -# -*- coding: utf-8 -*- import json import re +from unittest import mock -import mock import pytest from django.contrib.auth.models import User from test_manager import login diff --git a/tests/test_import_template.py b/tests/test_import_template.py index 4dac38f..7dce6d3 100644 --- a/tests/test_import_template.py +++ b/tests/test_import_template.py @@ -1,4 +1,5 @@ -import mock +from unittest import mock + import pytest from django.conf import settings from django.core.management import load_command_class diff --git a/tests/test_maintenance.py b/tests/test_maintenance.py index a553c3c..90592da 100644 --- a/tests/test_maintenance.py +++ b/tests/test_maintenance.py @@ -1,4 +1,5 @@ -import mock +from unittest import mock + from test_manager import login from hobo.environment.models import Variable diff --git a/tests/test_matomo_utils.py b/tests/test_matomo_utils.py index dba20a7..61c93ff 100644 --- a/tests/test_matomo_utils.py +++ b/tests/test_matomo_utils.py @@ -1,8 +1,6 @@ -# -*- coding: utf-8 -*- - import json +from unittest import mock -import mock import pytest from django.test import override_settings from django.utils.encoding import force_text @@ -630,20 +628,20 @@ def test_create_fake_first_tracking_visit(mocked_post): content = PING_NOSTATUS_ERROR response.json = mock.MagicMock(return_value=json.loads(content)) mocked_post.return_value = response - with pytest.raises(MatomoException, match='internal error on ping \(status expected\)'): + with pytest.raises(MatomoException, match=r'internal error on ping \(status expected\)'): matomo.create_fake_first_tracking_visit('42') # failure (no dict) content = PING_NODICT_ERROR response.json = mock.MagicMock(return_value=content) mocked_post.return_value = response - with pytest.raises(MatomoException, match='internal error on ping \(dict expected\)'): + with pytest.raises(MatomoException, match=r'internal error on ping \(dict expected\)'): matomo.create_fake_first_tracking_visit('42') # failure (no JSON) response.json = mock.MagicMock(side_effect=ValueError('not a JSON')) mocked_post.return_value = response - with pytest.raises(MatomoException, match='internal error on ping \(JSON expected\)'): + with pytest.raises(MatomoException, match=r'internal error on ping \(JSON expected\)'): matomo.create_fake_first_tracking_visit('42') # failure (status 500) diff --git a/tests/test_matomo_views.py b/tests/test_matomo_views.py index 5267668..1a2d0be 100644 --- a/tests/test_matomo_views.py +++ b/tests/test_matomo_views.py @@ -1,9 +1,7 @@ -# -*- coding: utf-8 -*- - import json import re +from unittest import mock -import mock import pytest from django.contrib.auth.models import User from django.test import override_settings diff --git a/tests/test_settings_loaders.py b/tests/test_settings_loaders.py index c7ceeaf..9bd79c3 100644 --- a/tests/test_settings_loaders.py +++ b/tests/test_settings_loaders.py @@ -21,17 +21,17 @@ def test_authentic_update_settings_from_path(tmpdir): env = get_hobo_json() fields = env['profile']['fields'] assert [x['name'] for x in fields] == [ - u'title', - u'first_name', - u'last_name', - u'email', - u'address', - u'zipcode', - u'city', - u'country', - u'birthdate', - u'phone', - u'mobile', + 'title', + 'first_name', + 'last_name', + 'email', + 'address', + 'zipcode', + 'city', + 'country', + 'birthdate', + 'phone', + 'mobile', ] # swap title and mobile fields @@ -44,30 +44,30 @@ def test_authentic_update_settings_from_path(tmpdir): env = get_hobo_json() fields = env['profile']['fields'] assert [x['name'] for x in fields] == [ - u'mobile', - u'first_name', - u'last_name', - u'email', - u'address', - u'zipcode', - u'city', - u'country', - u'birthdate', - u'phone', - u'title', + 'mobile', + 'first_name', + 'last_name', + 'email', + 'address', + 'zipcode', + 'city', + 'country', + 'birthdate', + 'phone', + 'title', ] assert [x['name'] for x in fields if x['disabled']] == ['country', 'birthdate', 'title'] profile_fields = [x['name'] for x in fields if not x['disabled']] assert profile_fields == [ - u'mobile', - u'first_name', - u'last_name', - u'email', - u'address', - u'zipcode', - u'city', - u'phone', + 'mobile', + 'first_name', + 'last_name', + 'email', + 'address', + 'zipcode', + 'city', + 'phone', ] # serialize hobo.json @@ -82,8 +82,8 @@ def test_authentic_update_settings_from_path(tmpdir): loader = AuthenticLoader() loader.update_settings_from_path(tenant_settings, path) assert tenant_settings.A2_PROFILE_FIELDS == profile_fields - assert tenant_settings.A2_REQUIRED_FIELDS == [u'first_name', u'last_name', u'email'] - assert tenant_settings.A2_REGISTRATION_FIELDS == [u'first_name', u'last_name'] + assert tenant_settings.A2_REQUIRED_FIELDS == ['first_name', 'last_name', 'email'] + assert tenant_settings.A2_REGISTRATION_FIELDS == ['first_name', 'last_name'] def test_mellon_backoffice_login_hint_setting_from_path(tmpdir): diff --git a/tests/test_signature.py b/tests/test_signature.py index 6bad146..bb6c779 100644 --- a/tests/test_signature.py +++ b/tests/test_signature.py @@ -32,8 +32,8 @@ def test_signature(): assert '&nonce=' in signature.sign_url(URL, KEY) # Test unicode key conversion to UTF-8 - assert signature.check_url(signature.sign_url(URL, u'\xe9\xe9'), b'\xc3\xa9\xc3\xa9') - assert signature.check_url(signature.sign_url(URL, b'\xc3\xa9\xc3\xa9'), u'\xe9\xe9') + assert signature.check_url(signature.sign_url(URL, '\xe9\xe9'), b'\xc3\xa9\xc3\xa9') + assert signature.check_url(signature.sign_url(URL, b'\xc3\xa9\xc3\xa9'), '\xe9\xe9') # Test timedelta parameter now = datetime.datetime.utcnow() diff --git a/tests/test_theme.py b/tests/test_theme.py index 00babcd..23f9b5a 100644 --- a/tests/test_theme.py +++ b/tests/test_theme.py @@ -1,7 +1,7 @@ -# -*- coding: utf-8 -*- +from unittest.mock import patch + import pytest from django.core.exceptions import ValidationError -from mock import patch from test_manager import login from hobo.environment.models import Variable diff --git a/tests/test_version.py b/tests/test_version.py index fe3cb4d..467b206 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -14,4 +14,4 @@ def test_version_middleware(settings, client): settings.MIDDLEWARE = ('hobo.middleware.version.VersionMiddleware',) + tuple(settings.MIDDLEWARE) json_response = client.get('/__version__').json() - assert set(json_response.keys()) == set(['pytest', 'pytest-django']) + assert set(json_response.keys()) == {'pytest', 'pytest-django'} diff --git a/tests_authentic/settings.py b/tests_authentic/settings.py index dddb37d..62175ff 100644 --- a/tests_authentic/settings.py +++ b/tests_authentic/settings.py @@ -1,7 +1,6 @@ import builtins import os - -from mock import mock_open, patch +from unittest.mock import mock_open, patch import hobo.test_utils diff --git a/tests_authentic/test_hobo_deploy.py b/tests_authentic/test_hobo_deploy.py index 6697507..bcaecde 100644 --- a/tests_authentic/test_hobo_deploy.py +++ b/tests_authentic/test_hobo_deploy.py @@ -1,11 +1,10 @@ -# -*- coding: utf-8 -*- import json import os import shutil import tempfile import time +from unittest import mock -import mock import pytest from authentic2.data_transfer import export_site from django.core.management import call_command @@ -45,15 +44,15 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): { 'model': 'a2_rbac.role', 'fields': { - 'name': u'Service petite enfance', - 'slug': u'service-petite-enfance', + 'name': 'Service petite enfance', + 'slug': 'service-petite-enfance', }, }, { 'model': 'a2_rbac.role', 'fields': { - 'name': u'Service état-civil', - 'slug': u'service-etat-civil', + 'name': 'Service état-civil', + 'slug': 'service-etat-civil', }, }, ], @@ -151,7 +150,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): 'description': '', 'required': False, 'user_visible': True, - 'label': u'Civilité', + 'label': 'Civilité', 'disabled': True, 'user_editable': True, 'asked_on_registration': False, @@ -162,7 +161,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): 'description': '', 'required': True, 'user_visible': True, - 'label': u'Prénom', + 'label': 'Prénom', 'disabled': False, 'user_editable': True, 'asked_on_registration': True, @@ -184,7 +183,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): 'description': '', 'required': True, 'user_visible': True, - 'label': u'Adresse électronique', + 'label': 'Adresse électronique', 'disabled': False, 'user_editable': True, 'asked_on_registration': False, @@ -228,7 +227,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): 'description': '', 'required': False, 'user_visible': True, - 'label': u'Téléphone', + 'label': 'Téléphone', 'disabled': False, 'user_editable': True, 'asked_on_registration': False, @@ -312,14 +311,14 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): 'service-id': 'wcs', 'template_name': 'commune', 'slug': 'montpellier-metropole', - 'title': u'Montpellier-Métropole', + 'title': 'Montpellier-Métropole', 'base_url': 'http://eservices.example.net', 'saml-sp-metadata-url': 'http://eservices.example.net/saml/metadata', }, { 'service-id': 'passerelle', 'slug': 'passerelle', - 'title': u'Passerelle', + 'title': 'Passerelle', 'base_url': 'http://passerelle.example.net', 'saml-sp-metadata-url': 'http://passerelle.example.net/saml/metadata', }, @@ -340,7 +339,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): 'service-id': 'combo', 'template_name': 'portal-user', 'slug': 'portal', - 'title': u'Portail Montpellier-Métropole', + 'title': 'Portail Montpellier-Métropole', 'base_url': 'http://portal.example.net', 'saml-sp-metadata-url': 'http://portal.example.net/saml/metadata', }, @@ -364,7 +363,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): # check role mass provisionning to new services # two wcs => two ous => two audiences assert mock_notify.call_count == 2 - audiences = sorted([arg[0][0]['audience'] for arg in mock_notify.call_args_list]) + audiences = sorted(arg[0][0]['audience'] for arg in mock_notify.call_args_list) assert audiences == [ ['http://clapiers.example.net/saml/metadata'], [ @@ -466,7 +465,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): # to their "ou" excluding their superuser role, 3 admin roles for users, # roles and services, and 2 loaded roles, petite enfance and état-civil assert Role.objects.filter(ou__isnull=False, service__isnull=True).count() == 10 - for service_id in set(s['service-id'] for s in other_services): + for service_id in {s['service-id'] for s in other_services}: same_services = [s for s in other_services if s['service-id'] == service_id] for i, service in enumerate(same_services): assert LibertyProvider.objects.filter(slug=service['slug']).count() == 1 @@ -517,13 +516,13 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path): assert Role.objects.filter(ou=provider.ou, service__isnull=True).count() == 5 assert ( Role.objects.filter( - ou=provider.ou, service__isnull=True, name=u'Service petite enfance' + ou=provider.ou, service__isnull=True, name='Service petite enfance' ).count() == 1 ) assert ( Role.objects.filter( - ou=provider.ou, service__isnull=True, name=u'Service état-civil' + ou=provider.ou, service__isnull=True, name='Service état-civil' ).count() == 1 ) @@ -679,7 +678,7 @@ def test_hobo_deploy_with_legacy_urls(monkeypatch, tenant_base, mocker, skeleton { 'service-id': 'passerelle', 'slug': 'passerelle', - 'title': u'Passerelle', + 'title': 'Passerelle', 'base_url': 'http://passerelle.example.net', 'saml-sp-metadata-url': 'http://passerelle.example.net/saml/metadata', }, @@ -731,7 +730,7 @@ def test_hobo_deploy_with_legacy_urls(monkeypatch, tenant_base, mocker, skeleton { 'service-id': 'passerelle', 'slug': 'passerelle', - 'title': u'Passerelle', + 'title': 'Passerelle', 'base_url': 'http://new-passerelle.example.net', 'saml-sp-metadata-url': 'http://new-passerelle.example.net/saml/metadata', 'legacy_urls': [ diff --git a/tests_authentic/test_provisionning.py b/tests_authentic/test_provisionning.py index b28525c..6525ad9 100644 --- a/tests_authentic/test_provisionning.py +++ b/tests_authentic/test_provisionning.py @@ -1,7 +1,6 @@ -# -*- coding: utf-8 -*- - import json import os +from unittest.mock import ANY, call, patch import lasso import pytest @@ -13,7 +12,6 @@ from authentic2.saml.models import LibertyProvider from django.contrib.auth import get_user_model from django.core.management import call_command from django_rbac.utils import get_ou_model -from mock import ANY, call, patch from tenant_schemas.utils import tenant_context from hobo import signature @@ -41,21 +39,27 @@ def test_provision_role(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'audience', '@type', 'objects', 'full'} assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'role' data = objects['data'] assert isinstance(data, list) assert len(data) == 1 o = data[0] - assert set(o.keys()) == set( - ['details', 'emails_to_members', 'description', 'uuid', 'name', 'slug', 'emails'] - ) + assert set(o.keys()) == { + 'details', + 'emails_to_members', + 'description', + 'uuid', + 'name', + 'slug', + 'emails', + } assert o['details'] == '' assert o['emails_to_members'] is True assert o['emails'] == [] @@ -73,21 +77,27 @@ def test_provision_role(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'audience', '@type', 'objects', 'full'} assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'role' data = objects['data'] assert isinstance(data, list) assert len(data) == 1 o = data[0] - assert set(o.keys()) == set( - ['details', 'emails_to_members', 'description', 'uuid', 'name', 'slug', 'emails'] - ) + assert set(o.keys()) == { + 'details', + 'emails_to_members', + 'description', + 'uuid', + 'name', + 'slug', + 'emails', + } assert o['details'] == '' assert o['emails_to_members'] is True assert o['emails'] == emails @@ -101,19 +111,19 @@ def test_provision_role(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'audience', '@type', 'objects', 'full'} assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'deprovision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'role' data = objects['data'] assert isinstance(data, list) assert len(data) == 1 o = data[0] - assert set(o.keys()) == set(['uuid']) + assert set(o.keys()) == {'uuid'} def test_provision_user(transactional_db, tenant, caplog): @@ -134,17 +144,17 @@ def test_provision_user(transactional_db, tenant, caplog): attribute = Attribute.objects.create(label='Code postal', name='code_postal', kind='string') with provisionning: user1 = User.objects.create( - username=u'Étienne', + username='Étienne', email='etienne.dugenou@example.net', - first_name=u'Étienne', - last_name=u'Dugenou', + first_name='Étienne', + last_name='Dugenou', ou=get_default_ou(), ) user2 = User.objects.create( - username=u'john.doe', + username='john.doe', email='iohn.doe@example.net', - first_name=u'John', - last_name=u'Doe', + first_name='John', + last_name='Doe', is_active=False, ou=get_default_ou(), ) @@ -156,23 +166,29 @@ def test_provision_user(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['issuer', 'audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'issuer', 'audience', '@type', 'objects', 'full'} assert arg['issuer'] == 'https://%s/idp/saml2/metadata' % tenant.domain_url assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'user' data = objects['data'] assert isinstance(data, list) assert len(data) == 2 - assert len(set([o['uuid'] for o in data])) == 2 + assert len({o['uuid'] for o in data}) == 2 for o in data: - assert set(o.keys()) >= set( - ['uuid', 'username', 'first_name', 'is_superuser', 'last_name', 'email', 'roles'] - ) + assert set(o.keys()) >= { + 'uuid', + 'username', + 'first_name', + 'is_superuser', + 'last_name', + 'email', + 'roles', + } assert o['uuid'] in users user = users[o['uuid']] assert o['username'] == user.username @@ -197,31 +213,29 @@ def test_provision_user(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['issuer', 'audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'issuer', 'audience', '@type', 'objects', 'full'} assert arg['issuer'] == 'https://%s/idp/saml2/metadata' % tenant.domain_url assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'user' data = objects['data'] assert isinstance(data, list) assert len(data) == 2 for o, user in zip(data, [user1, user2]): - assert set(o.keys()) >= set( - [ - 'code_postal', - 'uuid', - 'username', - 'first_name', - 'is_superuser', - 'last_name', - 'email', - 'roles', - ] - ) + assert set(o.keys()) >= { + 'code_postal', + 'uuid', + 'username', + 'first_name', + 'is_superuser', + 'last_name', + 'email', + 'roles', + } assert o['uuid'] == user.uuid assert o['username'] == user.username assert o['first_name'] == user.first_name @@ -235,7 +249,7 @@ def test_provision_user(transactional_db, tenant, caplog): # test a service in a second OU also get the provisionning message notify_agents.reset_mock() - ou2 = get_ou_model().objects.create(name=u'ou2', slug=u'ou2') + ou2 = get_ou_model().objects.create(name='ou2', slug='ou2') LibertyProvider.objects.create( ou=ou2, name='provider2', @@ -251,7 +265,7 @@ def test_provision_user(transactional_db, tenant, caplog): assert notify_agents.call_count == 2 assert set( notify_agents.mock_calls[0][1][0]['audience'] + notify_agents.mock_calls[1][1][0]['audience'] - ) == set(['http://provider.com', 'http://provider2.com']) + ) == {'http://provider.com', 'http://provider2.com'} ou2.delete() notify_agents.reset_mock() @@ -263,22 +277,28 @@ def test_provision_user(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['issuer', 'audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'issuer', 'audience', '@type', 'objects', 'full'} assert arg['issuer'] == 'https://%s/idp/saml2/metadata' % tenant.domain_url assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'user' data = objects['data'] assert isinstance(data, list) assert len(data) == 1 for o in data: - assert set(o.keys()) >= set( - ['uuid', 'username', 'first_name', 'is_superuser', 'last_name', 'email', 'roles'] - ) + assert set(o.keys()) >= { + 'uuid', + 'username', + 'first_name', + 'is_superuser', + 'last_name', + 'email', + 'roles', + } assert o['uuid'] == user1.uuid assert o['username'] == user1.username assert o['first_name'] == user1.first_name @@ -301,22 +321,28 @@ def test_provision_user(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['issuer', 'audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'issuer', 'audience', '@type', 'objects', 'full'} assert arg['issuer'] == 'https://%s/idp/saml2/metadata' % tenant.domain_url assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'user' data = objects['data'] assert isinstance(data, list) assert len(data) == 2 for o in data: - assert set(o.keys()) >= set( - ['uuid', 'username', 'first_name', 'is_superuser', 'last_name', 'email', 'roles'] - ) + assert set(o.keys()) >= { + 'uuid', + 'username', + 'first_name', + 'is_superuser', + 'last_name', + 'email', + 'roles', + } assert o['uuid'] in users user = users[o['uuid']] assert o['uuid'] == user.uuid @@ -328,7 +354,7 @@ def test_provision_user(transactional_db, tenant, caplog): {'name': r.name, 'slug': r.slug, 'uuid': r.uuid} for r in user.roles.all() ] assert o['is_superuser'] is (user == user1) - assert len(set(x['uuid'] for x in notify_agents.call_args_list[1][0][0]['objects']['data'])) == 2 + assert len({x['uuid'] for x in notify_agents.call_args_list[1][0][0]['objects']['data']}) == 2 notify_agents.reset_mock() with provisionning: @@ -339,22 +365,28 @@ def test_provision_user(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['issuer', 'audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'issuer', 'audience', '@type', 'objects', 'full'} assert arg['issuer'] == 'https://%s/idp/saml2/metadata' % tenant.domain_url assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'user' data = objects['data'] assert isinstance(data, list) assert len(data) == 1 for o in data: - assert set(o.keys()) >= set( - ['uuid', 'username', 'first_name', 'is_superuser', 'last_name', 'email', 'roles'] - ) + assert set(o.keys()) >= { + 'uuid', + 'username', + 'first_name', + 'is_superuser', + 'last_name', + 'email', + 'roles', + } assert o['uuid'] == user1.uuid assert o['username'] == user1.username assert o['first_name'] == user1.first_name @@ -373,22 +405,28 @@ def test_provision_user(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['issuer', 'audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'issuer', 'audience', '@type', 'objects', 'full'} assert arg['issuer'] == 'https://%s/idp/saml2/metadata' % tenant.domain_url assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'user' data = objects['data'] assert isinstance(data, list) assert len(data) == 1 for o in data: - assert set(o.keys()) >= set( - ['uuid', 'username', 'first_name', 'is_superuser', 'last_name', 'email', 'roles'] - ) + assert set(o.keys()) >= { + 'uuid', + 'username', + 'first_name', + 'is_superuser', + 'last_name', + 'email', + 'roles', + } assert o['uuid'] == user1.uuid assert o['username'] == user1.username assert o['first_name'] == user1.first_name @@ -399,7 +437,7 @@ def test_provision_user(transactional_db, tenant, caplog): r1 = {'uuid': role.uuid, 'name': role.name, 'slug': role.slug} r2 = {'uuid': child_role.uuid, 'name': child_role.name, 'slug': child_role.slug} assert r == r1 or r == r2 - assert len(set(r['uuid'] for r in o['roles'])) == 2 + assert len({r['uuid'] for r in o['roles']}) == 2 assert o['is_superuser'] is True notify_agents.reset_mock() @@ -411,22 +449,28 @@ def test_provision_user(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['issuer', 'audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'issuer', 'audience', '@type', 'objects', 'full'} assert arg['issuer'] == 'https://%s/idp/saml2/metadata' % tenant.domain_url assert arg['audience'] == ['http://provider.com'] assert arg['@type'] == 'provision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'user' data = objects['data'] assert isinstance(data, list) assert len(data) == 1 for o in data: - assert set(o.keys()) >= set( - ['uuid', 'username', 'first_name', 'is_superuser', 'last_name', 'email', 'roles'] - ) + assert set(o.keys()) >= { + 'uuid', + 'username', + 'first_name', + 'is_superuser', + 'last_name', + 'email', + 'roles', + } assert o['uuid'] == user1.uuid assert o['username'] == user1.username assert o['first_name'] == user1.first_name @@ -438,7 +482,7 @@ def test_provision_user(transactional_db, tenant, caplog): assert o['is_superuser'] is False notify_agents.reset_mock() - ou2 = get_ou_model().objects.create(name=u'ou2', slug=u'ou2') + ou2 = get_ou_model().objects.create(name='ou2', slug='ou2') LibertyProvider.objects.create( ou=get_default_ou(), name='provider2', @@ -454,21 +498,21 @@ def test_provision_user(transactional_db, tenant, caplog): assert arg == call(ANY) arg = arg[0][0] assert isinstance(arg, dict) - assert set(arg.keys()) == set(['issuer', 'audience', '@type', 'objects', 'full']) + assert set(arg.keys()) == {'issuer', 'audience', '@type', 'objects', 'full'} assert arg['issuer'] == 'https://%s/idp/saml2/metadata' % tenant.domain_url - assert set(arg['audience']) == set(['http://provider.com', 'http://provider2.com']) + assert set(arg['audience']) == {'http://provider.com', 'http://provider2.com'} assert arg['@type'] == 'deprovision' assert arg['full'] is False objects = arg['objects'] assert isinstance(objects, dict) - assert set(objects.keys()) == set(['data', '@type']) + assert set(objects.keys()) == {'data', '@type'} assert objects['@type'] == 'user' data = objects['data'] assert isinstance(data, list) assert len(data) == 2 - assert len(set([o['uuid'] for o in data])) == 2 + assert len({o['uuid'] for o in data}) == 2 for o in data: - assert set(o.keys()) == set(['uuid']) + assert set(o.keys()) == {'uuid'} assert o['uuid'] in users diff --git a/tests_multipublik/settings.py b/tests_multipublik/settings.py index fcf53c2..13a5515 100644 --- a/tests_multipublik/settings.py +++ b/tests_multipublik/settings.py @@ -1,7 +1,6 @@ import builtins import os.path - -from mock import mock_open, patch +from unittest.mock import mock_open, patch import hobo.test_utils from hobo.settings import * diff --git a/tests_multitenant/settings.py b/tests_multitenant/settings.py index 901e771..aa5659e 100644 --- a/tests_multitenant/settings.py +++ b/tests_multitenant/settings.py @@ -1,7 +1,6 @@ import builtins import os - -from mock import mock_open, patch +from unittest.mock import mock_open, patch import hobo.test_utils diff --git a/tests_multitenant/test_agent_worker.py b/tests_multitenant/test_agent_worker.py index 5379f08..44862b6 100644 --- a/tests_multitenant/test_agent_worker.py +++ b/tests_multitenant/test_agent_worker.py @@ -1,9 +1,8 @@ -# -*- coding: utf-8 -*- import importlib import json import os +from unittest import mock -import mock import pytest from django.utils.encoding import force_text @@ -11,47 +10,47 @@ from hobo.agent.worker import settings from hobo.agent.worker.services import deploy, notify ENVIRONMENT = { - u'timestamp': '2022-02-22', - u'services': [ + 'timestamp': '2022-02-22', + 'services': [ { - u'service-id': u'hobo', - u'slug': u'hobo', - u'title': u'Hobo primary', - u'base_url': u'https://hobo1.dev.publik.love/', - u'this': True, - u'secret_key': u'1nesüper5Cr!eteKAaY~', + 'service-id': 'hobo', + 'slug': 'hobo', + 'title': 'Hobo primary', + 'base_url': 'https://hobo1.dev.publik.love/', + 'this': True, + 'secret_key': '1nesüper5Cr!eteKAaY~', }, { - u'service-id': u'combo', - u'slug': u'portal', - u'title': u'Compte citoyen', - u'base_url': u'https://combo.dev.publik.love/', - u'secret_key': u'1nesüper5Cr!eteKAaY~', + 'service-id': 'combo', + 'slug': 'portal', + 'title': 'Compte citoyen', + 'base_url': 'https://combo.dev.publik.love/', + 'secret_key': '1nesüper5Cr!eteKAaY~', }, { - u'service-id': u'wcs', - u'slug': u'eservices', - u'title': u'Démarches', - u'base_url': u'https://wcs.dev.publik.love/', - u'secret_key': u'1nesüper5Cr!eteKAaY~', + 'service-id': 'wcs', + 'slug': 'eservices', + 'title': 'Démarches', + 'base_url': 'https://wcs.dev.publik.love/', + 'secret_key': '1nesüper5Cr!eteKAaY~', }, { - u'service-id': u'hobo', - u'slug': u'hobo', - u'title': u'skip beacause of agent host patterns', - u'base_url': u'https://hobo2.dev.publik.love/', - u'secondary': True, - u'secret_key': u'1nesüper5Cr!eteKAaY~', + 'service-id': 'hobo', + 'slug': 'hobo', + 'title': 'skip beacause of agent host patterns', + 'base_url': 'https://hobo2.dev.publik.love/', + 'secondary': True, + 'secret_key': '1nesüper5Cr!eteKAaY~', }, { - u'service-id': u'passerelle', - u'slug': u'passerelle', - u'title': u'skipped because no secret_key', - u'base_url': u'https://passerelle.dev.publik.love/', + 'service-id': 'passerelle', + 'slug': 'passerelle', + 'title': 'skipped because no secret_key', + 'base_url': 'https://passerelle.dev.publik.love/', }, { - u'service-id': u'unknown', - u'title': u'skipped because unknown service-id', + 'service-id': 'unknown', + 'title': 'skipped because unknown service-id', }, ], } @@ -96,7 +95,7 @@ def test_deploy(mocked_subprocess, mocked_exists): deploy(ENVIRONMENT) # process called - mock_calls = set(x[1][0] for x in mocked_subprocess.Popen.mock_calls) + mock_calls = {x[1][0] for x in mocked_subprocess.Popen.mock_calls} assert len(mock_calls) == 4 assert '/usr/bin/hobo-manage hobo_deploy https://hobo1.dev.publik.love/ -' in mock_calls assert '/usr/bin/wcsctl hobo_deploy https://wcs.dev.publik.love/ -' in mock_calls @@ -125,7 +124,7 @@ def test_deploy_host_with_agent_patterns(mocked_subprocess, mocked_exists, local importlib.reload(settings) deploy(ENVIRONMENT) # process called - mock_calls = set(x[1][0] for x in mocked_subprocess.Popen.mock_calls) + mock_calls = {x[1][0] for x in mocked_subprocess.Popen.mock_calls} assert len(mock_calls) == 3 assert '/usr/bin/hobo-manage hobo_deploy https://hobo1.dev.publik.love/ -' in mock_calls assert '/usr/bin/wcsctl hobo_deploy https://wcs.dev.publik.love/ -' in mock_calls @@ -146,7 +145,7 @@ def test_notify(mocked_subprocess, mocked_listdir, mocked_exists): notify(NOTIFICATION) # process called - mock_calls = set(x[1][0] for x in mocked_subprocess.Popen.mock_calls) + mock_calls = {x[1][0] for x in mocked_subprocess.Popen.mock_calls} assert mock_calls == {'/usr/lib/combo/manage.py hobo_notify -'} # notification sent diff --git a/tests_multitenant/test_create_tenant.py b/tests_multitenant/test_create_tenant.py index 9bc99c2..2de91a8 100644 --- a/tests_multitenant/test_create_tenant.py +++ b/tests_multitenant/test_create_tenant.py @@ -14,7 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import mock +from unittest import mock + import pytest from django.contrib.auth.models import User from django.core.management import call_command diff --git a/tests_multitenant/test_hobo_notify.py b/tests_multitenant/test_hobo_notify.py index 90d1b42..5d025c1 100644 --- a/tests_multitenant/test_hobo_notify.py +++ b/tests_multitenant/test_hobo_notify.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import logging import pytest @@ -20,16 +18,16 @@ def test_hobo_notify_roles(caplog, tenants): for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'provision', - u'audience': [u'http://coin.com/saml/metadata'], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'provision', + 'audience': ['http://coin.com/saml/metadata'], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'12345', - u'name': u'Service petite enfance', - u'slug': u'service-petite-enfance', - u'description': u'Role du service petite enfance %s' % tenant.domain_url, + 'uuid': '12345', + 'name': 'Service petite enfance', + 'slug': 'service-petite-enfance', + 'description': 'Role du service petite enfance %s' % tenant.domain_url, } ], }, @@ -42,19 +40,19 @@ def test_hobo_notify_roles(caplog, tenants): for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'provision', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'provision', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'12345', - u'name': u'Service petite enfance', - u'slug': u'service-petite-enfance', - u'description': u'Role du service petite enfance %s' % tenant.domain_url, - u'details': u'Some details', - u'emails': [u'foo@bar.com', u'test@entrouvert.org'], - u'emails_to_members': False, + 'uuid': '12345', + 'name': 'Service petite enfance', + 'slug': 'service-petite-enfance', + 'description': 'Role du service petite enfance %s' % tenant.domain_url, + 'details': 'Some details', + 'emails': ['foo@bar.com', 'test@entrouvert.org'], + 'emails_to_members': False, } ], }, @@ -63,28 +61,28 @@ def test_hobo_notify_roles(caplog, tenants): assert Group.objects.count() == 1 assert Role.objects.count() == 1 role = Role.objects.get() - assert role.uuid == u'12345' - assert role.name == u'Service petite enfance' - assert role.description == u'Role du service petite enfance %s' % tenant.domain_url - assert role.details == u'Some details' - assert role.emails == [u'foo@bar.com', u'test@entrouvert.org'] + assert role.uuid == '12345' + assert role.name == 'Service petite enfance' + assert role.description == 'Role du service petite enfance %s' % tenant.domain_url + assert role.details == 'Some details' + assert role.emails == ['foo@bar.com', 'test@entrouvert.org'] assert role.emails_to_members is False # test full provisionning for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'provision', - u'full': True, - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'provision', + 'full': True, + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'xyz', - u'name': u'Service état civil', - u'slug': u'service-etat-civil', - u'description': u'Role du service état civil %s' % tenant.domain_url, + 'uuid': 'xyz', + 'name': 'Service état civil', + 'slug': 'service-etat-civil', + 'description': 'Role du service état civil %s' % tenant.domain_url, } ], }, @@ -93,24 +91,24 @@ def test_hobo_notify_roles(caplog, tenants): assert Group.objects.count() == 1 assert Role.objects.count() == 1 role = Role.objects.get() - assert role.uuid == u'xyz' - assert role.name == u'Service état civil' - assert role.description == u'Role du service état civil %s' % tenant.domain_url + assert role.uuid == 'xyz' + assert role.name == 'Service état civil' + assert role.description == 'Role du service état civil %s' % tenant.domain_url # test deprovision for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'deprovision', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'deprovision', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'xyz', - u'name': u'Service état civil', - u'slug': u'service-etat-civil', - u'description': u'Role du service état civil %s' % tenant.domain_url, + 'uuid': 'xyz', + 'name': 'Service état civil', + 'slug': 'service-etat-civil', + 'description': 'Role du service état civil %s' % tenant.domain_url, } ], }, @@ -123,16 +121,16 @@ def test_hobo_notify_roles(caplog, tenants): for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'provision', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'provision', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'12345', - u'name': u'Service petite enfance', - u'slug': u'service-petite-enfance', - u'description': u'Role du service petite enfance %s' % tenant.domain_url, + 'uuid': '12345', + 'name': 'Service petite enfance', + 'slug': 'service-petite-enfance', + 'description': 'Role du service petite enfance %s' % tenant.domain_url, } ], }, @@ -142,16 +140,16 @@ def test_hobo_notify_roles(caplog, tenants): assert Role.objects.count() == 1 # test update by uuid notification = { - u'@type': u'provision', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'provision', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'12345', - u'name': u'Service petite enfance2', - u'slug': u'service-petite-enfance2', - u'description': u'Role du service petite enfance %s' % tenant.domain_url, + 'uuid': '12345', + 'name': 'Service petite enfance2', + 'slug': 'service-petite-enfance2', + 'description': 'Role du service petite enfance %s' % tenant.domain_url, } ], }, @@ -162,16 +160,16 @@ def test_hobo_notify_roles(caplog, tenants): assert Role.objects.get().name == notification['objects']['data'][0]['name'] # test uuid change notification = { - u'@type': u'provision', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'provision', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'xyz', - u'name': u'Service petite enfance2', - u'slug': u'service-petite-enfance2', - u'description': u'Role du service petite enfance %s' % tenant.domain_url, + 'uuid': 'xyz', + 'name': 'Service petite enfance2', + 'slug': 'service-petite-enfance2', + 'description': 'Role du service petite enfance %s' % tenant.domain_url, } ], }, @@ -184,16 +182,16 @@ def test_hobo_notify_roles(caplog, tenants): Role.objects.create(uuid='12345', name='Foo', description='foo') assert Role.objects.count() == 2 notification = { - u'@type': u'provision', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'provision', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'12345', - u'name': u'Service petite enfance2', - u'slug': u'service-petite-enfance2', - u'description': u'Role du service petite enfance %s' % tenant.domain_url, + 'uuid': '12345', + 'name': 'Service petite enfance2', + 'slug': 'service-petite-enfance2', + 'description': 'Role du service petite enfance %s' % tenant.domain_url, } ], }, @@ -201,10 +199,10 @@ def test_hobo_notify_roles(caplog, tenants): Command.process_notification(tenant, notification) assert Group.objects.count() == 2 assert Role.objects.count() == 2 - assert Role.objects.filter(uuid='12345', name=u'Service petite enfance').count() == 0 + assert Role.objects.filter(uuid='12345', name='Service petite enfance').count() == 0 assert caplog.records[-1].levelno == logging.ERROR assert caplog.records[-1].msg.startswith('cannot provision') - assert caplog.records[-1].args == (u'Service petite enfance2', u'12345') + assert caplog.records[-1].args == ('Service petite enfance2', '12345') def test_hobo_notify_roles_db_queries(caplog, tenants): @@ -358,16 +356,16 @@ def test_provision_users(tenants): for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'provision', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'role', - u'data': [ + '@type': 'provision', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'role', + 'data': [ { - u'uuid': u'12345', - u'name': u'Service petite enfance', - u'slug': u'service-petite-enfance', - u'description': u'Role du service petite enfance %s' % tenant.domain_url, + 'uuid': '12345', + 'name': 'Service petite enfance', + 'slug': 'service-petite-enfance', + 'description': 'Role du service petite enfance %s' % tenant.domain_url, } ], }, @@ -376,36 +374,36 @@ def test_provision_users(tenants): assert Group.objects.count() == 1 assert Role.objects.count() == 1 role = Role.objects.get() - assert role.uuid == u'12345' - assert role.name == u'Service petite enfance' - assert role.description == u'Role du service petite enfance %s' % tenant.domain_url + assert role.uuid == '12345' + assert role.name == 'Service petite enfance' + assert role.description == 'Role du service petite enfance %s' % tenant.domain_url # test user provisionning for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'provision', - u'issuer': 'http://idp.example.net/idp/saml/metadata', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'user', - u'data': [ + '@type': 'provision', + 'issuer': 'http://idp.example.net/idp/saml/metadata', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'user', + 'data': [ { - u'uuid': u'a' * 32, - u'first_name': u'John', - u'last_name': u'Doe', - u'email': u'john.doe@example.net', - u'is_superuser': False, - u'roles': [ + 'uuid': 'a' * 32, + 'first_name': 'John', + 'last_name': 'Doe', + 'email': 'john.doe@example.net', + 'is_superuser': False, + 'roles': [ { - u'uuid': u'12345', - u'name': u'Service petite enfance', - u'description': u'etc.', + 'uuid': '12345', + 'name': 'Service petite enfance', + 'description': 'etc.', }, { - u'uuid': u'xyz', - u'name': u'Service état civil', - u'description': u'etc.', + 'uuid': 'xyz', + 'name': 'Service état civil', + 'description': 'etc.', }, ], } @@ -437,28 +435,28 @@ def test_provision_users(tenants): for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'provision', - u'issuer': 'http://idp.example.net/idp/saml/metadata', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'user', - u'data': [ + '@type': 'provision', + 'issuer': 'http://idp.example.net/idp/saml/metadata', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'user', + 'data': [ { - u'uuid': u'a' * 32, - u'first_name': u'John', - u'last_name': u'Doe', - u'email': u'john.doe@example.net', - u'is_superuser': True, - u'roles': [ + 'uuid': 'a' * 32, + 'first_name': 'John', + 'last_name': 'Doe', + 'email': 'john.doe@example.net', + 'is_superuser': True, + 'roles': [ { - u'uuid': u'12345', - u'name': u'Service petite enfance', - u'description': u'etc.', + 'uuid': '12345', + 'name': 'Service petite enfance', + 'description': 'etc.', }, { - u'uuid': u'xyz', - u'name': u'Service état civil', - u'description': u'etc.', + 'uuid': 'xyz', + 'name': 'Service état civil', + 'description': 'etc.', }, ], } @@ -490,14 +488,14 @@ def test_provision_users(tenants): for tenant in tenants: with tenant_context(tenant): notification = { - u'@type': u'deprovision', - u'issuer': 'http://idp.example.net/idp/saml/metadata', - u'audience': [u'%s/saml/metadata' % tenant.get_base_url()], - u'objects': { - u'@type': 'user', - u'data': [ + '@type': 'deprovision', + 'issuer': 'http://idp.example.net/idp/saml/metadata', + 'audience': ['%s/saml/metadata' % tenant.get_base_url()], + 'objects': { + '@type': 'user', + 'data': [ { - u'uuid': u'a' * 32, + 'uuid': 'a' * 32, } ], }, diff --git a/tests_multitenant/test_middleware.py b/tests_multitenant/test_middleware.py index c6b3494..8aea2d2 100644 --- a/tests_multitenant/test_middleware.py +++ b/tests_multitenant/test_middleware.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - import pytest from hobo.multitenant.middleware import TenantMiddleware diff --git a/tests_multitenant/test_settings.py b/tests_multitenant/test_settings.py index cb33748..004ed18 100644 --- a/tests_multitenant/test_settings.py +++ b/tests_multitenant/test_settings.py @@ -193,24 +193,19 @@ def test_known_services(tenants, settings): assert 'authentic' in settings.KNOWN_SERVICES assert 'other' in settings.KNOWN_SERVICES['authentic'] authentic_other_keys = set(settings.KNOWN_SERVICES['authentic']['other'].keys()) - assert ( - set( - [ - 'url', - 'backoffice-menu-url', - 'title', - 'orig', - 'verif_orig', - 'secret', - 'template_name', - 'variables', - 'saml-sp-metadata-url', - 'provisionning-url', - 'secondary', - ] - ) - == authentic_other_keys - ) + assert { + 'url', + 'backoffice-menu-url', + 'title', + 'orig', + 'verif_orig', + 'secret', + 'template_name', + 'variables', + 'saml-sp-metadata-url', + 'provisionning-url', + 'secondary', + } == authentic_other_keys assert ( settings.KNOWN_SERVICES['authentic']['other']['url'] == hobo_json['services'][2]['base_url'] ) diff --git a/tests_multitenant/test_tenant_command.py b/tests_multitenant/test_tenant_command.py index 29fe5e7..4d7ece3 100644 --- a/tests_multitenant/test_tenant_command.py +++ b/tests_multitenant/test_tenant_command.py @@ -1,10 +1,8 @@ -# -*- coding: utf-8 -*- - import json import os import sys +from unittest import mock -import mock import pytest from django.core.management import BaseCommand, call_command, load_command_class from django.core.management.base import CommandError @@ -15,7 +13,7 @@ from hobo.multitenant.models import Tenant pytestmark = pytest.mark.django_db -class RecordTenant(object): +class RecordTenant: def __init__(self): self.tenants = [] @@ -41,9 +39,10 @@ def test_all_tenants(handle, tenants): execute_from_command_line(['manage.py', 'tenant_command', 'clearsessions', '--all-tenants']) assert handle.call_count == 2 assert len(handle.side_effect.tenants) == 2 - assert set(tenant.domain_url for tenant in handle.side_effect.tenants) == set( - ['tenant1.example.net', 'tenant2.example.net'] - ) + assert {tenant.domain_url for tenant in handle.side_effect.tenants} == { + 'tenant1.example.net', + 'tenant2.example.net', + } @mock.patch('django.contrib.sessions.management.commands.clearsessions.Command.handle') @@ -127,7 +126,7 @@ def test_tenant_command_all_tenants_errors(tenants, monkeypatch, capsys): class UnicodeErrorCommand(BaseCommand): def handle(self, *args, **kwargs): - raise Exception(u'héhé') + raise Exception('héhé') class BytesErrorCommand(BaseCommand): def handle(self, *args, **kwargs): @@ -135,7 +134,7 @@ def test_tenant_command_all_tenants_errors(tenants, monkeypatch, capsys): class MixOfBothCommand(BaseCommand): def handle(self, *args, **kwargs): - raise Exception([force_bytes('héhé'), u'hého']) + raise Exception([force_bytes('héhé'), 'hého']) class WtfExceptionCommand(BaseCommand): def handle(self, *args, **kwargs): @@ -166,9 +165,9 @@ def test_tenant_command_all_tenants_errors(tenants, monkeypatch, capsys): with pytest.raises(SystemExit): klass.run_from_argv(['manage.py', 'tenant_command', 'uni-error', '--all-tenants']) captured = capsys.readouterr() - assert u'Tenant tenant1.example.net (tenant1_example_net): Exception: héhé' in captured.err - assert u'Tenant tenant2.example.net (tenant2_example_net): Exception: héhé' in captured.err - assert u'Command failed on multiple tenants' in captured.err + assert 'Tenant tenant1.example.net (tenant1_example_net): Exception: héhé' in captured.err + assert 'Tenant tenant2.example.net (tenant2_example_net): Exception: héhé' in captured.err + assert 'Command failed on multiple tenants' in captured.err with pytest.raises(SystemExit): klass.run_from_argv(['manage.py', 'tenant_command', 'bytes-error', '--all-tenants']) @@ -181,7 +180,7 @@ def test_tenant_command_all_tenants_errors(tenants, monkeypatch, capsys): captured = capsys.readouterr() assert repr(force_bytes('héhé')) in captured.err - assert repr(u'hého') in captured.err + assert repr('hého') in captured.err with pytest.raises(SystemExit): klass.run_from_argv(['manage.py', 'tenant_command', 'wtf-error', '--all-tenants']) diff --git a/tests_multitenant/test_uwsgidecorators.py b/tests_multitenant/test_uwsgidecorators.py index 37d9167..67fb28c 100644 --- a/tests_multitenant/test_uwsgidecorators.py +++ b/tests_multitenant/test_uwsgidecorators.py @@ -1,7 +1,7 @@ import importlib import pickle +from unittest import mock -import mock import pytest import hobo.multitenant.uwsgidecorators diff --git a/tests_multitenant/utilities.py b/tests_multitenant/utilities.py index d3c5a16..031cfba 100644 --- a/tests_multitenant/utilities.py +++ b/tests_multitenant/utilities.py @@ -1,4 +1,4 @@ -class PatchDefaultSettings(object): +class PatchDefaultSettings: empty = object() def __init__(self, settings, **kwargs): diff --git a/tests_passerelle/settings.py b/tests_passerelle/settings.py index 1c39903..bc376a2 100644 --- a/tests_passerelle/settings.py +++ b/tests_passerelle/settings.py @@ -1,7 +1,6 @@ import builtins import os - -from mock import mock_open, patch +from unittest.mock import mock_open, patch import hobo.test_utils diff --git a/tests_schemas/test_cook.py b/tests_schemas/test_cook.py index f751687..500e42a 100644 --- a/tests_schemas/test_cook.py +++ b/tests_schemas/test_cook.py @@ -39,7 +39,7 @@ def test_cook_example(db, fake_notify, monkeypatch, fake_themes): # below JSON file was created by this instruction # json.dump(environment, open('tests_schemas/example_env.json', 'w'), # sort_keys=True, indent=4, separators=(',', ': ')) - expected_env = json.load(open('tests_schemas/example_env.json', 'r')) + expected_env = json.load(open('tests_schemas/example_env.json')) # remove secret_key and timestamp values that alway change environment['timestamp'] = 'XXXXXXXXXX.XX' diff --git a/tests_schemas/test_hobo_deploy.py b/tests_schemas/test_hobo_deploy.py index dc0b4c2..95ea5d1 100644 --- a/tests_schemas/test_hobo_deploy.py +++ b/tests_schemas/test_hobo_deploy.py @@ -1,6 +1,6 @@ import os +from unittest import mock -import mock import pytest from django.core.management import call_command, get_commands, load_command_class from tenant_schemas.utils import tenant_context diff --git a/tests_schemas/test_import_template.py b/tests_schemas/test_import_template.py index f5b9fdd..68533fc 100644 --- a/tests_schemas/test_import_template.py +++ b/tests_schemas/test_import_template.py @@ -1,6 +1,6 @@ import os +from unittest import mock -import mock import pytest from django.core.management import call_command, load_command_class diff --git a/tests_schemas/test_rename_hobo_service.py b/tests_schemas/test_rename_hobo_service.py index c8a0f0c..4e47b6e 100644 --- a/tests_schemas/test_rename_hobo_service.py +++ b/tests_schemas/test_rename_hobo_service.py @@ -1,7 +1,8 @@ +from unittest.mock import Mock + import pytest from django.core.management import call_command from django.core.management.base import CommandError -from mock import Mock from tenant_schemas.utils import tenant_context from hobo.environment.models import Passerelle, ServiceBase diff --git a/tests_schemas/test_rename_service.py b/tests_schemas/test_rename_service.py index 61c6998..e91850d 100644 --- a/tests_schemas/test_rename_service.py +++ b/tests_schemas/test_rename_service.py @@ -1,7 +1,8 @@ +from unittest.mock import Mock + import pytest from django.core.management import call_command from django.core.management.base import CommandError -from mock import Mock from tenant_schemas.utils import tenant_context from hobo.environment.models import Passerelle, ServiceBase