def test_user_has_permission(self): resolver = ActionAliasPermissionsResolver() # Admin user, should always return true user_db = self.users['admin'] self.assertTrue(resolver.user_has_permission(user_db=user_db, permission_type=PermissionType.ACTION_ALIAS_LIST)) # Observer, should always return true for VIEW permissions user_db = self.users['observer'] self.assertTrue(resolver.user_has_permission(user_db=user_db, permission_type=PermissionType.ACTION_ALIAS_LIST)) # No roles, should return false for everything user_db = self.users['no_roles'] self.assertFalse(resolver.user_has_permission(user_db=user_db, permission_type=PermissionType.ACTION_ALIAS_LIST)) # Custom role with no permission grants, should return false for everything user_db = self.users['1_custom_role_no_permissions'] self.assertFalse(resolver.user_has_permission(user_db=user_db, permission_type=PermissionType.ACTION_ALIAS_LIST)) # Custom role with "action_list" grant user_db = self.users['alias_list_grant'] self.assertTrue(resolver.user_has_permission(user_db=user_db, permission_type=PermissionType.ACTION_ALIAS_LIST))
def test_user_has_resource_db_permission(self): resolver = ActionAliasPermissionsResolver() all_permission_types = PermissionType.get_valid_permissions_for_resource_type( ResourceType.ACTION_ALIAS) # Admin user, should always return true resource_db = self.resources['alias_1'] user_db = self.users['admin'] self.assertUserHasResourceDbPermissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types) # Observer, should always return true for VIEW permission user_db = self.users['observer'] self.assertUserHasResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_VIEW) self.assertUserHasResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_VIEW) self.assertUserDoesntHaveResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_MODIFY) self.assertUserDoesntHaveResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_DELETE) # No roles, should return false for everything user_db = self.users['no_roles'] self.assertUserDoesntHaveResourceDbPermissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types) # Custom role with no permission grants, should return false for everything user_db = self.users['1_custom_role_no_permissions'] self.assertUserDoesntHaveResourceDbPermissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types) # Custom role with unrelated permission grant to parent pack user_db = self.users['alias_pack_grant'] self.assertUserDoesntHaveResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_DELETE) self.assertUserDoesntHaveResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_MODIFY) # Custom role with with grant on the parent pack user_db = self.users['alias_pack_grant'] self.assertUserHasResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_VIEW) self.assertUserHasResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_VIEW) self.assertUserDoesntHaveResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_DELETE) # Custom role with a direct grant on alias user_db = self.users['alias_grant'] self.assertUserHasResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_3'], permission_type=PermissionType.ACTION_ALIAS_VIEW) self.assertUserDoesntHaveResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_MODIFY) self.assertUserDoesntHaveResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=self.resources['alias_3'], permission_type=PermissionType.ACTION_ALIAS_MODIFY) # Custom role - "action_alias_all" grant on the parent pack user_db = self.users['pack_alias_all_grant'] resource_db = self.resources['alias_1'] self.assertUserHasResourceDbPermissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types) # Custom role - "action_alias_all" grant on the alias user_db = self.users['alias_all_grant'] resource_db = self.resources['alias_1'] self.assertUserHasResourceDbPermissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types) # Custom role - "action_alias_modify" grant on alias_1 user_db = self.users['alias_modify_grant'] resource_db = self.resources['alias_1'] self.assertUserHasResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_type=PermissionType.ACTION_ALIAS_MODIFY) # "modify" also grants "view" self.assertUserHasResourceDbPermission( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_type=PermissionType.ACTION_ALIAS_VIEW) permission_types = [ PermissionType.ACTION_ALIAS_CREATE, PermissionType.ACTION_ALIAS_DELETE ] self.assertUserDoesntHaveResourceDbPermissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=permission_types)
def test_user_has_resource_api_permission(self): resolver = ActionAliasPermissionsResolver() # Admin user, should always return true user_db = self.users['admin'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertUserHasResourceApiPermission( resolver=resolver, user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE) # Observer, should return false user_db = self.users['observer'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertUserDoesntHaveResourceApiPermission( resolver=resolver, user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE) # No roles, should return false user_db = self.users['no_roles'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertUserDoesntHaveResourceApiPermission( resolver=resolver, user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE) # Custom role with no permission grants, should return false user_db = self.users['1_custom_role_no_permissions'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertUserDoesntHaveResourceApiPermission( resolver=resolver, user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE) # Custom role with "action_alias_create" grant on parent pack user_db = self.users['alias_pack_alias_create_grant'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertUserHasResourceApiPermission( resolver=resolver, user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE) # Custom role with "action_alias_all" grant on the parent pack user_db = self.users['alias_pack_alias_all_grant'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertUserHasResourceApiPermission( resolver=resolver, user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE) # Custom role with "action_alias_create" grant directly on the resource user_db = self.users['alias_alias_create_grant'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertUserHasResourceApiPermission( resolver=resolver, user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE) # Custom role with "action_alias_all" grant directly on the resource user_db = self.users['alias_all_grant'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertUserHasResourceApiPermission( resolver=resolver, user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE)
def test_user_has_resource_db_permission(self): resolver = ActionAliasPermissionsResolver() all_permission_types = PermissionType.get_valid_permissions_for_resource_type( ResourceType.ACTION_ALIAS) # Admin user, should always return true resource_db = self.resources['alias_1'] user_db = self.users['admin'] self.assertTrue(self._user_has_resource_db_permissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types)) # Observer, should always return true for VIEW permission user_db = self.users['observer'] self.assertTrue(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_VIEW)) self.assertTrue(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_VIEW)) self.assertFalse(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_MODIFY)) self.assertFalse(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_DELETE)) # No roles, should return false for everything user_db = self.users['no_roles'] self.assertFalse(self._user_has_resource_db_permissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types)) # Custom role with no permission grants, should return false for everything user_db = self.users['1_custom_role_no_permissions'] self.assertFalse(self._user_has_resource_db_permissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types)) # Custom role with unrelated permission grant to parent pack user_db = self.users['alias_pack_grant'] self.assertFalse(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_DELETE)) self.assertFalse(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_MODIFY)) # Custom role with with grant on the parent pack user_db = self.users['alias_pack_grant'] self.assertTrue(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_1'], permission_type=PermissionType.ACTION_ALIAS_VIEW)) self.assertTrue(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_VIEW)) self.assertFalse(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_DELETE)) # Custom role with a direct grant on alias user_db = self.users['alias_grant'] self.assertTrue(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_3'], permission_type=PermissionType.ACTION_ALIAS_VIEW)) self.assertFalse(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_2'], permission_type=PermissionType.ACTION_ALIAS_MODIFY)) self.assertFalse(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=self.resources['alias_3'], permission_type=PermissionType.ACTION_ALIAS_MODIFY)) # Custom role - "action_alias_all" grant on the parent pack user_db = self.users['pack_alias_all_grant'] resource_db = self.resources['alias_1'] self.assertTrue(self._user_has_resource_db_permissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types)) # Custom role - "action_alias_all" grant on the alias user_db = self.users['alias_all_grant'] resource_db = self.resources['alias_1'] self.assertTrue(self._user_has_resource_db_permissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=all_permission_types)) # Custom role - "action_alias_modify" grant on alias_1 user_db = self.users['alias_modify_grant'] resource_db = self.resources['alias_1'] self.assertTrue(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=resource_db, permission_type=PermissionType.ACTION_ALIAS_MODIFY)) # "modify" also grants "view" self.assertTrue(resolver.user_has_resource_db_permission( user_db=user_db, resource_db=resource_db, permission_type=PermissionType.ACTION_ALIAS_VIEW)) permission_types = [ PermissionType.ACTION_ALIAS_CREATE, PermissionType.ACTION_ALIAS_MODIFY, PermissionType.ACTION_ALIAS_DELETE ] self.assertFalse(self._user_has_resource_db_permissions( resolver=resolver, user_db=user_db, resource_db=resource_db, permission_types=permission_types))
def test_user_has_resource_api_permission(self): resolver = ActionAliasPermissionsResolver() # Admin user, should always return true user_db = self.users['admin'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertTrue(resolver.user_has_resource_api_permission( user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE)) # Observer, should return false user_db = self.users['observer'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertFalse(resolver.user_has_resource_api_permission( user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE)) # No roles, should return false user_db = self.users['no_roles'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertFalse(resolver.user_has_resource_api_permission( user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE)) # Custom role with no permission grants, should return false user_db = self.users['1_custom_role_no_permissions'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertFalse(resolver.user_has_resource_api_permission( user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE)) # Custom role with "action_alias_create" grant on parent pack user_db = self.users['alias_pack_alias_create_grant'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertTrue(resolver.user_has_resource_api_permission( user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE)) # Custom role with "action_alias_all" grant on the parent pack user_db = self.users['alias_pack_alias_all_grant'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertTrue(resolver.user_has_resource_api_permission( user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE)) # Custom role with "action_alias_create" grant directly on the resource user_db = self.users['alias_alias_create_grant'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertTrue(resolver.user_has_resource_api_permission( user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE)) # Custom role with "action_alias_all" grant directly on the resource user_db = self.users['alias_all_grant'] resource_db = self.resources['alias_1'] resource_api = ActionAliasAPI.from_model(resource_db) self.assertTrue(resolver.user_has_resource_api_permission( user_db=user_db, resource_api=resource_api, permission_type=PermissionType.ACTION_ALIAS_CREATE))