root / snf-cyclades-app / synnefo / api / tests.py @ f6ff4b40
History | View | Annotate | Download (4.2 kB)
1 |
# Copyright 2012 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 |
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 |
def dummy_get_user(request, *args, **kwargs): |
54 |
request.user = {'username': user, 'groups': []} |
55 |
request.user_uniq = user |
56 |
|
57 |
with patch('synnefo.api.util.get_user') as m: |
58 |
m.side_effect = dummy_get_user |
59 |
yield
|
60 |
|
61 |
|
62 |
class BaseAPITest(TestCase): |
63 |
def get(self, url, user='user', *args, **kwargs): |
64 |
with astakos_user(user):
|
65 |
response = self.client.get(url, *args, **kwargs)
|
66 |
return response
|
67 |
|
68 |
def delete(self, url, user='user'): |
69 |
with astakos_user(user):
|
70 |
response = self.client.delete(url)
|
71 |
return response
|
72 |
|
73 |
def post(self, url, user='user', params={}, ctype='json', *args, **kwargs): |
74 |
if ctype == 'json': |
75 |
content_type = 'application/json'
|
76 |
with astakos_user(user):
|
77 |
response = self.client.post(url, params, content_type=content_type,
|
78 |
*args, **kwargs) |
79 |
return response
|
80 |
|
81 |
def put(self, url, user='user', params={}, ctype='json', *args, **kwargs): |
82 |
if ctype == 'json': |
83 |
content_type = 'application/json'
|
84 |
with astakos_user(user):
|
85 |
response = self.client.put(url, params, content_type=content_type,
|
86 |
*args, **kwargs) |
87 |
return response
|
88 |
|
89 |
def assertSuccess(self, response): |
90 |
self.assertTrue(response.status_code in [200, 203, 204]) |
91 |
|
92 |
def assertFault(self, response, status_code, name): |
93 |
self.assertEqual(response.status_code, status_code)
|
94 |
fault = json.loads(response.content) |
95 |
self.assertEqual(fault.keys(), [name])
|
96 |
|
97 |
def assertBadRequest(self, response): |
98 |
self.assertFault(response, 400, 'badRequest') |
99 |
|
100 |
def assertItemNotFound(self, response): |
101 |
self.assertFault(response, 404, 'itemNotFound') |
102 |
|
103 |
|
104 |
class APITest(TestCase): |
105 |
def test_api_version(self): |
106 |
"""Check API version."""
|
107 |
with astakos_user('user'): |
108 |
response = self.client.get('/api/v1.1/') |
109 |
self.assertEqual(response.status_code, 200) |
110 |
api_version = json.loads(response.content)['version']
|
111 |
self.assertEqual(api_version['id'], 'v1.1') |
112 |
self.assertEqual(api_version['status'], 'CURRENT') |
113 |
|
114 |
|
115 |
# Import TestCases
|
116 |
from synnefo.api.test.servers import * |
117 |
from synnefo.api.test.networks import * |
118 |
from synnefo.api.test.flavors import * |
119 |
from synnefo.api.test.images import * |