root / snf-astakos-app / astakos / im / tests / api.py @ 3848b521
History | View | Annotate | Download (24.8 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 |
is_active=False)
|
79 |
r_user = get_local_user('rej@example.com',
|
80 |
is_active=False, email_verified=True) |
81 |
backend.reject_user(r_user, "reason")
|
82 |
|
83 |
component2 = Component.objects.create(name="comp2")
|
84 |
register.add_service(component2, "service2", "type2", []) |
85 |
# create another service
|
86 |
resource21 = {"name": "service2.resource21", |
87 |
"desc": "resource11 desc", |
88 |
"service_type": "type2", |
89 |
"service_origin": "service2", |
90 |
"allow_in_projects": False} |
91 |
r, _ = register.add_resource(resource21) |
92 |
register.update_resource(r, 3)
|
93 |
|
94 |
resource_names = [r['name'] for r in |
95 |
[resource11, resource12, resource21]] |
96 |
|
97 |
# get resources
|
98 |
r = client.get(u('resources'))
|
99 |
self.assertEqual(r.status_code, 200) |
100 |
body = json.loads(r.content) |
101 |
for name in resource_names: |
102 |
assertIn(name, body) |
103 |
|
104 |
# get quota
|
105 |
r = client.get(u('quotas'))
|
106 |
self.assertEqual(r.status_code, 401) |
107 |
|
108 |
headers = {'HTTP_X_AUTH_TOKEN': user.auth_token}
|
109 |
r = client.get(u('quotas/'), **headers)
|
110 |
self.assertEqual(r.status_code, 200) |
111 |
body = json.loads(r.content) |
112 |
system_quota = body['system']
|
113 |
assertIn('system', body)
|
114 |
for name in resource_names: |
115 |
assertIn(name, system_quota) |
116 |
|
117 |
nmheaders = {'HTTP_X_AUTH_TOKEN': non_moderated_user.auth_token}
|
118 |
r = client.get(u('quotas/'), **nmheaders)
|
119 |
self.assertEqual(r.status_code, 401) |
120 |
|
121 |
q = quotas.get_user_quotas(non_moderated_user) |
122 |
self.assertEqual(q, {})
|
123 |
|
124 |
q = quotas.get_user_quotas(r_user) |
125 |
self.assertEqual(q, {})
|
126 |
|
127 |
r = client.get(u('service_quotas'))
|
128 |
self.assertEqual(r.status_code, 401) |
129 |
|
130 |
s1_headers = {'HTTP_X_AUTH_TOKEN': component1.auth_token}
|
131 |
r = client.get(u('service_quotas'), **s1_headers)
|
132 |
self.assertEqual(r.status_code, 200) |
133 |
body = json.loads(r.content) |
134 |
assertIn(user.uuid, body) |
135 |
|
136 |
r = client.get(u('commissions'), **s1_headers)
|
137 |
self.assertEqual(r.status_code, 200) |
138 |
body = json.loads(r.content) |
139 |
self.assertEqual(body, [])
|
140 |
|
141 |
# issue some commissions
|
142 |
commission_request = { |
143 |
"force": False, |
144 |
"auto_accept": False, |
145 |
"name": "my commission", |
146 |
"provisions": [
|
147 |
{ |
148 |
"holder": user.uuid,
|
149 |
"source": "system", |
150 |
"resource": resource11['name'], |
151 |
"quantity": 1 |
152 |
}, |
153 |
{ |
154 |
"holder": user.uuid,
|
155 |
"source": "system", |
156 |
"resource": resource12['name'], |
157 |
"quantity": 30000 |
158 |
}]} |
159 |
|
160 |
post_data = json.dumps(commission_request) |
161 |
r = client.post(u('commissions'), post_data,
|
162 |
content_type='application/json', **s1_headers)
|
163 |
self.assertEqual(r.status_code, 413) |
164 |
|
165 |
commission_request = { |
166 |
"force": False, |
167 |
"auto_accept": False, |
168 |
"name": "my commission", |
169 |
"provisions": [
|
170 |
{ |
171 |
"holder": user.uuid,
|
172 |
"source": "system", |
173 |
"resource": resource11['name'], |
174 |
"quantity": 1 |
175 |
}, |
176 |
{ |
177 |
"holder": user.uuid,
|
178 |
"source": "system", |
179 |
"resource": resource12['name'], |
180 |
"quantity": 100 |
181 |
}]} |
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 |
serial = body['serial']
|
189 |
self.assertEqual(serial, 1) |
190 |
|
191 |
post_data = json.dumps(commission_request) |
192 |
r = client.post(u('commissions'), post_data,
|
193 |
content_type='application/json', **s1_headers)
|
194 |
self.assertEqual(r.status_code, 201) |
195 |
body = json.loads(r.content) |
196 |
self.assertEqual(body['serial'], 2) |
197 |
|
198 |
post_data = json.dumps(commission_request) |
199 |
r = client.post(u('commissions'), post_data,
|
200 |
content_type='application/json', **s1_headers)
|
201 |
self.assertEqual(r.status_code, 201) |
202 |
body = json.loads(r.content) |
203 |
self.assertEqual(body['serial'], 3) |
204 |
|
205 |
r = client.get(u('commissions'), **s1_headers)
|
206 |
self.assertEqual(r.status_code, 200) |
207 |
body = json.loads(r.content) |
208 |
self.assertEqual(body, [1, 2, 3]) |
209 |
|
210 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
211 |
self.assertEqual(r.status_code, 200) |
212 |
body = json.loads(r.content) |
213 |
self.assertEqual(body['serial'], serial) |
214 |
assertIn('issue_time', body)
|
215 |
provisions = sorted(body['provisions'], key=lambda p: p['resource']) |
216 |
self.assertEqual(provisions, commission_request['provisions']) |
217 |
self.assertEqual(body['name'], commission_request['name']) |
218 |
|
219 |
r = client.get(u('service_quotas?user=' + user.uuid), **s1_headers)
|
220 |
self.assertEqual(r.status_code, 200) |
221 |
body = json.loads(r.content) |
222 |
user_quota = body[user.uuid] |
223 |
system_quota = user_quota['system']
|
224 |
r11 = system_quota[resource11['name']]
|
225 |
self.assertEqual(r11['usage'], 3) |
226 |
self.assertEqual(r11['pending'], 3) |
227 |
|
228 |
# resolve pending commissions
|
229 |
resolve_data = { |
230 |
"accept": [1, 3], |
231 |
"reject": [2, 3, 4], |
232 |
} |
233 |
post_data = json.dumps(resolve_data) |
234 |
|
235 |
r = client.post(u('commissions/action'), post_data,
|
236 |
content_type='application/json', **s1_headers)
|
237 |
self.assertEqual(r.status_code, 200) |
238 |
body = json.loads(r.content) |
239 |
self.assertEqual(body['accepted'], [1]) |
240 |
self.assertEqual(body['rejected'], [2]) |
241 |
failed = body['failed']
|
242 |
self.assertEqual(len(failed), 2) |
243 |
|
244 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
245 |
self.assertEqual(r.status_code, 404) |
246 |
|
247 |
# auto accept
|
248 |
commission_request = { |
249 |
"auto_accept": True, |
250 |
"name": "my commission", |
251 |
"provisions": [
|
252 |
{ |
253 |
"holder": user.uuid,
|
254 |
"source": "system", |
255 |
"resource": resource11['name'], |
256 |
"quantity": 1 |
257 |
}, |
258 |
{ |
259 |
"holder": user.uuid,
|
260 |
"source": "system", |
261 |
"resource": resource12['name'], |
262 |
"quantity": 100 |
263 |
}]} |
264 |
|
265 |
post_data = json.dumps(commission_request) |
266 |
r = client.post(u('commissions'), post_data,
|
267 |
content_type='application/json', **s1_headers)
|
268 |
self.assertEqual(r.status_code, 201) |
269 |
body = json.loads(r.content) |
270 |
serial = body['serial']
|
271 |
self.assertEqual(serial, 4) |
272 |
|
273 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
274 |
self.assertEqual(r.status_code, 404) |
275 |
|
276 |
# malformed
|
277 |
commission_request = { |
278 |
"auto_accept": True, |
279 |
"name": "my commission", |
280 |
"provisions": [
|
281 |
{ |
282 |
"holder": user.uuid,
|
283 |
"source": "system", |
284 |
"resource": resource11['name'], |
285 |
} |
286 |
]} |
287 |
|
288 |
post_data = json.dumps(commission_request) |
289 |
r = client.post(u('commissions'), post_data,
|
290 |
content_type='application/json', **s1_headers)
|
291 |
self.assertEqual(r.status_code, 400) |
292 |
|
293 |
commission_request = { |
294 |
"auto_accept": True, |
295 |
"name": "my commission", |
296 |
"provisions": "dummy"} |
297 |
|
298 |
post_data = json.dumps(commission_request) |
299 |
r = client.post(u('commissions'), post_data,
|
300 |
content_type='application/json', **s1_headers)
|
301 |
self.assertEqual(r.status_code, 400) |
302 |
|
303 |
r = client.post(u('commissions'), commission_request,
|
304 |
content_type='application/json', **s1_headers)
|
305 |
self.assertEqual(r.status_code, 400) |
306 |
|
307 |
# no holding
|
308 |
commission_request = { |
309 |
"auto_accept": True, |
310 |
"name": "my commission", |
311 |
"provisions": [
|
312 |
{ |
313 |
"holder": user.uuid,
|
314 |
"source": "system", |
315 |
"resource": "non existent", |
316 |
"quantity": 1 |
317 |
}, |
318 |
{ |
319 |
"holder": user.uuid,
|
320 |
"source": "system", |
321 |
"resource": resource12['name'], |
322 |
"quantity": 100 |
323 |
}]} |
324 |
|
325 |
post_data = json.dumps(commission_request) |
326 |
r = client.post(u('commissions'), post_data,
|
327 |
content_type='application/json', **s1_headers)
|
328 |
self.assertEqual(r.status_code, 404) |
329 |
|
330 |
# release
|
331 |
commission_request = { |
332 |
"provisions": [
|
333 |
{ |
334 |
"holder": user.uuid,
|
335 |
"source": "system", |
336 |
"resource": resource11['name'], |
337 |
"quantity": -1 |
338 |
} |
339 |
]} |
340 |
|
341 |
post_data = json.dumps(commission_request) |
342 |
r = client.post(u('commissions'), post_data,
|
343 |
content_type='application/json', **s1_headers)
|
344 |
self.assertEqual(r.status_code, 201) |
345 |
body = json.loads(r.content) |
346 |
serial = body['serial']
|
347 |
|
348 |
accept_data = {'accept': ""} |
349 |
post_data = json.dumps(accept_data) |
350 |
r = client.post(u('commissions/' + str(serial) + '/action'), post_data, |
351 |
content_type='application/json', **s1_headers)
|
352 |
self.assertEqual(r.status_code, 200) |
353 |
|
354 |
reject_data = {'reject': ""} |
355 |
post_data = json.dumps(accept_data) |
356 |
r = client.post(u('commissions/' + str(serial) + '/action'), post_data, |
357 |
content_type='application/json', **s1_headers)
|
358 |
self.assertEqual(r.status_code, 404) |
359 |
|
360 |
# force
|
361 |
commission_request = { |
362 |
"force": True, |
363 |
"provisions": [
|
364 |
{ |
365 |
"holder": user.uuid,
|
366 |
"source": "system", |
367 |
"resource": resource11['name'], |
368 |
"quantity": 100 |
369 |
}]} |
370 |
|
371 |
post_data = json.dumps(commission_request) |
372 |
r = client.post(u('commissions'), post_data,
|
373 |
content_type='application/json', **s1_headers)
|
374 |
self.assertEqual(r.status_code, 201) |
375 |
|
376 |
commission_request = { |
377 |
"force": True, |
378 |
"provisions": [
|
379 |
{ |
380 |
"holder": user.uuid,
|
381 |
"source": "system", |
382 |
"resource": resource11['name'], |
383 |
"quantity": -200 |
384 |
}]} |
385 |
|
386 |
post_data = json.dumps(commission_request) |
387 |
r = client.post(u('commissions'), post_data,
|
388 |
content_type='application/json', **s1_headers)
|
389 |
self.assertEqual(r.status_code, 413) |
390 |
|
391 |
r = client.get(u('quotas'), **headers)
|
392 |
self.assertEqual(r.status_code, 200) |
393 |
body = json.loads(r.content) |
394 |
system_quota = body['system']
|
395 |
r11 = system_quota[resource11['name']]
|
396 |
self.assertEqual(r11['usage'], 102) |
397 |
self.assertEqual(r11['pending'], 101) |
398 |
|
399 |
# Bad Request
|
400 |
r = client.head(u('commissions'))
|
401 |
self.assertEqual(r.status_code, 400) |
402 |
|
403 |
|
404 |
class TokensApiTest(TestCase): |
405 |
def setUp(self): |
406 |
backend = activation_backends.get_backend() |
407 |
|
408 |
self.user1 = AstakosUser.objects.create(
|
409 |
email='test1', email_verified=True, moderated=True, |
410 |
is_rejected=False)
|
411 |
backend.activate_user(self.user1)
|
412 |
assert self.user1.is_active is True |
413 |
|
414 |
self.user2 = AstakosUser.objects.create(
|
415 |
email='test2', email_verified=True, moderated=True, |
416 |
is_rejected=False)
|
417 |
backend.activate_user(self.user2)
|
418 |
assert self.user2.is_active is True |
419 |
|
420 |
c1 = Component(name='component1', url='http://localhost/component1') |
421 |
c1.save() |
422 |
s1 = Service(component=c1, type='type1', name='service1') |
423 |
s1.save() |
424 |
e1 = Endpoint(service=s1) |
425 |
e1.save() |
426 |
e1.data.create(key='versionId', value='v1.0') |
427 |
e1.data.create(key='publicURL', value='http://localhost:8000/s1/v1.0') |
428 |
|
429 |
s2 = Service(component=c1, type='type2', name='service2') |
430 |
s2.save() |
431 |
e2 = Endpoint(service=s2) |
432 |
e2.save() |
433 |
e2.data.create(key='versionId', value='v1.0') |
434 |
e2.data.create(key='publicURL', value='http://localhost:8000/s2/v1.0') |
435 |
|
436 |
c2 = Component(name='component2', url='http://localhost/component2') |
437 |
c2.save() |
438 |
s3 = Service(component=c2, type='type3', name='service3') |
439 |
s3.save() |
440 |
e3 = Endpoint(service=s3) |
441 |
e3.save() |
442 |
e3.data.create(key='versionId', value='v2.0') |
443 |
e3.data.create(key='publicURL', value='http://localhost:8000/s3/v2.0') |
444 |
|
445 |
def test_authenticate(self): |
446 |
client = Client() |
447 |
|
448 |
# Check not allowed method
|
449 |
url = reverse('astakos.api.tokens.authenticate')
|
450 |
r = client.get(url, post_data={}) |
451 |
self.assertEqual(r.status_code, 400) |
452 |
|
453 |
# check public mode
|
454 |
r = client.post(url, CONTENT_LENGTH=0)
|
455 |
self.assertEqual(r.status_code, 200) |
456 |
self.assertTrue(r['Content-Type'].startswith('application/json')) |
457 |
try:
|
458 |
body = json.loads(r.content) |
459 |
except Exception, e: |
460 |
self.fail(e)
|
461 |
self.assertTrue('token' not in body.get('access')) |
462 |
self.assertTrue('user' not in body.get('access')) |
463 |
self.assertTrue('serviceCatalog' in body.get('access')) |
464 |
|
465 |
# Check unsupported xml input
|
466 |
url = reverse('astakos.api.tokens.authenticate')
|
467 |
post_data = """
|
468 |
<?xml version="1.0" encoding="UTF-8"?>
|
469 |
<auth xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
470 |
xmlns="http://docs.openstack.org/identity/api/v2.0"
|
471 |
tenantName="%s">
|
472 |
<passwordCredentials username="%s" password="%s"/>
|
473 |
</auth>""" % (self.user1.uuid, self.user1.uuid, |
474 |
self.user1.auth_token)
|
475 |
r = client.post(url, post_data, content_type='application/xml')
|
476 |
self.assertEqual(r.status_code, 400) |
477 |
body = json.loads(r.content) |
478 |
self.assertEqual(body['badRequest']['message'], |
479 |
"Unsupported Content-type: 'application/xml'")
|
480 |
|
481 |
# Check malformed request: missing password
|
482 |
url = reverse('astakos.api.tokens.authenticate')
|
483 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s"},
|
484 |
"tenantName":"%s"}}""" % (
|
485 |
self.user1.uuid, self.user1.uuid) |
486 |
r = client.post(url, post_data, content_type='application/json')
|
487 |
self.assertEqual(r.status_code, 400) |
488 |
body = json.loads(r.content) |
489 |
self.assertTrue(body['badRequest']['message']. |
490 |
startswith('Malformed request'))
|
491 |
|
492 |
# Check malformed request: missing username
|
493 |
url = reverse('astakos.api.tokens.authenticate')
|
494 |
post_data = """{"auth":{"passwordCredentials":{"password":"%s"},
|
495 |
"tenantName":"%s"}}""" % (
|
496 |
self.user1.auth_token, self.user1.uuid) |
497 |
r = client.post(url, post_data, content_type='application/json')
|
498 |
self.assertEqual(r.status_code, 400) |
499 |
body = json.loads(r.content) |
500 |
self.assertTrue(body['badRequest']['message']. |
501 |
startswith('Malformed request'))
|
502 |
|
503 |
# Check invalid pass
|
504 |
url = reverse('astakos.api.tokens.authenticate')
|
505 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
506 |
"password":"%s"},
|
507 |
"tenantName":"%s"}}""" % (
|
508 |
self.user1.uuid, '', self.user1.uuid) |
509 |
r = client.post(url, post_data, content_type='application/json')
|
510 |
self.assertEqual(r.status_code, 401) |
511 |
body = json.loads(r.content) |
512 |
self.assertEqual(body['unauthorized']['message'], |
513 |
'Invalid token')
|
514 |
|
515 |
# Check inconsistent pass
|
516 |
url = reverse('astakos.api.tokens.authenticate')
|
517 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
518 |
"password":"%s"},
|
519 |
"tenantName":"%s"}}""" % (
|
520 |
self.user1.uuid, self.user2.auth_token, self.user2.uuid) |
521 |
r = client.post(url, post_data, content_type='application/json')
|
522 |
self.assertEqual(r.status_code, 401) |
523 |
body = json.loads(r.content) |
524 |
self.assertEqual(body['unauthorized']['message'], |
525 |
'Invalid credentials')
|
526 |
|
527 |
# Check invalid json data
|
528 |
url = reverse('astakos.api.tokens.authenticate')
|
529 |
r = client.post(url, "not json", content_type='application/json') |
530 |
self.assertEqual(r.status_code, 400) |
531 |
body = json.loads(r.content) |
532 |
self.assertEqual(body['badRequest']['message'], 'Invalid JSON data') |
533 |
|
534 |
# Check auth with token
|
535 |
url = reverse('astakos.api.tokens.authenticate')
|
536 |
post_data = """{"auth":{"token": {"id":"%s"},
|
537 |
"tenantName":"%s"}}""" % (
|
538 |
self.user1.auth_token, self.user1.uuid) |
539 |
r = client.post(url, post_data, content_type='application/json')
|
540 |
self.assertEqual(r.status_code, 200) |
541 |
self.assertTrue(r['Content-Type'].startswith('application/json')) |
542 |
try:
|
543 |
body = json.loads(r.content) |
544 |
except Exception, e: |
545 |
self.fail(e)
|
546 |
|
547 |
# Check malformed request: missing token
|
548 |
url = reverse('astakos.api.tokens.authenticate')
|
549 |
post_data = """{"auth":{"auth_token":{"id":"%s"},
|
550 |
"tenantName":"%s"}}""" % (
|
551 |
self.user1.auth_token, self.user1.uuid) |
552 |
r = client.post(url, post_data, content_type='application/json')
|
553 |
self.assertEqual(r.status_code, 400) |
554 |
body = json.loads(r.content) |
555 |
self.assertTrue(body['badRequest']['message']. |
556 |
startswith('Malformed request'))
|
557 |
|
558 |
# Check bad request: inconsistent tenant
|
559 |
url = reverse('astakos.api.tokens.authenticate')
|
560 |
post_data = """{"auth":{"token":{"id":"%s"},
|
561 |
"tenantName":"%s"}}""" % (
|
562 |
self.user1.auth_token, self.user2.uuid) |
563 |
r = client.post(url, post_data, content_type='application/json')
|
564 |
self.assertEqual(r.status_code, 400) |
565 |
body = json.loads(r.content) |
566 |
self.assertEqual(body['badRequest']['message'], |
567 |
'Not conforming tenantName')
|
568 |
|
569 |
# Check bad request: inconsistent tenant
|
570 |
url = reverse('astakos.api.tokens.authenticate')
|
571 |
post_data = """{"auth":{"token":{"id":"%s"},
|
572 |
"tenantName":""}}""" % (
|
573 |
self.user1.auth_token)
|
574 |
r = client.post(url, post_data, content_type='application/json')
|
575 |
self.assertEqual(r.status_code, 200) |
576 |
|
577 |
# Check successful json response
|
578 |
url = reverse('astakos.api.tokens.authenticate')
|
579 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
580 |
"password":"%s"},
|
581 |
"tenantName":"%s"}}""" % (
|
582 |
self.user1.uuid, self.user1.auth_token, self.user1.uuid) |
583 |
r = client.post(url, post_data, content_type='application/json')
|
584 |
self.assertEqual(r.status_code, 200) |
585 |
self.assertTrue(r['Content-Type'].startswith('application/json')) |
586 |
try:
|
587 |
body = json.loads(r.content) |
588 |
except Exception, e: |
589 |
self.fail(e)
|
590 |
|
591 |
try:
|
592 |
token = body['access']['token']['id'] |
593 |
user = body['access']['user']['id'] |
594 |
service_catalog = body['access']['serviceCatalog'] |
595 |
except KeyError: |
596 |
self.fail('Invalid response') |
597 |
|
598 |
self.assertEqual(token, self.user1.auth_token) |
599 |
self.assertEqual(user, self.user1.uuid) |
600 |
self.assertEqual(len(service_catalog), 3) |
601 |
|
602 |
# Check successful xml response
|
603 |
url = reverse('astakos.api.tokens.authenticate')
|
604 |
headers = {'HTTP_ACCEPT': 'application/xml'} |
605 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
606 |
"password":"%s"},
|
607 |
"tenantName":"%s"}}""" % (
|
608 |
self.user1.uuid, self.user1.auth_token, self.user1.uuid) |
609 |
r = client.post(url, post_data, content_type='application/json',
|
610 |
**headers) |
611 |
self.assertEqual(r.status_code, 200) |
612 |
self.assertTrue(r['Content-Type'].startswith('application/xml')) |
613 |
# try:
|
614 |
# body = minidom.parseString(r.content)
|
615 |
# except Exception, e:
|
616 |
# self.fail(e)
|
617 |
|
618 |
|
619 |
class WrongPathAPITest(TestCase): |
620 |
def test_catch_wrong_account_paths(self, *args): |
621 |
path = get_service_path(astakos_services, 'account', 'v1.0') |
622 |
path = join_urls(BASE_HOST, path, 'nonexistent')
|
623 |
response = self.client.get(path)
|
624 |
self.assertEqual(response.status_code, 400) |
625 |
try:
|
626 |
error = json.loads(response.content) |
627 |
except ValueError: |
628 |
self.assertTrue(False) |
629 |
|
630 |
def test_catch_wrong_identity_paths(self, *args): |
631 |
path = get_service_path(astakos_services, 'identity', 'v2.0') |
632 |
path = join_urls(BASE_HOST, path, 'nonexistent')
|
633 |
response = self.client.get(path)
|
634 |
self.assertEqual(response.status_code, 400) |
635 |
try:
|
636 |
error = json.loads(response.content) |
637 |
except ValueError: |
638 |
self.assertTrue(False) |