Skip to content

Commit 0358d86

Browse files
committed
Passing tests on mock
1 parent 632556c commit 0358d86

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/mock_vws/_mock_web_services_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
validate_metadata_encoding,
4242
validate_metadata_size,
4343
validate_metadata_type,
44+
validate_name_characters_in_range,
4445
validate_name_length,
4546
validate_name_type,
4647
validate_not_invalid_json,
@@ -167,6 +168,7 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]:
167168
validate_image_is_image,
168169
validate_image_encoding,
169170
validate_image_data_type,
171+
validate_name_characters_in_range,
170172
validate_name_length,
171173
validate_name_type,
172174
validate_width,

src/mock_vws/_validators.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,48 @@ def validate_name_length(
494494
return json_dump(body)
495495

496496

497+
@wrapt.decorator
498+
def validate_name_characters_in_range(
499+
wrapped: Callable[..., str],
500+
instance: Any, # pylint: disable=unused-argument
501+
args: Tuple[_RequestObjectProxy, _Context],
502+
kwargs: Dict,
503+
) -> str:
504+
"""
505+
Validate the characters in the name argument given to a VWS endpoint.
506+
507+
Args:
508+
wrapped: An endpoint function for `requests_mock`.
509+
instance: The class that the endpoint function is in.
510+
args: The arguments given to the endpoint function.
511+
kwargs: The keyword arguments given to the endpoint function.
512+
513+
Returns:
514+
The result of calling the endpoint.
515+
An ``INTERNAL_SERVER_ERROR`` response if the name is given includes
516+
characters outside of the accepted range.
517+
"""
518+
request, context = args
519+
520+
if not request.text:
521+
return wrapped(*args, **kwargs)
522+
523+
if 'name' not in request.json():
524+
return wrapped(*args, **kwargs)
525+
526+
name = request.json()['name']
527+
528+
if all(ord(character) <= 65535 for character in name):
529+
return wrapped(*args, **kwargs)
530+
531+
context.status_code = codes.INTERNAL_SERVER_ERROR
532+
body = {
533+
'transaction_id': uuid.uuid4().hex,
534+
'result_code': ResultCodes.FAIL.value,
535+
}
536+
return json_dump(body)
537+
538+
497539
@wrapt.decorator
498540
def validate_image_format(
499541
wrapped: Callable[..., str],

0 commit comments

Comments
 (0)