From bd0d4e3c8c0d142859b914076c93eb79c0bd200d Mon Sep 17 00:00:00 2001 From: David Stanek Date: Fri, 5 Feb 2016 16:15:24 +0000 Subject: [PATCH] Adds validation negative unit tests Change If3dd49af5e16bb3b741efa4573f9f8e9085ddd14 removed tests that are vital to ensuring validation is properly working. They ensure that the validator is actually being used. Without them it is possible to comment out the use of the validator and still have passing tests. Change-Id: I040f76e0f624ae19733a2d243c1fcc4e9ca181c1 --- keystone/tests/unit/test_validation.py | 32 ++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/keystone/tests/unit/test_validation.py b/keystone/tests/unit/test_validation.py index 582696f85f..73cb6ef622 100644 --- a/keystone/tests/unit/test_validation.py +++ b/keystone/tests/unit/test_validation.py @@ -107,12 +107,14 @@ _VALID_FILTERS = [{'interface': 'admin'}, _INVALID_FILTERS = ['some string', 1, 0, True, False] -def expected_validation_failure(f): - def wrapper(self, *args, **kwargs): - args = (self,) + args - e = self.assertRaises(exception.ValidationError, f, *args, **kwargs) - self.assertIn('Expecting to find entity in request body', - six.text_type(e)) +def expected_validation_failure(msg): + def wrapper(f): + def wrapped(self, *args, **kwargs): + args = (self,) + args + e = self.assertRaises(exception.ValidationError, f, + *args, **kwargs) + self.assertIn(msg, six.text_type(e)) + return wrapped return wrapper @@ -130,6 +132,10 @@ class ValidatedDecoratorTests(unit.BaseTestCase): 'name': uuid.uuid4().hex, } + invalid_entity = { + 'name': 1.0, # NOTE(dstanek): this is the incorrect type for name + } + @validation.validated(entity_create, 'entity') def create_entity(self, entity): """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.""" 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): self.create_entity(entity=None) @@ -160,11 +166,15 @@ class ValidatedDecoratorTests(unit.BaseTestCase): """Test the case when client passing in an empty entity reference.""" 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): 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): self.create_entity() @@ -182,6 +192,10 @@ class ValidatedDecoratorTests(unit.BaseTestCase): def test_calling_update_with_valid_entity_succeeds(self): 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): """Test the case when client passing in an empty entity reference.""" global entity_update