Revision d2b8ec7b snf-cyclades-app/synnefo/api/tests.py
b/snf-cyclades-app/synnefo/api/tests.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
from __future__ import with_statement |
|
35 |
|
|
36 |
from django.utils import simplejson as json |
|
37 |
from django.test import TestCase |
|
38 |
|
|
39 |
from mock import patch |
|
40 |
from contextlib import contextmanager |
|
41 |
|
|
42 |
|
|
43 |
@contextmanager |
|
44 |
def astakos_user(user): |
|
45 |
""" |
|
46 |
Context manager to mock astakos response. |
|
47 |
|
|
48 |
usage: |
|
49 |
with astakos_user("user@user.com"): |
|
50 |
.... make api calls .... |
|
51 |
|
|
52 |
""" |
|
53 |
with patch("snf_django.lib.api.get_token") as get_token: |
|
54 |
get_token.return_value = "DummyToken" |
|
55 |
with patch('astakosclient.AstakosClient.get_user_info') as m: |
|
56 |
m.return_value = {"uuid": user} |
|
57 |
yield |
|
58 |
|
|
59 |
|
|
60 |
class BaseAPITest(TestCase): |
|
61 |
def get(self, url, user='user', *args, **kwargs): |
|
62 |
with astakos_user(user): |
|
63 |
response = self.client.get(url, *args, **kwargs) |
|
64 |
return response |
|
65 |
|
|
66 |
def delete(self, url, user='user'): |
|
67 |
with astakos_user(user): |
|
68 |
response = self.client.delete(url) |
|
69 |
return response |
|
70 |
|
|
71 |
def post(self, url, user='user', params={}, ctype='json', *args, **kwargs): |
|
72 |
if ctype == 'json': |
|
73 |
content_type = 'application/json' |
|
74 |
with astakos_user(user): |
|
75 |
response = self.client.post(url, params, content_type=content_type, |
|
76 |
*args, **kwargs) |
|
77 |
return response |
|
78 |
|
|
79 |
def put(self, url, user='user', params={}, ctype='json', *args, **kwargs): |
|
80 |
if ctype == 'json': |
|
81 |
content_type = 'application/json' |
|
82 |
with astakos_user(user): |
|
83 |
response = self.client.put(url, params, content_type=content_type, |
|
84 |
*args, **kwargs) |
|
85 |
return response |
|
86 |
|
|
87 |
def assertSuccess(self, response): |
|
88 |
self.assertTrue(response.status_code in [200, 203, 204]) |
|
89 |
|
|
90 |
def assertFault(self, response, status_code, name): |
|
91 |
self.assertEqual(response.status_code, status_code) |
|
92 |
fault = json.loads(response.content) |
|
93 |
self.assertEqual(fault.keys(), [name]) |
|
94 |
|
|
95 |
def assertBadRequest(self, response): |
|
96 |
self.assertFault(response, 400, 'badRequest') |
|
97 |
|
|
98 |
def assertItemNotFound(self, response): |
|
99 |
self.assertFault(response, 404, 'itemNotFound') |
|
100 |
|
|
101 |
|
|
102 |
class APITest(TestCase): |
|
103 |
def test_api_version(self): |
|
104 |
"""Check API version.""" |
|
105 |
with astakos_user('user'): |
|
106 |
response = self.client.get('/api/v1.1/') |
|
107 |
self.assertEqual(response.status_code, 200) |
|
108 |
api_version = json.loads(response.content)['version'] |
|
109 |
self.assertEqual(api_version['id'], 'v1.1') |
|
110 |
self.assertEqual(api_version['status'], 'CURRENT') |
|
111 |
|
|
112 |
|
|
113 | 34 |
# Import TestCases |
114 | 35 |
from synnefo.api.test.servers import * |
115 | 36 |
from synnefo.api.test.networks import * |
116 | 37 |
from synnefo.api.test.flavors import * |
117 | 38 |
from synnefo.api.test.images import * |
39 |
from synnefo.api.test.versions import * |
Also available in: Unified diff