Statistics
| Branch: | Tag: | Revision:

root / api / tests_redux.py @ 29a59bc1

History | View | Annotate | Download (2.9 kB)

1
#
2
# Copyright (c) 2010 Greek Research and Technology Network
3
#
4

    
5
from django.test import TestCase
6
from django.test.client import Client
7
from django.utils import simplejson as json
8

    
9

    
10
API = 'v1.1redux'
11

    
12

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