Skip to content

Commit f4e6ee8

Browse files
Merge pull request #673 from adamtheturtle/corrupted-test-add
Add proper tests for image too large
2 parents bf10ae9 + e867d08 commit f4e6ee8

File tree

4 files changed

+112
-52
lines changed

4 files changed

+112
-52
lines changed

src/mock_vws/_validators.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,6 @@ def validate_image_size(
688688
The result of calling the endpoint.
689689
An `UNPROCESSABLE_ENTITY` response if the image is given and is not
690690
under a certain file size threshold.
691-
This threshold is documented as being 2 MB but it is actually
692-
slightly larger. See the `png_large` fixture for more details.
693691
"""
694692
request, context = args
695693

tests/mock_vws/fixtures/images.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,6 @@ def png_greyscale() -> io.BytesIO:
5050
)
5151

5252

53-
@pytest.fixture()
54-
def png_large(
55-
png_rgb: io.BytesIO, # pylint: disable=redefined-outer-name
56-
) -> io.BytesIO:
57-
"""
58-
Return a PNG file of the maximum allowed file size.
59-
60-
https://library.vuforia.com/articles/Training/Cloud-Recognition-Guide
61-
describes that the maximum allowed file size of an image is 2 MB.
62-
However, tests using this fixture demonstrate that the maximum allowed
63-
size is actually slightly greater than that.
64-
"""
65-
png_size = len(png_rgb.getbuffer())
66-
max_size = 2359293
67-
filler_length = max_size - png_size
68-
filler_data = b'\x00' * int(filler_length)
69-
original_data = png_rgb.getvalue()
70-
longer_data = original_data.replace(b'IEND', filler_data + b'IEND')
71-
png = io.BytesIO(longer_data)
72-
return png
73-
74-
7553
@pytest.fixture
7654
def jpeg_cmyk() -> io.BytesIO:
7755
"""
@@ -114,7 +92,7 @@ def tiff_rgb() -> io.BytesIO:
11492
)
11593

11694

117-
@pytest.fixture(params=['png_rgb', 'jpeg_rgb', 'png_greyscale', 'png_large'])
95+
@pytest.fixture(params=['png_rgb', 'jpeg_rgb', 'png_greyscale'])
11896
def image_file(request: SubRequest) -> io.BytesIO:
11997
"""
12098
Return an image file which is expected to work on Vuforia.

tests/mock_vws/test_add_target.py

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from tests.mock_vws.utils import (
1616
add_target_to_vws,
1717
delete_target,
18+
make_image_file,
1819
wait_for_target_processed,
1920
)
2021
from tests.mock_vws.utils.assertions import (
@@ -388,10 +389,7 @@ def test_image_valid(
388389
) -> None:
389390
"""
390391
JPEG and PNG files in the RGB and greyscale color spaces are
391-
allowed. The image must be under a threshold.
392-
393-
This threshold is documented as being 2 MB but it is actually
394-
slightly larger. See the `png_large` fixture for more details.
392+
allowed.
395393
"""
396394
image_data = image_file.read()
397395
image_data_encoded = base64.b64encode(image_data).decode('ascii')
@@ -468,24 +466,33 @@ def test_corrupted(
468466

469467
assert_success(response=response)
470468

471-
def test_too_large_and_corrupted(
469+
def test_image_too_large(
472470
self,
473471
vuforia_database_keys: VuforiaDatabaseKeys,
474-
png_large: io.BytesIO,
475472
) -> None:
476473
"""
477474
An `ImageTooLarge` result is returned if the image is above a certain
478-
threshold and is corrupted.
479-
480-
This threshold is documented as being 2 MB but it is actually
481-
slightly larger. See the `png_large` fixture for more details.
475+
threshold.
482476
"""
483-
original_data = png_large.getvalue()
484-
longer_data = original_data.replace(b'IEND', b'\x00' + b'IEND')
485-
too_large_file = io.BytesIO(longer_data)
477+
max_bytes = 2.3 * 1024 * 1024
478+
width = height = 886
479+
png_not_too_large = make_image_file(
480+
file_format='PNG',
481+
color_space='RGB',
482+
width=width,
483+
height=height,
484+
)
486485

487-
image_data = too_large_file.read()
486+
image_data = png_not_too_large.read()
488487
image_data_encoded = base64.b64encode(image_data).decode('ascii')
488+
image_content_size = len(image_data)
489+
# We check that the image we created is just slightly smaller than the
490+
# maximum file size.
491+
#
492+
# This is just because of the implementation details of
493+
# ``max_image_file``.
494+
assert image_content_size < max_bytes
495+
assert (image_content_size * 1.05) > max_bytes
489496

490497
data = {
491498
'name': 'example_name',
@@ -498,6 +505,39 @@ def test_too_large_and_corrupted(
498505
data=data,
499506
)
500507

508+
assert_success(response=response)
509+
510+
width = width + 1
511+
height = height + 1
512+
png_too_large = make_image_file(
513+
file_format='PNG',
514+
color_space='RGB',
515+
width=width,
516+
height=height,
517+
)
518+
519+
image_data = png_too_large.read()
520+
image_data_encoded = base64.b64encode(image_data).decode('ascii')
521+
image_content_size = len(image_data)
522+
# We check that the image we created is just slightly smaller than the
523+
# maximum file size.
524+
#
525+
# This is just because of the implementation details of
526+
# ``max_image_file``.
527+
assert image_content_size < max_bytes
528+
assert (image_content_size * 1.05) > max_bytes
529+
530+
data = {
531+
'name': 'example_name_2',
532+
'width': 1,
533+
'image': image_data_encoded,
534+
}
535+
536+
response = add_target_to_vws(
537+
vuforia_database_keys=vuforia_database_keys,
538+
data=data,
539+
)
540+
501541
assert_vws_failure(
502542
response=response,
503543
status_code=codes.UNPROCESSABLE_ENTITY,

tests/mock_vws/test_update_target.py

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from tests.mock_vws.utils import (
1616
add_target_to_vws,
1717
get_vws_target,
18+
make_image_file,
1819
update_target,
1920
wait_for_target_processed,
2021
)
@@ -710,9 +711,6 @@ def test_image_valid(
710711
"""
711712
JPEG and PNG files in the RGB and greyscale color spaces are
712713
allowed. The image must be under a threshold.
713-
714-
This threshold is documented as being 2 MB but it is actually
715-
slightly larger. See the `png_large` fixture for more details.
716714
"""
717715
image_data = image_file.read()
718716
image_data_encoded = base64.b64encode(image_data).decode('ascii')
@@ -796,31 +794,77 @@ def test_corrupted(
796794
result_code=ResultCodes.SUCCESS,
797795
)
798796

799-
def test_too_large_and_corrupted(
797+
def test_image_too_large(
800798
self,
801799
vuforia_database_keys: VuforiaDatabaseKeys,
802-
png_large: io.BytesIO,
803800
target_id: str,
804801
) -> None:
805802
"""
806803
An `ImageTooLarge` result is returned if the image is above a certain
807-
threshold and is corrupted.
808-
809-
This threshold is documented as being 2 MB but it is actually
810-
slightly larger. See the `png_large` fixture for more details.
804+
threshold.
811805
"""
812-
original_data = png_large.getvalue()
813-
longer_data = original_data.replace(b'IEND', b'\x00' + b'IEND')
814-
too_large_file = io.BytesIO(longer_data)
806+
max_bytes = 2.3 * 1024 * 1024
807+
width = height = 886
808+
png_not_too_large = make_image_file(
809+
file_format='PNG',
810+
color_space='RGB',
811+
width=width,
812+
height=height,
813+
)
814+
815+
wait_for_target_processed(
816+
vuforia_database_keys=vuforia_database_keys,
817+
target_id=target_id,
818+
)
815819

816-
image_data = too_large_file.read()
820+
image_data = png_not_too_large.read()
817821
image_data_encoded = base64.b64encode(image_data).decode('ascii')
822+
image_content_size = len(image_data)
823+
# We check that the image we created is just slightly smaller than the
824+
# maximum file size.
825+
#
826+
# This is just because of the implementation details of
827+
# ``max_image_file``.
828+
assert image_content_size < max_bytes
829+
assert (image_content_size * 1.05) > max_bytes
830+
831+
response = update_target(
832+
vuforia_database_keys=vuforia_database_keys,
833+
data={'image': image_data_encoded},
834+
target_id=target_id,
835+
)
836+
837+
assert_vws_response(
838+
response=response,
839+
status_code=codes.OK,
840+
result_code=ResultCodes.SUCCESS,
841+
)
818842

819843
wait_for_target_processed(
820844
vuforia_database_keys=vuforia_database_keys,
821845
target_id=target_id,
822846
)
823847

848+
width = width + 1
849+
height = height + 1
850+
png_too_large = make_image_file(
851+
file_format='PNG',
852+
color_space='RGB',
853+
width=width,
854+
height=height,
855+
)
856+
857+
image_data = png_too_large.read()
858+
image_data_encoded = base64.b64encode(image_data).decode('ascii')
859+
image_content_size = len(image_data)
860+
# We check that the image we created is just slightly smaller than the
861+
# maximum file size.
862+
#
863+
# This is just because of the implementation details of
864+
# ``max_image_file``.
865+
assert image_content_size < max_bytes
866+
assert (image_content_size * 1.05) > max_bytes
867+
824868
response = update_target(
825869
vuforia_database_keys=vuforia_database_keys,
826870
data={'image': image_data_encoded},

0 commit comments

Comments
 (0)