Added tests for the permissions framework
parent
fbc58de27a
commit
a89e2396db
|
@ -7,6 +7,150 @@ import milla.auth
|
|||
import nose.tools
|
||||
|
||||
|
||||
def test_permission_check():
|
||||
'''Ensure Permission.check returns True for lists of strings
|
||||
'''
|
||||
|
||||
perm = milla.auth.permissions.Permission('foo')
|
||||
assert perm.check(['foo'])
|
||||
|
||||
def test_permission_check_false():
|
||||
'''Ensure Permission.check returns False for lists of strings
|
||||
'''
|
||||
|
||||
perm = milla.auth.permissions.Permission('foo')
|
||||
assert not perm.check(['bar'])
|
||||
|
||||
def test_permission_check_perm():
|
||||
'''Ensure Permission.check returns True for lists of Permissions
|
||||
'''
|
||||
|
||||
req = milla.auth.permissions.Permission('foo')
|
||||
perm = milla.auth.permissions.Permission('foo')
|
||||
assert req.check([perm])
|
||||
|
||||
def test_permission_check_perm_false():
|
||||
'''Ensure Permission.check returns True for lists of Permissions
|
||||
'''
|
||||
|
||||
req = milla.auth.permissions.Permission('foo')
|
||||
perm = milla.auth.permissions.Permission('bar')
|
||||
assert not req.check([perm])
|
||||
|
||||
def test_permission_check_container():
|
||||
'''Ensure Permission.check returns True for PermissionContainers of strings
|
||||
'''
|
||||
|
||||
perm = milla.auth.permissions.Permission('foo')
|
||||
container = milla.auth.permissions.PermissionContainer(['foo'])
|
||||
assert perm.check(container)
|
||||
|
||||
def test_permission_check_container_false():
|
||||
'''Ensure Permission.check returns True for PermissionContainers of strings
|
||||
'''
|
||||
|
||||
perm = milla.auth.permissions.Permission('foo')
|
||||
container = milla.auth.permissions.PermissionContainer(['bar'])
|
||||
assert not perm.check(container)
|
||||
|
||||
def test_permission_check_container_perm():
|
||||
'''Ensure Permission.check returns True for PermissionContainers of Permissions
|
||||
'''
|
||||
|
||||
perm = milla.auth.permissions.Permission('foo')
|
||||
req = milla.auth.permissions.Permission('foo')
|
||||
container = milla.auth.permissions.PermissionContainer([perm])
|
||||
assert req.check(container)
|
||||
|
||||
def test_permission_check_container_perm_false():
|
||||
'''Ensure Permission.check returns False for PermissionContainers of Permissions
|
||||
'''
|
||||
|
||||
perm = milla.auth.permissions.Permission('foo')
|
||||
req = milla.auth.permissions.Permission('bar')
|
||||
container = milla.auth.permissions.PermissionContainer([perm])
|
||||
assert not req.check(container)
|
||||
|
||||
def test_permission_container_iter():
|
||||
'''Ensure iterating a PermissionContainer yields all permissions
|
||||
'''
|
||||
|
||||
container = milla.auth.permissions.PermissionContainer(['foo'], ['bar'])
|
||||
assert list(container) == ['foo', 'bar']
|
||||
|
||||
def test_permission_and():
|
||||
'''Ensure AND-ing Permissions returns a PermissionRequirementAll
|
||||
'''
|
||||
|
||||
perm1 = milla.auth.permissions.Permission('foo')
|
||||
perm2 = milla.auth.permissions.Permission('bar')
|
||||
req = perm1 & perm2
|
||||
assert isinstance(req, milla.auth.permissions.PermissionRequirementAll)
|
||||
assert req.requirements == (perm1, perm2)
|
||||
|
||||
def test_permission_or():
|
||||
'''Ensure OR-ing Permissions returns a PermissionRequirementAny
|
||||
'''
|
||||
|
||||
perm1 = milla.auth.permissions.Permission('foo')
|
||||
perm2 = milla.auth.permissions.Permission('bar')
|
||||
req = perm1 | perm2
|
||||
assert isinstance(req, milla.auth.permissions.PermissionRequirementAny)
|
||||
assert req.requirements == (perm1, perm2)
|
||||
|
||||
def test_permission_str():
|
||||
'''Ensure calling str on a Permission returns its name
|
||||
'''
|
||||
|
||||
perm_name = 'foo'
|
||||
perm = milla.auth.permissions.Permission(perm_name)
|
||||
assert str(perm) == perm_name
|
||||
|
||||
def test_permission_eq():
|
||||
'''Ensure two Permissions with the same name are equal but not identical
|
||||
'''
|
||||
|
||||
perm_name = 'foo'
|
||||
perm1 = milla.auth.permissions.Permission(perm_name)
|
||||
perm2 = milla.auth.permissions.Permission(perm_name)
|
||||
assert perm1 == perm2
|
||||
assert perm1 is not perm2
|
||||
|
||||
def test_permission_check_container_group():
|
||||
'''Test group permissions in PermissionContainer objects
|
||||
'''
|
||||
|
||||
perm = milla.auth.permissions.Permission('foo')
|
||||
req = milla.auth.permissions.Permission('foo')
|
||||
container = milla.auth.permissions.PermissionContainer([], [perm])
|
||||
assert req.check(container)
|
||||
|
||||
def test_permissionrequirement_all():
|
||||
'''Ensure PermissionRequirementAll requires all listed permissions
|
||||
'''
|
||||
|
||||
perm1 = milla.auth.permissions.Permission('foo')
|
||||
perm2 = milla.auth.permissions.Permission('bar')
|
||||
req = milla.auth.permissions.PermissionRequirementAll(perm1, perm2)
|
||||
assert req.check(['foo', 'bar'])
|
||||
assert not req.check(['foo'])
|
||||
assert not req.check(['bar'])
|
||||
assert not req.check([])
|
||||
assert not req.check(['baz'])
|
||||
|
||||
def test_permissionrequirement_any():
|
||||
'''Ensure PermissionRequirementAll requires only one permission
|
||||
'''
|
||||
|
||||
perm1 = milla.auth.permissions.Permission('foo')
|
||||
perm2 = milla.auth.permissions.Permission('bar')
|
||||
req = milla.auth.permissions.PermissionRequirementAny(perm1, perm2)
|
||||
assert req.check(['foo'])
|
||||
assert req.check(['bar'])
|
||||
assert req.check(['foo', 'bar'])
|
||||
assert not req.check([])
|
||||
assert not req.check(['baz'])
|
||||
|
||||
def test_exception_callable():
|
||||
'''Ensure that NotAuthorizedException is a valid controller callable
|
||||
'''
|
||||
|
|
Loading…
Reference in New Issue