Projet

Général

Profil

0001-commands-add-api-client-admin-role-creation-command-.patch

Paul Marillonnet, 14 novembre 2022 16:14

Télécharger (3,06 ko)

Voir les différences:

Subject: [PATCH] commands: add api client admin role creation command (#71267)

 .../commands/create_api_clients_admin_role.py | 35 +++++++++++++++++++
 tests/test_commands.py                        | 21 +++++++++++
 2 files changed, 56 insertions(+)
 create mode 100644 src/authentic2/management/commands/create_api_clients_admin_role.py
src/authentic2/management/commands/create_api_clients_admin_role.py
1
import logging
2

  
3
from django.conf import settings
4
from django.contrib.contenttypes.models import ContentType
5
from django.core.management.base import BaseCommand
6
from django.utils import translation
7
from django.utils.translation import gettext_lazy as _
8

  
9
from authentic2.a2_rbac.models import Role
10
from authentic2.models import APIClient
11

  
12
logger = logging.getLogger(__name__)
13

  
14

  
15
class Command(BaseCommand):
16
    help = 'Create API clients admin role'
17

  
18
    def handle(self, *args, **options):
19
        translation.activate(settings.LANGUAGE_CODE)
20

  
21
        try:
22
            admin_role = Role.objects.get(slug='_a2-manager')
23
        except Role.DoesNotExist:
24
            logger.warning('No admin role.')
25

  
26
        content_type = ContentType.objects.get_for_model(APIClient)
27
        role = Role.objects.get_admin_role(
28
            instance=content_type,
29
            slug='_a2-manager-of-api-clients',
30
            name=_('Manager of API clients'),
31
            update_name=True,
32
            update_slug=True,
33
            create=True,
34
        )
35
        role.add_child(admin_role)
tests/test_commands.py
475 475
    call_command('clean-user-exports')
476 476
    with pytest.raises(webtest.app.AppError):
477 477
        resp.click('Download CSV')
478

  
479

  
480
def test_create_api_clients_admin_role(db):
481
    from django.core.management.sql import emit_post_migrate_signal
482

  
483
    role = Role.objects.get(slug='_a2-manager-of-api-clients')
484

  
485
    # remove role created at test database setup
486
    Role.objects.get(slug='_a2-manager-of-api-clients').delete()
487

  
488
    # create role through command
489
    call_command('create_api_clients_admin_role')
490
    role = Role.objects.get(slug='_a2-manager-of-api-clients')
491
    assert role.permissions.count() == 1
492

  
493
    # check post_migrate update role
494
    emit_post_migrate_signal(verbosity=0, interactive=False, db='default', created_models=[])
495
    role_after_migrate = Role.objects.get(slug='_a2-manager-of-api-clients')
496
    assert role.pk == role_after_migrate.pk
497
    assert role.uuid == role_after_migrate.uuid
498
    assert role.permissions.count() == 2
478
-