root / snf-astakos-app / astakos / im / tests / api.py @ 1b27f8a2
History | View | Annotate | Download (24.5 kB)
1 |
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from astakos.im.tests.common import * |
35 |
from astakos.im.settings import astakos_services, BASE_HOST |
36 |
from synnefo.lib.services import get_service_path |
37 |
from synnefo.lib import join_urls |
38 |
|
39 |
from django.test import TestCase |
40 |
from django.core.urlresolvers import reverse |
41 |
|
42 |
#from xml.dom import minidom
|
43 |
|
44 |
import json |
45 |
|
46 |
ROOT = "/%s/%s/%s/" % (
|
47 |
astakos_settings.BASE_PATH, astakos_settings.ACCOUNTS_PREFIX, 'v1.0')
|
48 |
u = lambda url: ROOT + url
|
49 |
|
50 |
|
51 |
class QuotaAPITest(TestCase): |
52 |
def test_0(self): |
53 |
client = Client() |
54 |
backend = activation_backends.get_backend() |
55 |
|
56 |
component1 = Component.objects.create(name="comp1")
|
57 |
register.add_service(component1, "service1", "type1", []) |
58 |
# custom service resources
|
59 |
resource11 = {"name": "service1.resource11", |
60 |
"desc": "resource11 desc", |
61 |
"service_type": "type1", |
62 |
"service_origin": "service1", |
63 |
"allow_in_projects": True} |
64 |
r, _ = register.add_resource(resource11) |
65 |
register.update_resource(r, 100)
|
66 |
resource12 = {"name": "service1.resource12", |
67 |
"desc": "resource11 desc", |
68 |
"service_type": "type1", |
69 |
"service_origin": "service1", |
70 |
"unit": "bytes"} |
71 |
r, _ = register.add_resource(resource12) |
72 |
register.update_resource(r, 1024)
|
73 |
|
74 |
# create user
|
75 |
user = get_local_user('test@grnet.gr')
|
76 |
backend.accept_user(user) |
77 |
non_moderated_user = get_local_user('nonmon@example.com')
|
78 |
|
79 |
component2 = Component.objects.create(name="comp2")
|
80 |
register.add_service(component2, "service2", "type2", []) |
81 |
# create another service
|
82 |
resource21 = {"name": "service2.resource21", |
83 |
"desc": "resource11 desc", |
84 |
"service_type": "type2", |
85 |
"service_origin": "service2", |
86 |
"allow_in_projects": False} |
87 |
r, _ = register.add_resource(resource21) |
88 |
register.update_resource(r, 3)
|
89 |
|
90 |
resource_names = [r['name'] for r in |
91 |
[resource11, resource12, resource21]] |
92 |
|
93 |
# get resources
|
94 |
r = client.get(u('resources'))
|
95 |
self.assertEqual(r.status_code, 200) |
96 |
body = json.loads(r.content) |
97 |
for name in resource_names: |
98 |
assertIn(name, body) |
99 |
|
100 |
# get quota
|
101 |
r = client.get(u('quotas'))
|
102 |
self.assertEqual(r.status_code, 401) |
103 |
|
104 |
headers = {'HTTP_X_AUTH_TOKEN': user.auth_token}
|
105 |
r = client.get(u('quotas/'), **headers)
|
106 |
self.assertEqual(r.status_code, 200) |
107 |
body = json.loads(r.content) |
108 |
system_quota = body['system']
|
109 |
assertIn('system', body)
|
110 |
for name in resource_names: |
111 |
assertIn(name, system_quota) |
112 |
|
113 |
nmheaders = {'HTTP_X_AUTH_TOKEN': non_moderated_user.auth_token}
|
114 |
r = client.get(u('quotas/'), **nmheaders)
|
115 |
self.assertEqual(r.status_code, 200) |
116 |
body = json.loads(r.content) |
117 |
self.assertEqual(body, {})
|
118 |
|
119 |
r = client.get(u('service_quotas'))
|
120 |
self.assertEqual(r.status_code, 401) |
121 |
|
122 |
s1_headers = {'HTTP_X_AUTH_TOKEN': component1.auth_token}
|
123 |
r = client.get(u('service_quotas'), **s1_headers)
|
124 |
self.assertEqual(r.status_code, 200) |
125 |
body = json.loads(r.content) |
126 |
assertIn(user.uuid, body) |
127 |
|
128 |
r = client.get(u('commissions'), **s1_headers)
|
129 |
self.assertEqual(r.status_code, 200) |
130 |
body = json.loads(r.content) |
131 |
self.assertEqual(body, [])
|
132 |
|
133 |
# issue some commissions
|
134 |
commission_request = { |
135 |
"force": False, |
136 |
"auto_accept": False, |
137 |
"name": "my commission", |
138 |
"provisions": [
|
139 |
{ |
140 |
"holder": user.uuid,
|
141 |
"source": "system", |
142 |
"resource": resource11['name'], |
143 |
"quantity": 1 |
144 |
}, |
145 |
{ |
146 |
"holder": user.uuid,
|
147 |
"source": "system", |
148 |
"resource": resource12['name'], |
149 |
"quantity": 30000 |
150 |
}]} |
151 |
|
152 |
post_data = json.dumps(commission_request) |
153 |
r = client.post(u('commissions'), post_data,
|
154 |
content_type='application/json', **s1_headers)
|
155 |
self.assertEqual(r.status_code, 413) |
156 |
|
157 |
commission_request = { |
158 |
"force": False, |
159 |
"auto_accept": False, |
160 |
"name": "my commission", |
161 |
"provisions": [
|
162 |
{ |
163 |
"holder": user.uuid,
|
164 |
"source": "system", |
165 |
"resource": resource11['name'], |
166 |
"quantity": 1 |
167 |
}, |
168 |
{ |
169 |
"holder": user.uuid,
|
170 |
"source": "system", |
171 |
"resource": resource12['name'], |
172 |
"quantity": 100 |
173 |
}]} |
174 |
|
175 |
post_data = json.dumps(commission_request) |
176 |
r = client.post(u('commissions'), post_data,
|
177 |
content_type='application/json', **s1_headers)
|
178 |
self.assertEqual(r.status_code, 201) |
179 |
body = json.loads(r.content) |
180 |
serial = body['serial']
|
181 |
self.assertEqual(serial, 1) |
182 |
|
183 |
post_data = json.dumps(commission_request) |
184 |
r = client.post(u('commissions'), post_data,
|
185 |
content_type='application/json', **s1_headers)
|
186 |
self.assertEqual(r.status_code, 201) |
187 |
body = json.loads(r.content) |
188 |
self.assertEqual(body['serial'], 2) |
189 |
|
190 |
post_data = json.dumps(commission_request) |
191 |
r = client.post(u('commissions'), post_data,
|
192 |
content_type='application/json', **s1_headers)
|
193 |
self.assertEqual(r.status_code, 201) |
194 |
body = json.loads(r.content) |
195 |
self.assertEqual(body['serial'], 3) |
196 |
|
197 |
r = client.get(u('commissions'), **s1_headers)
|
198 |
self.assertEqual(r.status_code, 200) |
199 |
body = json.loads(r.content) |
200 |
self.assertEqual(body, [1, 2, 3]) |
201 |
|
202 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
203 |
self.assertEqual(r.status_code, 200) |
204 |
body = json.loads(r.content) |
205 |
self.assertEqual(body['serial'], serial) |
206 |
assertIn('issue_time', body)
|
207 |
provisions = sorted(body['provisions'], key=lambda p: p['resource']) |
208 |
self.assertEqual(provisions, commission_request['provisions']) |
209 |
self.assertEqual(body['name'], commission_request['name']) |
210 |
|
211 |
r = client.get(u('service_quotas?user=' + user.uuid), **s1_headers)
|
212 |
self.assertEqual(r.status_code, 200) |
213 |
body = json.loads(r.content) |
214 |
user_quota = body[user.uuid] |
215 |
system_quota = user_quota['system']
|
216 |
r11 = system_quota[resource11['name']]
|
217 |
self.assertEqual(r11['usage'], 3) |
218 |
self.assertEqual(r11['pending'], 3) |
219 |
|
220 |
# resolve pending commissions
|
221 |
resolve_data = { |
222 |
"accept": [1, 3], |
223 |
"reject": [2, 3, 4], |
224 |
} |
225 |
post_data = json.dumps(resolve_data) |
226 |
|
227 |
r = client.post(u('commissions/action'), post_data,
|
228 |
content_type='application/json', **s1_headers)
|
229 |
self.assertEqual(r.status_code, 200) |
230 |
body = json.loads(r.content) |
231 |
self.assertEqual(body['accepted'], [1]) |
232 |
self.assertEqual(body['rejected'], [2]) |
233 |
failed = body['failed']
|
234 |
self.assertEqual(len(failed), 2) |
235 |
|
236 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
237 |
self.assertEqual(r.status_code, 404) |
238 |
|
239 |
# auto accept
|
240 |
commission_request = { |
241 |
"auto_accept": True, |
242 |
"name": "my commission", |
243 |
"provisions": [
|
244 |
{ |
245 |
"holder": user.uuid,
|
246 |
"source": "system", |
247 |
"resource": resource11['name'], |
248 |
"quantity": 1 |
249 |
}, |
250 |
{ |
251 |
"holder": user.uuid,
|
252 |
"source": "system", |
253 |
"resource": resource12['name'], |
254 |
"quantity": 100 |
255 |
}]} |
256 |
|
257 |
post_data = json.dumps(commission_request) |
258 |
r = client.post(u('commissions'), post_data,
|
259 |
content_type='application/json', **s1_headers)
|
260 |
self.assertEqual(r.status_code, 201) |
261 |
body = json.loads(r.content) |
262 |
serial = body['serial']
|
263 |
self.assertEqual(serial, 4) |
264 |
|
265 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
266 |
self.assertEqual(r.status_code, 404) |
267 |
|
268 |
# malformed
|
269 |
commission_request = { |
270 |
"auto_accept": True, |
271 |
"name": "my commission", |
272 |
"provisions": [
|
273 |
{ |
274 |
"holder": user.uuid,
|
275 |
"source": "system", |
276 |
"resource": resource11['name'], |
277 |
} |
278 |
]} |
279 |
|
280 |
post_data = json.dumps(commission_request) |
281 |
r = client.post(u('commissions'), post_data,
|
282 |
content_type='application/json', **s1_headers)
|
283 |
self.assertEqual(r.status_code, 400) |
284 |
|
285 |
commission_request = { |
286 |
"auto_accept": True, |
287 |
"name": "my commission", |
288 |
"provisions": "dummy"} |
289 |
|
290 |
post_data = json.dumps(commission_request) |
291 |
r = client.post(u('commissions'), post_data,
|
292 |
content_type='application/json', **s1_headers)
|
293 |
self.assertEqual(r.status_code, 400) |
294 |
|
295 |
r = client.post(u('commissions'), commission_request,
|
296 |
content_type='application/json', **s1_headers)
|
297 |
self.assertEqual(r.status_code, 400) |
298 |
|
299 |
# no holding
|
300 |
commission_request = { |
301 |
"auto_accept": True, |
302 |
"name": "my commission", |
303 |
"provisions": [
|
304 |
{ |
305 |
"holder": user.uuid,
|
306 |
"source": "system", |
307 |
"resource": "non existent", |
308 |
"quantity": 1 |
309 |
}, |
310 |
{ |
311 |
"holder": user.uuid,
|
312 |
"source": "system", |
313 |
"resource": resource12['name'], |
314 |
"quantity": 100 |
315 |
}]} |
316 |
|
317 |
post_data = json.dumps(commission_request) |
318 |
r = client.post(u('commissions'), post_data,
|
319 |
content_type='application/json', **s1_headers)
|
320 |
self.assertEqual(r.status_code, 404) |
321 |
|
322 |
# release
|
323 |
commission_request = { |
324 |
"provisions": [
|
325 |
{ |
326 |
"holder": user.uuid,
|
327 |
"source": "system", |
328 |
"resource": resource11['name'], |
329 |
"quantity": -1 |
330 |
} |
331 |
]} |
332 |
|
333 |
post_data = json.dumps(commission_request) |
334 |
r = client.post(u('commissions'), post_data,
|
335 |
content_type='application/json', **s1_headers)
|
336 |
self.assertEqual(r.status_code, 201) |
337 |
body = json.loads(r.content) |
338 |
serial = body['serial']
|
339 |
|
340 |
accept_data = {'accept': ""} |
341 |
post_data = json.dumps(accept_data) |
342 |
r = client.post(u('commissions/' + str(serial) + '/action'), post_data, |
343 |
content_type='application/json', **s1_headers)
|
344 |
self.assertEqual(r.status_code, 200) |
345 |
|
346 |
reject_data = {'reject': ""} |
347 |
post_data = json.dumps(accept_data) |
348 |
r = client.post(u('commissions/' + str(serial) + '/action'), post_data, |
349 |
content_type='application/json', **s1_headers)
|
350 |
self.assertEqual(r.status_code, 404) |
351 |
|
352 |
# force
|
353 |
commission_request = { |
354 |
"force": True, |
355 |
"provisions": [
|
356 |
{ |
357 |
"holder": user.uuid,
|
358 |
"source": "system", |
359 |
"resource": resource11['name'], |
360 |
"quantity": 100 |
361 |
}]} |
362 |
|
363 |
post_data = json.dumps(commission_request) |
364 |
r = client.post(u('commissions'), post_data,
|
365 |
content_type='application/json', **s1_headers)
|
366 |
self.assertEqual(r.status_code, 201) |
367 |
|
368 |
commission_request = { |
369 |
"force": True, |
370 |
"provisions": [
|
371 |
{ |
372 |
"holder": user.uuid,
|
373 |
"source": "system", |
374 |
"resource": resource11['name'], |
375 |
"quantity": -200 |
376 |
}]} |
377 |
|
378 |
post_data = json.dumps(commission_request) |
379 |
r = client.post(u('commissions'), post_data,
|
380 |
content_type='application/json', **s1_headers)
|
381 |
self.assertEqual(r.status_code, 413) |
382 |
|
383 |
r = client.get(u('quotas'), **headers)
|
384 |
self.assertEqual(r.status_code, 200) |
385 |
body = json.loads(r.content) |
386 |
system_quota = body['system']
|
387 |
r11 = system_quota[resource11['name']]
|
388 |
self.assertEqual(r11['usage'], 102) |
389 |
self.assertEqual(r11['pending'], 101) |
390 |
|
391 |
# Bad Request
|
392 |
r = client.head(u('commissions'))
|
393 |
self.assertEqual(r.status_code, 400) |
394 |
|
395 |
|
396 |
class TokensApiTest(TestCase): |
397 |
def setUp(self): |
398 |
backend = activation_backends.get_backend() |
399 |
|
400 |
self.user1 = AstakosUser.objects.create(
|
401 |
email='test1', email_verified=True, moderated=True, |
402 |
is_rejected=False)
|
403 |
backend.activate_user(self.user1)
|
404 |
assert self.user1.is_active is True |
405 |
|
406 |
self.user2 = AstakosUser.objects.create(
|
407 |
email='test2', email_verified=True, moderated=True, |
408 |
is_rejected=False)
|
409 |
backend.activate_user(self.user2)
|
410 |
assert self.user2.is_active is True |
411 |
|
412 |
c1 = Component(name='component1', url='http://localhost/component1') |
413 |
c1.save() |
414 |
s1 = Service(component=c1, type='type1', name='service1') |
415 |
s1.save() |
416 |
e1 = Endpoint(service=s1) |
417 |
e1.save() |
418 |
e1.data.create(key='versionId', value='v1.0') |
419 |
e1.data.create(key='publicURL', value='http://localhost:8000/s1/v1.0') |
420 |
|
421 |
s2 = Service(component=c1, type='type2', name='service2') |
422 |
s2.save() |
423 |
e2 = Endpoint(service=s2) |
424 |
e2.save() |
425 |
e2.data.create(key='versionId', value='v1.0') |
426 |
e2.data.create(key='publicURL', value='http://localhost:8000/s2/v1.0') |
427 |
|
428 |
c2 = Component(name='component2', url='http://localhost/component2') |
429 |
c2.save() |
430 |
s3 = Service(component=c2, type='type3', name='service3') |
431 |
s3.save() |
432 |
e3 = Endpoint(service=s3) |
433 |
e3.save() |
434 |
e3.data.create(key='versionId', value='v2.0') |
435 |
e3.data.create(key='publicURL', value='http://localhost:8000/s3/v2.0') |
436 |
|
437 |
def test_authenticate(self): |
438 |
client = Client() |
439 |
|
440 |
# Check not allowed method
|
441 |
url = reverse('astakos.api.tokens.authenticate')
|
442 |
r = client.get(url, post_data={}) |
443 |
self.assertEqual(r.status_code, 400) |
444 |
|
445 |
# check public mode
|
446 |
r = client.post(url, CONTENT_LENGTH=0)
|
447 |
self.assertEqual(r.status_code, 200) |
448 |
self.assertTrue(r['Content-Type'].startswith('application/json')) |
449 |
try:
|
450 |
body = json.loads(r.content) |
451 |
except Exception, e: |
452 |
self.fail(e)
|
453 |
self.assertTrue('token' not in body.get('access')) |
454 |
self.assertTrue('user' not in body.get('access')) |
455 |
self.assertTrue('serviceCatalog' in body.get('access')) |
456 |
|
457 |
# Check unsupported xml input
|
458 |
url = reverse('astakos.api.tokens.authenticate')
|
459 |
post_data = """
|
460 |
<?xml version="1.0" encoding="UTF-8"?>
|
461 |
<auth xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
462 |
xmlns="http://docs.openstack.org/identity/api/v2.0"
|
463 |
tenantName="%s">
|
464 |
<passwordCredentials username="%s" password="%s"/>
|
465 |
</auth>""" % (self.user1.uuid, self.user1.uuid, |
466 |
self.user1.auth_token)
|
467 |
r = client.post(url, post_data, content_type='application/xml')
|
468 |
self.assertEqual(r.status_code, 400) |
469 |
body = json.loads(r.content) |
470 |
self.assertEqual(body['badRequest']['message'], |
471 |
"Unsupported Content-type: 'application/xml'")
|
472 |
|
473 |
# Check malformed request: missing password
|
474 |
url = reverse('astakos.api.tokens.authenticate')
|
475 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s"},
|
476 |
"tenantName":"%s"}}""" % (
|
477 |
self.user1.uuid, self.user1.uuid) |
478 |
r = client.post(url, post_data, content_type='application/json')
|
479 |
self.assertEqual(r.status_code, 400) |
480 |
body = json.loads(r.content) |
481 |
self.assertTrue(body['badRequest']['message']. |
482 |
startswith('Malformed request'))
|
483 |
|
484 |
# Check malformed request: missing username
|
485 |
url = reverse('astakos.api.tokens.authenticate')
|
486 |
post_data = """{"auth":{"passwordCredentials":{"password":"%s"},
|
487 |
"tenantName":"%s"}}""" % (
|
488 |
self.user1.auth_token, self.user1.uuid) |
489 |
r = client.post(url, post_data, content_type='application/json')
|
490 |
self.assertEqual(r.status_code, 400) |
491 |
body = json.loads(r.content) |
492 |
self.assertTrue(body['badRequest']['message']. |
493 |
startswith('Malformed request'))
|
494 |
|
495 |
# Check invalid pass
|
496 |
url = reverse('astakos.api.tokens.authenticate')
|
497 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
498 |
"password":"%s"},
|
499 |
"tenantName":"%s"}}""" % (
|
500 |
self.user1.uuid, '', self.user1.uuid) |
501 |
r = client.post(url, post_data, content_type='application/json')
|
502 |
self.assertEqual(r.status_code, 401) |
503 |
body = json.loads(r.content) |
504 |
self.assertEqual(body['unauthorized']['message'], |
505 |
'Invalid token')
|
506 |
|
507 |
# Check inconsistent pass
|
508 |
url = reverse('astakos.api.tokens.authenticate')
|
509 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
510 |
"password":"%s"},
|
511 |
"tenantName":"%s"}}""" % (
|
512 |
self.user1.uuid, self.user2.auth_token, self.user2.uuid) |
513 |
r = client.post(url, post_data, content_type='application/json')
|
514 |
self.assertEqual(r.status_code, 401) |
515 |
body = json.loads(r.content) |
516 |
self.assertEqual(body['unauthorized']['message'], |
517 |
'Invalid credentials')
|
518 |
|
519 |
# Check invalid json data
|
520 |
url = reverse('astakos.api.tokens.authenticate')
|
521 |
r = client.post(url, "not json", content_type='application/json') |
522 |
self.assertEqual(r.status_code, 400) |
523 |
body = json.loads(r.content) |
524 |
self.assertEqual(body['badRequest']['message'], 'Invalid JSON data') |
525 |
|
526 |
# Check auth with token
|
527 |
url = reverse('astakos.api.tokens.authenticate')
|
528 |
post_data = """{"auth":{"token": {"id":"%s"},
|
529 |
"tenantName":"%s"}}""" % (
|
530 |
self.user1.auth_token, self.user1.uuid) |
531 |
r = client.post(url, post_data, content_type='application/json')
|
532 |
self.assertEqual(r.status_code, 200) |
533 |
self.assertTrue(r['Content-Type'].startswith('application/json')) |
534 |
try:
|
535 |
body = json.loads(r.content) |
536 |
except Exception, e: |
537 |
self.fail(e)
|
538 |
|
539 |
# Check malformed request: missing token
|
540 |
url = reverse('astakos.api.tokens.authenticate')
|
541 |
post_data = """{"auth":{"auth_token":{"id":"%s"},
|
542 |
"tenantName":"%s"}}""" % (
|
543 |
self.user1.auth_token, self.user1.uuid) |
544 |
r = client.post(url, post_data, content_type='application/json')
|
545 |
self.assertEqual(r.status_code, 400) |
546 |
body = json.loads(r.content) |
547 |
self.assertTrue(body['badRequest']['message']. |
548 |
startswith('Malformed request'))
|
549 |
|
550 |
# Check bad request: inconsistent tenant
|
551 |
url = reverse('astakos.api.tokens.authenticate')
|
552 |
post_data = """{"auth":{"token":{"id":"%s"},
|
553 |
"tenantName":"%s"}}""" % (
|
554 |
self.user1.auth_token, self.user2.uuid) |
555 |
r = client.post(url, post_data, content_type='application/json')
|
556 |
self.assertEqual(r.status_code, 400) |
557 |
body = json.loads(r.content) |
558 |
self.assertEqual(body['badRequest']['message'], |
559 |
'Not conforming tenantName')
|
560 |
|
561 |
# Check bad request: inconsistent tenant
|
562 |
url = reverse('astakos.api.tokens.authenticate')
|
563 |
post_data = """{"auth":{"token":{"id":"%s"},
|
564 |
"tenantName":""}}""" % (
|
565 |
self.user1.auth_token)
|
566 |
r = client.post(url, post_data, content_type='application/json')
|
567 |
self.assertEqual(r.status_code, 200) |
568 |
|
569 |
# Check successful json response
|
570 |
url = reverse('astakos.api.tokens.authenticate')
|
571 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
572 |
"password":"%s"},
|
573 |
"tenantName":"%s"}}""" % (
|
574 |
self.user1.uuid, self.user1.auth_token, self.user1.uuid) |
575 |
r = client.post(url, post_data, content_type='application/json')
|
576 |
self.assertEqual(r.status_code, 200) |
577 |
self.assertTrue(r['Content-Type'].startswith('application/json')) |
578 |
try:
|
579 |
body = json.loads(r.content) |
580 |
except Exception, e: |
581 |
self.fail(e)
|
582 |
|
583 |
try:
|
584 |
token = body['access']['token']['id'] |
585 |
user = body['access']['user']['id'] |
586 |
service_catalog = body['access']['serviceCatalog'] |
587 |
except KeyError: |
588 |
self.fail('Invalid response') |
589 |
|
590 |
self.assertEqual(token, self.user1.auth_token) |
591 |
self.assertEqual(user, self.user1.uuid) |
592 |
self.assertEqual(len(service_catalog), 3) |
593 |
|
594 |
# Check successful xml response
|
595 |
url = reverse('astakos.api.tokens.authenticate')
|
596 |
headers = {'HTTP_ACCEPT': 'application/xml'} |
597 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
598 |
"password":"%s"},
|
599 |
"tenantName":"%s"}}""" % (
|
600 |
self.user1.uuid, self.user1.auth_token, self.user1.uuid) |
601 |
r = client.post(url, post_data, content_type='application/json',
|
602 |
**headers) |
603 |
self.assertEqual(r.status_code, 200) |
604 |
self.assertTrue(r['Content-Type'].startswith('application/xml')) |
605 |
# try:
|
606 |
# body = minidom.parseString(r.content)
|
607 |
# except Exception, e:
|
608 |
# self.fail(e)
|
609 |
|
610 |
|
611 |
class WrongPathAPITest(TestCase): |
612 |
def test_catch_wrong_account_paths(self, *args): |
613 |
path = get_service_path(astakos_services, 'account', 'v1.0') |
614 |
path = join_urls(BASE_HOST, path, 'nonexistent')
|
615 |
response = self.client.get(path)
|
616 |
self.assertEqual(response.status_code, 400) |
617 |
try:
|
618 |
error = json.loads(response.content) |
619 |
except ValueError: |
620 |
self.assertTrue(False) |
621 |
|
622 |
def test_catch_wrong_identity_paths(self, *args): |
623 |
path = get_service_path(astakos_services, 'identity', 'v2.0') |
624 |
path = join_urls(BASE_HOST, path, 'nonexistent')
|
625 |
response = self.client.get(path)
|
626 |
self.assertEqual(response.status_code, 400) |
627 |
try:
|
628 |
error = json.loads(response.content) |
629 |
except ValueError: |
630 |
self.assertTrue(False) |