Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / tests.py @ f6ff4b40

History | View | Annotate | Download (4.2 kB)

1 47eeffa9 Christos Stavrakakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 c25cc9ec Vangelis Koukis
#
3 adee02b8 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 adee02b8 Giorgos Verigakis
# without modification, are permitted provided that the following
5 adee02b8 Giorgos Verigakis
# conditions are met:
6 c25cc9ec Vangelis Koukis
#
7 adee02b8 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 adee02b8 Giorgos Verigakis
#      disclaimer.
10 c25cc9ec Vangelis Koukis
#
11 adee02b8 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 adee02b8 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 adee02b8 Giorgos Verigakis
#      provided with the distribution.
15 c25cc9ec Vangelis Koukis
#
16 adee02b8 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 adee02b8 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 adee02b8 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 adee02b8 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 adee02b8 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 adee02b8 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 adee02b8 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 adee02b8 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 adee02b8 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 adee02b8 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 adee02b8 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 adee02b8 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 c25cc9ec Vangelis Koukis
#
29 adee02b8 Giorgos Verigakis
# The views and conclusions contained in the software and
30 adee02b8 Giorgos Verigakis
# documentation are those of the authors and should not be
31 adee02b8 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 adee02b8 Giorgos Verigakis
# or implied, of GRNET S.A.
33 894f6b7e Dimitris Moraitis
34 53b1eea6 Giorgos Verigakis
from __future__ import with_statement
35 53b1eea6 Giorgos Verigakis
36 a237869d Faidon Liambotis
from django.utils import simplejson as json
37 894f6b7e Dimitris Moraitis
from django.test import TestCase
38 40777cc8 Giorgos Verigakis
39 039e3e61 Christos Stavrakakis
from mock import patch
40 08b079e2 Stavros Sachtouris
from contextlib import contextmanager
41 47eeffa9 Christos Stavrakakis
42 039e3e61 Christos Stavrakakis
43 08b079e2 Stavros Sachtouris
@contextmanager
44 08b079e2 Stavros Sachtouris
def astakos_user(user):
45 08b079e2 Stavros Sachtouris
    """
46 08b079e2 Stavros Sachtouris
    Context manager to mock astakos response.
47 08b079e2 Stavros Sachtouris

48 08b079e2 Stavros Sachtouris
    usage:
49 08b079e2 Stavros Sachtouris
    with astakos_user("user@user.com"):
50 47eeffa9 Christos Stavrakakis
        .... make api calls ....
51 08b079e2 Stavros Sachtouris

52 08b079e2 Stavros Sachtouris
    """
53 08b079e2 Stavros Sachtouris
    def dummy_get_user(request, *args, **kwargs):
54 47eeffa9 Christos Stavrakakis
        request.user = {'username': user, 'groups': []}
55 47eeffa9 Christos Stavrakakis
        request.user_uniq = user
56 ce55f211 Kostas Papadimitriou
57 52194c77 Christos Stavrakakis
    with patch('synnefo.api.util.get_user') as m:
58 52194c77 Christos Stavrakakis
        m.side_effect = dummy_get_user
59 52194c77 Christos Stavrakakis
        yield
60 54c30633 Markos Gogoulos
61 668c1361 Markos Gogoulos
62 52194c77 Christos Stavrakakis
class BaseAPITest(TestCase):
63 52194c77 Christos Stavrakakis
    def get(self, url, user='user', *args, **kwargs):
64 47eeffa9 Christos Stavrakakis
        with astakos_user(user):
65 52194c77 Christos Stavrakakis
            response = self.client.get(url, *args, **kwargs)
66 47eeffa9 Christos Stavrakakis
        return response
67 47eeffa9 Christos Stavrakakis
68 47eeffa9 Christos Stavrakakis
    def delete(self, url, user='user'):
69 47eeffa9 Christos Stavrakakis
        with astakos_user(user):
70 47eeffa9 Christos Stavrakakis
            response = self.client.delete(url)
71 47eeffa9 Christos Stavrakakis
        return response
72 47eeffa9 Christos Stavrakakis
73 52194c77 Christos Stavrakakis
    def post(self, url, user='user', params={}, ctype='json', *args, **kwargs):
74 47eeffa9 Christos Stavrakakis
        if ctype == 'json':
75 47eeffa9 Christos Stavrakakis
            content_type = 'application/json'
76 47eeffa9 Christos Stavrakakis
        with astakos_user(user):
77 52194c77 Christos Stavrakakis
            response = self.client.post(url, params, content_type=content_type,
78 52194c77 Christos Stavrakakis
                                        *args, **kwargs)
79 47eeffa9 Christos Stavrakakis
        return response
80 47eeffa9 Christos Stavrakakis
81 52194c77 Christos Stavrakakis
    def put(self, url, user='user', params={}, ctype='json', *args, **kwargs):
82 47eeffa9 Christos Stavrakakis
        if ctype == 'json':
83 47eeffa9 Christos Stavrakakis
            content_type = 'application/json'
84 47eeffa9 Christos Stavrakakis
        with astakos_user(user):
85 52194c77 Christos Stavrakakis
            response = self.client.put(url, params, content_type=content_type,
86 e440e835 Christos Stavrakakis
                                       *args, **kwargs)
87 47eeffa9 Christos Stavrakakis
        return response
88 47eeffa9 Christos Stavrakakis
89 47eeffa9 Christos Stavrakakis
    def assertSuccess(self, response):
90 52194c77 Christos Stavrakakis
        self.assertTrue(response.status_code in [200, 203, 204])
91 685ab2b6 Markos Gogoulos
92 47eeffa9 Christos Stavrakakis
    def assertFault(self, response, status_code, name):
93 47eeffa9 Christos Stavrakakis
        self.assertEqual(response.status_code, status_code)
94 47eeffa9 Christos Stavrakakis
        fault = json.loads(response.content)
95 47eeffa9 Christos Stavrakakis
        self.assertEqual(fault.keys(), [name])
96 668c1361 Markos Gogoulos
97 47eeffa9 Christos Stavrakakis
    def assertBadRequest(self, response):
98 47eeffa9 Christos Stavrakakis
        self.assertFault(response, 400, 'badRequest')
99 aa197ee4 Vangelis Koukis
100 47eeffa9 Christos Stavrakakis
    def assertItemNotFound(self, response):
101 47eeffa9 Christos Stavrakakis
        self.assertFault(response, 404, 'itemNotFound')
102 668c1361 Markos Gogoulos
103 aa197ee4 Vangelis Koukis
104 52194c77 Christos Stavrakakis
class APITest(TestCase):
105 52194c77 Christos Stavrakakis
    def test_api_version(self):
106 52194c77 Christos Stavrakakis
        """Check API version."""
107 52194c77 Christos Stavrakakis
        with astakos_user('user'):
108 52194c77 Christos Stavrakakis
            response = self.client.get('/api/v1.1/')
109 52194c77 Christos Stavrakakis
        self.assertEqual(response.status_code, 200)
110 52194c77 Christos Stavrakakis
        api_version = json.loads(response.content)['version']
111 52194c77 Christos Stavrakakis
        self.assertEqual(api_version['id'], 'v1.1')
112 52194c77 Christos Stavrakakis
        self.assertEqual(api_version['status'], 'CURRENT')
113 039e3e61 Christos Stavrakakis
114 039e3e61 Christos Stavrakakis
115 039e3e61 Christos Stavrakakis
# Import TestCases
116 039e3e61 Christos Stavrakakis
from synnefo.api.test.servers import *
117 039e3e61 Christos Stavrakakis
from synnefo.api.test.networks import *
118 039e3e61 Christos Stavrakakis
from synnefo.api.test.flavors import *
119 039e3e61 Christos Stavrakakis
from synnefo.api.test.images import *