Skip to content

Commit 4a42fbc

Browse files
Merge pull request #644 from adamtheturtle/separate-target-name-validators
Separate name validators
2 parents 34ac43c + 7cda203 commit 4a42fbc

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

src/mock_vws/_mock_web_services_api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
validate_metadata_encoding,
4242
validate_metadata_size,
4343
validate_metadata_type,
44-
validate_name,
44+
validate_name_length,
45+
validate_name_type,
4546
validate_not_invalid_json,
4647
validate_width,
4748
)
@@ -166,7 +167,8 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]:
166167
validate_image_is_image,
167168
validate_image_encoding,
168169
validate_image_data_type,
169-
validate_name,
170+
validate_name_length,
171+
validate_name_type,
170172
validate_width,
171173
key_validator,
172174
validate_date,

src/mock_vws/_validators.py

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)