Skip to content

Commit 57a9e7f

Browse files
committed
Use ids for metadata length test
1 parent 3b189e2 commit 57a9e7f

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,12 +646,11 @@ class TestApplicationMetadata:
646646
"""
647647

648648
@pytest.mark.parametrize(
649-
'metadata',
650-
[
649+
'metadata', [
651650
b'a',
652-
# This is the maximum metadata length.
653651
b'a' * (1024 * 1024),
654652
],
653+
ids=['Short', 'Max length']
655654
)
656655
def test_base64_encoded(
657656
self,
@@ -775,7 +774,6 @@ def test_not_base64_encoded(
775774
result_code=ResultCodes.FAIL,
776775
)
777776

778-
779777
def test_metadata_too_large(
780778
self,
781779
vuforia_database_keys: VuforiaDatabaseKeys,

0 commit comments

Comments
 (0)