Revision d2b8ec7b snf-cyclades-app/synnefo/plankton/tests.py
b/snf-cyclades-app/synnefo/plankton/tests.py | ||
---|---|---|
39 | 39 |
from mock import patch |
40 | 40 |
from functools import wraps |
41 | 41 |
from copy import deepcopy |
42 |
from snf_django.utils.testing import astakos_user, BaseAPITest |
|
42 | 43 |
|
43 | 44 |
|
44 | 45 |
FILTERS = ('name', 'container_format', 'disk_format', 'status', 'size_min', |
... | ... | |
58 | 59 |
'owner', 'properties', 'status') |
59 | 60 |
|
60 | 61 |
|
61 |
@contextmanager |
|
62 |
def astakos_user(user): |
|
63 |
""" |
|
64 |
Context manager to mock astakos response. |
|
65 |
|
|
66 |
usage: |
|
67 |
with astakos_user("user@user.com"): |
|
68 |
.... make api calls .... |
|
69 |
|
|
70 |
""" |
|
71 |
with patch("snf_django.lib.api.get_token") as get_token: |
|
72 |
get_token.return_value = "DummyToken" |
|
73 |
with patch('astakosclient.AstakosClient.get_user_info') as m: |
|
74 |
m.return_value = {"uuid": user} |
|
75 |
yield |
|
76 |
|
|
77 |
|
|
78 |
class BaseAPITest(TestCase): |
|
79 |
def get(self, url, user='user', *args, **kwargs): |
|
80 |
with astakos_user(user): |
|
81 |
response = self.client.get(url, *args, **kwargs) |
|
82 |
return response |
|
83 |
|
|
84 |
def delete(self, url, user='user'): |
|
85 |
with astakos_user(user): |
|
86 |
response = self.client.delete(url) |
|
87 |
return response |
|
88 |
|
|
89 |
def post(self, url, user='user', params={}, ctype='json', *args, **kwargs): |
|
90 |
if ctype == 'json': |
|
91 |
content_type = 'application/json' |
|
92 |
with astakos_user(user): |
|
93 |
response = self.client.post(url, params, content_type=content_type, |
|
94 |
*args, **kwargs) |
|
95 |
return response |
|
96 |
|
|
97 |
def put(self, url, user='user', params={}, ctype='json', *args, **kwargs): |
|
98 |
if ctype == 'json': |
|
99 |
content_type = 'application/json' |
|
100 |
with astakos_user(user): |
|
101 |
response = self.client.put(url, params, content_type=content_type, |
|
102 |
*args, **kwargs) |
|
103 |
return response |
|
104 |
|
|
105 |
def assertSuccess(self, response): |
|
106 |
self.assertTrue(response.status_code in [200, 203, 204]) |
|
107 |
|
|
108 |
def assertFault(self, response, status_code, name): |
|
109 |
self.assertEqual(response.status_code, status_code) |
|
110 |
fault = response.content |
|
111 |
self.assertEqual(fault, name) |
|
112 |
|
|
113 |
def assertBadRequest(self, response): |
|
114 |
self.assertFault(response, 400, '400 Bad Request') |
|
115 |
|
|
116 |
def assertItemNotFound(self, response): |
|
117 |
self.assertFault(response, 404, 'itemNotFound') |
|
118 |
|
|
119 |
|
|
120 | 62 |
DummyImages = { |
121 | 63 |
'0786a349-9725-48ec-8b86-8598eefc4043': |
122 | 64 |
{'checksum': u'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', |
Also available in: Unified diff