def test_bitwise_or_failure(self): """ Test the bitwise or method with an invalid permission. Expected result: The method raises an error. """ with self.assertRaises(ValueError) as exception_cm: # noinspection PyTypeChecker Permission.bitwise_or(Permission.EditGlobalSettings, None, Permission.EditRole) self.assertEqual('None is not a valid permission', str(exception_cm.exception))
def test_bitwise_or_success(self): """ Test the bitwise or method. Expected result: The method returns the same result as performing the operation manually. """ expectation = Permission.EditRole result = Permission.bitwise_or(Permission.EditRole) self.assertEqual(expectation, result) expectation = Permission.EditRole | Permission.EditUser result = Permission.bitwise_or(Permission.EditRole, Permission.EditUser) self.assertEqual(expectation, result) expectation = Permission.EditRole | Permission.EditUser | Permission.EditGlobalSettings result = Permission.bitwise_or(Permission.EditRole, Permission.EditUser, Permission.EditGlobalSettings) self.assertEqual(expectation, result)
def load_roles_with_permissions_all(*permissions: Permission) -> List['Role']: """ Get all roles that have all the given permissions. :param permissions: The permissions that the roles must have. :return: A list of roles that have all of the given permissions. """ permission = Permission.bitwise_or(*permissions) raw_value = permission.value return Role.query.filter(Role._permissions.op('&')(raw_value) == raw_value).all()
def has_permissions_all(self, *permissions: Permission) -> bool: """ Determine if the role has all of the given permissions. If the empty permission `Permission(0)` is given, the result will always be `False`. :param permissions: The permissions to check for. :return: `True` if the role has all of the requested permissions, `False` otherwise. """ permission = Permission.bitwise_or(*permissions) if permission.value == 0: return False return permission & self.permissions == permission