Implements cleaner fake_request.

Implements the fake request to be more flexible.
Changes the fake request calls in the tests.
fixes bug 1010191

Change-Id: Ic8199b1f8f479ffb3667cead2576c03591e30e43
This commit is contained in:
Nikhil Komawar 2012-06-07 16:02:26 -04:00
parent ba0e3afbb2
commit c4c69ac15c
7 changed files with 68 additions and 64 deletions

View File

@ -33,18 +33,20 @@ USER1 = '54492ba0-f4df-4e4e-be62-27f4d76b29cf'
USER2 = '0b3b3006-cb76-4517-ae32-51397e22c754'
class FakeRequest(wsgi.Request):
def __init__(self):
#TODO(bcwaldon): figure out how to fake this out cleanly
super(FakeRequest, self).__init__({'REQUEST_METHOD': 'POST'})
def get_fake_request(path='', method='POST'):
req = wsgi.Request.blank(path)
req.method = method
kwargs = {
kwargs = {
'user': USER1,
'tenant': TENANT1,
'roles': [],
'is_admin': False,
}
self.context = glance.common.context.RequestContext(**kwargs)
req.context = glance.common.context.RequestContext(**kwargs)
return req
class FakeDB(object):

View File

@ -33,7 +33,7 @@ class TestImageAccessController(test_utils.BaseTestCase):
self.controller = image_access.Controller({}, self.db)
def test_index(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
output = self.controller.index(req, unit_test_utils.UUID1)
expected = [
{
@ -52,19 +52,19 @@ class TestImageAccessController(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_index_zero_records(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
output = self.controller.index(req, unit_test_utils.UUID2)
expected = []
self.assertEqual(expected, output)
def test_index_nonexistant_image(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
image_id = utils.generate_uuid()
self.assertRaises(exception.NotFound,
self.controller.index, req, image_id)
def test_show(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
image_id = unit_test_utils.UUID1
tenant_id = unit_test_utils.TENANT1
output = self.controller.show(req, image_id, tenant_id)
@ -77,14 +77,14 @@ class TestImageAccessController(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_show_nonexistant_image(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
image_id = utils.generate_uuid()
tenant_id = unit_test_utils.TENANT1
self.assertRaises(webob.exc.HTTPNotFound,
self.controller.show, req, image_id, tenant_id)
def test_show_nonexistant_tenant(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
image_id = unit_test_utils.UUID1
tenant_id = utils.generate_uuid()
self.assertRaises(webob.exc.HTTPNotFound,
@ -102,7 +102,7 @@ class TestImageAccessController(test_utils.BaseTestCase):
'can_share': True,
'deleted': False,
}
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
output = self.controller.create(req, unit_test_utils.UUID1, fixture)
self.assertEqual(expected, output)
@ -125,7 +125,7 @@ class TestImageAccessDeserializer(test_utils.BaseTestCase):
'can_share': False,
},
}
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps(fixture)
output = self.deserializer.create(request)
self.assertEqual(expected, output)
@ -159,7 +159,7 @@ class TestImageAccessDeserializerWithExtendedSchema(test_utils.BaseTestCase):
'color': 'blue',
},
}
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps(fixture)
output = self.deserializer.create(request)
self.assertEqual(expected, output)
@ -170,7 +170,7 @@ class TestImageAccessDeserializerWithExtendedSchema(test_utils.BaseTestCase):
'can_share': False,
'color': 'purple',
}
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps(fixture)
self.assertRaises(exception.InvalidObject,
self.deserializer.create, request)

View File

@ -36,40 +36,40 @@ class TestImagesController(base.StoreClearingUnitTest):
store_api=unit_test_utils.FakeStoreAPI())
def test_download(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
output = self.controller.download(request, unit_test_utils.UUID1)
expected = {'data': 'XXX', 'size': 3}
self.assertEqual(expected, output)
def test_download_no_data(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.assertRaises(webob.exc.HTTPNotFound, self.controller.download,
request, unit_test_utils.UUID2)
def test_download_non_existant_image(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.assertRaises(webob.exc.HTTPNotFound, self.controller.download,
request, utils.generate_uuid())
def test_upload_download(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.controller.upload(request, unit_test_utils.UUID2, 'YYYY', 4)
output = self.controller.download(request, unit_test_utils.UUID2)
expected = {'data': 'YYYY', 'size': 4}
self.assertEqual(expected, output)
def test_upload_non_existant_image(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.assertRaises(webob.exc.HTTPNotFound, self.controller.upload,
request, utils.generate_uuid(), 'YYYY', 4)
def test_upload_data_exists(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.assertRaises(webob.exc.HTTPConflict, self.controller.upload,
request, unit_test_utils.UUID1, 'YYYY', 4)
def test_upload_download_no_size(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.controller.upload(request, unit_test_utils.UUID2, 'YYYY', None)
output = self.controller.download(request, unit_test_utils.UUID2)
expected = {'data': 'YYYY', 'size': 4}
@ -83,7 +83,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
self.deserializer = glance.api.v2.image_data.RequestDeserializer()
def test_upload(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.headers['Content-Type'] = 'application/octet-stream'
request.body = 'YYY'
request.headers['Content-Length'] = 3
@ -94,7 +94,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_upload_chunked(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.headers['Content-Type'] = 'application/octet-stream'
# If we use body_file, webob assumes we want to do a chunked upload,
# ignoring the Content-Length header
@ -106,7 +106,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_upload_chunked_with_content_length(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.headers['Content-Type'] = 'application/octet-stream'
request.body_file = StringIO.StringIO('YYY')
# The deserializer shouldn't care if the Content-Length is
@ -119,7 +119,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_upload_with_incorrect_content_length(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.headers['Content-Type'] = 'application/octet-stream'
# The deserializer shouldn't care if the Content-Length and
# actual request body length differ. That job is left up
@ -133,7 +133,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_upload_wrong_content_type(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.headers['Content-Type'] = 'application/json'
request.body = 'YYYYY'
self.assertRaises(webob.exc.HTTPUnsupportedMediaType,

View File

@ -31,21 +31,21 @@ class TestImageTagsController(test_utils.BaseTestCase):
self.controller = glance.api.v2.image_tags.Controller(conf, self.db)
def test_list_tags(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
tags = self.controller.index(request, unit_test_utils.UUID1)
expected = ['ping', 'pong']
self.assertEqual(expected, tags)
def test_create_tag(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.controller.update(request, unit_test_utils.UUID1, 'dink')
def test_delete_tag(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.controller.delete(request, unit_test_utils.UUID1, 'ping')
def test_delete_tag_not_found(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete,
request, unit_test_utils.UUID1, 'what')

View File

@ -38,7 +38,7 @@ class TestImagesController(test_utils.BaseTestCase):
self.controller = glance.api.v2.images.ImagesController({}, self.db)
def test_index(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
output = self.controller.index(request)
self.assertEqual(2, len(output))
self.assertEqual(output[0]['id'], unit_test_utils.UUID1)
@ -46,12 +46,12 @@ class TestImagesController(test_utils.BaseTestCase):
def test_index_zero_images(self):
self.db.reset()
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
output = self.controller.index(request)
self.assertEqual([], output)
def test_show(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
output = self.controller.show(request, image_id=unit_test_utils.UUID2)
for key in ['created_at', 'updated_at']:
output.pop(key)
@ -69,11 +69,13 @@ class TestImagesController(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_show_non_existant(self):
self.assertRaises(webob.exc.HTTPNotFound, self.controller.show,
unit_test_utils.FakeRequest(), image_id=utils.generate_uuid())
request = unit_test_utils.get_fake_request()
image_id = utils.generate_uuid()
self.assertRaises(webob.exc.HTTPNotFound,
self.controller.show, request, image_id)
def test_create(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
image = {'name': 'image-1'}
output = self.controller.create(request, image)
for key in ['id', 'created_at', 'updated_at']:
@ -90,13 +92,13 @@ class TestImagesController(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_create_with_owner_forbidden(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
image = {'name': 'image-1', 'owner': utils.generate_uuid()}
self.assertRaises(webob.exc.HTTPForbidden, self.controller.create,
request, image)
def test_create_public_image_as_admin(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
image = {'name': 'image-1', 'is_public': True}
output = self.controller.create(request, image)
for key in ['id', 'created_at', 'updated_at']:
@ -113,7 +115,7 @@ class TestImagesController(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_update(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
image = {'name': 'image-2'}
output = self.controller.update(request, unit_test_utils.UUID1, image)
for key in ['id', 'created_at', 'updated_at']:
@ -130,7 +132,7 @@ class TestImagesController(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_update_non_existant(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
image = {'name': 'image-2'}
self.assertRaises(webob.exc.HTTPNotFound, self.controller.update,
request, utils.generate_uuid(), image)
@ -145,7 +147,7 @@ class TestImagesDeserializer(test_utils.BaseTestCase):
{}, schema_api)
def test_create_with_id(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
image_id = utils.generate_uuid()
request.body = json.dumps({'id': image_id})
output = self.deserializer.create(request)
@ -153,21 +155,21 @@ class TestImagesDeserializer(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_create_with_name(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'name': 'image-1'})
output = self.deserializer.create(request)
expected = {'image': {'name': 'image-1', 'properties': {}}}
self.assertEqual(expected, output)
def test_create_public(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'visibility': 'public'})
output = self.deserializer.create(request)
expected = {'image': {'is_public': True, 'properties': {}}}
self.assertEqual(expected, output)
def test_create_private(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'visibility': 'private'})
output = self.deserializer.create(request)
expected = {'image': {'is_public': False, 'properties': {}}}
@ -175,21 +177,21 @@ class TestImagesDeserializer(test_utils.BaseTestCase):
def test_create_readonly_attributes_ignored(self):
for key in ['created_at', 'updated_at']:
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({key: ISOTIME})
output = self.deserializer.create(request)
expected = {'image': {'properties': {}}}
self.assertEqual(expected, output)
def test_create_with_tags(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'tags': ['one', 'two']})
output = self.deserializer.create(request)
expected = {'image': {'tags': ['one', 'two'], 'properties': {}}}
self.assertEqual(expected, output)
def test_update(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'name': 'image-1', 'visibility': 'public'})
output = self.deserializer.update(request)
expected = {
@ -203,7 +205,7 @@ class TestImagesDeserializer(test_utils.BaseTestCase):
def test_update_readonly_attributes_ignored(self):
for key in ['created_at', 'updated_at']:
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({key: ISOTIME})
output = self.deserializer.update(request)
expected = {'image': {'properties': {}}}
@ -227,7 +229,7 @@ class TestImagesDeserializerWithExtendedSchema(test_utils.BaseTestCase):
{}, schema_api)
def test_create(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'name': 'image-1', 'pants': 'on'})
output = self.deserializer.create(request)
expected = {
@ -239,13 +241,13 @@ class TestImagesDeserializerWithExtendedSchema(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_create_bad_data(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'name': 'image-1', 'pants': 'borked'})
self.assertRaises(exception.InvalidObject,
self.deserializer.create, request)
def test_update(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'name': 'image-1', 'pants': 'off'})
output = self.deserializer.update(request)
expected = {
@ -257,7 +259,7 @@ class TestImagesDeserializerWithExtendedSchema(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_update_bad_data(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'name': 'image-1', 'pants': 'borked'})
self.assertRaises(exception.InvalidObject,
self.deserializer.update, request)
@ -273,7 +275,7 @@ class TestImagesDeserializerWithAdditionalProperties(test_utils.BaseTestCase):
{}, schema_api)
def test_create(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'foo': 'bar'})
output = self.deserializer.create(request)
expected = {'image': {'properties': {'foo': 'bar'}}}
@ -281,25 +283,25 @@ class TestImagesDeserializerWithAdditionalProperties(test_utils.BaseTestCase):
def test_create_with_additional_properties_disallowed(self):
self.config(allow_additional_image_properties=False)
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'foo': 'bar'})
self.assertRaises(exception.InvalidObject,
self.deserializer.create, request)
def test_create_with_numeric_property(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'abc': 123})
self.assertRaises(exception.InvalidObject,
self.deserializer.create, request)
def test_create_with_list_property(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'foo': ['bar']})
self.assertRaises(exception.InvalidObject,
self.deserializer.create, request)
def test_update(self):
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'foo': 'bar'})
output = self.deserializer.update(request)
expected = {'image': {'properties': {'foo': 'bar'}}}
@ -307,7 +309,7 @@ class TestImagesDeserializerWithAdditionalProperties(test_utils.BaseTestCase):
def test_update_with_additional_properties_disallowed(self):
self.config(allow_additional_image_properties=False)
request = unit_test_utils.FakeRequest()
request = unit_test_utils.get_fake_request()
request.body = json.dumps({'foo': 'bar'})
self.assertRaises(exception.InvalidObject,
self.deserializer.update, request)

View File

@ -25,7 +25,7 @@ class TestRootController(test_utils.BaseTestCase):
self.controller = glance.api.v2.root.RootController()
def test_index(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
output = self.controller.index(req)
expected = {
'links': [

View File

@ -27,7 +27,7 @@ class TestSchemasController(test_utils.BaseTestCase):
self.controller = schemas.Controller({}, self.schema_api)
def test_index(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
output = self.controller.index(req)
expected = {'links': [
{'rel': 'image', 'href': '/v2/schemas/image'},
@ -36,11 +36,11 @@ class TestSchemasController(test_utils.BaseTestCase):
self.assertEqual(expected, output)
def test_image(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
output = self.controller.image(req)
self.assertEqual(self.schema_api.get_schema('image'), output)
def test_access(self):
req = unit_test_utils.FakeRequest()
req = unit_test_utils.get_fake_request()
output = self.controller.access(req)
self.assertEqual(self.schema_api.get_schema('access'), output)