summaryrefslogtreecommitdiff
path: root/openstack_dashboard/test/unit/api/test_keystone.py
blob: 260aab5dc1ef02c7c5b4b60510742052fe459335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
#    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.
from unittest import mock


from django.test.utils import override_settings
from openstack_dashboard import api
from openstack_dashboard.test import helpers as test


class RoleAPITests(test.APIMockTestCase):
    def setUp(self):
        super(RoleAPITests, self).setUp()
        self.role = self.roles.member
        self.roles = self.roles.list()

    @mock.patch.object(api.keystone, 'keystoneclient')
    def test_remove_tenant_user(self, mock_keystoneclient):
        """Tests api.keystone.remove_tenant_user

        Verifies that remove_tenant_user is called with the right arguments
        after iterating the user's roles.
        """
        keystoneclient = mock_keystoneclient.return_value
        tenant = self.tenants.first()

        keystoneclient.roles.roles_for_user.return_value = self.roles
        keystoneclient.roles.revoke.side_effect = [None for role in self.roles]

        api.keystone.remove_tenant_user(self.request, tenant.id, self.user.id)

        keystoneclient.roles.roles_for_user.assert_called_once_with(
            self.user.id, tenant.id)
        keystoneclient.roles.revoke.assert_has_calls(
            [mock.call(role.id,
                       domain=None,
                       group=None,
                       project=tenant.id,
                       user=self.user.id)
             for role in self.roles]
        )

    @mock.patch.object(api.keystone, 'keystoneclient')
    def test_get_default_role(self, mock_keystoneclient):
        keystoneclient = mock_keystoneclient.return_value
        keystoneclient.roles.list.return_value = self.roles

        role = api.keystone.get_default_role(self.request)
        self.assertEqual(self.role, role)

        # Verify that a second call doesn't hit the API again,
        # so we use assert_called_once_with() here.
        role = api.keystone.get_default_role(self.request)
        keystoneclient.roles.list.assert_called_once_with()


class ServiceAPITests(test.APIMockTestCase):
    @override_settings(OPENSTACK_ENDPOINT_TYPE='internalURL')
    def test_service_wrapper_for_internal_endpoint_type(self):
        catalog = self.service_catalog
        identity_data = api.base.get_service_from_catalog(catalog, "identity")
        # 'Service' class below requires 'id', so populate it here.
        identity_data['id'] = 1
        service = api.keystone.Service(identity_data, "RegionOne")
        self.assertEqual(u"identity (native backend)", str(service))
        self.assertEqual("RegionOne", service.region)
        self.assertEqual("http://int.keystone.example.com/identity/v3",
                         service.url)
        self.assertEqual("http://public.keystone.example.com/identity/v3",
                         service.public_url)
        self.assertEqual("int.keystone.example.com", service.host)

    @override_settings(OPENSTACK_ENDPOINT_TYPE='publicURL')
    def test_service_wrapper_for_public_endpoint_type(self):
        catalog = self.service_catalog
        identity_data = api.base.get_service_from_catalog(catalog, "identity")
        # 'Service' class below requires 'id', so populate it here.
        identity_data['id'] = 1
        service = api.keystone.Service(identity_data, "RegionOne")
        self.assertEqual(u"identity (native backend)", str(service))
        self.assertEqual("RegionOne", service.region)
        self.assertEqual("http://public.keystone.example.com/identity/v3",
                         service.url)
        self.assertEqual("http://public.keystone.example.com/identity/v3",
                         service.public_url)
        self.assertEqual("public.keystone.example.com", service.host)

    @override_settings(OPENSTACK_ENDPOINT_TYPE='internalURL')
    def test_service_wrapper_in_region_for_internal_endpoint_type(self):
        catalog = self.service_catalog
        compute_data = api.base.get_service_from_catalog(catalog, "compute")
        # 'Service' class below requires 'id', so populate it here.
        compute_data['id'] = 1
        service = api.keystone.Service(compute_data, 'RegionTwo')
        self.assertEqual(u"compute", str(service))
        self.assertEqual("RegionTwo", service.region)
        self.assertEqual("http://int.nova2.example.com:8774/v2",
                         service.url)
        self.assertEqual("http://public.nova2.example.com:8774/v2",
                         service.public_url)
        self.assertEqual("int.nova2.example.com", service.host)

    @override_settings(OPENSTACK_ENDPOINT_TYPE='publicURL')
    def test_service_wrapper_service_in_region_for_public_endpoint_type(self):
        catalog = self.service_catalog
        compute_data = api.base.get_service_from_catalog(catalog, "compute")
        # 'Service' class below requires 'id', so populate it here.
        compute_data['id'] = 1
        service = api.keystone.Service(compute_data, 'RegionTwo')
        self.assertEqual(u"compute", str(service))
        self.assertEqual("RegionTwo", service.region)
        self.assertEqual("http://public.nova2.example.com:8774/v2",
                         service.url)
        self.assertEqual("http://public.nova2.example.com:8774/v2",
                         service.public_url)
        self.assertEqual("public.nova2.example.com", service.host)


class APIVersionTests(test.APIMockTestCase):
    @mock.patch.object(api.keystone, 'keystoneclient')
    def test_get_identity_api_version(self, mock_keystoneclient):
        keystoneclient = mock_keystoneclient.return_value
        endpoint_data = mock.Mock()
        endpoint_data.api_version = (3, 10)
        keystoneclient.session.get_endpoint_data.return_value = endpoint_data
        api_version = api.keystone.get_identity_api_version(self.request)
        keystoneclient.session.get_endpoint_data.assert_called_once_with(
            service_type='identity')
        self.assertEqual((3, 10), api_version)