0001-add-command-to-create-authenticator-admin-role.patch
src/authentic2/management/commands/create_authenticators_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.apps.authenticators.models import BaseAuthenticator |
|
11 | ||
12 |
logger = logging.getLogger(__name__) |
|
13 | ||
14 | ||
15 |
class Command(BaseCommand): |
|
16 |
help = 'Create authenticators 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(BaseAuthenticator) |
|
27 |
role = Role.objects.get_admin_role( |
|
28 |
instance=content_type, |
|
29 |
slug='_a2-manager-of-authenticators', |
|
30 |
name=_('Manager of authenticators'), |
|
31 |
update_name=True, |
|
32 |
update_slug=True, |
|
33 |
create=True, |
|
34 |
) |
|
35 |
role.add_child(admin_role) |
tests/test_commands.py | ||
---|---|---|
476 | 476 |
call_command('clean-user-exports') |
477 | 477 |
with pytest.raises(webtest.app.AppError): |
478 | 478 |
resp.click('Download CSV') |
479 | ||
480 | ||
481 |
def test_create_authenticators_admin_role(db): |
|
482 |
from django.core.management.sql import emit_post_migrate_signal |
|
483 | ||
484 |
role = Role.objects.get(slug='_a2-manager-of-authenticators') |
|
485 | ||
486 |
# remove role created at test database setup |
|
487 |
Role.objects.get(slug='_a2-manager-of-authenticators').delete() |
|
488 | ||
489 |
# create role through command |
|
490 |
call_command('create_authenticators_admin_role') |
|
491 |
role = Role.objects.get(slug='_a2-manager-of-authenticators') |
|
492 |
assert role.permissions.count() == 1 |
|
493 | ||
494 |
# check post_migrate update role |
|
495 |
emit_post_migrate_signal(verbosity=0, interactive=False, db='default', created_models=[]) |
|
496 |
role_after_migrate = Role.objects.get(slug='_a2-manager-of-authenticators') |
|
497 |
assert role.pk == role_after_migrate.pk |
|
498 |
assert role.uuid == role_after_migrate.uuid |
|
499 |
assert role.permissions.count() == 2 |
|
479 |
- |