Statistics
| Branch: | Tag: | Revision:

root / astakosclient / astakosclient / tests.py @ 283a43e9

History | View | Annotate | Download (34.9 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 socket
46
import simplejson
47
from mock import patch
48
from contextlib import contextmanager
49

    
50
import astakosclient
51
from astakosclient import AstakosClient
52
from astakosclient.utils import join_urls
53
from astakosclient.errors import \
54
    AstakosClientException, Unauthorized, BadRequest, NotFound, \
55
    NoUserName, NoUUID, BadValue, QuotaLimit
56

    
57
# Use backported unittest functionality if Python < 2.7
58
try:
59
    import unittest2 as unittest
60
except ImportError:
61
    if sys.version_info < (2, 7):
62
        raise Exception("The unittest2 package is required for Python < 2.7")
63
    import unittest
64

    
65

    
66
# --------------------------------------------------------------------
67
# Helper functions
68
auth_url = "https://example.org/identity/v2.0"
69
account_prefix = "/account_prefix"
70
ui_prefix = "/ui_prefix"
71
api_authenticate = join_urls(account_prefix, "authenticate")
72
api_usercatalogs = join_urls(account_prefix, "user_catalogs")
73
api_resources = join_urls(account_prefix, "resources")
74
api_quotas = join_urls(account_prefix, "quotas")
75
api_commissions = join_urls(account_prefix, "commissions")
76

    
77
# --------------------------------------
78
# Local users
79
token_1 = "skzleaFlBl+fasFdaf24sx"
80
user_1 = \
81
    {"username": "user1@example.com",
82
     "name": "Example User One",
83
     "email": ["user1@example.com"],
84
     "uuid": "73917abc-abcd-477e-a1f1-1763abcdefab"}
85

    
86
resources = {
87
    "cyclades.ram": {
88
        "unit": "bytes",
89
        "description": "Virtual machine memory",
90
        "service": "cyclades"}}
91

    
92
endpoints = {
93
    "access": {
94
        "serviceCatalog": [{
95
            "endpoints": [{"SNF:uiURL": join_urls("https://example.org/",
96
                                                  ui_prefix),
97
                           "publicURL": join_urls("https://example.org/",
98
                                                  account_prefix),
99
                           "region": "default",
100
                           "versionId": "v1.0"}],
101
            "name": "astakos_account",
102
            "type": "account"}]
103
        }
104
    }
105

    
106
quotas = {
107
    "system": {
108
        "cyclades.ram": {
109
            "pending": 0,
110
            "limit": 1073741824,
111
            "usage": 536870912},
112
        "cyclades.vm": {
113
            "pending": 0,
114
            "limit": 2,
115
            "usage": 2}},
116
    "project:1": {
117
        "cyclades.ram": {
118
            "pending": 0,
119
            "limit": 2147483648,
120
            "usage": 2147483648},
121
        "cyclades.vm": {
122
            "pending": 1,
123
            "limit": 5,
124
            "usage": 2}}}
125

    
126
commission_request = {
127
    "force": False,
128
    "auto_accept": False,
129
    "name": "my commission",
130
    "provisions": [
131
        {
132
            "holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad",
133
            "source": "system",
134
            "resource": "cyclades.vm",
135
            "quantity": 1
136
        },
137
        {
138
            "holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad",
139
            "source": "system",
140
            "resource": "cyclades.ram",
141
            "quantity": 30000
142
        }]}
143

    
144
commission_successful_response = {"serial": 57}
145

    
146
commission_failure_response = {
147
    "overLimit": {
148
        "message": "a human-readable error message",
149
        "code": 413,
150
        "data": {
151
            "provision": {
152
                "holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad",
153
                "source": "system",
154
                "resource": "cyclades.ram",
155
                "quantity": 520000000},
156
            "name": "NoCapacityError",
157
            "limit": 600000000,
158
            "usage": 180000000}}}
159

    
160
pending_commissions = [100, 200]
161

    
162
commission_description = {
163
    "serial": 57,
164
    "issue_time": "2013-04-08T10:19:15.0373+00:00",
165
    "name": "a commission",
166
    "provisions": [
167
        {
168
            "holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad",
169
            "source": "system",
170
            "resource": "cyclades.vm",
171
            "quantity": 1
172
        },
173
        {
174
            "holder": "c02f315b-7d84-45bc-a383-552a3f97d2ad",
175
            "source": "system",
176
            "resource": "cyclades.ram",
177
            "quantity": 536870912
178
        }]}
179

    
180
resolve_commissions_req = {
181
    "accept": [56, 57],
182
    "reject": [56, 58, 59]}
183

    
184
resolve_commissions_rep = {
185
    "accepted": [57],
186
    "rejected": [59],
187
    "failed": [
188
        [56, {
189
            "badRequest": {
190
                "message": "cannot both accept and reject serial 56",
191
                "code": 400}}],
192
        [58, {
193
            "itemNotFound": {
194
                "message": "serial 58 does not exist",
195
                "code": 404}}]]}
196

    
197

    
198
# ----------------------------
199
# These functions will be used as mocked requests
200
def _request_offline(conn, method, url, **kwargs):
201
    """This request behaves as we were offline"""
202
    raise socket.gaierror
203

    
204

    
205
def _request_status_302(conn, method, url, **kwargs):
206
    """This request returns 302"""
207
    message = "FOUND"
208
    status = 302
209
    data = "302 Found"
210
    return (message, data, status)
211

    
212

    
213
def _request_status_404(conn, method, url, **kwargs):
214
    """This request returns 404"""
215
    message = "Not Found"
216
    status = 404
217
    data = "404 Not Found"
218
    return (message, data, status)
219

    
220

    
221
def _request_status_403(conn, method, url, **kwargs):
222
    """This request returns 403"""
223
    message = "UNAUTHORIZED"
224
    status = 403
225
    data = "Forbidden"
226
    return (message, data, status)
227

    
228

    
229
def _request_status_401(conn, method, url, **kwargs):
230
    """This request returns 401"""
231
    message = "UNAUTHORIZED"
232
    status = 401
233
    data = "Invalid X-Auth-Token\n"
234
    return (message, data, status)
235

    
236

    
237
def _request_status_400(conn, method, url, **kwargs):
238
    """This request returns 400"""
239
    message = "BAD REQUEST"
240
    status = 400
241
    data = "Method not allowed.\n"
242
    return (message, data, status)
243

    
244

    
245
def _request_ok(conn, method, url, **kwargs):
246
    """This request behaves like original Astakos does"""
247
    if api_authenticate == url:
248
        return _req_authenticate(conn, method, url, **kwargs)
249
    elif api_usercatalogs == url:
250
        return _req_catalogs(conn, method, url, **kwargs)
251
    elif api_resources == url:
252
        return _req_resources(conn, method, url, **kwargs)
253
    elif api_quotas == url:
254
        return _req_quotas(conn, method, url, **kwargs)
255
    elif url.startswith(api_commissions):
256
        return _req_commission(conn, method, url, **kwargs)
257
    else:
258
        return _request_status_404(conn, method, url, **kwargs)
259

    
260

    
261
def _req_authenticate(conn, method, url, **kwargs):
262
    """Check if user exists and return his profile"""
263
    global user_1, token_1
264

    
265
    # Check input
266
    if conn.__class__.__name__ != "HTTPSConnection":
267
        return _request_status_302(conn, method, url, **kwargs)
268
    if method != "GET":
269
        return _request_status_400(conn, method, url, **kwargs)
270
    token = kwargs['headers'].get('X-Auth-Token')
271
    if token == token_1:
272
        user = dict(user_1)
273
        return ("", simplejson.dumps(user), 200)
274
    else:
275
        # No user found
276
        return _request_status_401(conn, method, url, **kwargs)
277

    
278

    
279
def _req_catalogs(conn, method, url, **kwargs):
280
    """Return user catalogs"""
281
    global token_1, token_2, user_1, user_2
282

    
283
    # Check input
284
    if conn.__class__.__name__ != "HTTPSConnection":
285
        return _request_status_302(conn, method, url, **kwargs)
286
    if method != "POST":
287
        return _request_status_400(conn, method, url, **kwargs)
288
    token = kwargs['headers'].get('X-Auth-Token')
289
    if token != token_1:
290
        return _request_status_401(conn, method, url, **kwargs)
291

    
292
    # Return
293
    body = simplejson.loads(kwargs['body'])
294
    if 'uuids' in body:
295
        # Return uuid_catalog
296
        uuids = body['uuids']
297
        catalogs = {}
298
        if user_1['uuid'] in uuids:
299
            catalogs[user_1['uuid']] = user_1['username']
300
        return_catalog = {"displayname_catalog": {}, "uuid_catalog": catalogs}
301
    elif 'displaynames' in body:
302
        # Return displayname_catalog
303
        names = body['displaynames']
304
        catalogs = {}
305
        if user_1['username'] in names:
306
            catalogs[user_1['username']] = user_1['uuid']
307
        return_catalog = {"displayname_catalog": catalogs, "uuid_catalog": {}}
308
    else:
309
        return_catalog = {"displayname_catalog": {}, "uuid_catalog": {}}
310
    return ("", simplejson.dumps(return_catalog), 200)
311

    
312

    
313
def _req_resources(conn, method, url, **kwargs):
314
    """Return quota resources"""
315
    global resources
316

    
317
    # Check input
318
    if conn.__class__.__name__ != "HTTPSConnection":
319
        return _request_status_302(conn, method, url, **kwargs)
320
    if method != "GET":
321
        return _request_status_400(conn, method, url, **kwargs)
322

    
323
    # Return
324
    return ("", simplejson.dumps(resources), 200)
325

    
326

    
327
def _req_quotas(conn, method, url, **kwargs):
328
    """Return quotas for user_1"""
329
    global token_1, quotas
330

    
331
    # Check input
332
    if conn.__class__.__name__ != "HTTPSConnection":
333
        return _request_status_302(conn, method, url, **kwargs)
334
    if method != "GET":
335
        return _request_status_400(conn, method, url, **kwargs)
336
    token = kwargs['headers'].get('X-Auth-Token')
337
    if token != token_1:
338
        return _request_status_401(conn, method, url, **kwargs)
339

    
340
    # Return
341
    return ("", simplejson.dumps(quotas), 200)
342

    
343

    
344
def _req_commission(conn, method, url, **kwargs):
345
    """Perform a commission for user_1"""
346
    global token_1, pending_commissions, \
347
        commission_successful_response, commission_failure_response
348

    
349
    # Check input
350
    if conn.__class__.__name__ != "HTTPSConnection":
351
        return _request_status_302(conn, method, url, **kwargs)
352
    token = kwargs['headers'].get('X-Auth-Token')
353
    if token != token_1:
354
        return _request_status_401(conn, method, url, **kwargs)
355

    
356
    if method == "POST":
357
        if 'body' not in kwargs:
358
            return _request_status_400(conn, method, url, **kwargs)
359
        body = simplejson.loads(unicode(kwargs['body']))
360
        if re.match('/?'+api_commissions+'$', url) is not None:
361
            # Issue Commission
362
            # Check if we have enough resources to give
363
            if body['provisions'][1]['quantity'] > 420000000:
364
                return ("", simplejson.dumps(commission_failure_response), 413)
365
            else:
366
                return \
367
                    ("", simplejson.dumps(commission_successful_response), 200)
368
        else:
369
            # Issue commission action
370
            serial = url.split('/')[3]
371
            if serial == "action":
372
                # Resolve multiple actions
373
                if body == resolve_commissions_req:
374
                    return ("", simplejson.dumps(resolve_commissions_rep), 200)
375
                else:
376
                    return _request_status_400(conn, method, url, **kwargs)
377
            else:
378
                # Issue action for one commission
379
                if serial != str(57):
380
                    return _request_status_404(conn, method, url, **kwargs)
381
                if len(body) != 1:
382
                    return _request_status_400(conn, method, url, **kwargs)
383
                if "accept" not in body.keys() and "reject" not in body.keys():
384
                    return _request_status_400(conn, method, url, **kwargs)
385
                return ("", "", 200)
386

    
387
    elif method == "GET":
388
        if re.match('/?'+api_commissions+'$', url) is not None:
389
            # Return pending commission
390
            return ("", simplejson.dumps(pending_commissions), 200)
391
        else:
392
            # Return commissions's description
393
            serial = re.sub('/?' + api_commissions, '', url)[1:]
394
            if serial == str(57):
395
                return ("", simplejson.dumps(commission_description), 200)
396
            else:
397
                return _request_status_404(conn, method, url, **kwargs)
398
    else:
399
        return _request_status_400(conn, method, url, **kwargs)
400

    
401

    
402
# ----------------------------
403
# Mock the actual _doRequest
404
def _mock_request(new_requests):
405
    """Mock the actual request
406

407
    Given a list of requests to use (in rotation),
408
    replace the original _doRequest function with
409
    a new one
410

411
    """
412
    def _mock(conn, method, url, **kwargs):
413
        # Get first request
414
        request = _mock.requests[0]
415
        # Rotate requests
416
        _mock.requests = _mock.requests[1:] + _mock.requests[:1]
417
        # Use first request
418
        return request(conn, method, url, **kwargs)
419

    
420
    _mock.requests = new_requests
421
    # Replace `_doRequest' with our `_mock'
422
    astakosclient._do_request = _mock
423

    
424

    
425
# --------------------------------------
426
# Mock the get_endpoints method
427
@contextmanager
428
def patch_astakosclient(new_requests):
429
    _mock_request(new_requests)
430
    with patch('astakosclient.AstakosClient.get_endpoints') as patcher:
431
        patcher.return_value = endpoints
432
        yield
433

    
434

    
435
# --------------------------------------------------------------------
436
# The actual tests
437

    
438
class TestCallAstakos(unittest.TestCase):
439
    """Test cases for function _callAstakos"""
440

    
441
    # ----------------------------------
442
    # Test the response we get if we don't have internet access
443
    def _offline(self, pool):
444
        global token_1, auth_url
445
        _mock_request([_request_offline])
446
        try:
447
            client = AstakosClient(token_1, auth_url, use_pool=pool)
448
            client._call_astakos("offline")
449
        except AstakosClientException:
450
            pass
451
        else:
452
            self.fail("Should have raised AstakosClientException")
453

    
454
    def test_offline(self):
455
        """Test _offline without pool"""
456
        self._offline(False)
457

    
458
    def test_offline_pool(self):
459
        """Test _offline using pool"""
460
        self._offline(True)
461

    
462
    # ----------------------------------
463
    # Test the response we get if we send invalid token
464
    def _invalid_token(self, pool):
465
        global auth_url
466
        token = "skaksaFlBl+fasFdaf24sx"
467
        try:
468
            with patch_astakosclient([_request_ok]):
469
                client = AstakosClient(token, auth_url, use_pool=pool)
470
                client.get_user_info()
471
        except Unauthorized:
472
            pass
473
        except Exception:
474
            self.fail("Should have returned 401 (Invalid X-Auth-Token)")
475
        else:
476
            self.fail("Should have returned 401 (Invalid X-Auth-Token)")
477

    
478
    def test_invalid_token(self):
479
        """Test _invalid_token without pool"""
480
        self._invalid_token(False)
481

    
482
    def test_invalid_token_pool(self):
483
        """Test _invalid_token using pool"""
484
        self._invalid_token(True)
485

    
486
    # ----------------------------------
487
    # Test the response we get if we send invalid url
488
    def _invalid_url(self, pool):
489
        global token_1, auth_url
490
        try:
491
            with patch_astakosclient([_request_ok]):
492
                client = AstakosClient(token_1, auth_url, use_pool=pool)
493
                client._call_astakos("/astakos/api/misspelled")
494
        except NotFound:
495
            pass
496
        except Exception, e:
497
            self.fail("Got \"%s\" instead of 404" % e)
498
        else:
499
            self.fail("Should have returned 404 (Not Found)")
500

    
501
    def test_invalid_url(self):
502
        """Test _invalid_url without pool"""
503
        self._invalid_url(False)
504

    
505
    def test_invalid_url_pool(self):
506
        """Test _invalid_url using pool"""
507
        self._invalid_url(True)
508

    
509
    # ----------------------------------
510
    # Test the response we get if we use an unsupported scheme
511
    def _unsupported_scheme(self, pool):
512
        global token_1, auth_url
513
        try:
514
            with patch_astakosclient([_request_ok]):
515
                client = AstakosClient(token_1, "ftp://example.com",
516
                                       use_pool=pool)
517
                client.get_user_info()
518
        except BadValue:
519
            pass
520
        except Exception:
521
            self.fail("Should have raise BadValue Exception")
522
        else:
523
            self.fail("Should have raise BadValue Exception")
524

    
525
    def test_unsupported_scheme(self):
526
        """Test _unsupported_scheme without pool"""
527
        self._unsupported_scheme(False)
528

    
529
    def test_unsupported_scheme_pool(self):
530
        """Test _unsupported_scheme using pool"""
531
        self._unsupported_scheme(True)
532

    
533
    # ----------------------------------
534
    # Test the response we get if we use http instead of https
535
    def _http_scheme(self, pool):
536
        global token_1
537
        http_auth_url = "http://example.org/identity/v2.0"
538
        try:
539
            with patch_astakosclient([_request_ok]):
540
                client = AstakosClient(token_1, http_auth_url, use_pool=pool)
541
                client.get_user_info()
542
        except AstakosClientException as err:
543
            if err.status != 302:
544
                self.fail("Should have returned 302 (Found)")
545
        else:
546
            self.fail("Should have returned 302 (Found)")
547

    
548
    def test_http_scheme(self):
549
        """Test _http_scheme without pool"""
550
        self._http_scheme(False)
551

    
552
    def test_http_scheme_pool(self):
553
        """Test _http_scheme using pool"""
554
        self._http_scheme(True)
555

    
556
    # ----------------------------------
557
    # Test the response we get if we use authenticate with POST
558
    def _post_authenticate(self, pool):
559
        global token_1, auth_url
560
        try:
561
            with patch_astakosclient([_request_ok]):
562
                client = AstakosClient(token_1, auth_url, use_pool=pool)
563
                client._call_astakos(api_authenticate, method="POST")
564
        except BadRequest:
565
            pass
566
        except Exception:
567
            self.fail("Should have returned 400 (Method not allowed)")
568
        else:
569
            self.fail("Should have returned 400 (Method not allowed)")
570

    
571
    def test_post_authenticate(self):
572
        """Test _post_authenticate without pool"""
573
        self._post_authenticate(False)
574

    
575
    def test_post_authenticate_pool(self):
576
        """Test _post_authenticate using pool"""
577
        self._post_authenticate(True)
578

    
579
    # ----------------------------------
580
    # Test the response if we request user_catalogs with GET
581
    def _get_user_catalogs(self, pool):
582
        global token_1, auth_url, api_usercatalogs
583
        try:
584
            with patch_astakosclient([_request_ok]):
585
                client = AstakosClient(token_1, auth_url, use_pool=pool)
586
                client._call_astakos(api_usercatalogs)
587
        except BadRequest:
588
            pass
589
        except Exception:
590
            self.fail("Should have returned 400 (Method not allowed)")
591
        else:
592
            self.fail("Should have returned 400 (Method not allowed)")
593

    
594
    def test_get_user_catalogs(self):
595
        """Test _get_user_catalogs without pool"""
596
        self._get_user_catalogs(False)
597

    
598
    def test_get_user_catalogs_pool(self):
599
        """Test _get_user_catalogs using pool"""
600
        self._get_user_catalogs(True)
601

    
602

    
603
class TestAuthenticate(unittest.TestCase):
604
    """Test cases for function getUserInfo"""
605

    
606
    # ----------------------------------
607
    # Test the response we get if we don't have internet access
608
    def test_offline(self):
609
        """Test offline after 3 retries"""
610
        global token_1, auth_url
611
        try:
612
            with patch_astakosclient([_request_offline]):
613
                client = AstakosClient(token_1, auth_url, retry=3)
614
                client.get_user_info()
615
        except AstakosClientException:
616
            pass
617
        else:
618
            self.fail("Should have raised AstakosClientException exception")
619

    
620
    # ----------------------------------
621
    # Test the response we get for invalid token
622
    def _invalid_token(self, pool):
623
        global auth_url
624
        token = "skaksaFlBl+fasFdaf24sx"
625
        try:
626
            with patch_astakosclient([_request_ok]):
627
                client = AstakosClient(token, auth_url, use_pool=pool)
628
                client.get_user_info()
629
        except Unauthorized:
630
            pass
631
        except Exception:
632
            self.fail("Should have returned 401 (Invalid X-Auth-Token)")
633
        else:
634
            self.fail("Should have returned 401 (Invalid X-Auth-Token)")
635

    
636
    def test_invalid_token(self):
637
        """Test _invalid_token without pool"""
638
        self._invalid_token(False)
639

    
640
    def test_invalid_token_pool(self):
641
        """Test _invalid_token using pool"""
642
        self._invalid_token(True)
643

    
644
    #- ---------------------------------
645
    # Test response for user
646
    def _auth_user(self, token, user_info, pool):
647
        global auth_url
648
        try:
649
            with patch_astakosclient([_request_ok]):
650
                client = AstakosClient(token, auth_url, use_pool=pool)
651
                auth_info = client.get_user_info()
652
        except:
653
            self.fail("Shouldn't raise an Exception")
654
        self.assertEqual(user_info, auth_info)
655

    
656
    def test_auth_user(self):
657
        """Test _auth_user without pool"""
658
        global token_1, user_1
659
        user_info = dict(user_1)
660
        self._auth_user(token_1, user_info, False)
661

    
662
    def test_auth_user_pool(self):
663
        """Test _auth_user for User 1 using pool, with usage"""
664
        global token_1, user_1
665
        self._auth_user(token_1, user_1, True)
666

    
667
    # ----------------------------------
668
    # Test retry functionality
669
    def test_offline_retry(self):
670
        """Test retry functionality for getUserInfo"""
671
        global token_1, user_1, auth_url
672
        _mock_request([_request_offline, _request_offline, _request_ok])
673
        try:
674
            with patch_astakosclient([_request_offline, _request_offline,
675
                                      _request_ok]):
676
                client = AstakosClient(token_1, auth_url, retry=2)
677
                auth_info = client.get_user_info()
678
        except Exception, e:
679
            self.fail("Shouldn't raise an Exception \"%s\"" % e)
680
        self.assertEqual(user_1, auth_info)
681

    
682

    
683
class TestDisplayNames(unittest.TestCase):
684
    """Test cases for functions getDisplayNames/getDisplayName"""
685

    
686
    # ----------------------------------
687
    # Test the response we get for invalid token
688
    def test_invalid_token(self):
689
        """Test the response we get for invalid token (without pool)"""
690
        global user_1, auth_url
691
        token = "skaksaFlBl+fasFdaf24sx"
692
        try:
693
            with patch_astakosclient([_request_ok]):
694
                client = AstakosClient(token, auth_url)
695
                client.get_usernames([user_1['uuid']])
696
        except Unauthorized:
697
            pass
698
        except Exception:
699
            self.fail("Should have returned 401 (Invalid X-Auth-Token)")
700
        else:
701
            self.fail("Should have returned 401 (Invalid X-Auth-Token)")
702

    
703
    # ----------------------------------
704
    # Get info for user 1
705
    def test_username_user_one(self):
706
        """Test get_username for User One"""
707
        global token_1, user_1, auth_url
708
        try:
709
            with patch_astakosclient([_request_offline, _request_ok]):
710
                client = AstakosClient(token_1, auth_url,
711
                                       use_pool=True, retry=2)
712
                info = client.get_username(user_1['uuid'])
713
        except:
714
            self.fail("Shouldn't raise an Exception")
715
        self.assertEqual(info, user_1['username'])
716

    
717
    # ----------------------------------
718
    # Get info with wrong uuid
719
    def test_no_username(self):
720
        global token_1, auth_url
721
        try:
722
            with patch_astakosclient([_request_ok]):
723
                client = AstakosClient(token_1, auth_url)
724
                client.get_username("1234")
725
        except NoUserName:
726
            pass
727
        except:
728
            self.fail("Should have raised NoDisplayName exception")
729
        else:
730
            self.fail("Should have raised NoDisplayName exception")
731

    
732

    
733
class TestGetUUIDs(unittest.TestCase):
734
    """Test cases for functions getUUIDs/getUUID"""
735

    
736
    # ----------------------------------
737
    # Test the response we get for invalid token
738
    def test_invalid_token(self):
739
        """Test the response we get for invalid token (using pool)"""
740
        global user_1, auth_url
741
        token = "skaksaFlBl+fasFdaf24sx"
742
        try:
743
            with patch_astakosclient([_request_ok]):
744
                client = AstakosClient(token, auth_url)
745
                client.get_uuids([user_1['username']])
746
        except Unauthorized:
747
            pass
748
        except Exception:
749
            self.fail("Should have returned 401 (Invalid X-Auth-Token)")
750
        else:
751
            self.fail("Should have returned 401 (Invalid X-Auth-Token)")
752

    
753
    # ----------------------------------
754
    # Get uuid for user 1
755
    def test_get_uuid_user_two(self):
756
        """Test get_uuid for User Two"""
757
        global token_1, user_1, auth_url
758
        try:
759
            with patch_astakosclient([_request_ok]):
760
                client = AstakosClient(token_1, auth_url, retry=1)
761
                catalog = client.get_uuids([user_1['username']])
762
        except:
763
            self.fail("Shouldn't raise an Exception")
764
        self.assertEqual(catalog[user_1['username']], user_1['uuid'])
765

    
766
    # ----------------------------------
767
    # Get uuid with wrong username
768
    def test_no_uuid(self):
769
        global token_1, auth_url
770
        try:
771
            with patch_astakosclient([_request_ok]):
772
                client = AstakosClient(token_1, auth_url)
773
                client.get_uuid("1234")
774
        except NoUUID:
775
            pass
776
        except:
777
            self.fail("Should have raised NoUUID exception")
778
        else:
779
            self.fail("Should have raised NoUUID exception")
780

    
781

    
782
class TestResources(unittest.TestCase):
783
    """Test cases for function get_resources"""
784

    
785
    # ----------------------------------
786
    def test_get_resources(self):
787
        """Test function call of get_resources"""
788
        global resources, auth_url, token_1
789
        try:
790
            with patch_astakosclient([_request_offline, _request_ok]):
791
                client = AstakosClient(token_1, auth_url, retry=1)
792
                result = client.get_resources()
793
        except Exception as err:
794
            self.fail("Shouldn't raise Exception %s" % err)
795
        self.assertEqual(resources, result)
796

    
797

    
798
class TestQuotas(unittest.TestCase):
799
    """Test cases for function get_quotas"""
800

    
801
    # ----------------------------------
802
    def test_get_quotas(self):
803
        """Test function call of get_quotas"""
804
        global quotas, token_1, auth_url
805
        try:
806
            with patch_astakosclient([_request_ok]):
807
                client = AstakosClient(token_1, auth_url)
808
                result = client.get_quotas()
809
        except Exception as err:
810
            self.fail("Shouldn't raise Exception %s" % err)
811
        self.assertEqual(quotas, result)
812

    
813
    # -----------------------------------
814
    def test_get_quotas_unauthorized(self):
815
        """Test function call of get_quotas with wrong token"""
816
        global auth_url
817
        token = "buahfhsda"
818
        try:
819
            with patch_astakosclient([_request_ok]):
820
                client = AstakosClient(token, auth_url)
821
                client.get_quotas()
822
        except Unauthorized:
823
            pass
824
        except Exception as err:
825
            self.fail("Shouldn't raise Exception %s" % err)
826
        else:
827
            self.fail("Should have raised Unauthorized Exception")
828

    
829

    
830
class TestCommissions(unittest.TestCase):
831
    """Test cases for quota commissions"""
832

    
833
    # ----------------------------------
834
    def test_issue_commission(self):
835
        """Test function call of issue_commission"""
836
        global token_1, commission_request, commission_successful_reqsponse
837
        global auth_url
838
        try:
839
            with patch_astakosclient([_request_ok]):
840
                client = AstakosClient(token_1, auth_url)
841
                response = client.issue_commission(commission_request)
842
        except Exception as err:
843
            self.fail("Shouldn't raise Exception %s" % err)
844
        self.assertEqual(response, commission_successful_response['serial'])
845

    
846
    # ----------------------------------
847
    def test_issue_commission_quota_limit(self):
848
        """Test function call of issue_commission with limit exceeded"""
849
        global token_1, commission_request, commission_failure_response
850
        global auth_url
851
        new_request = dict(commission_request)
852
        new_request['provisions'][1]['quantity'] = 520000000
853
        try:
854
            with patch_astakosclient([_request_ok]):
855
                client = AstakosClient(token_1, auth_url)
856
                client.issue_commission(new_request)
857
        except QuotaLimit:
858
            pass
859
        except Exception as err:
860
            self.fail("Shouldn't raise Exception %s" % err)
861
        else:
862
            self.fail("Should have raised QuotaLimit Exception")
863

    
864
    # ----------------------------------
865
    def test_issue_one_commission(self):
866
        """Test function call of issue_one_commission"""
867
        global token_1, commission_successful_response, auth_url
868
        try:
869
            with patch_astakosclient([_request_ok]):
870
                client = AstakosClient(token_1, auth_url)
871
                response = client.issue_one_commission(
872
                    "c02f315b-7d84-45bc-a383-552a3f97d2ad",
873
                    "system", {"cyclades.vm": 1, "cyclades.ram": 30000})
874
        except Exception as err:
875
            self.fail("Shouldn't have raised Exception %s" % err)
876
        self.assertEqual(response, commission_successful_response['serial'])
877

    
878
    # ----------------------------------
879
    def test_get_pending_commissions(self):
880
        """Test function call of get_pending_commissions"""
881
        global token_1, pending_commissions, auth_url
882
        try:
883
            with patch_astakosclient([_request_ok]):
884
                client = AstakosClient(token_1, auth_url)
885
                response = client.get_pending_commissions()
886
        except Exception as err:
887
            self.fail("Shouldn't raise Exception %s" % err)
888
        self.assertEqual(response, pending_commissions)
889

    
890
    # ----------------------------------
891
    def test_get_commission_info(self):
892
        """Test function call of get_commission_info"""
893
        global token_1, commission_description, auth_url
894
        try:
895
            with patch_astakosclient([_request_ok]):
896
                client = AstakosClient(token_1, auth_url,
897
                                       use_pool=True, pool_size=2)
898
                response = client.get_commission_info(57)
899
        except Exception as err:
900
            self.fail("Shouldn't raise Exception %s" % err)
901
        self.assertEqual(response, commission_description)
902

    
903
    # ----------------------------------
904
    def test_get_commission_info_not_found(self):
905
        """Test function call of get_commission_info with invalid serial"""
906
        global token_1, auth_url
907
        _mock_request([_request_ok])
908
        try:
909
            with patch_astakosclient([_request_ok]):
910
                client = AstakosClient(token_1, auth_url)
911
                client.get_commission_info("57lala")
912
        except NotFound:
913
            pass
914
        except Exception as err:
915
            self.fail("Shouldn't raise Exception %s" % err)
916
        else:
917
            self.fail("Should have raised NotFound")
918

    
919
    # ----------------------------------
920
    def test_get_commission_info_without_serial(self):
921
        """Test function call of get_commission_info without serial"""
922
        global token_1, auth_url
923
        try:
924
            with patch_astakosclient([_request_ok]):
925
                client = AstakosClient(token_1, auth_url)
926
                client.get_commission_info(None)
927
        except BadValue:
928
            pass
929
        except Exception as err:
930
            self.fail("Shouldn't raise Exception %s" % err)
931
        else:
932
            self.fail("Should have raise BadValue")
933

    
934
    # ----------------------------------
935
    def test_commision_action(self):
936
        """Test function call of commision_action with wrong action"""
937
        global token_1, auth_url
938
        try:
939
            with patch_astakosclient([_request_ok]):
940
                client = AstakosClient(token_1, auth_url)
941
                client.commission_action(57, "lala")
942
        except BadRequest:
943
            pass
944
        except Exception as err:
945
            self.fail("Shouldn't raise Exception %s" % err)
946
        else:
947
            self.fail("Should have raised BadRequest")
948

    
949
    # ----------------------------------
950
    def test_accept_commission(self):
951
        """Test function call of accept_commission"""
952
        global token_1, auth_url
953
        try:
954
            with patch_astakosclient([_request_ok]):
955
                client = AstakosClient(token_1, auth_url)
956
                client.accept_commission(57)
957
        except Exception as err:
958
            self.fail("Shouldn't raise Exception %s" % err)
959

    
960
    # ----------------------------------
961
    def test_reject_commission(self):
962
        """Test function call of reject_commission"""
963
        global token_1, auth_url
964
        try:
965
            with patch_astakosclient([_request_ok]):
966
                client = AstakosClient(token_1, auth_url)
967
                client.reject_commission(57)
968
        except Exception as err:
969
            self.fail("Shouldn't raise Exception %s" % err)
970

    
971
    # ----------------------------------
972
    def test_accept_commission_not_found(self):
973
        """Test function call of accept_commission with wrong serial"""
974
        global token_1, auth_url
975
        try:
976
            with patch_astakosclient([_request_ok]):
977
                client = AstakosClient(token_1, auth_url)
978
                client.reject_commission(20)
979
        except NotFound:
980
            pass
981
        except Exception as err:
982
            self.fail("Shouldn't raise Exception %s" % err)
983
        else:
984
            self.fail("Should have raised NotFound")
985

    
986
    # ----------------------------------
987
    def test_resolve_commissions(self):
988
        """Test function call of resolve_commissions"""
989
        global token_1, auth_url
990
        try:
991
            with patch_astakosclient([_request_ok]):
992
                client = AstakosClient(token_1, auth_url)
993
                result = client.resolve_commissions([56, 57], [56, 58, 59])
994
        except Exception as err:
995
            self.fail("Shouldn't raise Exception %s" % err)
996
        self.assertEqual(result, resolve_commissions_rep)
997

    
998

    
999
# ----------------------------
1000
# Run tests
1001
if __name__ == "__main__":
1002
        unittest.main()