Skip to content

Commit 34ac43c

Browse files
Merge pull request #643 from adamtheturtle/fix-metadata-add-tests
Fix off by one error in metadata tests
2 parents 98f14bc + 88c1838 commit 34ac43c

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/mock_vws/_validators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,8 @@ def validate_metadata_size(
906906
return wrapped(*args, **kwargs)
907907
decoded = base64.b64decode(application_metadata)
908908

909-
if len(decoded) <= 1024 * 1024:
909+
max_metadata_bytes = 1024 * 1024 - 1
910+
if len(decoded) <= max_metadata_bytes:
910911
return wrapped(*args, **kwargs)
911912

912913
context.status_code = codes.UNPROCESSABLE_ENTITY

tests/mock_vws/test_add_target.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,13 @@ class TestApplicationMetadata:
645645
Tests for the application metadata parameter.
646646
"""
647647

648+
_MAX_METADATA_BYTES = 1024 * 1024 - 1
649+
648650
@pytest.mark.parametrize(
649651
'metadata',
650652
[
651653
b'a',
652-
b'a' * (1024 * 1024),
654+
b'a' * _MAX_METADATA_BYTES,
653655
],
654656
ids=['Short', 'Max length'],
655657
)
@@ -781,12 +783,12 @@ def test_metadata_too_large(
781783
png_rgb: io.BytesIO,
782784
) -> None:
783785
"""
784-
A base64 encoded string of up to 1024 * 1024 bytes is valid application
785-
metadata.
786+
A base64 encoded string of greater than 1024 * 1024 bytes is too large
787+
for application metadata.
786788
"""
787789
image_data = png_rgb.read()
788790
image_data_encoded = base64.b64encode(image_data).decode('ascii')
789-
metadata = b'a' * (1024 * 1024 + 1)
791+
metadata = b'a' * (self._MAX_METADATA_BYTES + 1)
790792
metadata_encoded = base64.b64encode(metadata).decode('ascii')
791793

792794
data = {

tests/mock_vws/test_update_target.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,25 @@ class TestApplicationMetadata:
347347
Tests for the application metadata parameter.
348348
"""
349349

350+
_MAX_METADATA_BYTES = 1024 * 1024 - 1
351+
352+
@pytest.mark.parametrize(
353+
'metadata',
354+
[
355+
b'a',
356+
b'a' * _MAX_METADATA_BYTES,
357+
],
358+
ids=['Short', 'Max length'],
359+
)
350360
def test_base64_encoded(
351361
self,
352362
vuforia_database_keys: VuforiaDatabaseKeys,
353363
target_id: str,
364+
metadata: bytes,
354365
) -> None:
355366
"""
356367
A base64 encoded string is valid application metadata.
357368
"""
358-
metadata = b'Some data'
359369
metadata_encoded = base64.b64encode(metadata).decode('ascii')
360370

361371
wait_for_target_processed(
@@ -432,6 +442,35 @@ def test_not_base64_encoded(
432442
result_code=ResultCodes.FAIL,
433443
)
434444

445+
def test_metadata_too_large(
446+
self,
447+
vuforia_database_keys: VuforiaDatabaseKeys,
448+
target_id: str,
449+
) -> None:
450+
"""
451+
A base64 encoded string of greater than 1024 * 1024 bytes is too large
452+
for application metadata.
453+
"""
454+
metadata = b'a' * (self._MAX_METADATA_BYTES + 1)
455+
metadata_encoded = base64.b64encode(metadata).decode('ascii')
456+
457+
wait_for_target_processed(
458+
vuforia_database_keys=vuforia_database_keys,
459+
target_id=target_id,
460+
)
461+
462+
response = update_target(
463+
vuforia_database_keys=vuforia_database_keys,
464+
data={'application_metadata': metadata_encoded},
465+
target_id=target_id,
466+
)
467+
468+
assert_vws_failure(
469+
response=response,
470+
status_code=codes.UNPROCESSABLE_ENTITY,
471+
result_code=ResultCodes.METADATA_TOO_LARGE,
472+
)
473+
435474

436475
@pytest.mark.usefixtures('verify_mock_vuforia')
437476
class TestTargetName:

0 commit comments

Comments
 (0)