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