File: test_validate.py

package info (click to toggle)
python-botocore 1.12.103%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 41,552 kB
  • sloc: python: 43,119; xml: 15,052; makefile: 131
file content (500 lines) | stat: -rw-r--r-- 16,031 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
from tests import unittest
from datetime import datetime
import decimal

from botocore.compat import six
from botocore.model import ShapeResolver
from botocore.validate import ParamValidator

BOILER_PLATE_SHAPES = {
    'StringType': {
        'type': 'string'
    }
}


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(
            given_shapes, input_params)
        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)
        error_message = errors_found.generate_report()
        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'},
                        'B': {'shape': 'StringType'}
                    }
                },
                'StringType': {'type': 'string'}
            },
            input_params={'A': 'foo'},
            errors=['Missing required parameter'])

    def test_validate_nested_required_param(self):
        self.assert_has_validation_errors(
            given_shapes={
                'Input': {
                    'type': 'structure',
                    'members': {
                        'A': {'shape': 'SubStruct'}
                    }
                },
                'SubStruct': {
                    'type': 'structure',
                    'required': ['B', 'C'],
                    'members': {
                        'B': {'shape': 'StringType'},
                        'C': {'shape': 'StringType'},
                    }
                },
                'StringType': {
                    'type': 'string',
                }
            },
            input_params={'A': {'B': 'foo'}},
            errors=['Missing required parameter'])

    def test_validate_unknown_param(self):
        self.assert_has_validation_errors(
            given_shapes={
                'Input': {
                    'type': 'structure',
                    'required': ['A'],
                    'members': {
                        'A': {'shape': 'StringType'},
                    }
                },
                'StringType': {'type': 'string'}
            },
            input_params={'A': 'foo', 'B': 'bar'},
            errors=['Unknown parameter'])


class TestValidateJSONValueTrait(BaseTestValidate):
    def test_accepts_jsonvalue_string(self):
        self.shapes = {
            'Input': {
                'type': 'structure',
                'members': {
                    'json': {
                        'shape': 'StrType',
                        'jsonvalue': True,
                        'location': 'header',
                        'locationName': 'header-name'
                    }
                }
            },
            'StrType': {'type': 'string'}
        }
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={
                'json': {'data': [1, 2.3, '3'], 'unicode': u'\u2713'}
            })
        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',
                        'locationName': 'header-name'
                    }
                }
            },
            'StrType': {'type': 'string'}
        }

        self.assert_has_validation_errors(
            given_shapes=self.shapes,
            input_params={
                'json': {'date': datetime(2017, 4, 27, 0, 0)}
            },
            errors=[
                ('Invalid parameter json must be json serializable: ')
            ])


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'},
                }
            },
            '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',
            ]
        )

    def test_datetime_type_accepts_datetime_obj(self):
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={'Timestamp': datetime.now(),})
        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,
            input_params={'Timestamp': '2014-01-01 12:00: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(
            given_shapes=self.shapes,
            input_params={'Timestamp': None})
        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'},
                }
            },
            'IntType': {
                'type': 'integer',
                'min': 0,
                'max': 1000,
            },
            'LongType': {
                'type': 'long',
                'min': 0,
                'max': 1000,
            },
            'StringType': {
                'type': 'string',
                'min': 1,
                'max': 10,
            },
            'MinStrOnly': {
                'type': 'string',
                'min': 1
            },
            'MaxStrOnly': {
                'type': 'string',
                'max': 10
            },
            'ListType': {
                'type': 'list',
                'min': 1,
                'max': 5,
                'member': {
                    'shape': 'StringType'
                }
            },
        }

    def test_less_than_range(self):
        self.assert_has_validation_errors(
            given_shapes=self.shapes,
            input_params={
                'Int': -10,
                'Long': -10,
            },
            errors=[
                'Invalid range for parameter Int',
                'Invalid range for parameter Long',
            ]
        )

    def test_does_not_validate_greater_than_range(self):
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={
                'Int': 100000000,
                'Long': 100000000,
            },
        )
        error_msg = errors.generate_report()
        self.assertEqual(error_msg, '')

    def test_within_range(self):
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={'Int': 10})
        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',
            ]
        )

    def test_does_not_validate_string_max_length_contraint(self):
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={
                'String': 'more than ten characters',
            },
        )
        error_msg = errors.generate_report()
        self.assertEqual(error_msg, '')

    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',
            ]
        )

    def test_does_not_validate_list_max_length_constraint(self):
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={
                'List': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
            },
        )
        error_msg = errors.generate_report()
        self.assertEqual(error_msg, '')

    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',
            ]
        )

    def test_does_not_validate_max_when_only_max_value_specified(self):
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={
                'OnlyMax': 'more than ten characters',
            },
        )
        error_msg = errors.generate_report()
        self.assertEqual(error_msg, '')


class TestValidateMapType(BaseTestValidate):
    def setUp(self):
        self.shapes = {
            'Input': {
                'type': 'structure',
                'members': {
                    'Map': {'shape': 'MapType'},
                }
            },
            '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,
            input_params={
                'Map': {'foo': '', 'a': 'foobar'}
            },
            errors=[
                'Invalid length for parameter Map',
            ]
        )


class TestValidationFloatType(BaseTestValidate):
    def setUp(self):
        self.shapes = {
            'Input': {
                'type': 'structure',
                'members': {
                    'Float': {'shape': 'FloatType'},
                }
            },
            '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=[
                'Invalid range for parameter Float',
            ]
        )

    def test_decimal_allowed(self):
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={'Float': decimal.Decimal('2.12345')})
        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=[
                'Invalid range for parameter Float',
            ]
        )


class TestValidateTypeBlob(BaseTestValidate):
    def setUp(self):
        self.shapes = {
            'Input': {
                'type': 'structure',
                'members': {
                    'Blob': {'shape': 'BlobType'},
                }
            },
            'BlobType': {
                'type': 'blob',
                'min': 2,
                'max': 5,
            },
        }

    def test_validates_bytes(self):
        errors = self.get_validation_error_message(
            given_shapes=self.shapes,
            input_params={'Blob': b'12345'}
        )
        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):
        value = six.BytesIO(b'foo')

        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',
            ]
        )