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