@@ -410,14 +410,14 @@ def validate_width(
410410
411411
412412@wrapt .decorator
413- def validate_name (
413+ def validate_name_type (
414414 wrapped : Callable [..., str ],
415415 instance : Any , # pylint: disable=unused-argument
416416 args : Tuple [_RequestObjectProxy , _Context ],
417417 kwargs : Dict ,
418418) -> str :
419419 """
420- Validate the name argument given to a VWS endpoint.
420+ Validate the type of the name argument given to a VWS endpoint.
421421
422422 Args:
423423 wrapped: An endpoint function for `requests_mock`.
@@ -427,7 +427,8 @@ def validate_name(
427427
428428 Returns:
429429 The result of calling the endpoint.
430- A `BAD_REQUEST` response if the name is given and is not between 1 and
430+ A `BAD_REQUEST` response if the name is given and not a string.
431+ is not between 1 and
431432 64 characters in length.
432433 """
433434 request , context = args
@@ -438,20 +439,59 @@ def validate_name(
438439 if 'name' not in request .json ():
439440 return wrapped (* args , ** kwargs )
440441
441- name = request .json (). get ( 'name' )
442+ name = request .json ()[ 'name' ]
442443
443- name_is_string = isinstance (name , str )
444- name_valid_length = name_is_string and 0 < len ( name ) < 65
444+ if isinstance (name , str ):
445+ return wrapped ( * args , ** kwargs )
445446
446- if not name_valid_length :
447- context .status_code = codes .BAD_REQUEST
448- body = {
449- 'transaction_id' : uuid .uuid4 ().hex ,
450- 'result_code' : ResultCodes .FAIL .value ,
451- }
452- return json_dump (body )
447+ context .status_code = codes .BAD_REQUEST
448+ body = {
449+ 'transaction_id' : uuid .uuid4 ().hex ,
450+ 'result_code' : ResultCodes .FAIL .value ,
451+ }
452+ return json_dump (body )
453453
454- return wrapped (* args , ** kwargs )
454+
455+ @wrapt .decorator
456+ def validate_name_length (
457+ wrapped : Callable [..., str ],
458+ instance : Any , # pylint: disable=unused-argument
459+ args : Tuple [_RequestObjectProxy , _Context ],
460+ kwargs : Dict ,
461+ ) -> str :
462+ """
463+ Validate the length of the name argument given to a VWS endpoint.
464+
465+ Args:
466+ wrapped: An endpoint function for `requests_mock`.
467+ instance: The class that the endpoint function is in.
468+ args: The arguments given to the endpoint function.
469+ kwargs: The keyword arguments given to the endpoint function.
470+
471+ Returns:
472+ The result of calling the endpoint.
473+ A `BAD_REQUEST` response if the name is given is not between 1 and 64
474+ characters in length.
475+ """
476+ request , context = args
477+
478+ if not request .text :
479+ return wrapped (* args , ** kwargs )
480+
481+ if 'name' not in request .json ():
482+ return wrapped (* args , ** kwargs )
483+
484+ name = request .json ()['name' ]
485+
486+ if name and len (name ) < 65 :
487+ return wrapped (* args , ** kwargs )
488+
489+ context .status_code = codes .BAD_REQUEST
490+ body = {
491+ 'transaction_id' : uuid .uuid4 ().hex ,
492+ 'result_code' : ResultCodes .FAIL .value ,
493+ }
494+ return json_dump (body )
455495
456496
457497@wrapt .decorator
0 commit comments