Ejemplo n.º 1
0
def test_zarr_rest_delete_missing_file(
    authenticated_api_client,
    user,
    storage,
    zarr_archive: ZarrArchive,
    zarr_upload_file_factory,
):
    assign_perm('owner', user, zarr_archive.dandiset)
    # Pretend like ZarrUploadFile was defined with the given storage
    ZarrUploadFile.blob.field.storage = storage
    upload = zarr_upload_file_factory(zarr_archive=zarr_archive)
    zarr_archive.complete_upload()

    resp = authenticated_api_client.delete(
        f'/api/zarr/{zarr_archive.zarr_id}/files/',
        [
            {
                'path': upload.path
            },
            {
                'path': 'does/not/exist'
            },
        ],
    )
    assert resp.status_code == 400
    assert resp.json() == [
        f'File test-prefix/test-zarr/{zarr_archive.zarr_id}/does/not/exist does not exist.'
    ]
    assert upload.blob.field.storage.exists(upload.blob.name)
    zarr_archive.refresh_from_db()
    assert zarr_archive.file_count == 1
    assert zarr_archive.size == upload.size()
Ejemplo n.º 2
0
def test_zarr_rest_delete_multiple_files(
    authenticated_api_client,
    user,
    storage,
    zarr_archive: ZarrArchive,
    zarr_upload_file_factory,
):
    assign_perm('owner', user, zarr_archive.dandiset)
    # Pretend like ZarrUploadFile was defined with the given storage
    ZarrUploadFile.blob.field.storage = storage
    uploads = [
        zarr_upload_file_factory(zarr_archive=zarr_archive)
        for i in range(0, 10)
    ]
    zarr_archive.complete_upload()

    resp = authenticated_api_client.delete(
        f'/api/zarr/{zarr_archive.zarr_id}/files/', [{
            'path': upload.path
        } for upload in uploads])
    assert resp.status_code == 204

    for upload in uploads:
        assert not upload.blob.field.storage.exists(upload.blob.name)
    zarr_archive.refresh_from_db()
    assert zarr_archive.file_count == 0
    assert zarr_archive.size == 0
Ejemplo n.º 3
0
def test_zarr_rest_delete_file_asset_metadata(
    authenticated_api_client,
    user,
    storage,
    zarr_archive: ZarrArchive,
    zarr_upload_file_factory,
    asset_factory,
):
    assign_perm('owner', user, zarr_archive.dandiset)
    # Pretend like ZarrUploadFile was defined with the given storage
    ZarrUploadFile.blob.field.storage = storage
    upload = zarr_upload_file_factory(zarr_archive=zarr_archive)
    zarr_archive.complete_upload()
    asset = asset_factory(zarr=zarr_archive, blob=None)
    assert asset.metadata['digest'] == zarr_archive.digest
    assert asset.metadata['contentSize'] == 100

    resp = authenticated_api_client.delete(
        f'/api/zarr/{zarr_archive.zarr_id}/files/', [{
            'path': upload.path
        }])
    assert resp.status_code == 204

    asset.refresh_from_db()
    assert asset.metadata['digest'][
        'dandi:dandi-zarr-checksum'] == EMPTY_CHECKSUM
    assert asset.metadata['contentSize'] == 0
Ejemplo n.º 4
0
def test_zarr_rest_upload_flow(authenticated_api_client, user, storage,
                               zarr_archive: ZarrArchive):
    assign_perm('owner', user, zarr_archive.dandiset)
    # Pretend like ZarrUploadFile was defined with the given storage
    ZarrUploadFile.blob.field.storage = storage

    path = 'foo/bar.txt'
    text = b'Some fascinating zarr content.\n'
    h = hashlib.md5()
    h.update(text)
    etag = h.hexdigest()

    resp = authenticated_api_client.post(
        f'/api/zarr/{zarr_archive.zarr_id}/upload/',
        [{
            'path': path,
            'etag': etag,
        }],
        format='json',
    )
    upload_url = resp.json()[0]['upload_url']

    resp = requests.put(upload_url,
                        data=text,
                        headers={'X-Amz-ACL': 'bucket-owner-full-control'})
    assert resp.status_code == 200

    resp = authenticated_api_client.post(
        f'/api/zarr/{zarr_archive.zarr_id}/upload/complete/')
    assert resp.status_code == 201

    zarr_archive.refresh_from_db()
    assert not zarr_archive.upload_in_progress
    assert zarr_archive.file_count == 1
    assert zarr_archive.size == len(text)
Ejemplo n.º 5
0
def test_zarr_rest_delete_file_not_an_owner(
    authenticated_api_client,
    storage,
    zarr_archive: ZarrArchive,
    zarr_upload_file_factory,
):
    # Pretend like ZarrUploadFile was defined with the given storage
    ZarrUploadFile.blob.field.storage = storage
    upload = zarr_upload_file_factory(zarr_archive=zarr_archive)
    zarr_archive.complete_upload()

    resp = authenticated_api_client.delete(
        f'/api/zarr/{zarr_archive.zarr_id}/files/', [{
            'path': upload.path
        }])
    assert resp.status_code == 403
Ejemplo n.º 6
0
def test_zarr_rest_get(authenticated_api_client, storage,
                       zarr_archive: ZarrArchive, zarr_upload_file_factory):
    # Pretend like ZarrUploadFile was defined with the given storage
    ZarrUploadFile.blob.field.storage = storage
    upload = zarr_upload_file_factory(zarr_archive=zarr_archive)
    # We need to complete the upload for the checksum files to be written.
    zarr_archive.complete_upload()

    resp = authenticated_api_client.get(f'/api/zarr/{zarr_archive.zarr_id}/')
    assert resp.status_code == 200
    assert resp.json() == {
        'name': zarr_archive.name,
        'zarr_id': zarr_archive.zarr_id,
        'dandiset': zarr_archive.dandiset.identifier,
        'checksum': zarr_archive.checksum,
        'upload_in_progress': False,
        'file_count': 1,
        'size': upload.size(),
    }