Esempio n. 1
0
def test_raw_request_sends():
    client = JWPlatformClient()

    with JWPlatformMock() as mock_api:
        client.raw_request("POST", "/v2/test_request/")

    mock_api.testRequest.request_mock.assert_called_once()
Esempio n. 2
0
def test_success_response_object():
    client = JWPlatformClient()

    with JWPlatformMock():
        response = client.raw_request("POST", "/v2/test_request/")

    assert response.status == 200
    assert response.body == b'{"field": "value"}'
    assert response.json_body["field"] == "value"
Esempio n. 3
0
def test_unknown_body_error():
    client = JWPlatformClient()

    with JWPlatformMock():
        try:
            client.raw_request("POST", "/v2/test_unknown_body_error/")
            pytest.fail("Expected to raise ClientError")
        except ClientError as ex:
            assert ex.errors is None
Esempio n. 4
0
    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()
Esempio n. 5
0
 def test_upload_method_with_direct_upload_throws_when_retries_exceeded_on_failed_s3_upload(
         self, s3_upload_response, 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}\"'
     s3_upload_response.side_effect = [
         S3UploadError, S3UploadError, S3UploadError
     ]
     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': 3,
             'site_id': 'siteDEid'
         }
         with JWPlatformMock() as mock_api:
             upload_context = media_client_instance.create_media_and_get_upload_context(
                 file, **kwargs)
             with self.assertRaises(MaxRetriesExceededError):
                 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")
Esempio n. 6
0
 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()
Esempio n. 7
0
 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()
Esempio n. 8
0
def test_error_code_access():
    client = JWPlatformClient()

    with JWPlatformMock():
        try:
            client.raw_request("POST", "/v2/test_bad_request/")
            pytest.fail("Expected to raise ClientError")
        except ClientError as ex:
            assert ex.has_error_code("invalid_body") is True
            assert ex.has_error_code("invalid_code") is False
            assert len(ex.get_errors_by_code(
                "invalid_body")) == 1, ex.get_errors_by_code("invalid_body")
            assert len(ex.get_errors_by_code(
                "invalid_code")) == 0, ex.get_errors_by_code("invalid_code")
            assert str(
                ex
            ) == "JWPlatform API Error:\n\ninvalid_body: The provided request body is invalid.\n"
def run_multipart_upload_with_auto_resume(site_id, video_file_path, retry_count):
    """
        Creates a media and uploads the media file using a direct or multi-part mechanism.
        Args:
            site_id: The site ID
            video_file_path: The <str> containing the absolute path to the media file.
            retry_count: Number of retries to attempt before exiting.

        Returns: None

        """
    media_client_instance = JWPlatformClient().Media
    upload_parameters = {
        'site_id': site_id,
        'target_part_size': 5 * 1024 * 1024
    }

    with open(video_file_path, "rb") as file:
        upload_context = media_client_instance.create_media_and_get_upload_context(file, **upload_parameters)
        try:
            media_client_instance.upload(file, upload_context, **upload_parameters)
            return
        except Exception as ex:
            logging.exception(ex)
            logging.debug("Resuming upload.")
        while retry_count < 10:
            try:
                media_client_instance.resume(file, upload_context, **upload_parameters)
                return
            except (DataIntegrityError, PartUploadError, IOError, OSError) as ex:
                retry_count = retry_count + 1
                logging.debug(f"Resuming upload again. Retry attempt:{retry_count}")
                logging.exception(ex)
Esempio n. 10
0
def test_request_modifies_input():
    client = JWPlatformClient(secret="test_secret")

    with patch.object(client, 'raw_request') as mock_raw_request:
        client.request(
            "POST", "/v2/test_request/",
            body={"field": "value"},
            query_params={"param": "value"}
        )

    mock_raw_request.assert_called_once()
    kwargs = mock_raw_request.call_args[1]
    assert kwargs["method"] == "POST"
    assert kwargs["url"] == "/v2/test_request/?param=value"
    assert kwargs["body"] == '{"field": "value"}'
    assert kwargs["headers"]["User-Agent"] == f"jwplatform_client-python/{__version__}"
    assert kwargs["headers"]["Content-Type"] == "application/json"
    assert kwargs["headers"]["Authorization"] == "Bearer test_secret"
def run_multipart_upload(site_id, video_file_path):
    """
    Creates a media and uploads the media file using a direct or multi-part mechanism.
    Args:
        site_id: The site ID
        video_file_path: The <str> containing the absolute path to the media file.

    Returns: None

    """
    media_client_instance = JWPlatformClient().Media
    upload_parameters = {
        'site_id': site_id,
        'target_part_size': 5 * 1024 * 1024,
        'retry_count': 3
    }
    with open(video_file_path, "rb") as file:
        upload_context = media_client_instance.create_media_and_get_upload_context(file, **upload_parameters)
        media_client_instance.upload(file, upload_context, **upload_parameters)
        logging.info(f"Successfully uploaded file:{file.name}")
Esempio n. 12
0
def test_resource_response():
    client = JWPlatformClient()

    with JWPlatformMock():
        response = client.Media.get(site_id="testsite", media_id="mediaid1")

    assert response.status == 200
    assert response.json_body["id"] == "mediaid1"
    assert response.json_body["type"] == "media"
    assert isinstance(response,
                      client.Media.__class__), response.__class__.__name__
Esempio n. 13
0
def test_resources_response():
    client = JWPlatformClient()

    with JWPlatformMock():
        response = client.Media.list(site_id="testsite")

    assert response.status == 200
    assert len(response) == 1
    for media in response:
        assert media["id"] == "mediaid1"
        assert media["type"] == "media"
    assert isinstance(response,
                      client.Media.__class__), response.__class__.__name__
Esempio n. 14
0
 def test_upload_method_is_direct_when_file_size_is_small(
         self, mock_file, os_stat):
     os_stat.return_value.st_size = 4 * 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': 10,
             'site_id': 'siteDEid'
         }
         with JWPlatformMock():
             upload_context = media_client_instance.create_media_and_get_upload_context(
                 file, **kwargs)
             self.assertTrue(
                 upload_context.upload_method == UploadType.direct.value)
             mock_file.assert_called_with(file_absolute_path, "rb")
             os_stat.assert_called_once()
Esempio n. 15
0
 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)
Esempio n. 16
0
 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")
Esempio n. 17
0
def test_server_error():
    client = JWPlatformClient()

    with JWPlatformMock():
        with pytest.raises(ServerError):
            client.raw_request("POST", "/v2/test_server_error/")
Esempio n. 18
0
def test_specific_error_class():
    client = JWPlatformClient()

    with JWPlatformMock():
        with pytest.raises(BadRequestError):
            client.raw_request("POST", "/v2/test_bad_request/")
Esempio n. 19
0
def test_unknown_status_error():
    client = JWPlatformClient()

    with JWPlatformMock():
        with pytest.raises(UnexpectedStatusError):
            client.raw_request("POST", "/v2/test_unknown_status_error/")