Projet

Général

Profil

0001-agenda-check-all-roles-before-importing-42135.patch

Valentin Deniaud, 18 juin 2020 16:26

Télécharger (3,32 ko)

Voir les différences:

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(-)
chrono/manager/utils.py
16 16

  
17 17
import itertools
18 18

  
19
from django.contrib.auth.models import Group
19 20
from django.db import transaction
20 21
from django.db.models import Q
21 22

  
......
39 40
        Agenda.objects.all().delete()
40 41

  
41 42
    results = {'created': 0, 'updated': 0}
43
    agendas = data.get('agendas', [])
44

  
45
    role_names = {name for data in agendas for _, name in data.get('permissions', {}).items() if name}
46
    existing_roles = Group.objects.filter(name__in=role_names)
47

  
48
    if existing_roles.count() != len(role_names):
49
        existing_roles_names = set(existing_roles.values_list('name', flat=True))
50
        raise AgendaImportError('Missing roles: "%s"' % ', '.join(role_names - existing_roles_names))
51

  
42 52
    with transaction.atomic():
43
        for data in data.get('agendas', []):
53
        for data in agendas:
44 54
            created = Agenda.import_json(data, overwrite=overwrite)
45 55
            if created:
46 56
                results['created'] += 1
tests/test_import_export.py
169 169

  
170 170
    with pytest.raises(AgendaImportError) as excinfo:
171 171
        import_site(json.loads(output), overwrite=True)
172
    assert u'%s' % excinfo.value == u'Missing "gé1" role'
172
    assert 'gé1' in str(excinfo.value) and 'gé2' in str(excinfo.value)
173 173

  
174 174
    group1 = Group(name=u'gé1')
175 175
    group1.save()
176 176
    with pytest.raises(AgendaImportError) as excinfo:
177 177
        import_site(json.loads(output), overwrite=True)
178
    assert u'%s' % excinfo.value == u'Missing "gé2" role'
178
    assert u'%s' % excinfo.value == u'Missing roles: "gé2"'
179 179

  
180 180
    with tempfile.NamedTemporaryFile() as f:
181 181
        f.write(force_bytes(output))
182 182
        f.flush()
183 183
        with pytest.raises(CommandError) as excinfo:
184 184
            call_command('import_site', f.name)
185
        assert u'%s' % excinfo.value == u'Missing "gé2" role'
185
        assert u'%s' % excinfo.value == u'Missing roles: "gé2"'
186 186

  
187 187
    group2 = Group(name=u'gé2')
188 188
    group2.save()
tests/test_manager.py
2956 2956
    resp = resp.click('Import')
2957 2957
    resp.form['agendas_json'] = Upload('export.json', agenda_export, 'application/json')
2958 2958
    resp = resp.form.submit()
2959
    assert u'Missing "gé1" role' in resp.text
2959
    assert u'Missing roles: "gé1"' in resp.text
2960
    del agenda_export_dict['agendas'][0]['permissions']['view']
2960 2961

  
2961 2962
    # missing field
2962 2963
    del agenda_export_dict['agendas'][0]['kind']
2963
-