def test_permissions_get_power_of_two_exist(self): """ Test getting the permission enum member if the permission integer is a power of two and the enum member exists. Expected result: The corresponding enum member is returned. """ role = Role('Administrator') role._permissions = Permission.EditRole.value self.assertEqual(Permission.EditRole, role.permissions) role._permissions = Permission.EditUser.value self.assertEqual(Permission.EditUser, role.permissions)
def test_permissions_get_less_than_zero(self): """ Test getting the permission enum member if the permission integer is < 0. Expected result: The empty permission. """ role = Role('Administrator') role._permissions = -1 self.assertEqual(Permission(0), role.permissions)
def test_permissions_get_combination_exists(self): """ Test getting the permission enum member if the permission integer is a combination of two existing enum members. Expected result: The corresponding enum member is returned. """ role = Role('Administrator') role._permissions = (Permission.EditRole | Permission.EditUser).value self.assertEqual(Permission.EditRole | Permission.EditUser, role.permissions)
def test_has_permissions_one_of_multiple_permissions(self): """ Test the has_permissions_one_of() method if a role has the requested permission (and others). Expected result: `True` when requesting the set permissions. """ role = Role('Administrator') role._permissions = (Permission.EditRole | Permission.EditUser).value self.assertTrue(role.has_permissions_one_of(Permission.EditRole)) self.assertTrue(role.has_permissions_one_of(Permission.EditUser)) self.assertTrue(role.has_permissions_one_of(Permission.EditRole, Permission.EditUser, Permission(0)))
def test_has_permissions_one_of_single_permission(self): """ Test the has_permissions_one_of() method if a role has the request permission (and only this one). Expected result: `True` when requesting this permission, `False` otherwise. """ role = Role('Administrator') role._permissions = Permission.EditRole.value self.assertTrue(role.has_permissions_one_of(Permission.EditRole)) self.assertFalse(role.has_permissions_one_of(Permission.EditUser)) # If a combination of multiple permissions is requested, the role does not have this permission. self.assertFalse(role.has_permissions_one_of(Permission.EditRole | Permission.EditUser)) role._permissions = Permission.EditUser.value self.assertFalse(role.has_permissions_one_of(Permission.EditRole)) self.assertTrue(role.has_permissions_one_of(Permission.EditUser)) # If a combination of multiple permissions is requested, the role does not have this permission. self.assertFalse(role.has_permissions_one_of(Permission.EditRole | Permission.EditUser))
def test_permissions_set_permission(self): """ Test setting permissions with giving a permission. Expected result: The permission is set, overwriting previous values. """ role = Role('Administrator') role._permissions = Permission.EditRole.value self.assertEqual(Permission.EditRole, role.permissions) role.permissions = Permission.EditUser self.assertEqual(Permission.EditUser.value, role._permissions)
def test_permissions_set_none(self): """ Test setting permissions without giving a permission. Expected result: The empty permission is set. """ role = Role('Administrator') role._permissions = Permission.EditRole.value self.assertEqual(Permission.EditRole, role.permissions) role.permissions = None self.assertEqual(Permission(0).value, role._permissions)
def test_permissions_get_combination_not_exists(self): """ Test getting the permission enum member if the permission integer is not a power of two and the enum member does not exist. Expected result: The corresponding enum member is returned. """ role = Role('Administrator') # Choose a value that is high enough to be very unlikely to exist as a permission. role._permissions = (2 ** 64) + (2 ** 63) with self.assertRaises(ValueError): permission = role.permissions self.assertIsNone(permission)
def test_has_permissions_one_of_empty_permission(self): """ Test the has_permissions_one_of() method with the empty permission. Expected result: `False`. """ role = Role('Administrator') db.session.add(role) db.session.commit() self.assertEqual(0, role._permissions) self.assertFalse(role.has_permissions_one_of(Permission(0))) role._permissions = Permission.EditRole.value self.assertFalse(role.has_permissions_one_of(Permission(0)))
def test_permissions_get_combination_not_exists(self): """ Test getting the permission enum member if the permission integer is not a power of two and the enum member does not exist. Expected result: The corresponding enum member is returned. """ role = Role('Administrator') # Choose a value that is high enough to be very unlikely to exist as a permission. role._permissions = (2**64) + (2**63) with self.assertRaises(ValueError): permission = role.permissions self.assertIsNone(permission)
def test_has_permissions_all_multiple_permissions(self): """ Test the has_permissions_all() method if a role has the requested permission (and others). Expected result: `True` when requesting the set permissions. """ role = Role('Administrator') role._permissions = (Permission.EditRole | Permission.EditUser).value self.assertTrue(role.has_permissions_all(Permission.EditRole)) self.assertTrue(role.has_permissions_all(Permission.EditUser)) self.assertTrue( role.has_permissions_all(Permission.EditRole, Permission.EditUser, Permission(0)))
def test_has_permission_single_permission(self): """ Test the has_permission() method if a role has the request permission (and only this one). Expected result: `True` when requesting this permission, `False` otherwise. """ role = Role('Administrator') role._permissions = Permission.EditRole.value self.assertTrue(role.has_permission(Permission.EditRole)) self.assertFalse(role.has_permission(Permission.EditUser)) # If a combination of multiple permissions is requested, the role does not have this permission. self.assertFalse( role.has_permission(Permission.EditRole | Permission.EditUser)) role._permissions = Permission.EditUser.value self.assertFalse(role.has_permission(Permission.EditRole)) self.assertTrue(role.has_permission(Permission.EditUser)) # If a combination of multiple permissions is requested, the role does not have this permission. self.assertFalse( role.has_permission(Permission.EditRole | Permission.EditUser))