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