Debian Codegraph @

keystoneclient/tests/unit/v3/test_client.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import copy
14 import uuid
15
16 from oslo_serialization import jsonutils
17
18 from keystoneauth1 import session as auth_session
19 from keystoneclient.auth import token_endpoint
20 from keystoneclient import exceptions
21 from keystoneclient import session
22 from keystoneclient.tests.unit.v3 import client_fixtures
23 from keystoneclient.tests.unit.v3 import utils
24 from keystoneclient.v3 import client
25
26
27 class KeystoneClientTest(utils.TestCase):
28
29 def test_unscoped_init(self):
30 token = client_fixtures.unscoped_token()
31 self.stub_auth(json=token)
32
33 # Creating a HTTPClient not using session is deprecated.
34 with self.deprecations.expect_deprecations_here():
35 c = client.Client(user_domain_name=token.user_domain_name,
36 username=token.user_name,
37 password='password',
38 auth_url=self.TEST_URL)
39 self.assertIsNotNone(c.auth_ref)
40 self.assertFalse(c.auth_ref.domain_scoped)
41 self.assertFalse(c.auth_ref.project_scoped)
42 self.assertEqual(token.user_id, c.auth_user_id)
43 self.assertFalse(c.has_service_catalog())
44
45 self.assertEqual(token.user_id, c.get_user_id(session=None))
46 self.assertIsNone(c.get_project_id(session=None))
47
48 def test_domain_scoped_init(self):
49 token = client_fixtures.domain_scoped_token()
50 self.stub_auth(json=token)
51
52 # Creating a HTTPClient not using session is deprecated.
53 with self.deprecations.expect_deprecations_here():
54 c = client.Client(user_id=token.user_id,
55 password='password',
56 domain_name=token.domain_name,
57 auth_url=self.TEST_URL)
58 self.assertIsNotNone(c.auth_ref)
59 self.assertTrue(c.auth_ref.domain_scoped)
60 self.assertFalse(c.auth_ref.project_scoped)
61 self.assertEqual(token.user_id, c.auth_user_id)
62 self.assertEqual(token.domain_id, c.auth_domain_id)
63
64 def test_project_scoped_init(self):
65 token = client_fixtures.project_scoped_token()
66 self.stub_auth(json=token),
67
68 # Creating a HTTPClient not using session is deprecated.
69 with self.deprecations.expect_deprecations_here():
70 c = client.Client(user_id=token.user_id,
71 password='password',
72 user_domain_name=token.user_domain_name,
73 project_name=token.project_name,
74 auth_url=self.TEST_URL)
75 self.assertIsNotNone(c.auth_ref)
76 self.assertFalse(c.auth_ref.domain_scoped)
77 self.assertTrue(c.auth_ref.project_scoped)
78 self.assertEqual(token.user_id, c.auth_user_id)
79 self.assertEqual(token.project_id, c.auth_tenant_id)
80 self.assertEqual(token.user_id, c.get_user_id(session=None))
81 self.assertEqual(token.project_id, c.get_project_id(session=None))
82
83 def test_auth_ref_load(self):
84 token = client_fixtures.project_scoped_token()
85 self.stub_auth(json=token)
86
87 # Creating a HTTPClient not using session is deprecated.
88 with self.deprecations.expect_deprecations_here():
89 c = client.Client(user_id=token.user_id,
90 password='password',
91 project_id=token.project_id,
92 auth_url=self.TEST_URL)
93 cache = jsonutils.dumps(c.auth_ref)
94 # Creating a HTTPClient not using session is deprecated.
95 with self.deprecations.expect_deprecations_here():
96 new_client = client.Client(auth_ref=jsonutils.loads(cache))
97 self.assertIsNotNone(new_client.auth_ref)
98 self.assertFalse(new_client.auth_ref.domain_scoped)
99 self.assertTrue(new_client.auth_ref.project_scoped)
100 self.assertEqual(token.user_name, new_client.username)
101 self.assertIsNone(new_client.password)
102 self.assertEqual(new_client.management_url,
103 'http://admin:35357/v3')
104
105 def test_auth_ref_load_with_overridden_arguments(self):
106 new_auth_url = 'https://newkeystone.com/v3'
107
108 user_id = uuid.uuid4().hex
109 user_name = uuid.uuid4().hex
110 project_id = uuid.uuid4().hex
111
112 first = client_fixtures.project_scoped_token(user_id=user_id,
113 user_name=user_name,
114 project_id=project_id)
115 second = client_fixtures.project_scoped_token(user_id=user_id,
116 user_name=user_name,
117 project_id=project_id)
118 self.stub_auth(json=first)
119 self.stub_auth(json=second, base_url=new_auth_url)
120
121 # Creating a HTTPClient not using session is deprecated.
122 with self.deprecations.expect_deprecations_here():
123 c = client.Client(user_id=user_id,
124 password='password',
125 project_id=project_id,
126 auth_url=self.TEST_URL)
127 cache = jsonutils.dumps(c.auth_ref)
128 # Creating a HTTPClient not using session is deprecated.
129 with self.deprecations.expect_deprecations_here():
130 new_client = client.Client(auth_ref=jsonutils.loads(cache),
131 auth_url=new_auth_url)
132 self.assertIsNotNone(new_client.auth_ref)
133 self.assertFalse(new_client.auth_ref.domain_scoped)
134 self.assertTrue(new_client.auth_ref.project_scoped)
135 self.assertEqual(new_auth_url, new_client.auth_url)
136 self.assertEqual(user_name, new_client.username)
137 self.assertIsNone(new_client.password)
138 self.assertEqual(new_client.management_url,
139 'http://admin:35357/v3')
140
141 def test_trust_init(self):
142 token = client_fixtures.trust_token()
143 self.stub_auth(json=token)
144
145 # Creating a HTTPClient not using session is deprecated.
146 with self.deprecations.expect_deprecations_here():
147 c = client.Client(user_domain_name=token.user_domain_name,
148 username=token.user_name,
149 password='password',
150 auth_url=self.TEST_URL,
151 trust_id=token.trust_id)
152 self.assertIsNotNone(c.auth_ref)
153 self.assertFalse(c.auth_ref.domain_scoped)
154 self.assertFalse(c.auth_ref.project_scoped)
155 self.assertEqual(token.trust_id, c.auth_ref.trust_id)
156 self.assertEqual(token.trustee_user_id, c.auth_ref.trustee_user_id)
157 self.assertEqual(token.trustor_user_id, c.auth_ref.trustor_user_id)
158 self.assertTrue(c.auth_ref.trust_scoped)
159 self.assertEqual(token.user_id, c.auth_user_id)
160
161 def test_init_err_no_auth_url(self):
162 # Creating a HTTPClient not using session is deprecated.
163 with self.deprecations.expect_deprecations_here():
164 self.assertRaises(exceptions.AuthorizationFailure,
165 client.Client,
166 username='exampleuser',
167 password='password')
168
169 def _management_url_is_updated(self, fixture, **kwargs):
170 second = copy.deepcopy(fixture)
171 first_url = 'http://admin:35357/v3'
172 second_url = "http://secondurl:%d/v3'"
173
174 for entry in second['token']['catalog']:
175 if entry['type'] == 'identity':
176 entry['endpoints'] = [{
177 'url': second_url % 5000,
178 'region': 'RegionOne',
179 'interface': 'public'
180 }, {
181 'url': second_url % 5000,
182 'region': 'RegionOne',
183 'interface': 'internal'
184 }, {
185 'url': second_url % 35357,
186 'region': 'RegionOne',
187 'interface': 'admin'
188 }]
189
190 self.stub_auth(response_list=[{'json': fixture}, {'json': second}])
191
192 # Creating a HTTPClient not using session is deprecated.
193 with self.deprecations.expect_deprecations_here():
194 cl = client.Client(username='exampleuser',
195 password='password',
196 auth_url=self.TEST_URL,
197 **kwargs)
198 self.assertEqual(cl.management_url, first_url)
199
200 with self.deprecations.expect_deprecations_here():
201 cl.authenticate()
202 self.assertEqual(cl.management_url, second_url % 35357)
203
204 def test_management_url_is_updated_with_project(self):
205 self._management_url_is_updated(client_fixtures.project_scoped_token(),
206 project_name='exampleproject')
207
208 def test_management_url_is_updated_with_domain(self):
209 self._management_url_is_updated(client_fixtures.domain_scoped_token(),
210 domain_name='exampledomain')
211
212 def test_client_with_region_name_passes_to_service_catalog(self):
213 # NOTE(jamielennox): this is deprecated behaviour that should be
214 # removed ASAP, however must remain compatible.
215 self.deprecations.expect_deprecations()
216
217 self.stub_auth(json=client_fixtures.auth_response_body())
218
219 cl = client.Client(username='exampleuser',
220 password='password',
221 project_name='exampleproject',
222 auth_url=self.TEST_URL,
223 region_name='North')
224 self.assertEqual(cl.service_catalog.url_for(service_type='image'),
225 'http://glance.north.host/glanceapi/public')
226
227 cl = client.Client(username='exampleuser',
228 password='password',
229 project_name='exampleproject',
230 auth_url=self.TEST_URL,
231 region_name='South')
232 self.assertEqual(cl.service_catalog.url_for(service_type='image'),
233 'http://glance.south.host/glanceapi/public')
234
235 def test_client_without_auth_params(self):
236 # Creating a HTTPClient not using session is deprecated.
237 with self.deprecations.expect_deprecations_here():
238 self.assertRaises(exceptions.AuthorizationFailure,
239 client.Client,
240 project_name='exampleproject',
241 auth_url=self.TEST_URL)
242
243 def test_client_params(self):
244 with self.deprecations.expect_deprecations_here():
245 sess = session.Session()
246 auth = token_endpoint.Token('a', 'b')
247
248 opts = {'auth': auth,
249 'connect_retries': 50,
250 'endpoint_override': uuid.uuid4().hex,
251 'interface': uuid.uuid4().hex,
252 'region_name': uuid.uuid4().hex,
253 'service_name': uuid.uuid4().hex,
254 'user_agent': uuid.uuid4().hex,
255 }
256
257 cl = client.Client(session=sess, **opts)
258
259 for k, v in opts.items():
260 self.assertEqual(v, getattr(cl._adapter, k))
261
262 self.assertEqual('identity', cl._adapter.service_type)
263 self.assertEqual((3, 0), cl._adapter.version)
264
265 def test_empty_service_catalog_param(self):
266 # Client().service_catalog should return None if the client is not
267 # authenticated
268 sess = auth_session.Session()
269 cl = client.Client(session=sess)
270 self.assertIsNone(cl.service_catalog)