def test_that_image_upload_handler_file_complete_raises_error_when_invalid_bytes_sequence(self, settings): imageBytesIO = BytesIO(b'a' * 100) imageBytes = imageBytesIO.getvalue() settings.FILE_UPLOAD_MAX_MEMORY_SIZE = MAX_FILE_SIZE imageHandler = ImageUploadHandler() assert_that(calling(imageHandler.new_file).with_args(None, None, 'image/png', len(imageBytes), 'utf-8'), raises(StopFutureHandlers)) imageHandler.receive_data_chunk(imageBytes, 0) assert_that(calling(imageHandler.file_complete).with_args(len(imageBytes)), raises(InvalidImageContent))
def test_that_image_upload_handler_file_complete_raises_error_when_size_does_not_match(self, settings): settings.FILE_UPLOAD_MAX_MEMORY_SIZE = MAX_FILE_SIZE imageHandler = ImageUploadHandler() assert_that(calling(imageHandler.new_file).with_args(None, None, 'image/png', 923874, 'utf-8'), raises(StopFutureHandlers)) imageHandler.receive_data_chunk(b'a' * int(settings.FILE_UPLOAD_MAX_MEMORY_SIZE / 2), 0) assert_that( calling(imageHandler.file_complete).with_args(int(settings.FILE_UPLOAD_MAX_MEMORY_SIZE / 2 + 1)), raises(StopUpload))
def test_that_image_upload_handler_receive_data_chunk_does_not_accept_chunk_when_total_upload_exceeds_the_limit( self, settings): settings.FILE_UPLOAD_MAX_MEMORY_SIZE = MAX_FILE_SIZE imageHandler = ImageUploadHandler() assert_that(calling(imageHandler.new_file).with_args(None, None, 'image/png', 923874, 'utf-8'), raises(StopFutureHandlers)) imageHandler.receive_data_chunk(b'a' * int(settings.FILE_UPLOAD_MAX_MEMORY_SIZE / 2), 0) assert_that( calling(imageHandler.receive_data_chunk).with_args(b'a' * int(settings.FILE_UPLOAD_MAX_MEMORY_SIZE / 2 + 1), settings.FILE_UPLOAD_MAX_MEMORY_SIZE / 2), raises(StopUpload))
def test_that_image_upload_handler_file_complete_returns_numpy_array_for_uploaded_image(self, settings): image = Image.new('RGB', (640, 480), (73, 109, 137)) imageBytesIO = BytesIO() image.save(imageBytesIO, 'PNG') imageBytes = imageBytesIO.getvalue() settings.FILE_UPLOAD_MAX_MEMORY_SIZE = MAX_FILE_SIZE imageHandler = ImageUploadHandler() assert_that(calling(imageHandler.new_file).with_args(None, None, 'image/png', 923874, 'utf-8'), raises(StopFutureHandlers)) imageHandler.receive_data_chunk(imageBytes, 0) numpyImg = imageHandler.file_complete(len(imageBytes)) np.testing.assert_array_equal(numpyImg, np.array(image))
def test_that_image_upload_handler_receive_data_chunk_does_accept_valid_chunk(self, settings): settings.FILE_UPLOAD_MAX_MEMORY_SIZE = MAX_FILE_SIZE imageHandler = ImageUploadHandler() assert_that(calling(imageHandler.new_file).with_args(None, None, 'image/png', 923874, 'utf-8'), raises(StopFutureHandlers)) imageHandler.receive_data_chunk(b'chunk content', 0)