Skip to content

Commit ee131e6

Browse files
chore: api restore methods are tested as unit tests (#718)
1 parent b7868de commit ee131e6

File tree

2 files changed

+73
-283
lines changed

2 files changed

+73
-283
lines changed

test/integration/api/admin/api_spec.js

Lines changed: 0 additions & 283 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,291 +1350,8 @@ describe("api", function () {
13501350
});
13511351
});
13521352
});
1353-
describe('.restore', function () {
1354-
it.skip('should restore different versions of a deleted asset', async function () {
1355-
this.timeout(TIMEOUT.LARGE);
1356-
1357-
// Upload the same file twice (upload->delete->upload->delete)
1358-
1359-
// Upload and delete a file
1360-
const firstUpload = await uploadImage({
1361-
public_id: PUBLIC_ID_BACKUP_1,
1362-
backup: true
1363-
});
1364-
await wait(1000)();
1365-
1366-
const firstDelete = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]);
1367-
1368-
1369-
// Upload and delete it again, this time add angle to create a different 'version'
1370-
const secondUpload = await uploadImage({
1371-
public_id: PUBLIC_ID_BACKUP_1,
1372-
backup: true,
1373-
angle: '0'
1374-
});
1375-
await wait(1000)();
1376-
1377-
const secondDelete = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]);
1378-
await wait(1000)();
1379-
1380-
// Sanity, ensure these uploads are different before we continue
1381-
expect(firstUpload.bytes).not.to.equal(secondUpload.bytes);
1382-
1383-
// Ensure all files were uploaded correctly
1384-
expect(firstUpload).not.to.be(null);
1385-
expect(secondUpload).not.to.be(null);
1386-
1387-
// Ensure all files were deleted correctly
1388-
expect(firstDelete).to.have.property("deleted");
1389-
expect(secondDelete).to.have.property("deleted");
1390-
1391-
// Get the versions of the deleted asset
1392-
const getVersionsResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true});
1393-
1394-
const firstAssetVersion = getVersionsResp.versions[0].version_id;
1395-
const secondAssetVersion = getVersionsResp.versions[1].version_id;
1396-
1397-
// Restore first version, ensure it's equal to the upload size
1398-
await wait(2000)();
1399-
const firstVerRestore = await API_V2.restore([PUBLIC_ID_BACKUP_1], {versions: [firstAssetVersion]});
1400-
expect(firstVerRestore[PUBLIC_ID_BACKUP_1].bytes).to.eql(firstUpload.bytes);
1401-
1402-
// Restore second version, ensure it's equal to the upload size
1403-
await wait(2000)();
1404-
const secondVerRestore = await API_V2.restore([PUBLIC_ID_BACKUP_1], {versions: [secondAssetVersion]});
1405-
expect(secondVerRestore[PUBLIC_ID_BACKUP_1].bytes).to.eql(secondUpload.bytes);
1406-
1407-
// Cleanup,
1408-
const finalDeleteResp = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]);
1409-
expect(finalDeleteResp).to.have.property("deleted");
1410-
});
1411-
1412-
it.skip('should restore two different deleted assets', async () => {
1413-
this.timeout(TIMEOUT.LARGE);
1414-
1415-
// Upload two different files
1416-
const firstUpload = await uploadImage({
1417-
public_id: PUBLIC_ID_BACKUP_1,
1418-
backup: true
1419-
});
1420-
const secondUpload = await uploadImage({
1421-
public_id: PUBLIC_ID_BACKUP_2,
1422-
backup: true,
1423-
angle: '0'
1424-
});
1425-
1426-
// delete both resources
1427-
const deleteAll = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2]);
1428-
1429-
// Expect correct deletion of the assets
1430-
expect(deleteAll.deleted[PUBLIC_ID_BACKUP_1]).to.be("deleted");
1431-
expect(deleteAll.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted");
1432-
1433-
const getFirstAssetVersion = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true});
1434-
const getSecondAssetVersion = await API_V2.resource(PUBLIC_ID_BACKUP_2, {versions: true});
1435-
1436-
const firstAssetVersion = getFirstAssetVersion.versions[0].version_id;
1437-
const secondAssetVersion = getSecondAssetVersion.versions[0].version_id;
1438-
1439-
const IDS_TO_RESTORE = [PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2];
1440-
const VERSIONS_TO_RESTORE = [firstAssetVersion, secondAssetVersion];
1441-
1442-
const restore = await API_V2.restore(IDS_TO_RESTORE, {versions: VERSIONS_TO_RESTORE});
1443-
1444-
// Expect correct restorations
1445-
expect(restore[PUBLIC_ID_BACKUP_1].bytes).to.equal(firstUpload.bytes);
1446-
expect(restore[PUBLIC_ID_BACKUP_2].bytes).to.equal(secondUpload.bytes);
1447-
1448-
// Cleanup
1449-
const finalDelete = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2]);
1450-
// Expect correct deletion of the assets
1451-
expect(finalDelete.deleted[PUBLIC_ID_BACKUP_1]).to.be("deleted");
1452-
expect(finalDelete.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted");
1453-
});
1454-
});
1455-
1456-
describe("restore_by_asset_ids", function () {
1457-
this.timeout(TIMEOUT.MEDIUM);
1458-
1459-
const publicId = "api_test_restore" + UNIQUE_JOB_SUFFIX_ID;
1460-
let uploadedAssetId;
1461-
1462-
before(() => uploadImage({
1463-
public_id: publicId,
1464-
backup: true,
1465-
tags: UPLOAD_TAGS
1466-
})
1467-
.then(wait(2000))
1468-
.then(() => cloudinary.v2.api.resource(publicId))
1469-
.then((resource) => {
1470-
uploadedAssetId = resource.asset_id;
1471-
expect(resource).not.to.be(null);
1472-
expect(resource.bytes).to.eql(3381);
1473-
return cloudinary.v2.api.delete_resources(publicId);
1474-
})
1475-
.then(() => cloudinary.v2.api.resource(publicId))
1476-
.then((resource) => {
1477-
expect(resource).not.to.be(null);
1478-
expect(resource.bytes).to.eql(0);
1479-
expect(resource.placeholder).to.eql(true);
1480-
})
1481-
);
1482-
1483-
it("should restore a deleted resource when passed an asset ID", () => cloudinary.v2.api
1484-
.restore_by_asset_ids([uploadedAssetId])
1485-
.then((response) => {
1486-
let info = response[uploadedAssetId];
1487-
expect(info).not.to.be(null);
1488-
expect(info.bytes).to.eql(3381);
1489-
return cloudinary.v2.api.resources_by_asset_ids([uploadedAssetId]);
1490-
})
1491-
.then((response) => {
1492-
const { resources } = response;
1493-
expect(resources[0]).not.to.be(null);
1494-
expect(resources[0].bytes).to.eql(3381);
1495-
}));
1496-
1497-
it.skip("should restore different versions of a deleted asset when passed an asset ID", async function () {
1498-
this.timeout(TIMEOUT.LARGE);
1499-
1500-
// Upload the same file twice (upload->delete->upload->delete)
1501-
1502-
// Upload and delete a file
1503-
const firstUpload = await uploadImage({
1504-
public_id: PUBLIC_ID_BACKUP_1,
1505-
backup: true
1506-
});
1507-
await wait(1000)();
1508-
1509-
const firstDelete = await API_V2.delete_resources([
1510-
PUBLIC_ID_BACKUP_1
1511-
]);
1512-
1513-
// Upload and delete it again, this time add angle to create a different 'version'
1514-
const secondUpload = await uploadImage({
1515-
public_id: PUBLIC_ID_BACKUP_1,
1516-
backup: true,
1517-
angle: "0"
1518-
});
1519-
await wait(1000)();
1520-
1521-
const secondDelete = await API_V2.delete_resources([
1522-
PUBLIC_ID_BACKUP_1
1523-
]);
1524-
await wait(1000)();
1525-
1526-
// Sanity, ensure these uploads are different before we continue
1527-
expect(firstUpload.bytes).not.to.equal(secondUpload.bytes);
1528-
1529-
// Ensure all files were uploaded correctly
1530-
expect(firstUpload).not.to.be(null);
1531-
expect(secondUpload).not.to.be(null);
1532-
1533-
// Ensure all files were deleted correctly
1534-
expect(firstDelete).to.have.property("deleted");
1535-
expect(secondDelete).to.have.property("deleted");
1536-
1537-
// Get the asset ID and versions of the deleted asset
1538-
const getVersionsResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {
1539-
versions: true
1540-
});
1541-
const assetId = getVersionsResp.asset_id;
1542-
1543-
const firstAssetVersion = getVersionsResp.versions[0].version_id;
1544-
const secondAssetVersion = getVersionsResp.versions[1].version_id;
1545-
1546-
// Restore first version by passing in the asset ID, ensure it's equal to the upload size
1547-
await wait(1000)();
1548-
const firstVerRestore = await API_V2.restore_by_asset_ids([assetId], {
1549-
versions: [firstAssetVersion]
1550-
});
15511353

1552-
expect(firstVerRestore[assetId].bytes).to.eql(
1553-
firstUpload.bytes
1554-
);
1555-
1556-
// Restore second version by passing in the asset ID, ensure it's equal to the upload size
1557-
await wait(1000)();
1558-
const secondVerRestore = await API_V2.restore_by_asset_ids(
1559-
[assetId],
1560-
{ versions: [secondAssetVersion] }
1561-
);
1562-
expect(secondVerRestore[assetId].bytes).to.eql(
1563-
secondUpload.bytes
1564-
);
1565-
1566-
// Cleanup,
1567-
const finalDeleteResp = await API_V2.delete_resources([
1568-
PUBLIC_ID_BACKUP_1
1569-
]);
1570-
expect(finalDeleteResp).to.have.property("deleted");
1571-
});
15721354

1573-
it("should restore two different deleted assets when passed asset IDs", async () => {
1574-
// Upload two different files
1575-
const firstUpload = await uploadImage({
1576-
public_id: PUBLIC_ID_BACKUP_1,
1577-
backup: true
1578-
});
1579-
const secondUpload = await uploadImage({
1580-
public_id: PUBLIC_ID_BACKUP_2,
1581-
backup: true,
1582-
angle: "0"
1583-
});
1584-
1585-
// delete both resources
1586-
const deleteAll = await API_V2.delete_resources([
1587-
PUBLIC_ID_BACKUP_1,
1588-
PUBLIC_ID_BACKUP_2
1589-
]);
1590-
1591-
// Expect correct deletion of the assets
1592-
expect(deleteAll.deleted[PUBLIC_ID_BACKUP_1]).to.be("deleted");
1593-
expect(deleteAll.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted");
1594-
1595-
const getFirstAssetVersion = await API_V2.resource(
1596-
PUBLIC_ID_BACKUP_1,
1597-
{ versions: true }
1598-
);
1599-
1600-
const getSecondAssetVersion = await API_V2.resource(
1601-
PUBLIC_ID_BACKUP_2,
1602-
{ versions: true }
1603-
);
1604-
1605-
const firstAssetId = getFirstAssetVersion.asset_id;
1606-
const secondAssetId = getSecondAssetVersion.asset_id;
1607-
1608-
const firstAssetVersion =
1609-
getFirstAssetVersion.versions[0].version_id;
1610-
const secondAssetVersion =
1611-
getSecondAssetVersion.versions[0].version_id;
1612-
1613-
const IDS_TO_RESTORE = [firstAssetId, secondAssetId];
1614-
const VERSIONS_TO_RESTORE = [firstAssetVersion, secondAssetVersion];
1615-
1616-
const restore = await API_V2.restore_by_asset_ids(IDS_TO_RESTORE, {
1617-
versions: VERSIONS_TO_RESTORE
1618-
});
1619-
1620-
// Expect correct restorations
1621-
expect(restore[firstAssetId].bytes).to.equal(
1622-
firstUpload.bytes
1623-
);
1624-
expect(restore[secondAssetId].bytes).to.equal(
1625-
secondUpload.bytes
1626-
);
1627-
1628-
// Cleanup
1629-
const finalDelete = await API_V2.delete_resources([
1630-
PUBLIC_ID_BACKUP_1,
1631-
PUBLIC_ID_BACKUP_2
1632-
]);
1633-
// Expect correct deletion of the assets
1634-
expect(finalDelete.deleted[PUBLIC_ID_BACKUP_1]).to.be("deleted");
1635-
expect(finalDelete.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted");
1636-
});
1637-
});
16381355

16391356

16401357
describe('mapping', function () {

test/unit/api_restore_spec.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const assert = require('assert');
2+
const sinon = require('sinon');
3+
4+
const cloudinary = require('../../lib/cloudinary');
5+
const createTestConfig = require('../testUtils/createTestConfig');
6+
const helper = require('../spechelper');
7+
const api_http = require("https");
8+
const ClientRequest = require('_http_client').ClientRequest;
9+
10+
describe('api restore handlers', function () {
11+
const mocked = {};
12+
13+
beforeEach(function () {
14+
cloudinary.config(createTestConfig());
15+
mocked.xhr = sinon.useFakeXMLHttpRequest();
16+
mocked.write = sinon.spy(ClientRequest.prototype, 'write');
17+
mocked.request = sinon.spy(api_http, 'request');
18+
});
19+
20+
afterEach(function () {
21+
mocked.request.restore();
22+
mocked.write.restore();
23+
mocked.xhr.restore();
24+
});
25+
26+
describe('.restore', function () {
27+
it('sends public_ids and versions with JSON payload', function () {
28+
const options = {
29+
resource_type: 'video',
30+
type: 'authenticated',
31+
versions: ['ver-1', 'ver-2']
32+
};
33+
34+
cloudinary.v2.api.restore(['pub-1', 'pub-2'], options);
35+
36+
sinon.assert.calledWith(mocked.request, sinon.match({
37+
pathname: sinon.match('resources/video/authenticated/restore'),
38+
method: sinon.match('POST')
39+
}));
40+
41+
sinon.assert.calledWith(mocked.write, sinon.match(helper.apiJsonParamMatcher('public_ids', ['pub-1', 'pub-2'])));
42+
sinon.assert.calledWith(mocked.write, sinon.match(helper.apiJsonParamMatcher('versions', ['ver-1', 'ver-2'])));
43+
});
44+
});
45+
46+
describe('.restore_by_asset_ids', function () {
47+
it('sends asset_ids and versions with JSON payload', function () {
48+
const options = { versions: ['ver-3'] };
49+
const assetIds = ['asset-1', 'asset-2'];
50+
51+
cloudinary.v2.api.restore_by_asset_ids(assetIds, options);
52+
53+
sinon.assert.calledWith(mocked.request, sinon.match({
54+
pathname: sinon.match('resources/restore'),
55+
method: sinon.match('POST')
56+
}));
57+
58+
sinon.assert.calledWith(mocked.write, sinon.match(helper.apiJsonParamMatcher('asset_ids', assetIds)));
59+
sinon.assert.calledWith(mocked.write, sinon.match(helper.apiJsonParamMatcher('versions', ['ver-3'])));
60+
});
61+
62+
it('wraps a single asset id into an array before calling the API', function () {
63+
cloudinary.v2.api.restore_by_asset_ids('single-asset-id');
64+
65+
sinon.assert.calledWith(mocked.request, sinon.match({
66+
pathname: sinon.match('resources/restore'),
67+
method: sinon.match('POST')
68+
}));
69+
70+
sinon.assert.calledWith(mocked.write, sinon.match(helper.apiJsonParamMatcher('asset_ids', ['single-asset-id'])));
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)