summaryrefslogtreecommitdiffstats
path: root/nova/tests
diff options
context:
space:
mode:
authorMark McLoughlin <markmc@redhat.com>2012-09-12 12:50:53 +0100
committerMark McLoughlin <markmc@redhat.com>2012-09-13 16:10:29 +0100
commitd1ad73eebd28f33c98b56c033c2c4d305d66d5dc (patch)
tree5c6478c53d19bfcc1146825fa0a43bcd6dee1313 /nova/tests
parente8d0f9940edd1043162c187ec92f93e8c9fafb02 (diff)
Add entity body validation helper
Add a _valid_body() helper for the servers API to avoid repeating the same tests in multiple methods. Include a check that the 'server' value is actually a dict. Also remove the superfluous checking of req.body which is a leftover from when the body wasn't a method parameter. Change-Id: If8114cc76d68567005c85c803f29e30e034db89a
Diffstat (limited to 'nova/tests')
-rw-r--r--nova/tests/api/openstack/compute/test_servers.py18
-rw-r--r--nova/tests/api/openstack/test_wsgi.py29
2 files changed, 37 insertions, 10 deletions
diff --git a/nova/tests/api/openstack/compute/test_servers.py b/nova/tests/api/openstack/compute/test_servers.py
index fa811fd2c..1a2026900 100644
--- a/nova/tests/api/openstack/compute/test_servers.py
+++ b/nova/tests/api/openstack/compute/test_servers.py
@@ -2765,16 +2765,6 @@ class ServersControllerCreateTest(test.TestCase):
# The fact that the action doesn't raise is enough validation
self.controller.create(req, body)
- def test_create_instance_malformed_entity(self):
- req = fakes.HTTPRequest.blank('/v2/fake/servers')
- req.method = 'POST'
- body = {'server': 'string'}
- req.body = jsonutils.dumps(body)
- req.headers['content-type'] = "application/json"
-
- self.assertRaises(webob.exc.HTTPBadRequest,
- self.controller.create, req, body)
-
def test_create_location(self):
selfhref = 'http://localhost/v2/fake/servers/%s' % FAKE_UUID
bookhref = 'http://localhost/fake/servers/%s' % FAKE_UUID
@@ -4866,6 +4856,10 @@ class ServersUnprocessableEntityTestCase(test.TestCase):
body = {'foo': {'a': 'b'}}
self._unprocessable_server_create(body=body)
+ def test_create_server_malformed_entity(self):
+ body = {'server': 'string'}
+ self._unprocessable_server_create(body=body)
+
def _unprocessable_server_update(self, body):
req = fakes.HTTPRequest.blank('/v2/fake/servers/%s' % FAKE_UUID)
req.method = 'PUT'
@@ -4879,3 +4873,7 @@ class ServersUnprocessableEntityTestCase(test.TestCase):
def test_update_server_missing_server(self):
body = {'foo': {'a': 'b'}}
self._unprocessable_server_update(body=body)
+
+ def test_create_update_malformed_entity(self):
+ body = {'server': 'string'}
+ self._unprocessable_server_update(body=body)
diff --git a/nova/tests/api/openstack/test_wsgi.py b/nova/tests/api/openstack/test_wsgi.py
index 7427eb2af..387940fc2 100644
--- a/nova/tests/api/openstack/test_wsgi.py
+++ b/nova/tests/api/openstack/test_wsgi.py
@@ -861,3 +861,32 @@ class ResponseObjectTest(test.TestCase):
self.assertEqual(response.headers['X-header2'], 'header2')
self.assertEqual(response.status_int, 202)
self.assertEqual(response.body, mtype)
+
+
+class ValidBodyTest(test.TestCase):
+
+ def setUp(self):
+ super(ValidBodyTest, self).setUp()
+ self.controller = wsgi.Controller()
+
+ def test_is_valid_body(self):
+ body = {'foo': {}}
+ self.assertTrue(self.controller.is_valid_body(body, 'foo'))
+
+ def test_is_valid_body_none(self):
+ resource = wsgi.Resource(controller=None)
+ self.assertFalse(self.controller.is_valid_body(None, 'foo'))
+
+ def test_is_valid_body_empty(self):
+ resource = wsgi.Resource(controller=None)
+ self.assertFalse(self.controller.is_valid_body({}, 'foo'))
+
+ def test_is_valid_body_no_entity(self):
+ resource = wsgi.Resource(controller=None)
+ body = {'bar': {}}
+ self.assertFalse(self.controller.is_valid_body(body, 'foo'))
+
+ def test_is_valid_body_malformed_entity(self):
+ resource = wsgi.Resource(controller=None)
+ body = {'foo': 'bar'}
+ self.assertFalse(self.controller.is_valid_body(body, 'foo'))