Statistics
| Branch: | Tag: | Revision:

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

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
    with patch("snf_django.lib.api.get_token") as get_token:
54
        get_token.return_value = "DummyToken"
55
        with patch('astakosclient.AstakosClient.get_user_info') as m:
56
            m.return_value = {"uuid": user}
57
            yield
58

    
59

    
60
class BaseAPITest(TestCase):
61
    def get(self, url, user='user', *args, **kwargs):
62
        with astakos_user(user):
63
            response = self.client.get(url, *args, **kwargs)
64
        return response
65

    
66
    def delete(self, url, user='user'):
67
        with astakos_user(user):
68
            response = self.client.delete(url)
69
        return response
70

    
71
    def post(self, url, user='user', params={}, ctype='json', *args, **kwargs):
72
        if ctype == 'json':
73
            content_type = 'application/json'
74
        with astakos_user(user):
75
            response = self.client.post(url, params, content_type=content_type,
76
                                        *args, **kwargs)
77
        return response
78

    
79
    def put(self, url, user='user', params={}, ctype='json', *args, **kwargs):
80
        if ctype == 'json':
81
            content_type = 'application/json'
82
        with astakos_user(user):
83
            response = self.client.put(url, params, content_type=content_type,
84
                                       *args, **kwargs)
85
        return response
86

    
87
    def assertSuccess(self, response):
88
        self.assertTrue(response.status_code in [200, 203, 204])
89

    
90
    def assertFault(self, response, status_code, name):
91
        self.assertEqual(response.status_code, status_code)
92
        fault = json.loads(response.content)
93
        self.assertEqual(fault.keys(), [name])
94

    
95
    def assertBadRequest(self, response):
96
        self.assertFault(response, 400, 'badRequest')
97

    
98
    def assertItemNotFound(self, response):
99
        self.assertFault(response, 404, 'itemNotFound')
100

    
101

    
102
class APITest(TestCase):
103
    def test_api_version(self):
104
        """Check API version."""
105
        with astakos_user('user'):
106
            response = self.client.get('/api/v1.1/')
107
        self.assertEqual(response.status_code, 200)
108
        api_version = json.loads(response.content)['version']
109
        self.assertEqual(api_version['id'], 'v1.1')
110
        self.assertEqual(api_version['status'], 'CURRENT')
111

    
112

    
113
# Import TestCases
114
from synnefo.api.test.servers import *
115
from synnefo.api.test.networks import *
116
from synnefo.api.test.flavors import *
117
from synnefo.api.test.images import *