Skip to content

Commit 1fd08f4

Browse files
Merge pull request #642 from adamtheturtle/metadata-too-large
Tests for "MetadataTooLarge"
2 parents a060afd + e8daf96 commit 1fd08f4

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

src/mock_vws/_mock_web_services_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
validate_image_size,
4040
validate_keys,
4141
validate_metadata_encoding,
42+
validate_metadata_size,
4243
validate_metadata_type,
4344
validate_name,
4445
validate_not_invalid_json,
@@ -155,6 +156,7 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]:
155156
decorators = [
156157
parse_target_id,
157158
validate_authorization,
159+
validate_metadata_size,
158160
validate_metadata_encoding,
159161
validate_metadata_type,
160162
validate_active_flag,

src/mock_vws/_validators.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,46 @@ def validate_metadata_type(
872872
'result_code': ResultCodes.FAIL.value,
873873
}
874874
return json_dump(body)
875+
876+
877+
@wrapt.decorator
878+
def validate_metadata_size(
879+
wrapped: Callable[..., str],
880+
instance: Any, # pylint: disable=unused-argument
881+
args: Tuple[_RequestObjectProxy, _Context],
882+
kwargs: Dict,
883+
) -> str:
884+
"""
885+
Validate that the given application metadata is a string or 1024 * 1024
886+
bytes or fewer.
887+
888+
Args:
889+
wrapped: An endpoint function for `requests_mock`.
890+
instance: The class that the endpoint function is in.
891+
args: The arguments given to the endpoint function.
892+
kwargs: The keyword arguments given to the endpoint function.
893+
894+
Returns:
895+
The result of calling the endpoint.
896+
An `UNPROCESSABLE_ENTITY` response if application metadata is given and
897+
it is too large.
898+
"""
899+
request, context = args
900+
901+
if not request.text:
902+
return wrapped(*args, **kwargs)
903+
904+
application_metadata = request.json().get('application_metadata')
905+
if application_metadata is None:
906+
return wrapped(*args, **kwargs)
907+
decoded = base64.b64decode(application_metadata)
908+
909+
if len(decoded) <= 1024 * 1024:
910+
return wrapped(*args, **kwargs)
911+
912+
context.status_code = codes.UNPROCESSABLE_ENTITY
913+
body = {
914+
'transaction_id': uuid.uuid4().hex,
915+
'result_code': ResultCodes.METADATA_TOO_LARGE.value,
916+
}
917+
return json_dump(body)

tests/mock_vws/test_add_target.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,18 +645,25 @@ class TestApplicationMetadata:
645645
Tests for the application metadata parameter.
646646
"""
647647

648+
@pytest.mark.parametrize(
649+
'metadata',
650+
[
651+
b'a',
652+
b'a' * (1024 * 1024),
653+
],
654+
ids=['Short', 'Max length'],
655+
)
648656
def test_base64_encoded(
649657
self,
650658
vuforia_database_keys: VuforiaDatabaseKeys,
651659
png_rgb: io.BytesIO,
660+
metadata: bytes,
652661
) -> None:
653662
"""
654663
A base64 encoded string is valid application metadata.
655664
"""
656665
image_data = png_rgb.read()
657666
image_data_encoded = base64.b64encode(image_data).decode('ascii')
658-
659-
metadata = b'Some data'
660667
metadata_encoded = base64.b64encode(metadata).decode('ascii')
661668

662669
data = {
@@ -767,3 +774,35 @@ def test_not_base64_encoded(
767774
status_code=codes.UNPROCESSABLE_ENTITY,
768775
result_code=ResultCodes.FAIL,
769776
)
777+
778+
def test_metadata_too_large(
779+
self,
780+
vuforia_database_keys: VuforiaDatabaseKeys,
781+
png_rgb: io.BytesIO,
782+
) -> None:
783+
"""
784+
A base64 encoded string of up to 1024 * 1024 bytes is valid application
785+
metadata.
786+
"""
787+
image_data = png_rgb.read()
788+
image_data_encoded = base64.b64encode(image_data).decode('ascii')
789+
metadata = b'a' * (1024 * 1024 + 1)
790+
metadata_encoded = base64.b64encode(metadata).decode('ascii')
791+
792+
data = {
793+
'name': 'example_name',
794+
'width': 1,
795+
'image': image_data_encoded,
796+
'application_metadata': metadata_encoded,
797+
}
798+
799+
response = add_target_to_vws(
800+
vuforia_database_keys=vuforia_database_keys,
801+
data=data,
802+
)
803+
804+
assert_vws_failure(
805+
response=response,
806+
status_code=codes.UNPROCESSABLE_ENTITY,
807+
result_code=ResultCodes.METADATA_TOO_LARGE,
808+
)

0 commit comments

Comments
 (0)