1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
19
"""Tests for Direct API."""
26
from nova import compute
27
from nova import context
28
from nova import exception
30
from nova import utils
31
from nova.api import direct
32
from nova.tests import test_cloud
35
class FakeService(object):
36
def echo(self, context, data):
39
def context(self, context):
40
return {'user': context.user_id,
41
'project': context.project_id}
44
class DirectTestCase(test.TestCase):
46
super(DirectTestCase, self).setUp()
47
direct.register_service('fake', FakeService())
48
self.router = direct.PostParamsMiddleware(
49
direct.JsonParamsMiddleware(
51
self.auth_router = direct.DelegatedAuthMiddleware(self.router)
52
self.context = context.RequestContext('user1', 'proj1')
57
def test_delegated_auth(self):
58
req = webob.Request.blank('/fake/context')
59
req.headers['X-OpenStack-User'] = 'user1'
60
req.headers['X-OpenStack-Project'] = 'proj1'
61
resp = req.get_response(self.auth_router)
62
data = json.loads(resp.body)
63
self.assertEqual(data['user'], 'user1')
64
self.assertEqual(data['project'], 'proj1')
66
def test_json_params(self):
67
req = webob.Request.blank('/fake/echo')
68
req.environ['openstack.context'] = self.context
70
req.body = 'json=%s' % json.dumps({'data': 'foo'})
71
resp = req.get_response(self.router)
72
resp_parsed = json.loads(resp.body)
73
self.assertEqual(resp_parsed['data'], 'foo')
75
def test_post_params(self):
76
req = webob.Request.blank('/fake/echo')
77
req.environ['openstack.context'] = self.context
80
resp = req.get_response(self.router)
81
resp_parsed = json.loads(resp.body)
82
self.assertEqual(resp_parsed['data'], 'foo')
85
proxy = direct.Proxy(self.router)
86
rv = proxy.fake.echo(self.context, data='baz')
87
self.assertEqual(rv['data'], 'baz')
90
class DirectCloudTestCase(test_cloud.CloudTestCase):
92
super(DirectCloudTestCase, self).setUp()
93
compute_handle = compute.API(image_service=self.cloud.image_service,
94
network_api=self.cloud.network_api,
95
volume_api=self.cloud.volume_api)
96
direct.register_service('compute', compute_handle)
97
self.router = direct.JsonParamsMiddleware(direct.Router())
98
proxy = direct.Proxy(self.router)
99
self.cloud.compute_api = proxy.compute
102
super(DirectCloudTestCase, self).tearDown()