Projet

Général

Profil

0003-manager-filter-api-client-s-assignable-roles-dependi.patch

Paul Marillonnet, 23 décembre 2022 12:51

Télécharger (4,37 ko)

Voir les différences:

Subject: [PATCH 3/4] manager: filter api client's assignable roles depending
 on its OU (#72703)

 src/authentic2/manager/apiclient_views.py |  7 +++++-
 tests/test_manager_apiclient.py           | 26 +++++++++++++++++++++--
 2 files changed, 30 insertions(+), 3 deletions(-)
src/authentic2/manager/apiclient_views.py
20 20
from django.utils.translation import gettext_lazy as _
21 21
from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView
22 22

  
23
from authentic2.a2_rbac.models import OrganizationalUnit
23
from authentic2.a2_rbac.models import OrganizationalUnit, Role
24 24
from authentic2.manager import forms
25 25
from authentic2.manager.views import MediaMixin, PermissionMixin, TitleMixin
26 26
from authentic2.models import APIClient
......
57 57
            form.fields['ou'].queryset = OrganizationalUnit.objects.filter(id__in=allowed_ous)
58 58
            form.fields['ou'].required = True
59 59
            form.fields['ou'].empty_label = None
60
        api_client = self.object
61
        if api_client and api_client.ou is not None:
62
            form.fields['apiclient_roles'].queryset = Role.objects.filter(ou=api_client.ou).exclude(
63
                slug__startswith='_'
64
            )
60 65
        return form
61 66

  
62 67

  
tests/test_manager_apiclient.py
241 241
    assert delete_button.text() == 'Delete'
242 242

  
243 243

  
244
def test_edit(superuser, app):
244
def test_edit(superuser, app, ou1, ou2):
245
    role_1 = Role.objects.create(name='role-1', ou=ou1)
246
    role_2 = Role.objects.create(name='role-2', ou=ou2)
247
    role_3 = Role.objects.create(name='role-3', ou=ou1)
245 248
    api_client = APIClient.objects.create(
246
        name='foo', description='foo-description', identifier='foo-identifier', password='foo-password'
249
        name='foo',
250
        description='foo-description',
251
        identifier='foo-identifier',
252
        password='foo-password',
253
        ou=ou1,
247 254
    )
248 255
    assert APIClient.objects.count() == 1
249 256
    resp = login(app, superuser, 'a2-manager-api-client-edit', kwargs={'pk': api_client.pk})
......
251 258
    assert form.get('password').value == 'foo-password'
252 259
    assert ('', False, '---------') in form['ou'].options
253 260
    resp.form.set('password', 'easy')
261
    with pytest.raises(KeyError):
262
        # forcing values not presented by the Select2ModelMultipleChoiceField,
263
        # should not happen in UI
264
        form['apiclient_roles'].force_value([role_1.id, role_2.id])
265
        form.submit()
266
    form['apiclient_roles'].force_value([role_1.id, role_3.id])
254 267
    response = form.submit().follow()
255 268
    assert urlparse(response.request.url).path == api_client.get_absolute_url()
256 269
    assert APIClient.objects.count() == 1
......
259 272

  
260 273

  
261 274
def test_edit_local_admin(admin_ou1, app, ou1, ou2):
275
    role_1 = Role.objects.create(name='role-1', ou=ou1)
276
    role_2 = Role.objects.create(name='role-2', ou=ou2)
277
    role_3 = Role.objects.create(name='role-3', ou=ou1)
262 278
    api_client_ou1 = APIClient.objects.create(
263 279
        name='foo',
264 280
        description='foo-description',
......
278 294
    assert form.get('password').value == 'foo-password'
279 295
    resp.form.set('password', 'easy')
280 296
    assert ('', False, '---------') not in form['ou'].options
297
    with pytest.raises(KeyError):
298
        # forcing values not presented by the Select2ModelMultipleChoiceField,
299
        # should not happen in UI
300
        form['apiclient_roles'].force_value([role_1.id, role_2.id])
301
        form.submit()
302
    form['apiclient_roles'].force_value([role_1.id, role_3.id])
281 303
    response = form.submit().follow()
282 304
    assert urlparse(response.request.url).path == api_client_ou1.get_absolute_url()
283 305
    api_client = APIClient.objects.get(password='easy')
284
-