Skip to content

Commit

Permalink
centralize validation of permission sets in testing
Browse files Browse the repository at this point in the history
fixes #128
  • Loading branch information
guruofgentoo committed Sep 13, 2021
1 parent 7d72fc3 commit 9f04f1d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
8 changes: 8 additions & 0 deletions keg_auth/core.py
Expand Up @@ -269,6 +269,14 @@ def sync_permissions(app):
if 'permissions' not in str(exc):
raise

def validate_permission_set(self, permissions):
defined_tokens = set(
tolist(perm)[0] for perm in self.permissions
)
undefined_tokens = set(tolist(permissions)) - defined_tokens
if undefined_tokens:
raise Exception(f'permission(s) {undefined_tokens} not specified in the auth manager')

def add_navigation_menu(self, name, menu):
"""Create a navigation menu that may be referenced with the given name."""
self.menus[name] = menu
Expand Down
11 changes: 10 additions & 1 deletion keg_auth/model/__init__.py
Expand Up @@ -128,8 +128,17 @@ def testing_create(cls, **kwargs):

if 'permissions' in kwargs:
perm_cls = registry().permission_cls

# ensure all of the tokens exists
flask.current_app.auth_manager.validate_permission_set(
list(filter(
lambda perm: not isinstance(perm, perm_cls),
tolist(kwargs['permissions'])
))
)

kwargs['permissions'] = [
perm_cls.get_by_token(perm)
perm_cls.testing_create(token=perm)
if not isinstance(perm, perm_cls) else perm
for perm in tolist(kwargs['permissions'])
]
Expand Down
10 changes: 2 additions & 8 deletions keg_auth/testing.py
Expand Up @@ -11,7 +11,7 @@
import passlib
import pytest
import wrapt
from blazeutils import randchars, tolist
from blazeutils import randchars
from blazeutils.containers import LazyDict
from keg import current_app

Expand Down Expand Up @@ -1193,13 +1193,7 @@ def setup_class(cls):
cls.permission_ent = flask.current_app.auth_manager.entity_registry.permission_cls

# ensure all of the tokens exists
defined_perms = set(
tolist(perm)[0] for perm in flask.current_app.auth_manager.permissions
)
for perm in tolist(cls.permissions):
if perm not in defined_perms:
raise Exception('permission {} not specified in the auth manager'.format(perm))
cls.permission_ent.testing_create(token=perm)
flask.current_app.auth_manager.validate_permission_set(cls.permissions)

cls.current_user = cls.create_user()
cls.setup_user()
Expand Down
2 changes: 1 addition & 1 deletion keg_auth/tests/test_views.py
Expand Up @@ -1264,7 +1264,7 @@ class Foo(ViewTestBase):
'flask.current_app.auth_manager.permissions', ['foo', 'baz']
):
with pytest.raises(
Exception, match='permission bar not specified in the auth manager'
Exception, match=r"permission\(s\) \{'bar'\} not specified in the auth manager"
):
Foo.setup_class()

Expand Down
2 changes: 2 additions & 0 deletions keg_auth_ta/extensions.py
Expand Up @@ -16,6 +16,8 @@ class Grid(webgrid.BaseGrid):

permissions = (
'auth-manage',
'permission1',
'permission2',
)

auth_entity_registry = AuthEntityRegistry()
Expand Down

0 comments on commit 9f04f1d

Please sign in to comment.