Merge "Adds validation negative unit tests"

This commit is contained in:
Jenkins 2016-02-07 08:20:13 +00:00 committed by Gerrit Code Review
commit 52d0972ef0
1 changed files with 23 additions and 9 deletions

View File

@ -107,12 +107,14 @@ _VALID_FILTERS = [{'interface': 'admin'},
_INVALID_FILTERS = ['some string', 1, 0, True, False] _INVALID_FILTERS = ['some string', 1, 0, True, False]
def expected_validation_failure(f): def expected_validation_failure(msg):
def wrapper(self, *args, **kwargs): def wrapper(f):
args = (self,) + args def wrapped(self, *args, **kwargs):
e = self.assertRaises(exception.ValidationError, f, *args, **kwargs) args = (self,) + args
self.assertIn('Expecting to find entity in request body', e = self.assertRaises(exception.ValidationError, f,
six.text_type(e)) *args, **kwargs)
self.assertIn(msg, six.text_type(e))
return wrapped
return wrapper return wrapper
@ -130,6 +132,10 @@ class ValidatedDecoratorTests(unit.BaseTestCase):
'name': uuid.uuid4().hex, 'name': uuid.uuid4().hex,
} }
invalid_entity = {
'name': 1.0, # NOTE(dstanek): this is the incorrect type for name
}
@validation.validated(entity_create, 'entity') @validation.validated(entity_create, 'entity')
def create_entity(self, entity): def create_entity(self, entity):
"""Used to test cases where validated param is the only param.""" """Used to test cases where validated param is the only param."""
@ -149,7 +155,7 @@ class ValidatedDecoratorTests(unit.BaseTestCase):
"""Test the case when client passing in an empty kwarg reference.""" """Test the case when client passing in an empty kwarg reference."""
self.create_entity_optional_body(entity={}) self.create_entity_optional_body(entity={})
@expected_validation_failure @expected_validation_failure('Expecting to find entity in request body')
def test_calling_create_with_kwarg_as_None_fails(self): def test_calling_create_with_kwarg_as_None_fails(self):
self.create_entity(entity=None) self.create_entity(entity=None)
@ -160,11 +166,15 @@ class ValidatedDecoratorTests(unit.BaseTestCase):
"""Test the case when client passing in an empty entity reference.""" """Test the case when client passing in an empty entity reference."""
self.create_entity_optional_body({}) self.create_entity_optional_body({})
@expected_validation_failure @expected_validation_failure("Invalid input for field 'name'")
def test_calling_create_with_invalid_entity_fails(self):
self.create_entity(self.invalid_entity)
@expected_validation_failure('Expecting to find entity in request body')
def test_calling_create_with_entity_arg_as_None_fails(self): def test_calling_create_with_entity_arg_as_None_fails(self):
self.create_entity(None) self.create_entity(None)
@expected_validation_failure @expected_validation_failure('Expecting to find entity in request body')
def test_calling_create_without_an_entity_fails(self): def test_calling_create_without_an_entity_fails(self):
self.create_entity() self.create_entity()
@ -182,6 +192,10 @@ class ValidatedDecoratorTests(unit.BaseTestCase):
def test_calling_update_with_valid_entity_succeeds(self): def test_calling_update_with_valid_entity_succeeds(self):
self.update_entity(uuid.uuid4().hex, self.valid_entity) self.update_entity(uuid.uuid4().hex, self.valid_entity)
@expected_validation_failure("Invalid input for field 'name'")
def test_calling_update_with_invalid_entity_fails(self):
self.update_entity(uuid.uuid4().hex, self.invalid_entity)
def test_calling_update_with_empty_entity_kwarg_succeeds(self): def test_calling_update_with_empty_entity_kwarg_succeeds(self):
"""Test the case when client passing in an empty entity reference.""" """Test the case when client passing in an empty entity reference."""
global entity_update global entity_update