Skip to content

Commit

Permalink
resolve bug in testing permission existence check
Browse files Browse the repository at this point in the history
  • Loading branch information
guruofgentoo committed Jun 17, 2019
1 parent 331c532 commit feccb98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion keg_auth/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,11 @@ def setup_class(cls):
cls.user_ent.delete_cascaded()

# 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 flask.current_app.auth_manager.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)

Expand Down
40 changes: 40 additions & 0 deletions keg_auth/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,43 @@ def test_request_loader_user_returned(self):
])

assert get_current_user().id == user.id


class TestViewTestBase:
def test_permission_validated_from_tuple(self):
class Foo(ViewTestBase):
permissions = 'foo'

with mock.patch(
'keg.current_app.auth_manager.permissions', [('foo', 'bar'), ('baz', 'bam')]
):
Foo.setup_class()

def test_permission_validated_from_string(self):
class Foo(ViewTestBase):
permissions = 'foo'

with mock.patch(
'flask.current_app.auth_manager.permissions', ['foo', 'baz']
):
Foo.setup_class()

def test_permission_invalid(self):
class Foo(ViewTestBase):
permissions = 'bar'

with mock.patch(
'flask.current_app.auth_manager.permissions', ['foo', 'baz']
):
with pytest.raises(Exception,
message='permission bar not specified in the auth manager'):
Foo.setup_class()

def test_multiple_permissions_validated(self):
class Foo(ViewTestBase):
permissions = 'foo', 'baz'

with mock.patch(
'flask.current_app.auth_manager.permissions', ['foo', 'baz']
):
Foo.setup_class()

0 comments on commit feccb98

Please sign in to comment.