Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Add support for DELETE /resources/backup/:asset_id #700

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ exports.delete_all_resources = function delete_all_resources(callback, options =
}), callback, options);
};

exports.delete_backed_up_assets = (assetId, versionIds, callback, options = {}) => {
const params = deleteBackupParams(versionIds);

return call_api('delete', ['resources', 'backup', assetId], params, callback, options);
}

const deleteBackupParams = (versionIds = []) => {
return {
"version_ids[]": Array.isArray(versionIds) ? versionIds : [versionIds]
};
};

const createRelationParams = (publicIds = []) => {
return {
assets_to_relate: Array.isArray(publicIds) ? publicIds : [publicIds]
Expand Down
1 change: 1 addition & 0 deletions lib/v2/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ v1_adapters(exports, api, {
add_related_assets_by_asset_id: 2,
delete_related_assets: 2,
delete_related_assets_by_asset_id: 2,
delete_backed_up_assets: 2,
config: 0
});
61 changes: 61 additions & 0 deletions test/integration/api/admin/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,67 @@ describe("api", function () {
});
});
});
describe('delete_backed_up_assets', function () {
it('should delete specific version IDs', async function () {
this.timeout(TIMEOUT.LARGE);

// Process:
// - Upload to the same public ID three times
// - Delete a single version (string)
// - Delete a two versions (array)
// - Cleanup

// Perform three uploads
const firstUpload = await uploadImage({
public_id: PUBLIC_ID_BACKUP_1,
backup: true
});
await wait(1000)();

const secondUpload = await uploadImage({
public_id: PUBLIC_ID_BACKUP_1,
backup: true,
angle: '0', // To create a unique version
overwrite: true
});
await wait(1000)();

const thirdUpload = await uploadImage({
public_id: PUBLIC_ID_BACKUP_1,
backup: true,
angle: '100', // To create a unique version
overwrite: true
});
await wait(1000)();

// Ensure all files were uploaded correctly
expect(firstUpload).not.to.be(null);
expect(secondUpload).not.to.be(null);
expect(thirdUpload).not.to.be(null);

// Get the asset ID and versions of the uploaded asset
const resourceResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true});
const assetId = resourceResp.asset_id;
const firstAssetVersion = resourceResp.versions[0].version_id;
const secondAssetVersion = resourceResp.versions[1].version_id;
const thirdAssetVersion = resourceResp.versions[2].version_id;

// Delete the first version
const removeSingleVersion = await cloudinary.v2.api.delete_backed_up_assets(assetId, firstAssetVersion);
const removeSingleVersionResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true});
expect(removeSingleVersionResp.versions).not.to.contain(firstAssetVersion);

// Delete the remaining two versions
const removeMultipleVersions = await cloudinary.v2.api.delete_backed_up_assets(assetId, [secondAssetVersion, thirdAssetVersion]);
const removeMultipleVersionsResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true});
expect(removeMultipleVersionsResp.versions).not.to.contain(secondAssetVersion);
expect(removeMultipleVersionsResp.versions).not.to.contain(thirdAssetVersion);

// Cleanup,
const finalDeleteResp = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]);
expect(finalDeleteResp).to.have.property("deleted");
});
});
describe("update", function () {
describe("notification url", function () {
var writeSpy, xhr;
Expand Down