misc: apply pyupgrade (#69708)

This commit is contained in:
Valentin Deniaud 2022-09-29 15:23:21 +02:00
parent 5470d0083e
commit a107b7ef0e
114 changed files with 573 additions and 640 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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]

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -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

View File

@ -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)

View File

@ -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')

View File

@ -14,7 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import
from celery import Celery
from kombu.common import Broadcast

View File

@ -14,7 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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):

View File

@ -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

View File

@ -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):

View File

@ -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()

View File

@ -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}

View File

@ -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}
)

View File

@ -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:

View File

@ -1,5 +1,3 @@
from __future__ import print_function
from optparse import make_option
from django.core.management.base import BaseCommand

View File

@ -14,7 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import json
import os

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

View File

@ -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

View File

@ -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

View File

@ -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, []):

View File

@ -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):

View File

@ -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

View File

@ -1,4 +1,3 @@
# -*- Mode: python; coding:utf-8; indent-tabs-mode: nil -*- */
#
#
# Copyright 2012 David Strauss <david@davidstrauss.net>
@ -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):

View File

@ -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)

View File

@ -109,7 +109,7 @@ class MatomoError(MatomoException):
"""expected Matomo error responses"""
class MatomoWS(object):
class MatomoWS:
"""api for matomo webservices"""
def __init__(self):

View File

@ -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()

View File

@ -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

View File

@ -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)

View File

@ -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):

View File

@ -14,7 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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

View File

@ -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:

View File

@ -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):

View File

@ -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:

View File

@ -1,5 +1,3 @@
from __future__ import print_function
from django.core.management.base import BaseCommand
from django.db import connection

View File

@ -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}")

View File

@ -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():

View File

@ -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)

View File

@ -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):

View File

@ -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)

View File

@ -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():

View File

@ -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)

View File

@ -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)

View File

@ -1,5 +1,3 @@
from __future__ import absolute_import
import json
import logging
import os

View File

@ -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)

View File

@ -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):

View File

@ -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']:

View File

@ -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

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models

View File

@ -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):

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)'

View File

@ -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

View File

@ -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

View File

@ -1,4 +1,5 @@
import mock
from unittest import mock
from test_manager import login
from hobo.environment.models import Variable

View File

@ -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)

View File

@ -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

View File

@ -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):

View File

@ -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()

View File

@ -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

View File

@ -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'}

View File

@ -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

View File

@ -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': [

View File

@ -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

View File

@ -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 *

View File

@ -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

Some files were not shown because too many files have changed in this diff Show More