|
|
@ -1,8 +1,10 @@ |
|
|
|
from django.apps import apps |
|
|
|
from django.contrib.auth.management import _get_all_permissions |
|
|
|
from django.contrib.auth.models import Group, Permission |
|
|
|
from django.contrib.contenttypes.models import ContentType |
|
|
|
from django.core.exceptions import ValidationError |
|
|
|
from django.utils.encoding import force_text |
|
|
|
from django.utils.translation import string_concat |
|
|
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
|
import pytest |
|
|
|
|
|
|
@ -15,10 +17,11 @@ apps_com_permissao_padrao = [ |
|
|
|
'comissoes', 'norma', 'sessao', 'painel'] |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('app_label', apps_com_permissao_padrao) |
|
|
|
def test_grupo_padrao_tem_permissoes_sobre_todo_o_app(app_label): |
|
|
|
def create_perms_post_migrate(app): |
|
|
|
|
|
|
|
app = apps.get_app_config(app_label) |
|
|
|
searched_perms = list() |
|
|
|
# The codenames and ctypes that should exist. |
|
|
|
ctypes = set() |
|
|
|
|
|
|
|
for klass in list(app.get_models()): |
|
|
|
opts = klass._meta |
|
|
@ -32,14 +35,62 @@ def test_grupo_padrao_tem_permissoes_sobre_todo_o_app(app_label): |
|
|
|
_('Visualização dos detalhes de'), ' ', |
|
|
|
opts.verbose_name_plural)), |
|
|
|
) |
|
|
|
opts.permissions = tuple( |
|
|
|
set(list(permissions) + list(opts.permissions))) |
|
|
|
|
|
|
|
if opts.proxy: |
|
|
|
# Force looking up the content types in the current database |
|
|
|
# before creating foreign keys to them. |
|
|
|
app_label, model = opts.app_label, opts.model_name |
|
|
|
|
|
|
|
try: |
|
|
|
ctype = ContentType.objects.get_by_natural_key( |
|
|
|
app_label, model) |
|
|
|
except: |
|
|
|
ctype = ContentType.objects.create( |
|
|
|
app_label=app_label, model=model) |
|
|
|
else: |
|
|
|
ctype = ContentType.objects.get_for_model(klass) |
|
|
|
|
|
|
|
ctypes.add(ctype) |
|
|
|
for perm in _get_all_permissions(klass._meta, ctype): |
|
|
|
searched_perms.append((ctype, perm)) |
|
|
|
|
|
|
|
all_perms = set(Permission.objects.filter( |
|
|
|
content_type__in=ctypes, |
|
|
|
).values_list( |
|
|
|
"content_type", "codename" |
|
|
|
)) |
|
|
|
|
|
|
|
perms = [ |
|
|
|
Permission(codename=codename, name=name, content_type=ct) |
|
|
|
for ct, (codename, name) in searched_perms |
|
|
|
if (ct.pk, codename) not in all_perms |
|
|
|
] |
|
|
|
Permission.objects.bulk_create(perms) |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('app_label', apps_com_permissao_padrao) |
|
|
|
def test_grupo_padrao_tem_permissoes_sobre_todo_o_app(app_label): |
|
|
|
|
|
|
|
app = apps.get_app_config(app_label) |
|
|
|
|
|
|
|
create_perms_post_migrate(app) |
|
|
|
|
|
|
|
# código testado |
|
|
|
cria_grupos_permissoes() |
|
|
|
|
|
|
|
def gerar_permissoes(app): |
|
|
|
for model in app.get_models(): |
|
|
|
for op in ['add', 'change', 'delete', 'list', 'detail']: |
|
|
|
for op in ['add', 'change', 'delete', ]: |
|
|
|
yield model, 'Can %s %s' % (op, model._meta.verbose_name) |
|
|
|
yield model, force_text(string_concat( |
|
|
|
_('Visualizaçao da lista de'), ' ', |
|
|
|
model._meta.verbose_name_plural)) |
|
|
|
yield model, force_text(string_concat( |
|
|
|
_('Visualização dos detalhes de'), |
|
|
|
' ', |
|
|
|
model._meta.verbose_name_plural)) |
|
|
|
grupo = Group.objects.get(name='Operador de %s' % app.verbose_name) |
|
|
|
esperado = set(gerar_permissoes(app)) |
|
|
|
|
|
|
@ -53,18 +104,7 @@ def test_permissoes_extras_sao_apagadas(app_label): |
|
|
|
|
|
|
|
app = apps.get_app_config(app_label) |
|
|
|
|
|
|
|
for klass in list(app.get_models()): |
|
|
|
opts = klass._meta |
|
|
|
permissions = ( |
|
|
|
("list_" + opts.model_name, |
|
|
|
string_concat( |
|
|
|
_('Visualizaçao da lista de'), ' ', |
|
|
|
opts.verbose_name_plural)), |
|
|
|
("detail_" + opts.model_name, |
|
|
|
string_concat( |
|
|
|
_('Visualização dos detalhes de'), ' ', |
|
|
|
opts.verbose_name_plural)), |
|
|
|
) |
|
|
|
# create_perms_post_migrate(app) |
|
|
|
|
|
|
|
grupo = Group.objects.create(name='Operador de %s' % app.verbose_name) |
|
|
|
|
|
|
|