root / snf-django-lib / snf_django / utils / testing.py @ 3f9db536
History | View | Annotate | Download (9.4 kB)
1 |
# Copyright 2011-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 |
|
35 |
from contextlib import contextmanager |
36 |
from django.test import TestCase |
37 |
from django.utils import simplejson as json |
38 |
from mock import patch |
39 |
|
40 |
|
41 |
@contextmanager
|
42 |
def override_settings(settings, **kwargs): |
43 |
"""
|
44 |
Helper context manager to override django settings within the provided
|
45 |
context.
|
46 |
|
47 |
All keyword arguments provided are set to the django settings object and
|
48 |
get reverted/removed when the manager exits.
|
49 |
|
50 |
>>> from synnefo.util.testing import override_settings
|
51 |
>>> from django.conf import settings
|
52 |
>>> with override_settings(settings, DEBUG=True):
|
53 |
>>> assert settings.DEBUG == True
|
54 |
|
55 |
The special arguemnt ``prefix`` can be set to prefix all setting keys with
|
56 |
the provided value.
|
57 |
|
58 |
>>> from django.conf import settings
|
59 |
>>> from django.core import mail
|
60 |
>>> with override_settings(settings, CONTACT_EMAILS=['kpap@grnet.gr'],
|
61 |
>>> prefix='MYAPP_'):
|
62 |
>>> from django.core.mail import send_mail
|
63 |
>>> send_mail("hello", "I love you kpap", settings.DEFAULT_FROM_EMAIL,
|
64 |
>>> settings.MYAPP_CONTACT_EMAILS)
|
65 |
>>> assert 'kpap@grnet.gr' in mail.mailbox[0].recipients()
|
66 |
|
67 |
If you plan to reuse it
|
68 |
|
69 |
>>> import functools
|
70 |
>>> from synnefo.util.testing import override_settings
|
71 |
>>> from django.conf import settings
|
72 |
>>> myapp_settings = functools.partial(override_settings, prefix='MYAPP_')
|
73 |
>>> with myapp_settings(CONTACT_EMAILS=['kpap@grnet.gr'])
|
74 |
>>> assert settings.MYAPP_CONTACT_EMAILS == ['kpap@grnet.gr']
|
75 |
|
76 |
"""
|
77 |
|
78 |
_prefix = kwargs.get('prefix', '') |
79 |
prefix = lambda key: '%s%s' % (_prefix, key) |
80 |
|
81 |
oldkeys = [k for k in dir(settings) if k.upper() == k] |
82 |
oldsettings = dict([(k, getattr(settings, k)) for k in oldkeys]) |
83 |
|
84 |
toremove = [] |
85 |
for key, value in kwargs.iteritems(): |
86 |
key = prefix(key) |
87 |
if not hasattr(settings, key): |
88 |
toremove.append(key) |
89 |
setattr(settings, key, value)
|
90 |
|
91 |
yield
|
92 |
|
93 |
# Remove keys that didn't exist
|
94 |
for key in toremove: |
95 |
delattr(settings, key)
|
96 |
|
97 |
# Remove keys that added during the execution of the context
|
98 |
if kwargs.get('reset_changes', True): |
99 |
newkeys = [k for k in dir(settings) if k.upper() == k] |
100 |
for key in newkeys: |
101 |
if not key in oldkeys: |
102 |
delattr(settings, key)
|
103 |
|
104 |
# Revert old keys
|
105 |
for key in oldkeys: |
106 |
if key == key.upper():
|
107 |
setattr(settings, key, oldsettings.get(key))
|
108 |
|
109 |
|
110 |
def with_settings(settings, prefix='', **override): |
111 |
def wrapper(func): |
112 |
def inner(*args, **kwargs): |
113 |
with override_settings(settings, prefix=prefix, **override):
|
114 |
ret = func(*args, **kwargs) |
115 |
return ret
|
116 |
return inner
|
117 |
return wrapper
|
118 |
|
119 |
|
120 |
@contextmanager
|
121 |
def astakos_user(user): |
122 |
"""
|
123 |
Context manager to mock astakos response.
|
124 |
|
125 |
usage:
|
126 |
with astakos_user("user@user.com"):
|
127 |
.... make api calls ....
|
128 |
|
129 |
"""
|
130 |
with patch("snf_django.lib.api.get_token") as get_token: |
131 |
get_token.return_value = "DummyToken"
|
132 |
with patch('astakosclient.AstakosClient.get_user_info') as m: |
133 |
m.return_value = {"uuid": uenc(user, 'utf8')} |
134 |
with patch('astakosclient.AstakosClient.get_quotas') as m2: |
135 |
m2.return_value = { |
136 |
"system": {
|
137 |
"pithos.diskspace": {
|
138 |
"usage": 0, |
139 |
"limit": 1073741824, # 1GB |
140 |
"pending": 0 |
141 |
} |
142 |
} |
143 |
} |
144 |
issue_fun = "astakosclient.AstakosClient.issue_one_commission"
|
145 |
with patch(issue_fun) as m3: |
146 |
serials = [] |
147 |
append = serials.append |
148 |
|
149 |
def get_serial(*args, **kwargs): |
150 |
global serial
|
151 |
serial += 1
|
152 |
append(serial) |
153 |
return serial
|
154 |
|
155 |
m3.side_effect = get_serial |
156 |
resolv_fun = \ |
157 |
'astakosclient.AstakosClient.resolve_commissions'
|
158 |
with patch(resolv_fun) as m4: |
159 |
m4.return_value = {'accepted': serials,
|
160 |
'rejected': [],
|
161 |
'failed': []}
|
162 |
users_fun = 'astakosclient.AstakosClient.get_usernames'
|
163 |
with patch(users_fun) as m5: |
164 |
|
165 |
def get_usernames(*args, **kwargs): |
166 |
uuids = args[-1]
|
167 |
return dict((uuid, uuid) for uuid in uuids) |
168 |
|
169 |
m5.side_effect = get_usernames |
170 |
yield
|
171 |
|
172 |
|
173 |
@contextmanager
|
174 |
def mocked_quotaholder(success=True): |
175 |
with patch("synnefo.quotas.Quotaholder.get") as astakos: |
176 |
global serial
|
177 |
serial += 10
|
178 |
|
179 |
def foo(*args, **kwargs): |
180 |
return (len(astakos.return_value.issue_one_commission.mock_calls) + |
181 |
serial) |
182 |
astakos.return_value.issue_one_commission.side_effect = foo |
183 |
astakos.return_value.resolve_commissions.return_value = {"failed": []}
|
184 |
yield
|
185 |
|
186 |
|
187 |
class BaseAPITest(TestCase): |
188 |
def get(self, url, user='user', *args, **kwargs): |
189 |
with astakos_user(user):
|
190 |
response = self.client.get(url, *args, **kwargs)
|
191 |
return response
|
192 |
|
193 |
def delete(self, url, user='user'): |
194 |
with astakos_user(user):
|
195 |
response = self.client.delete(url)
|
196 |
return response
|
197 |
|
198 |
def post(self, url, user='user', params={}, ctype='json', *args, **kwargs): |
199 |
if ctype == 'json': |
200 |
content_type = 'application/json'
|
201 |
with astakos_user(user):
|
202 |
response = self.client.post(url, params, content_type=content_type,
|
203 |
*args, **kwargs) |
204 |
return response
|
205 |
|
206 |
def put(self, url, user='user', params={}, ctype='json', *args, **kwargs): |
207 |
if ctype == 'json': |
208 |
content_type = 'application/json'
|
209 |
with astakos_user(user):
|
210 |
response = self.client.put(url, params, content_type=content_type,
|
211 |
*args, **kwargs) |
212 |
return response
|
213 |
|
214 |
def assertSuccess(self, response): |
215 |
self.assertTrue(response.status_code in [200, 203, 204]) |
216 |
|
217 |
def assertFault(self, response, status_code, name): |
218 |
self.assertEqual(response.status_code, status_code)
|
219 |
fault = json.loads(response.content) |
220 |
self.assertEqual(fault.keys(), [name])
|
221 |
|
222 |
def assertBadRequest(self, response): |
223 |
self.assertFault(response, 400, 'badRequest') |
224 |
|
225 |
def assertItemNotFound(self, response): |
226 |
self.assertFault(response, 404, 'itemNotFound') |
227 |
|
228 |
def assertMethodNotAllowed(self, response): |
229 |
self.assertFault(response, 400, 'badRequest') |
230 |
try:
|
231 |
error = json.loads(response.content) |
232 |
except ValueError: |
233 |
self.assertTrue(False) |
234 |
self.assertEqual(error['badRequest']['message'], 'Method not allowed') |
235 |
|
236 |
|
237 |
# Imitate unittest assertions new in Python 2.7
|
238 |
|
239 |
class _AssertRaisesContext(object): |
240 |
"""
|
241 |
A context manager used to implement TestCase.assertRaises* methods.
|
242 |
Adapted from unittest2.
|
243 |
"""
|
244 |
|
245 |
def __init__(self, expected): |
246 |
self.expected = expected
|
247 |
|
248 |
def __enter__(self): |
249 |
return self |
250 |
|
251 |
def __exit__(self, exc_type, exc_value, tb): |
252 |
if exc_type is None: |
253 |
try:
|
254 |
exc_name = self.expected.__name__
|
255 |
except AttributeError: |
256 |
exc_name = str(self.expected) |
257 |
raise AssertionError( |
258 |
"%s not raised" % (exc_name,))
|
259 |
if not issubclass(exc_type, self.expected): |
260 |
# let unexpected exceptions pass through
|
261 |
return False |
262 |
self.exception = exc_value # store for later retrieval |
263 |
return True |
264 |
|
265 |
|
266 |
def assertRaises(excClass): |
267 |
return _AssertRaisesContext(excClass)
|
268 |
|
269 |
|
270 |
def assertGreater(x, y): |
271 |
assert x > y
|
272 |
|
273 |
|
274 |
def assertIn(x, y): |
275 |
assert x in y |