Add string id type validation

This will allow us more flexibility when validating a string
representing an ID. Previously we were just checking that the ID was in
fact a string. This change introduces a new validation type that fits a
general case for all Keystone resources.

Change-Id: I4878a1c718159304960fc7c40c1a80b8599c5657
bp: api-validation
This commit is contained in:
Lance Bragstad 2014-07-22 22:43:49 +00:00
parent 50fbd0b72a
commit daae3ad5bf
3 changed files with 49 additions and 8 deletions

View File

@ -16,9 +16,10 @@ from keystone.common.validation import parameter_types
_project_properties = {
'description': validation.nullable(parameter_types.description),
'domain_id': {
'type': 'string'
},
# NOTE(lbragstad): domain_id isn't nullable according to some backends.
# The identity-api should be updated to be consistent with the
# implementation.
'domain_id': parameter_types.id_string,
'enabled': parameter_types.boolean,
'name': {
'type': 'string',

View File

@ -28,11 +28,13 @@ name = {
'maxLength': 255
}
hex_uuid = {
id_string = {
'type': 'string',
'maxLength': 32,
'minLength': 32,
'pattern': '^[a-fA-F0-9]*$'
'minLength': 1,
'maxLength': 64,
# TODO(lbragstad): Find a way to make this configurable such that the end
# user chooses how much control they want over id_strings with a regex
'pattern': '^[a-zA-Z0-9-]+$'
}
description = {

View File

@ -46,7 +46,8 @@ _entity_properties = {
'description': validation.nullable(parameter_types.description),
'enabled': parameter_types.boolean,
'url': validation.nullable(parameter_types.url),
'email': validation.nullable(parameter_types.email)
'email': validation.nullable(parameter_types.email),
'id_string': validation.nullable(parameter_types.id_string)
}
entity_create = {
@ -195,6 +196,37 @@ class EntityValidationTestCase(testtools.TestCase):
self.create_schema_validator.validate,
request_to_validate)
def test_create_entity_with_valid_id_strings(self):
"""Validate acceptable id strings."""
valid_id_strings = [str(uuid.uuid4()), uuid.uuid4().hex, 'default']
for valid_id in valid_id_strings:
request_to_validate = {'name': self.resource_name,
'id_string': valid_id}
self.create_schema_validator.validate(request_to_validate)
def test_create_entity_with_invalid_id_strings(self):
"""Exception raised when using invalid id strings."""
long_string = 'A' * 65
invalid_id_strings = ['', long_string, 'this,should,fail']
for invalid_id in invalid_id_strings:
request_to_validate = {'name': self.resource_name,
'id_string': invalid_id}
self.assertRaises(exception.SchemaValidationError,
self.create_schema_validator.validate,
request_to_validate)
def test_create_entity_with_null_id_string(self):
"""Validate that None is an acceptable optional string type."""
request_to_validate = {'name': self.resource_name,
'id_string': None}
self.create_schema_validator.validate(request_to_validate)
def test_create_entity_with_null_string_succeeds(self):
"""Exception raised when passing None on required id strings."""
request_to_validate = {'name': self.resource_name,
'id_string': None}
self.create_schema_validator.validate(request_to_validate)
def test_update_entity_with_no_parameters_fails(self):
"""At least one parameter needs to be present for an update."""
request_to_validate = {}
@ -345,6 +377,12 @@ class ProjectValidationTestCase(testtools.TestCase):
self.update_project_validator.validate,
request_to_validate)
def test_validate_project_update_request_with_null_domain_id_fails(self):
request_to_validate = {'domain_id': None}
self.assertRaises(exception.SchemaValidationError,
self.update_project_validator.validate,
request_to_validate)
class DomainValidationTestCase(testtools.TestCase):
"""Test for V3 Domain API validation."""