root / astakosclient / astakosclient / tests.py @ 45c0bcf8
History | View | Annotate | Download (31.2 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
#
|
3 |
# Copyright (C) 2012, 2013 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
13 |
# 2. Redistributions in binary form must reproduce the above
|
14 |
# copyright notice, this list of conditions and the following
|
15 |
# disclaimer in the documentation and/or other materials
|
16 |
# provided with the distribution.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 |
# POSSIBILITY OF SUCH DAMAGE.
|
30 |
#
|
31 |
# The views and conclusions contained in the software and
|
32 |
# documentation are those of the authors and should not be
|
33 |
# interpreted as representing official policies, either expressed
|
34 |
# or implied, of GRNET S.A.
|
35 |
|
36 |
"""Unit Tests for the astakos-client module
|
37 |
|
38 |
Provides unit tests for the code implementing
|
39 |
the astakos client library
|
40 |
|
41 |
"""
|
42 |
|
43 |
import re |
44 |
import sys |
45 |
import simplejson |
46 |
|
47 |
import astakosclient |
48 |
from astakosclient import AstakosClient |
49 |
from astakosclient.utils import join_urls |
50 |
from astakosclient.errors import \ |
51 |
AstakosClientException, Unauthorized, BadRequest, NotFound, \ |
52 |
NoUserName, NoUUID, BadValue, QuotaLimit |
53 |
|
54 |
# Use backported unittest functionality if Python < 2.7
|
55 |
try:
|
56 |
import unittest2 as unittest |
57 |
except ImportError: |
58 |
if sys.version_info < (2, 7): |
59 |
raise Exception("The unittest2 package is required for Python < 2.7") |
60 |
import unittest |
61 |
|
62 |
|
63 |
# --------------------------------------------------------------------
|
64 |
# Helper functions
|
65 |
auth_url = "https://example.org/identity/v2.0"
|
66 |
account_prefix = "/account_prefix"
|
67 |
ui_prefix = "/ui_prefix"
|
68 |
api_tokens = "/identity/v2.0/tokens"
|
69 |
api_usercatalogs = join_urls(account_prefix, "user_catalogs")
|
70 |
api_resources = join_urls(account_prefix, "resources")
|
71 |
api_quotas = join_urls(account_prefix, "quotas")
|
72 |
api_commissions = join_urls(account_prefix, "commissions")
|
73 |
|
74 |
# --------------------------------------
|
75 |
# Local users
|
76 |
token = { |
77 |
'id': "skzleaFlBl+fasFdaf24sx", |
78 |
'tenant': {
|
79 |
'id': "73917abc-abcd-477e-a1f1-1763abcdefab", |
80 |
'name': "Example User One", |
81 |
}, |
82 |
} |
83 |
|
84 |
user = { |
85 |
'id': "73917abc-abcd-477e-a1f1-1763abcdefab", |
86 |
'name': "Example User One", |
87 |
'roles': [{u'id': 1, u'name': u'default'}, |
88 |
{u'id': 5, u'name': u'academic-login-users'}], |
89 |
'roles_links': []
|
90 |
} |
91 |
|
92 |
resources = { |
93 |
"cyclades.ram": {
|
94 |
"unit": "bytes", |
95 |
"description": "Virtual machine memory", |
96 |
"service": "cyclades"}} |
97 |
|
98 |
endpoints = { |
99 |
"access": {
|
100 |
"serviceCatalog": [{
|
101 |
"endpoints": [{"SNF:uiURL": join_urls("https://example.org/", |
102 |
ui_prefix), |
103 |
"publicURL": join_urls("https://example.org/", |
104 |
account_prefix), |
105 |
"region": "default", |
106 |
"versionId": "v1.0"}], |
107 |
"name": "astakos_account", |
108 |
"type": "account"}] |
109 |
} |
110 |
} |
111 |
|
112 |
endpoints_with_info = dict(endpoints)
|
113 |
endpoints_with_info['access']['token'] = dict(token) |
114 |
endpoints_with_info['access']['user'] = dict(user) |
115 |
|
116 |
quotas = { |
117 |
"system": {
|
118 |
"cyclades.ram": {
|
119 |
"pending": 0, |
120 |
"limit": 1073741824, |
121 |
"usage": 536870912}, |
122 |
"cyclades.vm": {
|
123 |
"pending": 0, |
124 |
"limit": 2, |
125 |
"usage": 2}}, |
126 |
"project:1": {
|
127 |
"cyclades.ram": {
|
128 |
"pending": 0, |
129 |
"limit": 2147483648, |
130 |
"usage": 2147483648}, |
131 |
"cyclades.vm": {
|
132 |
"pending": 1, |
133 |
"limit": 5, |
134 |
"usage": 2}}} |
135 |
|
136 |
commission_request = { |
137 |
"force": False, |
138 |
"auto_accept": False, |
139 |
"name": "my commission", |
140 |
"provisions": [
|
141 |
{ |
142 |
"holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad", |
143 |
"source": "system", |
144 |
"resource": "cyclades.vm", |
145 |
"quantity": 1 |
146 |
}, |
147 |
{ |
148 |
"holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad", |
149 |
"source": "system", |
150 |
"resource": "cyclades.ram", |
151 |
"quantity": 30000 |
152 |
}]} |
153 |
|
154 |
commission_successful_response = {"serial": 57} |
155 |
|
156 |
commission_failure_response = { |
157 |
"overLimit": {
|
158 |
"message": "a human-readable error message", |
159 |
"code": 413, |
160 |
"data": {
|
161 |
"provision": {
|
162 |
"holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad", |
163 |
"source": "system", |
164 |
"resource": "cyclades.ram", |
165 |
"quantity": 520000000}, |
166 |
"name": "NoCapacityError", |
167 |
"limit": 600000000, |
168 |
"usage": 180000000}}} |
169 |
|
170 |
pending_commissions = [100, 200] |
171 |
|
172 |
commission_description = { |
173 |
"serial": 57, |
174 |
"issue_time": "2013-04-08T10:19:15.0373+00:00", |
175 |
"name": "a commission", |
176 |
"provisions": [
|
177 |
{ |
178 |
"holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad", |
179 |
"source": "system", |
180 |
"resource": "cyclades.vm", |
181 |
"quantity": 1 |
182 |
}, |
183 |
{ |
184 |
"holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad", |
185 |
"source": "system", |
186 |
"resource": "cyclades.ram", |
187 |
"quantity": 536870912 |
188 |
}]} |
189 |
|
190 |
resolve_commissions_req = { |
191 |
"accept": [56, 57], |
192 |
"reject": [56, 58, 59]} |
193 |
|
194 |
resolve_commissions_rep = { |
195 |
"accepted": [57], |
196 |
"rejected": [59], |
197 |
"failed": [
|
198 |
[56, {
|
199 |
"badRequest": {
|
200 |
"message": "cannot both accept and reject serial 56", |
201 |
"code": 400}}], |
202 |
[58, {
|
203 |
"itemNotFound": {
|
204 |
"message": "serial 58 does not exist", |
205 |
"code": 404}}]]} |
206 |
|
207 |
|
208 |
# ----------------------------
|
209 |
# These functions will be used as mocked requests
|
210 |
def _request_status_302(conn, method, url, **kwargs): |
211 |
"""This request returns 302"""
|
212 |
message = "FOUND"
|
213 |
status = 302
|
214 |
data = "302 Found"
|
215 |
return (message, data, status)
|
216 |
|
217 |
|
218 |
def _request_status_404(conn, method, url, **kwargs): |
219 |
"""This request returns 404"""
|
220 |
message = "Not Found"
|
221 |
status = 404
|
222 |
data = "404 Not Found"
|
223 |
return (message, data, status)
|
224 |
|
225 |
|
226 |
def _request_status_403(conn, method, url, **kwargs): |
227 |
"""This request returns 403"""
|
228 |
message = "UNAUTHORIZED"
|
229 |
status = 403
|
230 |
data = "Forbidden"
|
231 |
return (message, data, status)
|
232 |
|
233 |
|
234 |
def _request_status_401(conn, method, url, **kwargs): |
235 |
"""This request returns 401"""
|
236 |
message = "UNAUTHORIZED"
|
237 |
status = 401
|
238 |
data = "Invalid X-Auth-Token\n"
|
239 |
return (message, data, status)
|
240 |
|
241 |
|
242 |
def _request_status_400(conn, method, url, **kwargs): |
243 |
"""This request returns 400"""
|
244 |
message = "BAD REQUEST"
|
245 |
status = 400
|
246 |
data = "Method not allowed.\n"
|
247 |
return (message, data, status)
|
248 |
|
249 |
|
250 |
def _mock_request(conn, method, url, **kwargs): |
251 |
"""This request behaves like original Astakos does"""
|
252 |
if api_tokens == url:
|
253 |
return _req_tokens(conn, method, url, **kwargs)
|
254 |
elif api_usercatalogs == url:
|
255 |
return _req_catalogs(conn, method, url, **kwargs)
|
256 |
elif api_resources == url:
|
257 |
return _req_resources(conn, method, url, **kwargs)
|
258 |
elif api_quotas == url:
|
259 |
return _req_quotas(conn, method, url, **kwargs)
|
260 |
elif url.startswith(api_commissions):
|
261 |
return _req_commission(conn, method, url, **kwargs)
|
262 |
else:
|
263 |
return _request_status_404(conn, method, url, **kwargs)
|
264 |
|
265 |
|
266 |
def _req_tokens(conn, method, url, **kwargs): |
267 |
"""Return endpoints"""
|
268 |
global token, user, endpoints
|
269 |
|
270 |
# Check input
|
271 |
if conn.__class__.__name__ != "HTTPSConnection": |
272 |
return _request_status_302(conn, method, url, **kwargs)
|
273 |
if method != "POST": |
274 |
return _request_status_400(conn, method, url, **kwargs)
|
275 |
req_token = kwargs['headers'].get('X-Auth-Token') |
276 |
if req_token != token['id']: |
277 |
return _request_status_401(conn, method, url, **kwargs)
|
278 |
|
279 |
if 'body' in kwargs: |
280 |
# Return endpoints with authenticate info
|
281 |
return ("", simplejson.dumps(endpoints_with_info), 200) |
282 |
else:
|
283 |
# Return endpoints without authenticate info
|
284 |
return ("", simplejson.dumps(endpoints), 200) |
285 |
|
286 |
|
287 |
def _req_catalogs(conn, method, url, **kwargs): |
288 |
"""Return user catalogs"""
|
289 |
global token, user
|
290 |
|
291 |
# Check input
|
292 |
if conn.__class__.__name__ != "HTTPSConnection": |
293 |
return _request_status_302(conn, method, url, **kwargs)
|
294 |
if method != "POST": |
295 |
return _request_status_400(conn, method, url, **kwargs)
|
296 |
req_token = kwargs['headers'].get('X-Auth-Token') |
297 |
if req_token != token['id']: |
298 |
return _request_status_401(conn, method, url, **kwargs)
|
299 |
|
300 |
# Return
|
301 |
body = simplejson.loads(kwargs['body'])
|
302 |
if 'uuids' in body: |
303 |
# Return uuid_catalog
|
304 |
uuids = body['uuids']
|
305 |
catalogs = {} |
306 |
if user['id'] in uuids: |
307 |
catalogs[user['id']] = user['name'] |
308 |
return_catalog = {"displayname_catalog": {}, "uuid_catalog": catalogs} |
309 |
elif 'displaynames' in body: |
310 |
# Return displayname_catalog
|
311 |
names = body['displaynames']
|
312 |
catalogs = {} |
313 |
if user['name'] in names: |
314 |
catalogs[user['name']] = user['id'] |
315 |
return_catalog = {"displayname_catalog": catalogs, "uuid_catalog": {}} |
316 |
else:
|
317 |
return_catalog = {"displayname_catalog": {}, "uuid_catalog": {}} |
318 |
return ("", simplejson.dumps(return_catalog), 200) |
319 |
|
320 |
|
321 |
def _req_resources(conn, method, url, **kwargs): |
322 |
"""Return quota resources"""
|
323 |
global resources
|
324 |
|
325 |
# Check input
|
326 |
if conn.__class__.__name__ != "HTTPSConnection": |
327 |
return _request_status_302(conn, method, url, **kwargs)
|
328 |
if method != "GET": |
329 |
return _request_status_400(conn, method, url, **kwargs)
|
330 |
|
331 |
# Return
|
332 |
return ("", simplejson.dumps(resources), 200) |
333 |
|
334 |
|
335 |
def _req_quotas(conn, method, url, **kwargs): |
336 |
"""Return quotas for user_1"""
|
337 |
global token, quotas
|
338 |
|
339 |
# Check input
|
340 |
if conn.__class__.__name__ != "HTTPSConnection": |
341 |
return _request_status_302(conn, method, url, **kwargs)
|
342 |
if method != "GET": |
343 |
return _request_status_400(conn, method, url, **kwargs)
|
344 |
req_token = kwargs['headers'].get('X-Auth-Token') |
345 |
if req_token != token['id']: |
346 |
return _request_status_401(conn, method, url, **kwargs)
|
347 |
|
348 |
# Return
|
349 |
return ("", simplejson.dumps(quotas), 200) |
350 |
|
351 |
|
352 |
def _req_commission(conn, method, url, **kwargs): |
353 |
"""Perform a commission for user_1"""
|
354 |
global token, pending_commissions, \
|
355 |
commission_successful_response, commission_failure_response |
356 |
|
357 |
# Check input
|
358 |
if conn.__class__.__name__ != "HTTPSConnection": |
359 |
return _request_status_302(conn, method, url, **kwargs)
|
360 |
req_token = kwargs['headers'].get('X-Auth-Token') |
361 |
if req_token != token['id']: |
362 |
return _request_status_401(conn, method, url, **kwargs)
|
363 |
|
364 |
if method == "POST": |
365 |
if 'body' not in kwargs: |
366 |
return _request_status_400(conn, method, url, **kwargs)
|
367 |
body = simplejson.loads(unicode(kwargs['body'])) |
368 |
if re.match('/?'+api_commissions+'$', url) is not None: |
369 |
# Issue Commission
|
370 |
# Check if we have enough resources to give
|
371 |
if body['provisions'][1]['quantity'] > 420000000: |
372 |
return ("", simplejson.dumps(commission_failure_response), 413) |
373 |
else:
|
374 |
return \
|
375 |
("", simplejson.dumps(commission_successful_response), 200) |
376 |
else:
|
377 |
# Issue commission action
|
378 |
serial = url.split('/')[3] |
379 |
if serial == "action": |
380 |
# Resolve multiple actions
|
381 |
if body == resolve_commissions_req:
|
382 |
return ("", simplejson.dumps(resolve_commissions_rep), 200) |
383 |
else:
|
384 |
return _request_status_400(conn, method, url, **kwargs)
|
385 |
else:
|
386 |
# Issue action for one commission
|
387 |
if serial != str(57): |
388 |
return _request_status_404(conn, method, url, **kwargs)
|
389 |
if len(body) != 1: |
390 |
return _request_status_400(conn, method, url, **kwargs)
|
391 |
if "accept" not in body.keys() and "reject" not in body.keys(): |
392 |
return _request_status_400(conn, method, url, **kwargs)
|
393 |
return ("", "", 200) |
394 |
|
395 |
elif method == "GET": |
396 |
if re.match('/?'+api_commissions+'$', url) is not None: |
397 |
# Return pending commission
|
398 |
return ("", simplejson.dumps(pending_commissions), 200) |
399 |
else:
|
400 |
# Return commissions's description
|
401 |
serial = re.sub('/?' + api_commissions, '', url)[1:] |
402 |
if serial == str(57): |
403 |
return ("", simplejson.dumps(commission_description), 200) |
404 |
else:
|
405 |
return _request_status_404(conn, method, url, **kwargs)
|
406 |
else:
|
407 |
return _request_status_400(conn, method, url, **kwargs)
|
408 |
|
409 |
|
410 |
# --------------------------------------------------------------------
|
411 |
# The actual tests
|
412 |
|
413 |
class TestCallAstakos(unittest.TestCase): |
414 |
"""Test cases for function _callAstakos"""
|
415 |
|
416 |
# Patch astakosclient's _do_request function
|
417 |
def setUp(self): |
418 |
astakosclient._do_request = _mock_request |
419 |
|
420 |
# ----------------------------------
|
421 |
# Test the response we get if we send invalid token
|
422 |
def _invalid_token(self, pool): |
423 |
global auth_url
|
424 |
token = "skaksaFlBl+fasFdaf24sx"
|
425 |
try:
|
426 |
client = AstakosClient(token, auth_url, use_pool=pool) |
427 |
client.authenticate() |
428 |
except Unauthorized:
|
429 |
pass
|
430 |
except Exception: |
431 |
self.fail("Should have returned 401 (Invalid X-Auth-Token)") |
432 |
else:
|
433 |
self.fail("Should have returned 401 (Invalid X-Auth-Token)") |
434 |
|
435 |
def test_invalid_token(self): |
436 |
"""Test _invalid_token without pool"""
|
437 |
self._invalid_token(False) |
438 |
|
439 |
def test_invalid_token_pool(self): |
440 |
"""Test _invalid_token using pool"""
|
441 |
self._invalid_token(True) |
442 |
|
443 |
# ----------------------------------
|
444 |
# Test the response we get if we send invalid url
|
445 |
def _invalid_url(self, pool): |
446 |
global token, auth_url
|
447 |
try:
|
448 |
client = AstakosClient(token['id'], auth_url, use_pool=pool)
|
449 |
client._call_astakos("/astakos/api/misspelled")
|
450 |
except NotFound:
|
451 |
pass
|
452 |
except Exception, e: |
453 |
self.fail("Got \"%s\" instead of 404" % e) |
454 |
else:
|
455 |
self.fail("Should have returned 404 (Not Found)") |
456 |
|
457 |
def test_invalid_url(self): |
458 |
"""Test _invalid_url without pool"""
|
459 |
self._invalid_url(False) |
460 |
|
461 |
def test_invalid_url_pool(self): |
462 |
"""Test _invalid_url using pool"""
|
463 |
self._invalid_url(True) |
464 |
|
465 |
# ----------------------------------
|
466 |
# Test the response we get if we use an unsupported scheme
|
467 |
def _unsupported_scheme(self, pool): |
468 |
global token, auth_url
|
469 |
try:
|
470 |
client = AstakosClient( |
471 |
token['id'], "ftp://example.com", use_pool=pool) |
472 |
client.authenticate() |
473 |
except BadValue:
|
474 |
pass
|
475 |
except Exception: |
476 |
self.fail("Should have raise BadValue Exception") |
477 |
else:
|
478 |
self.fail("Should have raise BadValue Exception") |
479 |
|
480 |
def test_unsupported_scheme(self): |
481 |
"""Test _unsupported_scheme without pool"""
|
482 |
self._unsupported_scheme(False) |
483 |
|
484 |
def test_unsupported_scheme_pool(self): |
485 |
"""Test _unsupported_scheme using pool"""
|
486 |
self._unsupported_scheme(True) |
487 |
|
488 |
# ----------------------------------
|
489 |
# Test the response we get if we use http instead of https
|
490 |
def _http_scheme(self, pool): |
491 |
global token
|
492 |
http_auth_url = "http://example.org/identity/v2.0"
|
493 |
try:
|
494 |
client = AstakosClient(token['id'], http_auth_url, use_pool=pool)
|
495 |
client.authenticate() |
496 |
except AstakosClientException as err: |
497 |
if err.status != 302: |
498 |
self.fail("Should have returned 302 (Found)") |
499 |
else:
|
500 |
self.fail("Should have returned 302 (Found)") |
501 |
|
502 |
def test_http_scheme(self): |
503 |
"""Test _http_scheme without pool"""
|
504 |
self._http_scheme(False) |
505 |
|
506 |
def test_http_scheme_pool(self): |
507 |
"""Test _http_scheme using pool"""
|
508 |
self._http_scheme(True) |
509 |
|
510 |
# ----------------------------------
|
511 |
# Test the response we get if we use authenticate with GET
|
512 |
def _get_authenticate(self, pool): |
513 |
global token, auth_url
|
514 |
try:
|
515 |
client = AstakosClient(token['id'], auth_url, use_pool=pool)
|
516 |
client._call_astakos(api_tokens, method="GET")
|
517 |
except BadRequest:
|
518 |
pass
|
519 |
except Exception: |
520 |
self.fail("Should have returned 400 (Method not allowed)") |
521 |
else:
|
522 |
self.fail("Should have returned 400 (Method not allowed)") |
523 |
|
524 |
def test_get_authenticate(self): |
525 |
"""Test _get_authenticate without pool"""
|
526 |
self._get_authenticate(False) |
527 |
|
528 |
def test_get_authenticate_pool(self): |
529 |
"""Test _get_authenticate using pool"""
|
530 |
self._get_authenticate(True) |
531 |
|
532 |
# ----------------------------------
|
533 |
# Test the response if we request user_catalogs with GET
|
534 |
def _get_user_catalogs(self, pool): |
535 |
global token, auth_url, api_usercatalogs
|
536 |
try:
|
537 |
client = AstakosClient(token['id'], auth_url, use_pool=pool)
|
538 |
client._call_astakos(api_usercatalogs) |
539 |
except BadRequest:
|
540 |
pass
|
541 |
except Exception: |
542 |
self.fail("Should have returned 400 (Method not allowed)") |
543 |
else:
|
544 |
self.fail("Should have returned 400 (Method not allowed)") |
545 |
|
546 |
def test_get_user_catalogs(self): |
547 |
"""Test _get_user_catalogs without pool"""
|
548 |
self._get_user_catalogs(False) |
549 |
|
550 |
def test_get_user_catalogs_pool(self): |
551 |
"""Test _get_user_catalogs using pool"""
|
552 |
self._get_user_catalogs(True) |
553 |
|
554 |
|
555 |
class TestAuthenticate(unittest.TestCase): |
556 |
"""Test cases for function getUserInfo"""
|
557 |
|
558 |
# Patch astakosclient's _do_request function
|
559 |
def setUp(self): |
560 |
astakosclient._do_request = _mock_request |
561 |
|
562 |
# ----------------------------------
|
563 |
# Test the response we get for invalid token
|
564 |
def _invalid_token(self, pool): |
565 |
global auth_url
|
566 |
token = "skaksaFlBl+fasFdaf24sx"
|
567 |
try:
|
568 |
client = AstakosClient(token, auth_url, use_pool=pool) |
569 |
client.authenticate() |
570 |
except Unauthorized:
|
571 |
pass
|
572 |
except Exception: |
573 |
self.fail("Should have returned 401 (Invalid X-Auth-Token)") |
574 |
else:
|
575 |
self.fail("Should have returned 401 (Invalid X-Auth-Token)") |
576 |
|
577 |
def test_invalid_token(self): |
578 |
"""Test _invalid_token without pool"""
|
579 |
self._invalid_token(False) |
580 |
|
581 |
def test_invalid_token_pool(self): |
582 |
"""Test _invalid_token using pool"""
|
583 |
self._invalid_token(True) |
584 |
|
585 |
# ----------------------------------
|
586 |
# Test response for user
|
587 |
def _auth_user(self, pool): |
588 |
global token, endpoints_with_info, auth_url
|
589 |
try:
|
590 |
client = AstakosClient(token['id'], auth_url, use_pool=pool)
|
591 |
auth_info = client.authenticate() |
592 |
except:
|
593 |
self.fail("Shouldn't raise an Exception") |
594 |
self.assertEqual(endpoints_with_info, auth_info)
|
595 |
|
596 |
def test_auth_user(self): |
597 |
"""Test _auth_user without pool"""
|
598 |
self._auth_user(False) |
599 |
|
600 |
def test_auth_user_pool(self): |
601 |
"""Test _auth_user for User 1 using pool, with usage"""
|
602 |
self._auth_user(True) |
603 |
|
604 |
|
605 |
class TestDisplayNames(unittest.TestCase): |
606 |
"""Test cases for functions getDisplayNames/getDisplayName"""
|
607 |
|
608 |
# Patch astakosclient's _do_request function
|
609 |
def setUp(self): |
610 |
astakosclient._do_request = _mock_request |
611 |
|
612 |
# ----------------------------------
|
613 |
# Test the response we get for invalid token
|
614 |
def test_invalid_token(self): |
615 |
"""Test the response we get for invalid token (without pool)"""
|
616 |
global auth_url
|
617 |
token = "skaksaFlBl+fasFdaf24sx"
|
618 |
try:
|
619 |
client = AstakosClient(token, auth_url) |
620 |
client.get_usernames(["12412351"])
|
621 |
except Unauthorized:
|
622 |
pass
|
623 |
except Exception: |
624 |
self.fail("Should have returned 401 (Invalid X-Auth-Token)") |
625 |
else:
|
626 |
self.fail("Should have returned 401 (Invalid X-Auth-Token)") |
627 |
|
628 |
# ----------------------------------
|
629 |
# Get username
|
630 |
def test_username(self): |
631 |
"""Test get_username"""
|
632 |
global token, user, auth_url
|
633 |
try:
|
634 |
client = AstakosClient(token['id'], auth_url,
|
635 |
use_pool=False, retry=2) |
636 |
info = client.get_username(user['id'])
|
637 |
except Exception, e: |
638 |
self.fail("Shouldn't raise an Exception: %s" % e) |
639 |
self.assertEqual(info, user['name']) |
640 |
|
641 |
# ----------------------------------
|
642 |
# Get info with wrong uuid
|
643 |
def test_no_username(self): |
644 |
global token, auth_url
|
645 |
try:
|
646 |
client = AstakosClient(token['id'], auth_url)
|
647 |
client.get_username("1234")
|
648 |
except NoUserName:
|
649 |
pass
|
650 |
except:
|
651 |
self.fail("Should have raised NoDisplayName exception") |
652 |
else:
|
653 |
self.fail("Should have raised NoDisplayName exception") |
654 |
|
655 |
|
656 |
class TestGetUUIDs(unittest.TestCase): |
657 |
"""Test cases for functions getUUIDs/getUUID"""
|
658 |
|
659 |
# Patch astakosclient's _do_request function
|
660 |
def setUp(self): |
661 |
astakosclient._do_request = _mock_request |
662 |
|
663 |
# ----------------------------------
|
664 |
# Test the response we get for invalid token
|
665 |
def test_invalid_token(self): |
666 |
"""Test the response we get for invalid token (using pool)"""
|
667 |
global user, auth_url
|
668 |
token = "skaksaFlBl+fasFdaf24sx"
|
669 |
try:
|
670 |
client = AstakosClient(token, auth_url) |
671 |
client.get_uuids([user['name']])
|
672 |
except Unauthorized:
|
673 |
pass
|
674 |
except Exception: |
675 |
self.fail("Should have returned 401 (Invalid X-Auth-Token)") |
676 |
else:
|
677 |
self.fail("Should have returned 401 (Invalid X-Auth-Token)") |
678 |
|
679 |
# ----------------------------------
|
680 |
# Get uuid
|
681 |
def test_get_uuid(self): |
682 |
"""Test get_uuid"""
|
683 |
global token, user, auth_url
|
684 |
try:
|
685 |
client = AstakosClient(token['id'], auth_url, retry=1) |
686 |
catalog = client.get_uuids([user['name']])
|
687 |
except:
|
688 |
self.fail("Shouldn't raise an Exception") |
689 |
self.assertEqual(catalog[user['name']], user['id']) |
690 |
|
691 |
# ----------------------------------
|
692 |
# Get uuid with wrong username
|
693 |
def test_no_uuid(self): |
694 |
global token, auth_url
|
695 |
try:
|
696 |
client = AstakosClient(token['id'], auth_url)
|
697 |
client.get_uuid("1234")
|
698 |
except NoUUID:
|
699 |
pass
|
700 |
except:
|
701 |
self.fail("Should have raised NoUUID exception") |
702 |
else:
|
703 |
self.fail("Should have raised NoUUID exception") |
704 |
|
705 |
|
706 |
class TestResources(unittest.TestCase): |
707 |
"""Test cases for function get_resources"""
|
708 |
|
709 |
# Patch astakosclient's _do_request function
|
710 |
def setUp(self): |
711 |
astakosclient._do_request = _mock_request |
712 |
|
713 |
# ----------------------------------
|
714 |
def test_get_resources(self): |
715 |
"""Test function call of get_resources"""
|
716 |
global resources, auth_url, token
|
717 |
try:
|
718 |
client = AstakosClient(token['id'], auth_url, retry=1) |
719 |
result = client.get_resources() |
720 |
except Exception as err: |
721 |
self.fail("Shouldn't raise Exception %s" % err) |
722 |
self.assertEqual(resources, result)
|
723 |
|
724 |
|
725 |
class TestQuotas(unittest.TestCase): |
726 |
"""Test cases for function get_quotas"""
|
727 |
|
728 |
# Patch astakosclient's _do_request function
|
729 |
def setUp(self): |
730 |
astakosclient._do_request = _mock_request |
731 |
|
732 |
# ----------------------------------
|
733 |
def test_get_quotas(self): |
734 |
"""Test function call of get_quotas"""
|
735 |
global quotas, token, auth_url
|
736 |
try:
|
737 |
client = AstakosClient(token['id'], auth_url)
|
738 |
result = client.get_quotas() |
739 |
except Exception as err: |
740 |
self.fail("Shouldn't raise Exception %s" % err) |
741 |
self.assertEqual(quotas, result)
|
742 |
|
743 |
# -----------------------------------
|
744 |
def test_get_quotas_unauthorized(self): |
745 |
"""Test function call of get_quotas with wrong token"""
|
746 |
global auth_url
|
747 |
token = "buahfhsda"
|
748 |
try:
|
749 |
client = AstakosClient(token, auth_url) |
750 |
client.get_quotas() |
751 |
except Unauthorized:
|
752 |
pass
|
753 |
except Exception as err: |
754 |
self.fail("Shouldn't raise Exception %s" % err) |
755 |
else:
|
756 |
self.fail("Should have raised Unauthorized Exception") |
757 |
|
758 |
|
759 |
class TestCommissions(unittest.TestCase): |
760 |
"""Test cases for quota commissions"""
|
761 |
|
762 |
# Patch astakosclient's _do_request function
|
763 |
def setUp(self): |
764 |
astakosclient._do_request = _mock_request |
765 |
|
766 |
# ----------------------------------
|
767 |
def test_issue_commission(self): |
768 |
"""Test function call of issue_commission"""
|
769 |
global token, commission_request, commission_successful_reqsponse
|
770 |
global auth_url
|
771 |
try:
|
772 |
client = AstakosClient(token['id'], auth_url)
|
773 |
response = client.issue_commission(commission_request) |
774 |
except Exception as err: |
775 |
self.fail("Shouldn't raise Exception %s" % err) |
776 |
self.assertEqual(response, commission_successful_response['serial']) |
777 |
|
778 |
# ----------------------------------
|
779 |
def test_issue_commission_quota_limit(self): |
780 |
"""Test function call of issue_commission with limit exceeded"""
|
781 |
global token, commission_request, commission_failure_response
|
782 |
global auth_url
|
783 |
new_request = dict(commission_request)
|
784 |
new_request['provisions'][1]['quantity'] = 520000000 |
785 |
try:
|
786 |
client = AstakosClient(token['id'], auth_url)
|
787 |
client.issue_commission(new_request) |
788 |
except QuotaLimit:
|
789 |
pass
|
790 |
except Exception as err: |
791 |
self.fail("Shouldn't raise Exception %s" % err) |
792 |
else:
|
793 |
self.fail("Should have raised QuotaLimit Exception") |
794 |
|
795 |
# ----------------------------------
|
796 |
def test_issue_one_commission(self): |
797 |
"""Test function call of issue_one_commission"""
|
798 |
global token, commission_successful_response, auth_url
|
799 |
try:
|
800 |
client = AstakosClient(token['id'], auth_url)
|
801 |
response = client.issue_one_commission( |
802 |
"c02f315b-7d84-45bc-a383-552a3f97d2ad",
|
803 |
"system", {"cyclades.vm": 1, "cyclades.ram": 30000}) |
804 |
except Exception as err: |
805 |
self.fail("Shouldn't have raised Exception %s" % err) |
806 |
self.assertEqual(response, commission_successful_response['serial']) |
807 |
|
808 |
# ----------------------------------
|
809 |
def test_get_pending_commissions(self): |
810 |
"""Test function call of get_pending_commissions"""
|
811 |
global token, pending_commissions, auth_url
|
812 |
try:
|
813 |
client = AstakosClient(token['id'], auth_url)
|
814 |
response = client.get_pending_commissions() |
815 |
except Exception as err: |
816 |
self.fail("Shouldn't raise Exception %s" % err) |
817 |
self.assertEqual(response, pending_commissions)
|
818 |
|
819 |
# ----------------------------------
|
820 |
def test_get_commission_info(self): |
821 |
"""Test function call of get_commission_info"""
|
822 |
global token, commission_description, auth_url
|
823 |
try:
|
824 |
client = AstakosClient(token['id'], auth_url,
|
825 |
use_pool=True, pool_size=2) |
826 |
response = client.get_commission_info(57)
|
827 |
except Exception as err: |
828 |
self.fail("Shouldn't raise Exception %s" % err) |
829 |
self.assertEqual(response, commission_description)
|
830 |
|
831 |
# ----------------------------------
|
832 |
def test_get_commission_info_not_found(self): |
833 |
"""Test function call of get_commission_info with invalid serial"""
|
834 |
global token, auth_url
|
835 |
try:
|
836 |
client = AstakosClient(token['id'], auth_url)
|
837 |
client.get_commission_info("57lala")
|
838 |
except NotFound:
|
839 |
pass
|
840 |
except Exception as err: |
841 |
self.fail("Shouldn't raise Exception %s" % err) |
842 |
else:
|
843 |
self.fail("Should have raised NotFound") |
844 |
|
845 |
# ----------------------------------
|
846 |
def test_get_commission_info_without_serial(self): |
847 |
"""Test function call of get_commission_info without serial"""
|
848 |
global token, auth_url
|
849 |
try:
|
850 |
client = AstakosClient(token['id'], auth_url)
|
851 |
client.get_commission_info(None)
|
852 |
except BadValue:
|
853 |
pass
|
854 |
except Exception as err: |
855 |
self.fail("Shouldn't raise Exception %s" % err) |
856 |
else:
|
857 |
self.fail("Should have raise BadValue") |
858 |
|
859 |
# ----------------------------------
|
860 |
def test_commision_action(self): |
861 |
"""Test function call of commision_action with wrong action"""
|
862 |
global token, auth_url
|
863 |
try:
|
864 |
client = AstakosClient(token['id'], auth_url)
|
865 |
client.commission_action(57, "lala") |
866 |
except BadRequest:
|
867 |
pass
|
868 |
except Exception as err: |
869 |
self.fail("Shouldn't raise Exception %s" % err) |
870 |
else:
|
871 |
self.fail("Should have raised BadRequest") |
872 |
|
873 |
# ----------------------------------
|
874 |
def test_accept_commission(self): |
875 |
"""Test function call of accept_commission"""
|
876 |
global token, auth_url
|
877 |
try:
|
878 |
client = AstakosClient(token['id'], auth_url)
|
879 |
client.accept_commission(57)
|
880 |
except Exception as err: |
881 |
self.fail("Shouldn't raise Exception %s" % err) |
882 |
|
883 |
# ----------------------------------
|
884 |
def test_reject_commission(self): |
885 |
"""Test function call of reject_commission"""
|
886 |
global token, auth_url
|
887 |
try:
|
888 |
client = AstakosClient(token['id'], auth_url)
|
889 |
client.reject_commission(57)
|
890 |
except Exception as err: |
891 |
self.fail("Shouldn't raise Exception %s" % err) |
892 |
|
893 |
# ----------------------------------
|
894 |
def test_accept_commission_not_found(self): |
895 |
"""Test function call of accept_commission with wrong serial"""
|
896 |
global token, auth_url
|
897 |
try:
|
898 |
client = AstakosClient(token['id'], auth_url)
|
899 |
client.reject_commission(20)
|
900 |
except NotFound:
|
901 |
pass
|
902 |
except Exception as err: |
903 |
self.fail("Shouldn't raise Exception %s" % err) |
904 |
else:
|
905 |
self.fail("Should have raised NotFound") |
906 |
|
907 |
# ----------------------------------
|
908 |
def test_resolve_commissions(self): |
909 |
"""Test function call of resolve_commissions"""
|
910 |
global token, auth_url
|
911 |
try:
|
912 |
client = AstakosClient(token['id'], auth_url)
|
913 |
result = client.resolve_commissions([56, 57], [56, 58, 59]) |
914 |
except Exception as err: |
915 |
self.fail("Shouldn't raise Exception %s" % err) |
916 |
self.assertEqual(result, resolve_commissions_rep)
|
917 |
|
918 |
|
919 |
# ----------------------------
|
920 |
# Run tests
|
921 |
if __name__ == "__main__": |
922 |
unittest.main() |