def test_upload_method_with_direct_upload(self, get_returned_hash, get_bytes_hash, os_stat, mock_file): file_hash = md5(self.file_content_mock_data_simple).hexdigest() get_bytes_hash.return_value = file_hash get_returned_hash.return_value = f'\"{file_hash}\"' os_stat.return_value.st_size = 5 * 1024 * 1024 client = JWPlatformClient() media_client_instance = client.Media file_absolute_path = "mock_file_path" with open(file_absolute_path, "rb") as file: kwargs = { 'target_part_size': 10 * 1024 * 1024, 'retry_count': 1, 'site_id': 'siteDEid' } with JWPlatformMock() as mock_api, S3Mock() as s3_api: upload_context = media_client_instance.create_media_and_get_upload_context( file, **kwargs) media_client_instance.upload(file, upload_context, **kwargs) self.assertTrue( upload_context.upload_method == UploadType.direct.value) mock_file.assert_called_with(file_absolute_path, "rb") os_stat.assert_called_once() mock_api.createMedia.request_mock.assert_called_once() s3_api.uploadToS3.request_mock.assert_called_once()
def test_upload_method_with_multipart_upload_throws_on_unexpected_error( self, s3_upload_response, retrieve_part_links, get_returned_hash, get_bytes_hash, os_stat, mock_file): file_hash = md5(self.file_content_mock_data_large).hexdigest() get_bytes_hash.return_value = file_hash get_returned_hash.return_value = f'\"{file_hash}\"' os_stat.return_value.st_size = len(self.file_content_mock_data_large) target_part_size = 5 * 1024 * 1024 part_count = len( self.file_content_mock_data_large) // target_part_size + 1 retrieve_part_links.return_value = _get_parts_responses(part_count) s3_upload_response.side_effect = Exception site_id = 'siteDEid' client = JWPlatformClient() media_client_instance = client.Media file_absolute_path = "mock_file_path" with open(file_absolute_path, "rb") as file: kwargs = { 'target_part_size': 5 * 1024 * 1024, 'retry_count': 3, 'base_url': JWPLATFORM_API_HOST, 'site_id': 'siteDEid' } with JWPlatformMock() as mock_api, S3Mock() as s3_api: upload_context = media_client_instance.create_media_and_get_upload_context( file, **kwargs) self.assertTrue( upload_context.upload_method == UploadType.multipart.value) with self.assertRaises(Exception): media_client_instance.upload(file, upload_context, **kwargs) mock_api.completeUpload.request_mock.assert_not_called() mock_file.assert_called_with(file_absolute_path, "rb") s3_upload_response.assert_called_once()
def test_upload_method_with_multipart_upload_resume_success( self, get_uploaded_part_hash, retrieve_part_links, get_returned_hash, get_bytes_hash, os_stat, mock_file): file_hash = md5(self.file_content_mock_data_large).hexdigest() get_bytes_hash.return_value = file_hash get_returned_hash.return_value = f'\"{file_hash}\"' get_uploaded_part_hash.return_value = file_hash os_stat.return_value.st_size = len(self.file_content_mock_data_large) target_part_size = 5 * 1024 * 1024 part_count = len( self.file_content_mock_data_large) // target_part_size + 1 retrieve_part_links.return_value = _get_parts_responses(part_count) site_id = 'siteDEid' client = JWPlatformClient() media_client_instance = client.Media file_absolute_path = "mock_file_path" with open(file_absolute_path, "rb") as file: kwargs = { 'target_part_size': 5 * 1024 * 1024, 'retry_count': 3, 'base_url': JWPLATFORM_API_HOST, 'site_id': 'siteDEid' } with JWPlatformMock() as mock_api, S3Mock() as s3_api: upload_context = UploadContext(UploadType.multipart.value, 'NL3OL1JB', 'upload_token', None) media_client_instance.resume(file, upload_context, **kwargs) mock_file.assert_called_with(file_absolute_path, "rb") mock_api.createMedia.request_mock.assert_not_called() mock_api.completeUpload.request_mock.assert_called_once() retrieve_part_links.assert_called_once() s3_api.uploadToS3.request_mock.assert_not_called()
def test_upload_method_with_multipart_upload_failure_on_mismatched_upload_hash( self, retrieve_part_links, get_uploaded_part_hash, get_returned_hash, get_bytes_hash, os_stat, mock_file): file_hash = md5(self.file_content_mock_data_large).hexdigest() get_bytes_hash.return_value = file_hash os_stat.return_value.st_size = len(self.file_content_mock_data_large) target_part_size = 5 * 1024 * 1024 part_count = math.ceil( len(self.file_content_mock_data_large) / target_part_size) get_uploaded_part_hash.return_value = None get_returned_hash.return_value = f'\"wrong_hash\"' retrieve_part_links.return_value = _get_parts_responses(part_count) site_id = 'siteDEid' client = JWPlatformClient() media_client_instance = client.Media file_absolute_path = "mock_file_path" retry_count = 3 with open(file_absolute_path, "rb") as file: kwargs = { 'target_part_size': 5 * 1024 * 1024, 'retry_count': retry_count, 'base_url': JWPLATFORM_API_HOST, 'site_id': 'siteDEid' } with JWPlatformMock() as mock_api, S3Mock() as s3_api: upload_context = media_client_instance.create_media_and_get_upload_context( file, **kwargs) self.assertTrue( upload_context.upload_method == UploadType.multipart.value) with self.assertRaises(MaxRetriesExceededError): media_client_instance.upload(file, upload_context, **kwargs) mock_file.assert_called_with(file_absolute_path, "rb") mock_api.createMedia.request_mock.assert_called_once() mock_api.completeUpload.request_mock.assert_not_called() self.assertEqual(get_uploaded_part_hash.call_count, retry_count) self.assertEqual(get_returned_hash.call_count, retry_count) retrieve_part_links.assert_called_once() self.assertEqual(s3_api.uploadToS3.request_mock.call_count, retry_count)
def test_upload_method_with_direct_upload_fails_hash_check( self, get_returned_hash, get_bytes_hash, os_stat, mock_file): file_hash = md5(self.file_content_mock_data_simple).hexdigest() get_bytes_hash.return_value = file_hash os_stat.return_value.st_size = 5 * 1024 * 1024 client = JWPlatformClient() media_client_instance = client.Media file_absolute_path = "mock_file_path" with self.assertRaises(MaxRetriesExceededError): with open(file_absolute_path, "rb") as file: kwargs = { 'target_part_size': 10 * 1024 * 1024, 'retry_count': 1, 'site_id': 'siteDEid' } with JWPlatformMock() as mock_api, S3Mock() as s3_api: upload_context = media_client_instance.create_media_and_get_upload_context( file, **kwargs) media_client_instance.upload(file, upload_context, **kwargs) s3_api.uploadToS3.request_mock.assert_not_called() mock_api.completeUpload.request_mock.assert_not_called() mock_file.assert_called_with(file_absolute_path, "rb")