<commit_before>#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import mock
import six

from keystoneclient import exceptions as keystone_exceptions

from heat.common import exception
from heat.engine.clients.os import keystone as client
from heat.tests import common


class KeystoneRoleConstraintTest(common.HeatTestCase):

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,),
                         client.KeystoneRoleConstraint.expected_exceptions,
                         "KeystoneRoleConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneRoleConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_role_id.return_value = None
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         'role_1'))

        client_plugin_mock.get_role_id.assert_called_once_with('role_1')


class KeystoneProjectConstraintTest(common.HeatTestCase):

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,),
                         client.KeystoneProjectConstraint.expected_exceptions,
                         "KeystoneProjectConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneProjectConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_project_id.return_value = None
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         'project_1'))

        client_plugin_mock.get_project_id.assert_called_once_with('project_1')


class KeystoneGroupConstraintTest(common.HeatTestCase):

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,),
                         client.KeystoneGroupConstraint.expected_exceptions,
                         "KeystoneGroupConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneGroupConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_group_id.return_value = None
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         'group_1'))

        client_plugin_mock.get_group_id.assert_called_once_with('group_1')


class KeystoneDomainConstraintTest(common.HeatTestCase):

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,),
                         client.KeystoneDomainConstraint.expected_exceptions,
                         "KeystoneDomainConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneDomainConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_domain_id.return_value = None
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         'domain_1'))

        client_plugin_mock.get_domain_id.assert_called_once_with('domain_1')


class KeystoneServiceConstraintTest(common.HeatTestCase):

    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd151'

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,
                          exception.KeystoneServiceNameConflict,),
                         client.KeystoneServiceConstraint.expected_exceptions,
                         "KeystoneServiceConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneServiceConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_service_id.return_value = self.sample_uuid
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         self.sample_uuid))

        client_plugin_mock.get_service_id.assert_called_once_with(
            self.sample_uuid
        )


class KeystoneClientPluginServiceTest(common.HeatTestCase):

    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
    sample_name = 'sample_service'

    def _get_mock_service(self):
        srv = mock.MagicMock()
        srv.id = self.sample_uuid
        srv.name = self.sample_name
        return srv

    def setUp(self):
        super(KeystoneClientPluginServiceTest, self).setUp()
        self._client = mock.MagicMock()
        self._client.client = mock.MagicMock()
        self._client.client.services = mock.MagicMock()

    @mock.patch.object(client.KeystoneClientPlugin, 'client')
    def test_get_service_id(self, client_keystone):

        self._client.client.services.get.return_value = (self
                                                         ._get_mock_service())

        client_keystone.return_value = self._client
        client_plugin = client.KeystoneClientPlugin(
            context=mock.MagicMock()
        )

        self.assertEqual(self.sample_uuid,
                         client_plugin.get_service_id(self.sample_uuid))

    @mock.patch.object(client.KeystoneClientPlugin, 'client')
    def test_get_service_id_with_name(self, client_keystone):
        self._client.client.services.get.side_effect = (keystone_exceptions
                                                        .NotFound)
        self._client.client.services.list.return_value = [
            self._get_mock_service()
        ]

        client_keystone.return_value = self._client
        client_plugin = client.KeystoneClientPlugin(
            context=mock.MagicMock()
        )

        self.assertEqual(self.sample_uuid,
                         client_plugin.get_service_id(self.sample_name))

    @mock.patch.object(client.KeystoneClientPlugin, 'client')
    def test_get_service_id_with_name_conflict(self, client_keystone):
        self._client.client.services.get.side_effect = (keystone_exceptions
                                                        .NotFound)
        self._client.client.services.list.return_value = [
            self._get_mock_service(),
            self._get_mock_service()
        ]

        client_keystone.return_value = self._client
        client_plugin = client.KeystoneClientPlugin(
            context=mock.MagicMock()
        )

        ex = self.assertRaises(exception.KeystoneServiceNameConflict,
                               client_plugin.get_service_id,
                               self.sample_name)
        msg = ("Keystone has more than one service with same name "
               "%s. Please use service id instead of name" %
               self.sample_name)
        self.assertEqual(msg, six.text_type(ex))

    @mock.patch.object(client.KeystoneClientPlugin, 'client')
    def test_get_service_id_not_found(self, client_keystone):
        self._client.client.services.get.side_effect = (keystone_exceptions
                                                        .NotFound)
        self._client.client.services.list.return_value = [
        ]

        client_keystone.return_value = self._client
        client_plugin = client.KeystoneClientPlugin(
            context=mock.MagicMock()
        )

        ex = self.assertRaises(exception.EntityNotFound,
                               client_plugin.get_service_id,
                               self.sample_name)
        msg = ("The KeystoneService (%(name)s) could not be found." %
               {'name': self.sample_name})
        self.assertEqual(msg, six.text_type(ex))


class KeystoneClientPluginRoleTest(common.HeatTestCase):

    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
    sample_name = 'sample_role'

    def _get_mock_role(self):
        role = mock.MagicMock()
        role.id = self.sample_uuid
        role.name = self.sample_name<commit_msg>Update unit test case for Keystone client plug-in for service<commit_after>#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import mock
import six

from keystoneclient import exceptions as keystone_exceptions

from heat.common import exception
from heat.engine.clients.os import keystone as client
from heat.tests import common


class KeystoneRoleConstraintTest(common.HeatTestCase):

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,),
                         client.KeystoneRoleConstraint.expected_exceptions,
                         "KeystoneRoleConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneRoleConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_role_id.return_value = None
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         'role_1'))

        client_plugin_mock.get_role_id.assert_called_once_with('role_1')


class KeystoneProjectConstraintTest(common.HeatTestCase):

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,),
                         client.KeystoneProjectConstraint.expected_exceptions,
                         "KeystoneProjectConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneProjectConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_project_id.return_value = None
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         'project_1'))

        client_plugin_mock.get_project_id.assert_called_once_with('project_1')


class KeystoneGroupConstraintTest(common.HeatTestCase):

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,),
                         client.KeystoneGroupConstraint.expected_exceptions,
                         "KeystoneGroupConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneGroupConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_group_id.return_value = None
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         'group_1'))

        client_plugin_mock.get_group_id.assert_called_once_with('group_1')


class KeystoneDomainConstraintTest(common.HeatTestCase):

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,),
                         client.KeystoneDomainConstraint.expected_exceptions,
                         "KeystoneDomainConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneDomainConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_domain_id.return_value = None
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         'domain_1'))

        client_plugin_mock.get_domain_id.assert_called_once_with('domain_1')


class KeystoneServiceConstraintTest(common.HeatTestCase):

    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd151'

    def test_expected_exceptions(self):
        self.assertEqual((exception.EntityNotFound,
                          exception.KeystoneServiceNameConflict,),
                         client.KeystoneServiceConstraint.expected_exceptions,
                         "KeystoneServiceConstraint expected exceptions error")

    def test_constrain(self):
        constrain = client.KeystoneServiceConstraint()
        client_mock = mock.MagicMock()
        client_plugin_mock = mock.MagicMock()
        client_plugin_mock.get_service_id.return_value = self.sample_uuid
        client_mock.client_plugin.return_value = client_plugin_mock

        self.assertIsNone(constrain.validate_with_client(client_mock,
                                                         self.sample_uuid))

        client_plugin_mock.get_service_id.assert_called_once_with(
            self.sample_uuid
        )


class KeystoneClientPluginServiceTest(common.HeatTestCase):

    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
    sample_name = 'sample_service'

    def _get_mock_service(self):
        srv = mock.MagicMock()
        srv.id = self.sample_uuid
        srv.name = self.sample_name
        return srv

    def setUp(self):
        super(KeystoneClientPluginServiceTest, self).setUp()
        self._client = mock.MagicMock()

    @mock.patch.object(client.KeystoneClientPlugin, 'client')
    def test_get_service_id(self, client_keystone):

        self._client.client.services.get.return_value = (self
                                                         ._get_mock_service())

        client_keystone.return_value = self._client
        client_plugin = client.KeystoneClientPlugin(
            context=mock.MagicMock()
        )

        self.assertEqual(self.sample_uuid,
                         client_plugin.get_service_id(self.sample_uuid))
        self._client.client.services.get.assert_called_once_with(
            self.sample_uuid)

    @mock.patch.object(client.KeystoneClientPlugin, 'client')
    def test_get_service_id_with_name(self, client_keystone):
        self._client.client.services.get.side_effect = (keystone_exceptions
                                                        .NotFound)
        self._client.client.services.list.return_value = [
            self._get_mock_service()
        ]

        client_keystone.return_value = self._client
        client_plugin = client.KeystoneClientPlugin(
            context=mock.MagicMock()
        )

        self.assertEqual(self.sample_uuid,
                         client_plugin.get_service_id(self.sample_name))
        self.assertRaises(keystone_exceptions.NotFound,
                          self._client.client.services.get,
                          self.sample_name)
        self._client.client.services.list.assert_called_once_with(
            name=self.sample_name)

    @mock.patch.object(client.KeystoneClientPlugin, 'client')
    def test_get_service_id_with_name_conflict(self, client_keystone):
        self._client.client.services.get.side_effect = (keystone_exceptions
                                                        .NotFound)
        self._client.client.services.list.return_value = [
            self._get_mock_service(),
            self._get_mock_service()
        ]

        client_keystone.return_value = self._client
        client_plugin = client.KeystoneClientPlugin(
            context=mock.MagicMock()
        )

        ex = self.assertRaises(exception.KeystoneServiceNameConflict,
                               client_plugin.get_service_id,
                               self.sample_name)
        msg = ("Keystone has more than one service with same name "
               "%s. Please use service id instead of name" %
               self.sample_name)
        self.assertEqual(msg, six.text_type(ex))
        self.assertRaises(keystone_exceptions.NotFound,
                          self._client.client.services.get,
                          self.sample_name)
        self._client.client.services.list.assert_called_once_with(
            name=self.sample_name)

    @mock.patch.object(client.KeystoneClientPlugin, 'client')
    def test_get_service_id_not_found(self, client_keystone):
        self._client.client.services.get.side_effect = (keystone_exceptions
                                                        .NotFound)
        self._client.client.services.list.return_value = [
        ]

        client_keystone.return_value = self._client
        client_plugin = client.KeystoneClientPlugin(
            context=mock.MagicMock()
        )

        ex = self.assertRaises(exception.EntityNotFound,
                               client_plugin.get_service_id,
                               self.sample_name)
        msg = ("The KeystoneService (%(name)s) could not be found." %
               {'name': self.sample_name})
        self.assertEqual(msg, six.text_type(ex))
        self.assertRaises(keystone_exceptions.NotFound,
                          self._client.client.services.get,
                          self.sample_name)
        self._client.client.services.list.assert_called_once_with(
            name=self.sample_name)


class KeystoneClientPluginRoleTest(common.HeatTestCase):

    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
    sample_name = 'sample_role'

    def _get_mock_role(self):
        role = mock.MagicMock()
        role.id = self.sample_uuid
        role.name = self.sample_name
{'fraction_non_alphanumeric': 0.07399042856374684, 'fraction_numerical': 0.008065817067268914, 'mean_word_length': 2.336562612127736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1345919', 'n_tokens_mistral': 5119, 'n_tokens_neox': 4772, 'n_words': 771}