Debian Codegraph @

keystoneclient/tests/unit/v3/test_auth.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 from oslo_serialization import jsonutils
14 from testtools import testcase
15
16 from keystoneclient import exceptions
17 from keystoneclient.tests.unit.v3 import utils
18 from keystoneclient.v3 import client
19
20
21 class AuthenticateAgainstKeystoneTests(utils.TestCase):
22 def setUp(self):
23 super(AuthenticateAgainstKeystoneTests, self).setUp()
24 self.TEST_RESPONSE_DICT = {
25 "token": {
26 "methods": [
27 "token",
28 "password"
29 ],
30
31 "expires_at": "2999-01-01T00:00:10.000123Z",
32 "project": {
33 "domain": {
34 "id": self.TEST_DOMAIN_ID,
35 "name": self.TEST_DOMAIN_NAME
36 },
37 "id": self.TEST_TENANT_ID,
38 "name": self.TEST_TENANT_NAME
39 },
40 "user": {
41 "domain": {
42 "id": self.TEST_DOMAIN_ID,
43 "name": self.TEST_DOMAIN_NAME
44 },
45 "id": self.TEST_USER,
46 "name": self.TEST_USER
47 },
48 "issued_at": "2013-05-29T16:55:21.468960Z",
49 "catalog": self.TEST_SERVICE_CATALOG
50 },
51 }
52 self.TEST_REQUEST_BODY = {
53 "auth": {
54 "identity": {
55 "methods": ["password"],
56 "password": {
57 "user": {
58 "domain": {
59 "name": self.TEST_DOMAIN_NAME
60 },
61 "name": self.TEST_USER,
62 "password": self.TEST_TOKEN
63 }
64 }
65 },
66 "scope": {
67 "project": {
68 "id": self.TEST_TENANT_ID
69 },
70 }
71 }
72 }
73 self.TEST_REQUEST_HEADERS = {
74 'Content-Type': 'application/json',
75 'User-Agent': 'python-keystoneclient'
76 }
77 self.TEST_RESPONSE_HEADERS = {
78 'X-Subject-Token': self.TEST_TOKEN
79 }
80
81 def test_authenticate_success(self):
82 TEST_TOKEN = "abcdef"
83 ident = self.TEST_REQUEST_BODY['auth']['identity']
84 del ident['password']['user']['domain']
85 del ident['password']['user']['name']
86 ident['password']['user']['id'] = self.TEST_USER
87
88 self.stub_auth(json=self.TEST_RESPONSE_DICT, subject_token=TEST_TOKEN)
89
90 # Creating a HTTPClient not using session is deprecated.
91 with self.deprecations.expect_deprecations_here():
92 cs = client.Client(user_id=self.TEST_USER,
93 password=self.TEST_TOKEN,
94 project_id=self.TEST_TENANT_ID,
95 auth_url=self.TEST_URL)
96 self.assertEqual(cs.auth_token, TEST_TOKEN)
97 self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY)
98
99 def test_authenticate_failure(self):
100 ident = self.TEST_REQUEST_BODY['auth']['identity']
101 ident['password']['user']['password'] = 'bad_key'
102 error = {"unauthorized": {"message": "Unauthorized",
103 "code": "401"}}
104
105 self.stub_auth(status_code=401, json=error)
106
107 with testcase.ExpectedException(exceptions.Unauthorized):
108 with self.deprecations.expect_deprecations_here():
109 client.Client(user_domain_name=self.TEST_DOMAIN_NAME,
110 username=self.TEST_USER,
111 password="bad_key",
112 project_id=self.TEST_TENANT_ID,
113 auth_url=self.TEST_URL)
114
115 self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY)
116
117 def test_auth_redirect(self):
118 headers = {'Location': self.TEST_ADMIN_URL + '/auth/tokens'}
119 self.stub_auth(status_code=305, text='Use proxy', headers=headers)
120
121 self.stub_auth(json=self.TEST_RESPONSE_DICT,
122 base_url=self.TEST_ADMIN_URL)
123
124 # Creating a HTTPClient not using session is deprecated.
125 with self.deprecations.expect_deprecations_here():
126 cs = client.Client(user_domain_name=self.TEST_DOMAIN_NAME,
127 username=self.TEST_USER,
128 password=self.TEST_TOKEN,
129 project_id=self.TEST_TENANT_ID,
130 auth_url=self.TEST_URL)
131
132 self.assertEqual(cs.management_url,
133 self.TEST_RESPONSE_DICT["token"]["catalog"][3]
134 ['endpoints'][2]["url"])
135 self.assertEqual(cs.auth_token,
136 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])
137
138 def test_authenticate_success_domain_username_password_scoped(self):
139 self.stub_auth(json=self.TEST_RESPONSE_DICT)
140
141 # Creating a HTTPClient not using session is deprecated.
142 with self.deprecations.expect_deprecations_here():
143 cs = client.Client(user_domain_name=self.TEST_DOMAIN_NAME,
144 username=self.TEST_USER,
145 password=self.TEST_TOKEN,
146 project_id=self.TEST_TENANT_ID,
147 auth_url=self.TEST_URL)
148 self.assertEqual(cs.management_url,
149 self.TEST_RESPONSE_DICT["token"]["catalog"][3]
150 ['endpoints'][2]["url"])
151 self.assertEqual(cs.auth_token,
152 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])
153
154 def test_authenticate_success_userid_password_domain_scoped(self):
155 ident = self.TEST_REQUEST_BODY['auth']['identity']
156 del ident['password']['user']['domain']
157 del ident['password']['user']['name']
158 ident['password']['user']['id'] = self.TEST_USER
159
160 scope = self.TEST_REQUEST_BODY['auth']['scope']
161 del scope['project']
162 scope['domain'] = {}
163 scope['domain']['id'] = self.TEST_DOMAIN_ID
164
165 token = self.TEST_RESPONSE_DICT['token']
166 del token['project']
167 token['domain'] = {}
168 token['domain']['id'] = self.TEST_DOMAIN_ID
169 token['domain']['name'] = self.TEST_DOMAIN_NAME
170
171 self.stub_auth(json=self.TEST_RESPONSE_DICT)
172
173 # Creating a HTTPClient not using session is deprecated.
174 with self.deprecations.expect_deprecations_here():
175 cs = client.Client(user_id=self.TEST_USER,
176 password=self.TEST_TOKEN,
177 domain_id=self.TEST_DOMAIN_ID,
178 auth_url=self.TEST_URL)
179 self.assertEqual(cs.auth_domain_id,
180 self.TEST_DOMAIN_ID)
181 self.assertEqual(cs.management_url,
182 self.TEST_RESPONSE_DICT["token"]["catalog"][3]
183 ['endpoints'][2]["url"])
184 self.assertEqual(cs.auth_token,
185 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])
186 self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY)
187
188 def test_authenticate_success_userid_password_project_scoped(self):
189 ident = self.TEST_REQUEST_BODY['auth']['identity']
190 del ident['password']['user']['domain']
191 del ident['password']['user']['name']
192 ident['password']['user']['id'] = self.TEST_USER
193
194 self.stub_auth(json=self.TEST_RESPONSE_DICT)
195
196 # Creating a HTTPClient not using session is deprecated.
197 with self.deprecations.expect_deprecations_here():
198 cs = client.Client(user_id=self.TEST_USER,
199 password=self.TEST_TOKEN,
200 project_id=self.TEST_TENANT_ID,
201 auth_url=self.TEST_URL)
202 self.assertEqual(cs.auth_tenant_id,
203 self.TEST_TENANT_ID)
204 self.assertEqual(cs.management_url,
205 self.TEST_RESPONSE_DICT["token"]["catalog"][3]
206 ['endpoints'][2]["url"])
207 self.assertEqual(cs.auth_token,
208 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])
209 self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY)
210
211 def test_authenticate_success_password_unscoped(self):
212 del self.TEST_RESPONSE_DICT['token']['catalog']
213 del self.TEST_REQUEST_BODY['auth']['scope']
214
215 self.stub_auth(json=self.TEST_RESPONSE_DICT)
216
217 # Creating a HTTPClient not using session is deprecated.
218 with self.deprecations.expect_deprecations_here():
219 cs = client.Client(user_domain_name=self.TEST_DOMAIN_NAME,
220 username=self.TEST_USER,
221 password=self.TEST_TOKEN,
222 auth_url=self.TEST_URL)
223 self.assertEqual(cs.auth_token,
224 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])
225 self.assertNotIn('catalog', cs.service_catalog.catalog)
226 self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY)
227
228 def test_auth_url_token_authentication(self):
229 fake_token = 'fake_token'
230 fake_url = '/fake-url'
231 fake_resp = {'result': True}
232
233 self.stub_auth(json=self.TEST_RESPONSE_DICT)
234 self.stub_url('GET', [fake_url], json=fake_resp,
235 base_url=self.TEST_PUBLIC_IDENTITY_ENDPOINT)
236
237 # Creating a HTTPClient not using session is deprecated.
238 with self.deprecations.expect_deprecations_here():
239 cl = client.Client(auth_url=self.TEST_URL,
240 token=fake_token)
241 body = jsonutils.loads(self.requests_mock.last_request.body)
242 self.assertEqual(body['auth']['identity']['token']['id'], fake_token)
243
244 resp, body = cl._adapter.get(fake_url)
245 self.assertEqual(fake_resp, body)
246
247 token = self.requests_mock.last_request.headers.get('X-Auth-Token')
248 self.assertEqual(self.TEST_TOKEN, token)
249
250 def test_authenticate_success_token_domain_scoped(self):
251 ident = self.TEST_REQUEST_BODY['auth']['identity']
252 del ident['password']
253 ident['methods'] = ['token']
254 ident['token'] = {}
255 ident['token']['id'] = self.TEST_TOKEN
256
257 scope = self.TEST_REQUEST_BODY['auth']['scope']
258 del scope['project']
259 scope['domain'] = {}
260 scope['domain']['id'] = self.TEST_DOMAIN_ID
261
262 token = self.TEST_RESPONSE_DICT['token']
263 del token['project']
264 token['domain'] = {}
265 token['domain']['id'] = self.TEST_DOMAIN_ID
266 token['domain']['name'] = self.TEST_DOMAIN_NAME
267
268 self.TEST_REQUEST_HEADERS['X-Auth-Token'] = self.TEST_TOKEN
269
270 self.stub_auth(json=self.TEST_RESPONSE_DICT)
271
272 # Creating a HTTPClient not using session is deprecated.
273 with self.deprecations.expect_deprecations_here():
274 cs = client.Client(token=self.TEST_TOKEN,
275 domain_id=self.TEST_DOMAIN_ID,
276 auth_url=self.TEST_URL)
277 self.assertEqual(cs.auth_domain_id,
278 self.TEST_DOMAIN_ID)
279 self.assertEqual(cs.management_url,
280 self.TEST_RESPONSE_DICT["token"]["catalog"][3]
281 ['endpoints'][2]["url"])
282 self.assertEqual(cs.auth_token,
283 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])
284 self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY)
285
286 def test_authenticate_success_token_project_scoped(self):
287 ident = self.TEST_REQUEST_BODY['auth']['identity']
288 del ident['password']
289 ident['methods'] = ['token']
290 ident['token'] = {}
291 ident['token']['id'] = self.TEST_TOKEN
292 self.TEST_REQUEST_HEADERS['X-Auth-Token'] = self.TEST_TOKEN
293
294 self.stub_auth(json=self.TEST_RESPONSE_DICT)
295
296 # Creating a HTTPClient not using session is deprecated.
297 with self.deprecations.expect_deprecations_here():
298 cs = client.Client(token=self.TEST_TOKEN,
299 project_id=self.TEST_TENANT_ID,
300 auth_url=self.TEST_URL)
301 self.assertEqual(cs.auth_tenant_id,
302 self.TEST_TENANT_ID)
303 self.assertEqual(cs.management_url,
304 self.TEST_RESPONSE_DICT["token"]["catalog"][3]
305 ['endpoints'][2]["url"])
306 self.assertEqual(cs.auth_token,
307 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])
308 self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY)
309
310 def test_authenticate_success_token_unscoped(self):
311 ident = self.TEST_REQUEST_BODY['auth']['identity']
312 del ident['password']
313 ident['methods'] = ['token']
314 ident['token'] = {}
315 ident['token']['id'] = self.TEST_TOKEN
316 del self.TEST_REQUEST_BODY['auth']['scope']
317 del self.TEST_RESPONSE_DICT['token']['catalog']
318 self.TEST_REQUEST_HEADERS['X-Auth-Token'] = self.TEST_TOKEN
319
320 self.stub_auth(json=self.TEST_RESPONSE_DICT)
321
322 # Creating a HTTPClient not using session is deprecated.
323 with self.deprecations.expect_deprecations_here():
324 cs = client.Client(token=self.TEST_TOKEN,
325 auth_url=self.TEST_URL)
326 self.assertEqual(cs.auth_token,
327 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])
328 self.assertNotIn('catalog', cs.service_catalog.catalog)
329 self.assertRequestBodyIs(json=self.TEST_REQUEST_BODY)
330
331 def test_allow_override_of_auth_token(self):
332 fake_url = '/fake-url'
333 fake_token = 'fake_token'
334 fake_resp = {'result': True}
335
336 self.stub_auth(json=self.TEST_RESPONSE_DICT)
337 self.stub_url('GET', [fake_url], json=fake_resp,
338 base_url=self.TEST_PUBLIC_IDENTITY_ENDPOINT)
339
340 # Creating a HTTPClient not using session is deprecated.
341 with self.deprecations.expect_deprecations_here():
342 cl = client.Client(username='exampleuser',
343 password='password',
344 project_name='exampleproject',
345 auth_url=self.TEST_URL)
346
347 self.assertEqual(cl.auth_token, self.TEST_TOKEN)
348
349 # the token returned from the authentication will be used
350 resp, body = cl._adapter.get(fake_url)
351 self.assertEqual(fake_resp, body)
352
353 token = self.requests_mock.last_request.headers.get('X-Auth-Token')
354 self.assertEqual(self.TEST_TOKEN, token)
355
356 # then override that token and the new token shall be used
357 cl.auth_token = fake_token
358
359 resp, body = cl._adapter.get(fake_url)
360 self.assertEqual(fake_resp, body)
361
362 token = self.requests_mock.last_request.headers.get('X-Auth-Token')
363 self.assertEqual(fake_token, token)
364
365 # if we clear that overridden token then we fall back to the original
366 del cl.auth_token
367
368 resp, body = cl._adapter.get(fake_url)
369 self.assertEqual(fake_resp, body)
370
371 token = self.requests_mock.last_request.headers.get('X-Auth-Token')
372 self.assertEqual(self.TEST_TOKEN, token)