2015-11-24 12:34:53 +01:00
|
|
|
import decimal
|
2022-12-12 17:14:19 +01:00
|
|
|
import io
|
2022-05-26 00:10:07 +02:00
|
|
|
from datetime import datetime
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
from botocore.model import ShapeResolver
|
|
|
|
from botocore.validate import ParamValidator
|
2022-05-26 00:10:07 +02:00
|
|
|
from tests import unittest
|
2015-11-24 12:34:53 +01:00
|
|
|
|
2022-05-26 00:10:07 +02:00
|
|
|
BOILER_PLATE_SHAPES = {'StringType': {'type': 'string'}}
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BaseTestValidate(unittest.TestCase):
|
|
|
|
def assert_has_validation_errors(self, given_shapes, input_params, errors):
|
|
|
|
# Given the shape definitions ``given_shape`` and the user input
|
|
|
|
# parameters ``input_params``, verify that the validation has
|
|
|
|
# validation errors containing the list of ``errors``.
|
|
|
|
# Also, this assumes the input shape name is "Input".
|
|
|
|
errors_found = self.get_validation_error_message(
|
2022-05-26 00:10:07 +02:00
|
|
|
given_shapes, input_params
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
self.assertTrue(errors_found.has_errors())
|
|
|
|
error_message = errors_found.generate_report()
|
|
|
|
for error in errors:
|
|
|
|
self.assertIn(error, error_message)
|
|
|
|
|
|
|
|
def get_validation_error_message(self, given_shapes, input_params):
|
|
|
|
s = ShapeResolver(given_shapes)
|
|
|
|
input_shape = s.get_shape_by_name('Input')
|
|
|
|
validator = ParamValidator()
|
|
|
|
errors_found = validator.validate(input_params, input_shape)
|
|
|
|
return errors_found
|
|
|
|
|
|
|
|
|
|
|
|
class TestValidateRequiredParams(BaseTestValidate):
|
|
|
|
def test_validate_required_params(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes={
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'required': ['A', 'B'],
|
|
|
|
'members': {
|
|
|
|
'A': {'shape': 'StringType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
'B': {'shape': 'StringType'},
|
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'StringType': {'type': 'string'},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
input_params={'A': 'foo'},
|
2022-05-26 00:10:07 +02:00
|
|
|
errors=['Missing required parameter'],
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
def test_validate_nested_required_param(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes={
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
2022-05-26 00:10:07 +02:00
|
|
|
'members': {'A': {'shape': 'SubStruct'}},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
'SubStruct': {
|
|
|
|
'type': 'structure',
|
|
|
|
'required': ['B', 'C'],
|
|
|
|
'members': {
|
|
|
|
'B': {'shape': 'StringType'},
|
|
|
|
'C': {'shape': 'StringType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
'StringType': {
|
|
|
|
'type': 'string',
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
input_params={'A': {'B': 'foo'}},
|
2022-05-26 00:10:07 +02:00
|
|
|
errors=['Missing required parameter'],
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
def test_validate_unknown_param(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes={
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'required': ['A'],
|
|
|
|
'members': {
|
|
|
|
'A': {'shape': 'StringType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'StringType': {'type': 'string'},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
input_params={'A': 'foo', 'B': 'bar'},
|
2022-05-26 00:10:07 +02:00
|
|
|
errors=['Unknown parameter'],
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
|
2017-06-27 11:52:19 +02:00
|
|
|
class TestValidateJSONValueTrait(BaseTestValidate):
|
|
|
|
def test_accepts_jsonvalue_string(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'json': {
|
|
|
|
'shape': 'StrType',
|
|
|
|
'jsonvalue': True,
|
|
|
|
'location': 'header',
|
2022-05-26 00:10:07 +02:00
|
|
|
'locationName': 'header-name',
|
2017-06-27 11:52:19 +02:00
|
|
|
}
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2017-06-27 11:52:19 +02:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'StrType': {'type': 'string'},
|
2017-06-27 11:52:19 +02:00
|
|
|
}
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
2022-05-26 00:10:07 +02:00
|
|
|
'json': {'data': [1, 2.3, '3'], 'unicode': '\u2713'}
|
|
|
|
},
|
|
|
|
)
|
2017-06-27 11:52:19 +02:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_validate_jsonvalue_string(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'json': {
|
|
|
|
'shape': 'StrType',
|
|
|
|
'jsonvalue': True,
|
|
|
|
'location': 'header',
|
2022-05-26 00:10:07 +02:00
|
|
|
'locationName': 'header-name',
|
2017-06-27 11:52:19 +02:00
|
|
|
}
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2017-06-27 11:52:19 +02:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'StrType': {'type': 'string'},
|
2017-06-27 11:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
2022-05-26 00:10:07 +02:00
|
|
|
input_params={'json': {'date': datetime(2017, 4, 27, 0, 0)}},
|
|
|
|
errors=[('Invalid parameter json must be json serializable: ')],
|
|
|
|
)
|
2017-06-27 11:52:19 +02:00
|
|
|
|
|
|
|
|
2021-08-18 17:45:16 +02:00
|
|
|
class TestValidateDocumentType(BaseTestValidate):
|
|
|
|
def test_accepts_document_type_string(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'inlineDocument': {
|
|
|
|
'shape': 'DocumentType',
|
|
|
|
}
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-08-18 17:45:16 +02:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'DocumentType': {'type': 'structure', 'document': True},
|
2021-08-18 17:45:16 +02:00
|
|
|
}
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
2022-05-26 00:10:07 +02:00
|
|
|
'inlineDocument': {
|
|
|
|
'data': [1, 2.3, '3', {'foo': None}],
|
|
|
|
'unicode': '\u2713',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2021-08-18 17:45:16 +02:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_validate_document_type_string(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'inlineDocument': {
|
|
|
|
'shape': 'DocumentType',
|
|
|
|
}
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-08-18 17:45:16 +02:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'DocumentType': {'type': 'structure', 'document': True},
|
2021-08-18 17:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
invalid_document = object()
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'inlineDocument': {
|
|
|
|
'number': complex(1j),
|
|
|
|
'date': datetime(2017, 4, 27, 0, 0),
|
|
|
|
'list': [invalid_document],
|
2022-05-26 00:10:07 +02:00
|
|
|
'dict': {'foo': (1, 2, 3)},
|
2021-08-18 17:45:16 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
errors=[
|
|
|
|
('Invalid type for document parameter number'),
|
|
|
|
('Invalid type for document parameter date'),
|
|
|
|
('Invalid type for document parameter list[0]'),
|
|
|
|
('Invalid type for document parameter foo'),
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
|
|
|
)
|
2021-08-18 17:45:16 +02:00
|
|
|
|
|
|
|
|
2021-09-22 22:53:42 +02:00
|
|
|
class TestValidateTaggedUnion(BaseTestValidate):
|
|
|
|
def test_accepts_one_member(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'taggedUnion': {
|
|
|
|
'shape': 'TaggedUnionType',
|
|
|
|
}
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
},
|
|
|
|
'TaggedUnionType': {
|
|
|
|
'type': 'structure',
|
|
|
|
'union': True,
|
|
|
|
'members': {
|
|
|
|
'Foo': {'shape': 'StringType'},
|
|
|
|
'Bar': {'shape': 'StringType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'StringType': {'type': 'string'},
|
2021-09-22 22:53:42 +02:00
|
|
|
}
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
2022-05-26 00:10:07 +02:00
|
|
|
input_params={'taggedUnion': {'Foo': "mystring"}},
|
2021-09-22 22:53:42 +02:00
|
|
|
)
|
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_validate_one_member_is_set(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'taggedUnion': {
|
|
|
|
'shape': 'TaggedUnionType',
|
|
|
|
}
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
},
|
|
|
|
'TaggedUnionType': {
|
|
|
|
'type': 'structure',
|
|
|
|
'union': True,
|
|
|
|
'members': {
|
|
|
|
'Foo': {'shape': 'StringType'},
|
|
|
|
'Bar': {'shape': 'StringType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'StringType': {'type': 'string'},
|
2021-09-22 22:53:42 +02:00
|
|
|
}
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
2022-05-26 00:10:07 +02:00
|
|
|
'taggedUnion': {'Foo': "mystring", 'Bar': "mystring2"}
|
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
)
|
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertIn(
|
|
|
|
'Invalid number of parameters set for tagged union structure',
|
2022-05-26 00:10:07 +02:00
|
|
|
error_msg,
|
2021-09-22 22:53:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_validate_known_member_is_set(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'taggedUnion': {
|
|
|
|
'shape': 'TaggedUnionType',
|
|
|
|
}
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
},
|
|
|
|
'TaggedUnionType': {
|
|
|
|
'type': 'structure',
|
|
|
|
'union': True,
|
|
|
|
'members': {
|
|
|
|
'Foo': {'shape': 'StringType'},
|
|
|
|
'Bar': {'shape': 'StringType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'StringType': {'type': 'string'},
|
2021-09-22 22:53:42 +02:00
|
|
|
}
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
2022-05-26 00:10:07 +02:00
|
|
|
input_params={'taggedUnion': {'unknown': "mystring"}},
|
2021-09-22 22:53:42 +02:00
|
|
|
)
|
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertIn('Unknown parameter in taggedUnion', error_msg)
|
|
|
|
|
|
|
|
def test_validate_structure_is_not_empty(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'taggedUnion': {
|
|
|
|
'shape': 'TaggedUnionType',
|
|
|
|
}
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
},
|
|
|
|
'TaggedUnionType': {
|
|
|
|
'type': 'structure',
|
|
|
|
'union': True,
|
|
|
|
'members': {
|
|
|
|
'Foo': {'shape': 'StringType'},
|
|
|
|
'Bar': {'shape': 'StringType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2021-09-22 22:53:42 +02:00
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'StringType': {'type': 'string'},
|
2021-09-22 22:53:42 +02:00
|
|
|
}
|
|
|
|
errors = self.get_validation_error_message(
|
2022-05-26 00:10:07 +02:00
|
|
|
given_shapes=self.shapes, input_params={'taggedUnion': {}}
|
2021-09-22 22:53:42 +02:00
|
|
|
)
|
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertIn('Must set one of the following keys', error_msg)
|
|
|
|
|
2021-08-18 17:45:16 +02:00
|
|
|
|
2015-11-24 12:34:53 +01:00
|
|
|
class TestValidateTypes(BaseTestValidate):
|
|
|
|
def setUp(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'Str': {'shape': 'StrType'},
|
|
|
|
'Int': {'shape': 'IntType'},
|
|
|
|
'Bool': {'shape': 'BoolType'},
|
|
|
|
'List': {'shape': 'ListType'},
|
|
|
|
'Struct': {'shape': 'StructType'},
|
|
|
|
'Double': {'shape': 'DoubleType'},
|
|
|
|
'Long': {'shape': 'LongType'},
|
|
|
|
'Map': {'shape': 'MapType'},
|
|
|
|
'Timestamp': {'shape': 'TimeType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
'StrType': {'type': 'string'},
|
|
|
|
'IntType': {'type': 'integer'},
|
|
|
|
'BoolType': {'type': 'boolean'},
|
|
|
|
'ListType': {'type': 'list'},
|
|
|
|
'StructType': {'type': 'structure'},
|
|
|
|
'DoubleType': {'type': 'double'},
|
|
|
|
'LongType': {'type': 'long'},
|
|
|
|
'MapType': {'type': 'map'},
|
|
|
|
'TimeType': {'type': 'timestamp'},
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_validate_string(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'Str': 24,
|
|
|
|
'Int': 'notInt',
|
|
|
|
'Bool': 'notBool',
|
|
|
|
'List': 'notList',
|
|
|
|
'Struct': 'notDict',
|
|
|
|
'Double': 'notDouble',
|
|
|
|
'Long': 'notLong',
|
|
|
|
'Map': 'notDict',
|
|
|
|
'Timestamp': 'notTimestamp',
|
|
|
|
},
|
|
|
|
errors=[
|
|
|
|
'Invalid type for parameter Str',
|
|
|
|
'Invalid type for parameter Int',
|
|
|
|
'Invalid type for parameter Bool',
|
|
|
|
'Invalid type for parameter List',
|
|
|
|
'Invalid type for parameter Struct',
|
|
|
|
'Invalid type for parameter Double',
|
|
|
|
'Invalid type for parameter Long',
|
|
|
|
'Invalid type for parameter Map',
|
|
|
|
'Invalid type for parameter Timestamp',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_datetime_type_accepts_datetime_obj(self):
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
2022-05-26 00:10:07 +02:00
|
|
|
input_params={'Timestamp': datetime.now()},
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_datetime_accepts_string_timestamp(self):
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
2022-05-26 00:10:07 +02:00
|
|
|
input_params={'Timestamp': '2014-01-01 12:00:00'},
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_can_handle_none_datetimes(self):
|
|
|
|
# This is specifically to test a workaround a bug in dateutil
|
|
|
|
# where low level exceptions can propogate back up to
|
|
|
|
# us.
|
|
|
|
errors = self.get_validation_error_message(
|
2022-05-26 00:10:07 +02:00
|
|
|
given_shapes=self.shapes, input_params={'Timestamp': None}
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertIn('Invalid type for parameter Timestamp', error_msg)
|
|
|
|
|
|
|
|
|
|
|
|
class TestValidateRanges(BaseTestValidate):
|
|
|
|
def setUp(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'Int': {'shape': 'IntType'},
|
|
|
|
'Long': {'shape': 'IntType'},
|
|
|
|
'String': {'shape': 'StringType'},
|
|
|
|
'List': {'shape': 'ListType'},
|
|
|
|
'OnlyMin': {'shape': 'MinStrOnly'},
|
|
|
|
'OnlyMax': {'shape': 'MaxStrOnly'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
'IntType': {
|
|
|
|
'type': 'integer',
|
|
|
|
'min': 0,
|
|
|
|
'max': 1000,
|
|
|
|
},
|
|
|
|
'LongType': {
|
|
|
|
'type': 'long',
|
|
|
|
'min': 0,
|
|
|
|
'max': 1000,
|
|
|
|
},
|
|
|
|
'StringType': {
|
|
|
|
'type': 'string',
|
|
|
|
'min': 1,
|
|
|
|
'max': 10,
|
|
|
|
},
|
2022-05-26 00:10:07 +02:00
|
|
|
'MinStrOnly': {'type': 'string', 'min': 1},
|
|
|
|
'MaxStrOnly': {'type': 'string', 'max': 10},
|
2015-11-24 12:34:53 +01:00
|
|
|
'ListType': {
|
|
|
|
'type': 'list',
|
|
|
|
'min': 1,
|
|
|
|
'max': 5,
|
2022-05-26 00:10:07 +02:00
|
|
|
'member': {'shape': 'StringType'},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_less_than_range(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'Int': -10,
|
|
|
|
'Long': -10,
|
|
|
|
},
|
|
|
|
errors=[
|
2021-01-30 19:16:40 +01:00
|
|
|
'Invalid value for parameter Int',
|
|
|
|
'Invalid value for parameter Long',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
|
2016-10-11 02:22:54 +02:00
|
|
|
def test_does_not_validate_greater_than_range(self):
|
|
|
|
errors = self.get_validation_error_message(
|
2015-11-24 12:34:53 +01:00
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'Int': 100000000,
|
|
|
|
'Long': 100000000,
|
|
|
|
},
|
|
|
|
)
|
2016-10-11 02:22:54 +02:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
def test_within_range(self):
|
|
|
|
errors = self.get_validation_error_message(
|
2022-05-26 00:10:07 +02:00
|
|
|
given_shapes=self.shapes, input_params={'Int': 10}
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_string_min_length_contraint(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'String': '',
|
|
|
|
},
|
|
|
|
errors=[
|
|
|
|
'Invalid length for parameter String',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
|
2016-10-11 02:22:54 +02:00
|
|
|
def test_does_not_validate_string_max_length_contraint(self):
|
|
|
|
errors = self.get_validation_error_message(
|
2015-11-24 12:34:53 +01:00
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'String': 'more than ten characters',
|
|
|
|
},
|
|
|
|
)
|
2016-10-11 02:22:54 +02:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
def test_list_min_length_constraint(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'List': [],
|
|
|
|
},
|
|
|
|
errors=[
|
|
|
|
'Invalid length for parameter List',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
|
2016-10-11 02:22:54 +02:00
|
|
|
def test_does_not_validate_list_max_length_constraint(self):
|
|
|
|
errors = self.get_validation_error_message(
|
2015-11-24 12:34:53 +01:00
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'List': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
|
|
|
|
},
|
|
|
|
)
|
2016-10-11 02:22:54 +02:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
def test_only_min_value_specified(self):
|
|
|
|
# min anx max don't have to both be provided.
|
|
|
|
# It's valid to just have min with no max, and
|
|
|
|
# vice versa.
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'OnlyMin': '',
|
|
|
|
},
|
|
|
|
errors=[
|
|
|
|
'Invalid length for parameter OnlyMin',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
|
2016-10-11 02:22:54 +02:00
|
|
|
def test_does_not_validate_max_when_only_max_value_specified(self):
|
|
|
|
errors = self.get_validation_error_message(
|
2015-11-24 12:34:53 +01:00
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'OnlyMax': 'more than ten characters',
|
|
|
|
},
|
|
|
|
)
|
2016-10-11 02:22:54 +02:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
class TestValidateMapType(BaseTestValidate):
|
|
|
|
def setUp(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'Map': {'shape': 'MapType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
'MapType': {
|
|
|
|
'type': 'map',
|
|
|
|
'key': {'shape': 'StringType'},
|
|
|
|
'value': {'shape': 'StringType'},
|
|
|
|
},
|
|
|
|
'StringType': {
|
|
|
|
'type': 'string',
|
|
|
|
'min': 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_validate_keys_and_values(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
2022-05-26 00:10:07 +02:00
|
|
|
input_params={'Map': {'foo': '', 'a': 'foobar'}},
|
2015-11-24 12:34:53 +01:00
|
|
|
errors=[
|
|
|
|
'Invalid length for parameter Map',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestValidationFloatType(BaseTestValidate):
|
|
|
|
def setUp(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'Float': {'shape': 'FloatType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
'FloatType': {
|
|
|
|
'type': 'float',
|
|
|
|
'min': 2,
|
|
|
|
'max': 5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_range_float(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'Float': 1,
|
|
|
|
},
|
|
|
|
errors=[
|
2021-01-30 19:16:40 +01:00
|
|
|
'Invalid value for parameter Float',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_decimal_allowed(self):
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
2022-05-26 00:10:07 +02:00
|
|
|
input_params={'Float': decimal.Decimal('2.12345')},
|
|
|
|
)
|
2015-11-24 12:34:53 +01:00
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_decimal_still_validates_range(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'Float': decimal.Decimal('1'),
|
|
|
|
},
|
|
|
|
errors=[
|
2021-01-30 19:16:40 +01:00
|
|
|
'Invalid value for parameter Float',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestValidateTypeBlob(BaseTestValidate):
|
|
|
|
def setUp(self):
|
|
|
|
self.shapes = {
|
|
|
|
'Input': {
|
|
|
|
'type': 'structure',
|
|
|
|
'members': {
|
|
|
|
'Blob': {'shape': 'BlobType'},
|
2022-05-26 00:10:07 +02:00
|
|
|
},
|
2015-11-24 12:34:53 +01:00
|
|
|
},
|
|
|
|
'BlobType': {
|
|
|
|
'type': 'blob',
|
|
|
|
'min': 2,
|
|
|
|
'max': 5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_validates_bytes(self):
|
|
|
|
errors = self.get_validation_error_message(
|
2022-05-26 00:10:07 +02:00
|
|
|
given_shapes=self.shapes, input_params={'Blob': b'12345'}
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_validates_bytearray(self):
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={'Blob': bytearray(b'12345')},
|
|
|
|
)
|
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_validates_file_like_object(self):
|
2022-12-12 17:14:19 +01:00
|
|
|
value = io.BytesIO(b'foo')
|
2015-11-24 12:34:53 +01:00
|
|
|
|
|
|
|
errors = self.get_validation_error_message(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={'Blob': value},
|
|
|
|
)
|
|
|
|
error_msg = errors.generate_report()
|
|
|
|
self.assertEqual(error_msg, '')
|
|
|
|
|
|
|
|
def test_validate_type(self):
|
|
|
|
self.assert_has_validation_errors(
|
|
|
|
given_shapes=self.shapes,
|
|
|
|
input_params={
|
|
|
|
'Blob': 24,
|
|
|
|
},
|
|
|
|
errors=[
|
|
|
|
'Invalid type for parameter Blob',
|
2022-05-26 00:10:07 +02:00
|
|
|
],
|
2015-11-24 12:34:53 +01:00
|
|
|
)
|