def test_check_tasks_cancel_acl_realm_no_pool_dimension(self):
        # mock
        self.mock(acl, 'can_edit_all_tasks', lambda: False)

        # call
        with self.assertRaises(auth.AuthorizationError):
            realms.check_tasks_cancel_acl(None)
        self._has_permission_mock.assert_not_called()
    def test_check_tasks_cancel_acl_realm_unknown_pool(self):
        # mock
        self.mock(acl, 'can_edit_all_tasks', lambda: False)
        self.mock(pools_config, 'get_pool_config', lambda _: None)

        # call
        with self.assertRaises(endpoints.BadRequestException):
            realms.check_tasks_cancel_acl(['unknown'])
        self._has_permission_mock.assert_not_called()
    def test_check_tasks_cancel_acl_realm_no_pool_realms(self):
        # mock
        self.mock(acl, 'can_edit_all_tasks', lambda: False)
        get_pool_config = lambda p: _gen_pool_config(realm=None)
        self.mock(pools_config, 'get_pool_config', get_pool_config)

        # call
        with self.assertRaises(auth.AuthorizationError):
            realms.check_tasks_cancel_acl(['pool1', 'pool2'])
        self._has_permission_mock.assert_not_called()
    def test_check_tasks_cancel_acl_realm_allowed(self):
        # mock
        self.mock(acl, 'can_edit_all_tasks', lambda: False)
        get_pool_config = lambda p: _gen_pool_config(realm='test:' + p)
        self.mock(pools_config, 'get_pool_config', get_pool_config)

        # call
        realms.check_tasks_cancel_acl(['pool1', 'pool2'])
        for pool_realm in ['test:pool1', 'test:pool2']:
            self._has_permission_mock.assert_any_call(
                _PERM_POOLS_CANCEL_TASK, [pool_realm],
                identity=auth.get_current_identity())
    def test_check_tasks_cancel_acl_realm_missing_any_permission(self):
        # mock
        self.mock(acl, 'can_edit_all_tasks', lambda: False)
        get_pool_config = lambda p: _gen_pool_config(realm='test:' + p)
        self.mock(pools_config, 'get_pool_config', get_pool_config)
        self._has_permission_mock.side_effect = [True, False]

        # call
        with self.assertRaises(auth.AuthorizationError):
            realms.check_tasks_cancel_acl(['pool1', 'pool2'])
        for pool_realm in ['test:pool1', 'test:pool2']:
            self._has_permission_mock.assert_any_call(
                _PERM_POOLS_CANCEL_TASK, [pool_realm],
                identity=auth.get_current_identity())
    def test_check_tasks_cancel_acl_realm_not_allowed(self):
        # mock
        self.mock(acl, 'can_edit_all_tasks', lambda: False)
        get_pool_config = lambda p: _gen_pool_config(realm='test:' + p)
        self.mock(pools_config, 'get_pool_config', get_pool_config)
        self._has_permission_mock.return_value = False

        # call
        with self.assertRaises(auth.AuthorizationError):
            realms.check_tasks_cancel_acl(['pool1', 'pool2'])
        # it checks only the first realm and fails.
        self._has_permission_mock.assert_any_call(
            _PERM_POOLS_CANCEL_TASK, ['test:pool1'],
            identity=auth.get_current_identity())
    def test_check_tasks_cancel_acl_with_global_permission(self):
        self.mock(acl, 'can_edit_all_tasks', lambda: True)

        realms.check_tasks_cancel_acl(None)
        self._has_permission_mock.assert_not_called()