From a11eed62521b1738606dfe029cea64995d63c777 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 18 Jun 2020 16:20:18 +0200 Subject: [PATCH] agenda: check all roles before importing (#42135) --- chrono/manager/utils.py | 12 +++++++++++- tests/test_import_export.py | 6 +++--- tests/test_manager.py | 3 ++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/chrono/manager/utils.py b/chrono/manager/utils.py index 82bc981..36fa663 100644 --- a/chrono/manager/utils.py +++ b/chrono/manager/utils.py @@ -16,6 +16,7 @@ import itertools +from django.contrib.auth.models import Group from django.db import transaction from django.db.models import Q @@ -39,8 +40,17 @@ def import_site(data, if_empty=False, clean=False, overwrite=False): Agenda.objects.all().delete() results = {'created': 0, 'updated': 0} + agendas = data.get('agendas', []) + + role_names = {name for data in agendas for _, name in data.get('permissions', {}).items() if name} + existing_roles = Group.objects.filter(name__in=role_names) + + if existing_roles.count() != len(role_names): + existing_roles_names = set(existing_roles.values_list('name', flat=True)) + raise AgendaImportError('Missing roles: "%s"' % ', '.join(role_names - existing_roles_names)) + with transaction.atomic(): - for data in data.get('agendas', []): + for data in agendas: created = Agenda.import_json(data, overwrite=overwrite) if created: results['created'] += 1 diff --git a/tests/test_import_export.py b/tests/test_import_export.py index 4c37dc1..ef7a137 100644 --- a/tests/test_import_export.py +++ b/tests/test_import_export.py @@ -169,20 +169,20 @@ def test_import_export_permissions(app, some_data, meetings_agenda): with pytest.raises(AgendaImportError) as excinfo: import_site(json.loads(output), overwrite=True) - assert u'%s' % excinfo.value == u'Missing "gé1" role' + assert 'gé1' in str(excinfo.value) and 'gé2' in str(excinfo.value) group1 = Group(name=u'gé1') group1.save() with pytest.raises(AgendaImportError) as excinfo: import_site(json.loads(output), overwrite=True) - assert u'%s' % excinfo.value == u'Missing "gé2" role' + assert u'%s' % excinfo.value == u'Missing roles: "gé2"' with tempfile.NamedTemporaryFile() as f: f.write(force_bytes(output)) f.flush() with pytest.raises(CommandError) as excinfo: call_command('import_site', f.name) - assert u'%s' % excinfo.value == u'Missing "gé2" role' + assert u'%s' % excinfo.value == u'Missing roles: "gé2"' group2 = Group(name=u'gé2') group2.save() diff --git a/tests/test_manager.py b/tests/test_manager.py index 0bfa57b..2d4a3dd 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -2956,7 +2956,8 @@ def test_import_agenda(app, admin_user): resp = resp.click('Import') resp.form['agendas_json'] = Upload('export.json', agenda_export, 'application/json') resp = resp.form.submit() - assert u'Missing "gé1" role' in resp.text + assert u'Missing roles: "gé1"' in resp.text + del agenda_export_dict['agendas'][0]['permissions']['view'] # missing field del agenda_export_dict['agendas'][0]['kind'] -- 2.20.1