Validate keypair create request body

Check if keypair create request body has manditory keys like keypair, name
and raise HTTPBadRequest if the required keys are missing.

Fixes bug 1049619

Change-Id: Ia6129e1b04e910fbea81b62b921a228aafa32f64
This commit is contained in:
Sirisha Devineni 2012-09-13 00:00:32 +05:30
parent e72db9fe93
commit 760e59b863
2 changed files with 19 additions and 2 deletions

View File

@ -70,8 +70,13 @@ class KeypairController(object):
context = req.environ['nova.context'] context = req.environ['nova.context']
authorize(context) authorize(context)
try:
params = body['keypair'] params = body['keypair']
name = params['name'] name = params['name']
except KeyError:
msg = _("Invalid request body")
raise webob.exc.HTTPBadRequest(explanation=msg)
try: try:
if 'public_key' in params: if 'public_key' in params:

View File

@ -295,6 +295,18 @@ class KeypairsTest(test.TestCase):
self.assertTrue('key_name' in server_dict) self.assertTrue('key_name' in server_dict)
self.assertEquals(server_dict['key_name'], '') self.assertEquals(server_dict['key_name'], '')
def test_keypair_create_with_invalid_keypairBody(self):
body = {'alpha': {'name': 'create_test'}}
req = webob.Request.blank('/v1.1/fake/os-keypairs')
req.method = 'POST'
req.body = jsonutils.dumps(body)
req.headers['Content-Type'] = 'application/json'
res = req.get_response(fakes.wsgi_app())
res_dict = jsonutils.loads(res.body)
self.assertEqual(res.status_int, 400)
self.assertEqual(res_dict['badRequest']['message'],
"Invalid request body")
class KeypairsXMLSerializerTest(test.TestCase): class KeypairsXMLSerializerTest(test.TestCase):
def setUp(self): def setUp(self):