tests_redux.py 2.88 KB
Newer Older
1
2
3
4
5
6
#
# Copyright (c) 2010 Greek Research and Technology Network
#

from django.test import TestCase
from django.test.client import Client
7
from django.utils import simplejson as json
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


API = 'v1.1redux'


class APIReduxTestCase(TestCase):
    fixtures = [ 'api_redux_test_data' ]
    
    def setUp(self):
        self.client = Client()
        self.server_id = 0
    
    def create_server_name(self):
        self.server_id += 1
        return 'server%d' % self.server_id
    
    def test_create_server_json(self):
        TEMPLATE = '''
        {
            "server" : {
                "name" : "%(name)s",
Giorgos Verigakis's avatar
Giorgos Verigakis committed
29
30
                "flavorRef" : "%(flavorRef)s",
                "imageRef" : "%(imageRef)s"
31
32
33
34
            }
        }
        '''
        
Giorgos Verigakis's avatar
Giorgos Verigakis committed
35
        def new_server(imageRef=1, flavorRef=1):
36
            name = self.create_server_name()
Giorgos Verigakis's avatar
Giorgos Verigakis committed
37
            return name, TEMPLATE % dict(name=name, imageRef=imageRef, flavorRef=flavorRef)
38
39
40
41
42
43
        
        def verify_response(response, name):
            assert response.status_code == 202
            reply =  json.loads(response.content)
            server = reply['server']
            assert server['name'] == name
Giorgos Verigakis's avatar
Giorgos Verigakis committed
44
45
            assert server['imageRef'] == 1
            assert server['flavorRef'] == 1
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
            assert server['status'] == 'BUILD'
            assert server['adminPass']
            assert server['addresses']
        
        def verify_error(response, code, name):
            assert response.status_code == code
            reply =  json.loads(response.content)
            assert name in reply
            assert reply[name]['code'] == code
        
        name, data = new_server()
        url = '/api/%s/servers' % API
        response = self.client.post(url, content_type='application/json', data=data)
        verify_response(response, name)
        
        name, data = new_server()
        url = '/api/%s/servers.json' % API
        response = self.client.post(url, content_type='application/json', data=data)
        verify_response(response, name)
        
        name, data = new_server()
        url = '/api/%s/servers.json' % API
        response = self.client.post(url, content_type='application/json', data=data,
                                    HTTP_ACCEPT='application/xml')
        verify_response(response, name)
        
Giorgos Verigakis's avatar
Giorgos Verigakis committed
72
        name, data = new_server(imageRef=0)
73
74
75
76
        url = '/api/%s/servers' % API
        response = self.client.post(url, content_type='application/json', data=data)
        verify_error(response, 404, 'itemNotFound')
        
Giorgos Verigakis's avatar
Giorgos Verigakis committed
77
        name, data = new_server(flavorRef=0)
78
79
80
81
82
83
84
        url = '/api/%s/servers' % API
        response = self.client.post(url, content_type='application/json', data=data)
        verify_error(response, 404, 'itemNotFound')
        
        url = '/api/%s/servers' % API
        response = self.client.post(url, content_type='application/json', data='INVALID')
        verify_error(response, 400, 'badRequest')