root / snf-astakos-app / astakos / im / tests / api.py @ a24b5bda
History | View | Annotate | Download (21.1 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 |
|
36 |
from django.test import TestCase |
37 |
from django.core.urlresolvers import reverse |
38 |
|
39 |
#from xml.dom import minidom
|
40 |
|
41 |
import json |
42 |
|
43 |
ROOT = "/%s/%s/" % (
|
44 |
astakos_settings.BASE_PATH, astakos_settings.ACCOUNTS_PREFIX) |
45 |
u = lambda url: ROOT + url
|
46 |
|
47 |
|
48 |
class QuotaAPITest(TestCase): |
49 |
def test_0(self): |
50 |
client = Client() |
51 |
|
52 |
component1 = Component.objects.create(name="comp1")
|
53 |
register.add_service(component1, "service1", "type1", []) |
54 |
# custom service resources
|
55 |
resource11 = {"name": "service1.resource11", |
56 |
"desc": "resource11 desc", |
57 |
"service_type": "type1", |
58 |
"allow_in_projects": True} |
59 |
r, _ = register.add_resource(resource11) |
60 |
register.update_resource(r, 100)
|
61 |
resource12 = {"name": "service1.resource12", |
62 |
"desc": "resource11 desc", |
63 |
"service_type": "type1", |
64 |
"unit": "bytes"} |
65 |
r, _ = register.add_resource(resource12) |
66 |
register.update_resource(r, 1024)
|
67 |
|
68 |
# create user
|
69 |
user = get_local_user('test@grnet.gr')
|
70 |
quotas.qh_sync_user(user) |
71 |
|
72 |
component2 = Component.objects.create(name="comp2")
|
73 |
register.add_service(component2, "service2", "type2", []) |
74 |
# create another service
|
75 |
resource21 = {"name": "service2.resource21", |
76 |
"desc": "resource11 desc", |
77 |
"service_type": "type2", |
78 |
"allow_in_projects": False} |
79 |
r, _ = register.add_resource(resource21) |
80 |
register.update_resource(r, 3)
|
81 |
|
82 |
resource_names = [r['name'] for r in |
83 |
[resource11, resource12, resource21]] |
84 |
|
85 |
# get resources
|
86 |
r = client.get(u('resources'))
|
87 |
self.assertEqual(r.status_code, 200) |
88 |
body = json.loads(r.content) |
89 |
for name in resource_names: |
90 |
assertIn(name, body) |
91 |
|
92 |
# get quota
|
93 |
r = client.get(u('quotas'))
|
94 |
self.assertEqual(r.status_code, 401) |
95 |
|
96 |
headers = {'HTTP_X_AUTH_TOKEN': user.auth_token}
|
97 |
r = client.get(u('quotas/'), **headers)
|
98 |
self.assertEqual(r.status_code, 200) |
99 |
body = json.loads(r.content) |
100 |
system_quota = body['system']
|
101 |
assertIn('system', body)
|
102 |
for name in resource_names: |
103 |
assertIn(name, system_quota) |
104 |
|
105 |
r = client.get(u('service_quotas'))
|
106 |
self.assertEqual(r.status_code, 401) |
107 |
|
108 |
s1_headers = {'HTTP_X_AUTH_TOKEN': component1.auth_token}
|
109 |
r = client.get(u('service_quotas'), **s1_headers)
|
110 |
self.assertEqual(r.status_code, 200) |
111 |
body = json.loads(r.content) |
112 |
assertIn(user.uuid, body) |
113 |
|
114 |
r = client.get(u('commissions'), **s1_headers)
|
115 |
self.assertEqual(r.status_code, 200) |
116 |
body = json.loads(r.content) |
117 |
self.assertEqual(body, [])
|
118 |
|
119 |
# issue some commissions
|
120 |
commission_request = { |
121 |
"force": False, |
122 |
"auto_accept": False, |
123 |
"name": "my commission", |
124 |
"provisions": [
|
125 |
{ |
126 |
"holder": user.uuid,
|
127 |
"source": "system", |
128 |
"resource": resource11['name'], |
129 |
"quantity": 1 |
130 |
}, |
131 |
{ |
132 |
"holder": user.uuid,
|
133 |
"source": "system", |
134 |
"resource": resource12['name'], |
135 |
"quantity": 30000 |
136 |
}]} |
137 |
|
138 |
post_data = json.dumps(commission_request) |
139 |
r = client.post(u('commissions'), post_data,
|
140 |
content_type='application/json', **s1_headers)
|
141 |
self.assertEqual(r.status_code, 413) |
142 |
|
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": 100 |
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, 201) |
165 |
body = json.loads(r.content) |
166 |
serial = body['serial']
|
167 |
self.assertEqual(serial, 1) |
168 |
|
169 |
post_data = json.dumps(commission_request) |
170 |
r = client.post(u('commissions'), post_data,
|
171 |
content_type='application/json', **s1_headers)
|
172 |
self.assertEqual(r.status_code, 201) |
173 |
body = json.loads(r.content) |
174 |
self.assertEqual(body['serial'], 2) |
175 |
|
176 |
post_data = json.dumps(commission_request) |
177 |
r = client.post(u('commissions'), post_data,
|
178 |
content_type='application/json', **s1_headers)
|
179 |
self.assertEqual(r.status_code, 201) |
180 |
body = json.loads(r.content) |
181 |
self.assertEqual(body['serial'], 3) |
182 |
|
183 |
r = client.get(u('commissions'), **s1_headers)
|
184 |
self.assertEqual(r.status_code, 200) |
185 |
body = json.loads(r.content) |
186 |
self.assertEqual(body, [1, 2, 3]) |
187 |
|
188 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
189 |
self.assertEqual(r.status_code, 200) |
190 |
body = json.loads(r.content) |
191 |
self.assertEqual(body['serial'], serial) |
192 |
assertIn('issue_time', body)
|
193 |
self.assertEqual(body['provisions'], commission_request['provisions']) |
194 |
self.assertEqual(body['name'], commission_request['name']) |
195 |
|
196 |
r = client.get(u('service_quotas?user=' + user.uuid), **s1_headers)
|
197 |
self.assertEqual(r.status_code, 200) |
198 |
body = json.loads(r.content) |
199 |
user_quota = body[user.uuid] |
200 |
system_quota = user_quota['system']
|
201 |
r11 = system_quota[resource11['name']]
|
202 |
self.assertEqual(r11['usage'], 3) |
203 |
self.assertEqual(r11['pending'], 3) |
204 |
|
205 |
# resolve pending commissions
|
206 |
resolve_data = { |
207 |
"accept": [1, 3], |
208 |
"reject": [2, 3, 4], |
209 |
} |
210 |
post_data = json.dumps(resolve_data) |
211 |
|
212 |
r = client.post(u('commissions/action'), post_data,
|
213 |
content_type='application/json', **s1_headers)
|
214 |
self.assertEqual(r.status_code, 200) |
215 |
body = json.loads(r.content) |
216 |
self.assertEqual(body['accepted'], [1]) |
217 |
self.assertEqual(body['rejected'], [2]) |
218 |
failed = body['failed']
|
219 |
self.assertEqual(len(failed), 2) |
220 |
|
221 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
222 |
self.assertEqual(r.status_code, 404) |
223 |
|
224 |
# auto accept
|
225 |
commission_request = { |
226 |
"auto_accept": True, |
227 |
"name": "my commission", |
228 |
"provisions": [
|
229 |
{ |
230 |
"holder": user.uuid,
|
231 |
"source": "system", |
232 |
"resource": resource11['name'], |
233 |
"quantity": 1 |
234 |
}, |
235 |
{ |
236 |
"holder": user.uuid,
|
237 |
"source": "system", |
238 |
"resource": resource12['name'], |
239 |
"quantity": 100 |
240 |
}]} |
241 |
|
242 |
post_data = json.dumps(commission_request) |
243 |
r = client.post(u('commissions'), post_data,
|
244 |
content_type='application/json', **s1_headers)
|
245 |
self.assertEqual(r.status_code, 201) |
246 |
body = json.loads(r.content) |
247 |
serial = body['serial']
|
248 |
self.assertEqual(serial, 4) |
249 |
|
250 |
r = client.get(u('commissions/' + str(serial)), **s1_headers) |
251 |
self.assertEqual(r.status_code, 404) |
252 |
|
253 |
# malformed
|
254 |
commission_request = { |
255 |
"auto_accept": True, |
256 |
"name": "my commission", |
257 |
"provisions": [
|
258 |
{ |
259 |
"holder": user.uuid,
|
260 |
"source": "system", |
261 |
"resource": resource11['name'], |
262 |
} |
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, 400) |
269 |
|
270 |
commission_request = { |
271 |
"auto_accept": True, |
272 |
"name": "my commission", |
273 |
"provisions": "dummy"} |
274 |
|
275 |
post_data = json.dumps(commission_request) |
276 |
r = client.post(u('commissions'), post_data,
|
277 |
content_type='application/json', **s1_headers)
|
278 |
self.assertEqual(r.status_code, 400) |
279 |
|
280 |
r = client.post(u('commissions'), commission_request,
|
281 |
content_type='application/json', **s1_headers)
|
282 |
self.assertEqual(r.status_code, 400) |
283 |
|
284 |
# no holding
|
285 |
commission_request = { |
286 |
"auto_accept": True, |
287 |
"name": "my commission", |
288 |
"provisions": [
|
289 |
{ |
290 |
"holder": user.uuid,
|
291 |
"source": "system", |
292 |
"resource": "non existent", |
293 |
"quantity": 1 |
294 |
}, |
295 |
{ |
296 |
"holder": user.uuid,
|
297 |
"source": "system", |
298 |
"resource": resource12['name'], |
299 |
"quantity": 100 |
300 |
}]} |
301 |
|
302 |
post_data = json.dumps(commission_request) |
303 |
r = client.post(u('commissions'), post_data,
|
304 |
content_type='application/json', **s1_headers)
|
305 |
self.assertEqual(r.status_code, 404) |
306 |
|
307 |
# release
|
308 |
commission_request = { |
309 |
"provisions": [
|
310 |
{ |
311 |
"holder": user.uuid,
|
312 |
"source": "system", |
313 |
"resource": resource11['name'], |
314 |
"quantity": -1 |
315 |
} |
316 |
]} |
317 |
|
318 |
post_data = json.dumps(commission_request) |
319 |
r = client.post(u('commissions'), post_data,
|
320 |
content_type='application/json', **s1_headers)
|
321 |
self.assertEqual(r.status_code, 201) |
322 |
body = json.loads(r.content) |
323 |
serial = body['serial']
|
324 |
|
325 |
accept_data = {'accept': ""} |
326 |
post_data = json.dumps(accept_data) |
327 |
r = client.post(u('commissions/' + str(serial) + '/action'), post_data, |
328 |
content_type='application/json', **s1_headers)
|
329 |
self.assertEqual(r.status_code, 200) |
330 |
|
331 |
reject_data = {'reject': ""} |
332 |
post_data = json.dumps(accept_data) |
333 |
r = client.post(u('commissions/' + str(serial) + '/action'), post_data, |
334 |
content_type='application/json', **s1_headers)
|
335 |
self.assertEqual(r.status_code, 404) |
336 |
|
337 |
# force
|
338 |
commission_request = { |
339 |
"force": True, |
340 |
"provisions": [
|
341 |
{ |
342 |
"holder": user.uuid,
|
343 |
"source": "system", |
344 |
"resource": resource11['name'], |
345 |
"quantity": 100 |
346 |
}]} |
347 |
|
348 |
post_data = json.dumps(commission_request) |
349 |
r = client.post(u('commissions'), post_data,
|
350 |
content_type='application/json', **s1_headers)
|
351 |
self.assertEqual(r.status_code, 201) |
352 |
|
353 |
commission_request = { |
354 |
"force": True, |
355 |
"provisions": [
|
356 |
{ |
357 |
"holder": user.uuid,
|
358 |
"source": "system", |
359 |
"resource": resource11['name'], |
360 |
"quantity": -200 |
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, 413) |
367 |
|
368 |
r = client.get(u('quotas'), **headers)
|
369 |
self.assertEqual(r.status_code, 200) |
370 |
body = json.loads(r.content) |
371 |
system_quota = body['system']
|
372 |
r11 = system_quota[resource11['name']]
|
373 |
self.assertEqual(r11['usage'], 102) |
374 |
self.assertEqual(r11['pending'], 101) |
375 |
|
376 |
|
377 |
class TokensApiTest(TestCase): |
378 |
def setUp(self): |
379 |
backend = activation_backends.get_backend() |
380 |
|
381 |
self.user1 = AstakosUser.objects.create(
|
382 |
email='test1', email_verified=True, moderated=True, |
383 |
is_rejected=False)
|
384 |
backend.activate_user(self.user1)
|
385 |
assert self.user1.is_active is True |
386 |
|
387 |
self.user2 = AstakosUser.objects.create(
|
388 |
email='test2', email_verified=True, moderated=True, |
389 |
is_rejected=False)
|
390 |
backend.activate_user(self.user2)
|
391 |
assert self.user2.is_active is True |
392 |
|
393 |
c1 = Component(name='component1', url='http://localhost/component1') |
394 |
c1.save() |
395 |
s1 = Service(component=c1, type='type1', name='service1') |
396 |
s1.save() |
397 |
e1 = Endpoint(service=s1) |
398 |
e1.save() |
399 |
e1.data.create(key='versionId', value='v1.0') |
400 |
e1.data.create(key='publicURL', value='http://localhost:8000/s1/v1.0') |
401 |
|
402 |
s2 = Service(component=c1, type='type2', name='service2') |
403 |
s2.save() |
404 |
e2 = Endpoint(service=s2) |
405 |
e2.save() |
406 |
e2.data.create(key='versionId', value='v1.0') |
407 |
e2.data.create(key='publicURL', value='http://localhost:8000/s2/v1.0') |
408 |
|
409 |
c2 = Component(name='component2', url='http://localhost/component2') |
410 |
c2.save() |
411 |
s3 = Service(component=c2, type='type3', name='service3') |
412 |
s3.save() |
413 |
e3 = Endpoint(service=s3) |
414 |
e3.save() |
415 |
e3.data.create(key='versionId', value='v2.0') |
416 |
e3.data.create(key='publicURL', value='http://localhost:8000/s3/v2.0') |
417 |
|
418 |
def test_authenticate(self): |
419 |
client = Client() |
420 |
|
421 |
# Check not allowed method
|
422 |
url = reverse('astakos.api.tokens.authenticate')
|
423 |
r = client.get(url, post_data={}) |
424 |
self.assertEqual(r.status_code, 400) |
425 |
|
426 |
# Malformed request
|
427 |
url = reverse('astakos.api.tokens.authenticate')
|
428 |
r = client.post(url, post_data={}) |
429 |
self.assertEqual(r.status_code, 400) |
430 |
|
431 |
# Check unsupported xml input
|
432 |
url = reverse('astakos.api.tokens.authenticate')
|
433 |
post_data = """
|
434 |
<?xml version="1.0" encoding="UTF-8"?>
|
435 |
<auth xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
436 |
xmlns="http://docs.openstack.org/identity/api/v2.0"
|
437 |
tenantName="%s">
|
438 |
<passwordCredentials username="%s" password="%s"/>
|
439 |
</auth>""" % (self.user1.uuid, self.user1.uuid, |
440 |
self.user1.auth_token)
|
441 |
r = client.post(url, post_data, content_type='application/xml')
|
442 |
self.assertEqual(r.status_code, 400) |
443 |
body = json.loads(r.content) |
444 |
self.assertEqual(body['badRequest']['message'], |
445 |
"Unsupported Content-type: 'application/xml'")
|
446 |
|
447 |
# Check malformed request: missing password
|
448 |
url = reverse('astakos.api.tokens.authenticate')
|
449 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s"},
|
450 |
"tenantName":"%s"}}""" % (
|
451 |
self.user1.uuid, self.user1.uuid) |
452 |
r = client.post(url, post_data, content_type='application/json')
|
453 |
self.assertEqual(r.status_code, 400) |
454 |
body = json.loads(r.content) |
455 |
self.assertEqual(body['badRequest']['message'], |
456 |
'Malformed request')
|
457 |
|
458 |
# Check malformed request: missing username
|
459 |
url = reverse('astakos.api.tokens.authenticate')
|
460 |
post_data = """{"auth":{"passwordCredentials":{"password":"%s"},
|
461 |
"tenantName":"%s"}}""" % (
|
462 |
self.user1.auth_token, self.user1.uuid) |
463 |
r = client.post(url, post_data, content_type='application/json')
|
464 |
self.assertEqual(r.status_code, 400) |
465 |
body = json.loads(r.content) |
466 |
self.assertEqual(body['badRequest']['message'], |
467 |
'Malformed request')
|
468 |
|
469 |
# Check invalid pass
|
470 |
url = reverse('astakos.api.tokens.authenticate')
|
471 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
472 |
"password":"%s"},
|
473 |
"tenantName":"%s"}}""" % (
|
474 |
self.user1.uuid, '', self.user1.uuid) |
475 |
r = client.post(url, post_data, content_type='application/json')
|
476 |
self.assertEqual(r.status_code, 401) |
477 |
body = json.loads(r.content) |
478 |
self.assertEqual(body['unauthorized']['message'], |
479 |
'Invalid token')
|
480 |
|
481 |
# Check inconsistent pass
|
482 |
url = reverse('astakos.api.tokens.authenticate')
|
483 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
484 |
"password":"%s"},
|
485 |
"tenantName":"%s"}}""" % (
|
486 |
self.user1.uuid, self.user2.auth_token, self.user2.uuid) |
487 |
r = client.post(url, post_data, content_type='application/json')
|
488 |
self.assertEqual(r.status_code, 401) |
489 |
body = json.loads(r.content) |
490 |
self.assertEqual(body['unauthorized']['message'], |
491 |
'Invalid credentials')
|
492 |
|
493 |
# Check invalid json data
|
494 |
url = reverse('astakos.api.tokens.authenticate')
|
495 |
r = client.post(url, "not json", content_type='application/json') |
496 |
self.assertEqual(r.status_code, 400) |
497 |
body = json.loads(r.content) |
498 |
self.assertEqual(body['badRequest']['message'], 'Invalid JSON data') |
499 |
|
500 |
# Check auth with token
|
501 |
url = reverse('astakos.api.tokens.authenticate')
|
502 |
post_data = """{"auth":{"token": {"id":"%s"},
|
503 |
"tenantName":"%s"}}""" % (
|
504 |
self.user1.auth_token, self.user1.uuid) |
505 |
r = client.post(url, post_data, content_type='application/json')
|
506 |
self.assertEqual(r.status_code, 200) |
507 |
self.assertTrue(r['Content-Type'].startswith('application/json')) |
508 |
try:
|
509 |
body = json.loads(r.content) |
510 |
except Exception, e: |
511 |
self.fail(e)
|
512 |
|
513 |
# Check successful json response
|
514 |
url = reverse('astakos.api.tokens.authenticate')
|
515 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
516 |
"password":"%s"},
|
517 |
"tenantName":"%s"}}""" % (
|
518 |
self.user1.uuid, self.user1.auth_token, self.user1.uuid) |
519 |
r = client.post(url, post_data, content_type='application/json')
|
520 |
self.assertEqual(r.status_code, 200) |
521 |
self.assertTrue(r['Content-Type'].startswith('application/json')) |
522 |
try:
|
523 |
body = json.loads(r.content) |
524 |
except Exception, e: |
525 |
self.fail(e)
|
526 |
|
527 |
try:
|
528 |
token = body['access']['token']['id'] |
529 |
user = body['access']['user']['id'] |
530 |
service_catalog = body['access']['serviceCatalog'] |
531 |
except KeyError: |
532 |
self.fail('Invalid response') |
533 |
|
534 |
self.assertEqual(token, self.user1.auth_token) |
535 |
self.assertEqual(user, self.user1.uuid) |
536 |
self.assertEqual(len(service_catalog), 3) |
537 |
|
538 |
# Check successful xml response
|
539 |
url = reverse('astakos.api.tokens.authenticate')
|
540 |
headers = {'HTTP_ACCEPT': 'application/xml'} |
541 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s",
|
542 |
"password":"%s"},
|
543 |
"tenantName":"%s"}}""" % (
|
544 |
self.user1.uuid, self.user1.auth_token, self.user1.uuid) |
545 |
r = client.post(url, post_data, content_type='application/json',
|
546 |
**headers) |
547 |
self.assertEqual(r.status_code, 200) |
548 |
self.assertTrue(r['Content-Type'].startswith('application/xml')) |
549 |
# try:
|
550 |
# body = minidom.parseString(r.content)
|
551 |
# except Exception, e:
|
552 |
# self.fail(e)
|