Merge "Validate keypair create request body"

This commit is contained in:
Jenkins 2012-09-13 10:33:34 +00:00 committed by Gerrit Code Review
commit 0296a2d249
2 changed files with 19 additions and 2 deletions

View File

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

View File

@ -295,6 +295,18 @@ class KeypairsTest(test.TestCase):
self.assertTrue('key_name' in server_dict)
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):
def setUp(self):