Esempio n. 1
0
def test_multipart_upload_with_bad_checksum(authorized_transport, checksum,
                                            bucket):
    with open(ICO_FILE, u"rb") as file_obj:
        actual_contents = file_obj.read()

    blob_name = os.path.basename(ICO_FILE)
    check_does_not_exist(authorized_transport, blob_name)

    # Create the actual upload object.
    upload_url = utils.MULTIPART_UPLOAD
    upload = resumable_requests.MultipartUpload(upload_url, checksum=checksum)
    # Transmit the resource.
    metadata = {u"name": blob_name, u"metadata": {u"color": u"yellow"}}
    fake_checksum_object = _helpers._get_checksum_object(checksum)
    fake_checksum_object.update(b"bad data")
    fake_prepared_checksum_digest = _helpers.prepare_checksum_digest(
        fake_checksum_object.digest())
    with mock.patch.object(_helpers,
                           "prepare_checksum_digest",
                           return_value=fake_prepared_checksum_digest):
        with pytest.raises(common.InvalidResponse) as exc_info:
            response = upload.transmit(authorized_transport, actual_contents,
                                       metadata, ICO_CONTENT_TYPE)
    response = exc_info.value.response
    message = response.json()["error"]["message"]
    # Attempt to verify that this is a checksum mismatch error.
    assert checksum.upper() in message
    assert fake_prepared_checksum_digest in message

    # Make sure the upload is tombstoned.
    check_tombstoned(upload, authorized_transport, actual_contents, metadata,
                     ICO_CONTENT_TYPE)
Esempio n. 2
0
def add_files(authorized_transport, bucket):
    blob_names = []
    for info in ALL_FILES:
        to_upload = get_contents_for_upload(info)
        blob_name = get_blob_name(info)

        blob_names.append(blob_name)
        if u"metadata" in info:
            upload = resumable_requests.MultipartUpload(utils.MULTIPART_UPLOAD)
            metadata = copy.deepcopy(info[u"metadata"])
            metadata[u"name"] = blob_name
            response = upload.transmit(authorized_transport, to_upload,
                                       metadata, info[u"content_type"])
        else:
            upload_url = utils.SIMPLE_UPLOAD_TEMPLATE.format(
                blob_name=blob_name)
            upload = resumable_requests.SimpleUpload(upload_url)
            response = upload.transmit(authorized_transport, to_upload,
                                       info[u"content_type"])

        assert response.status_code == http_client.OK

    yield

    # Clean-up the blobs we created.
    for blob_name in blob_names:
        delete_blob(authorized_transport, blob_name)
Esempio n. 3
0
def test_multipart_upload(authorized_transport, bucket, cleanup, checksum):
    with open(ICO_FILE, u"rb") as file_obj:
        actual_contents = file_obj.read()

    blob_name = os.path.basename(ICO_FILE)
    # Make sure to clean up the uploaded blob when we are done.
    cleanup(blob_name, authorized_transport)
    check_does_not_exist(authorized_transport, blob_name)

    # Create the actual upload object.
    upload_url = utils.MULTIPART_UPLOAD
    upload = resumable_requests.MultipartUpload(upload_url, checksum=checksum)
    # Transmit the resource.
    metadata = {u"name": blob_name, u"metadata": {u"color": u"yellow"}}
    response = upload.transmit(authorized_transport, actual_contents, metadata,
                               ICO_CONTENT_TYPE)
    check_response(
        response,
        blob_name,
        actual_contents=actual_contents,
        metadata=metadata[u"metadata"],
    )
    # Download the content to make sure it's "working as expected".
    check_content(blob_name, actual_contents, authorized_transport)
    # Make sure the upload is tombstoned.
    check_tombstoned(upload, authorized_transport, actual_contents, metadata,
                     ICO_CONTENT_TYPE)
Esempio n. 4
0
def test_multipart_upload_with_headers(authorized_transport, bucket, cleanup):
    blob_name = u"some-multipart-stuff.bin"
    # Make sure to clean up the uploaded blob when we are done.
    cleanup(blob_name, authorized_transport)
    check_does_not_exist(authorized_transport, blob_name)

    # Create the actual upload object.
    upload_url = utils.MULTIPART_UPLOAD
    headers = utils.get_encryption_headers()
    upload = resumable_requests.MultipartUpload(upload_url, headers=headers)
    # Transmit the resource.
    metadata = {u"name": blob_name}
    data = b"Other binary contents\x03\x04\x05."
    response = upload.transmit(authorized_transport, data, metadata, BYTES_CONTENT_TYPE)
    check_response(
        response, blob_name, actual_contents=data, content_type=BYTES_CONTENT_TYPE
    )
    # Download the content to make sure it's "working as expected".
    check_content(blob_name, data, authorized_transport, headers=headers)
    # Make sure the upload is tombstoned.
    check_tombstoned(upload, authorized_transport, data, metadata, BYTES_CONTENT_TYPE)