|
| 1 | +/* eslint-disable no-tabs */ |
1 | 2 | const sinon = require('sinon'); |
2 | 3 | const formatDate = require("date-fns").format; |
3 | 4 | const subDate = require("date-fns").sub; |
@@ -1431,6 +1432,191 @@ describe("api", function () { |
1431 | 1432 | expect(finalDelete.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted"); |
1432 | 1433 | }); |
1433 | 1434 | }); |
| 1435 | + |
| 1436 | + describe("restore_by_asset_ids", function () { |
| 1437 | + this.timeout(TIMEOUT.MEDIUM); |
| 1438 | + |
| 1439 | + const publicId = "api_test_restore" + UNIQUE_JOB_SUFFIX_ID; |
| 1440 | + let uploadedAssetId; |
| 1441 | + |
| 1442 | + before(() => uploadImage({ |
| 1443 | + public_id: publicId, |
| 1444 | + backup: true, |
| 1445 | + tags: UPLOAD_TAGS |
| 1446 | + }) |
| 1447 | + .then(wait(2000)) |
| 1448 | + .then(() => cloudinary.v2.api.resource(publicId)) |
| 1449 | + .then((resource) => { |
| 1450 | + uploadedAssetId = resource.asset_id; |
| 1451 | + expect(resource).not.to.be(null); |
| 1452 | + expect(resource.bytes).to.eql(3381); |
| 1453 | + return cloudinary.v2.api.delete_resources(publicId); |
| 1454 | + }) |
| 1455 | + .then(() => cloudinary.v2.api.resource(publicId)) |
| 1456 | + .then((resource) => { |
| 1457 | + expect(resource).not.to.be(null); |
| 1458 | + expect(resource.bytes).to.eql(0); |
| 1459 | + expect(resource.placeholder).to.eql(true); |
| 1460 | + }) |
| 1461 | + ); |
| 1462 | + |
| 1463 | + it("should restore a deleted resource when passed an asset ID", () => cloudinary.v2.api |
| 1464 | + .restore_by_asset_ids([uploadedAssetId]) |
| 1465 | + .then((response) => { |
| 1466 | + let info = response[uploadedAssetId]; |
| 1467 | + expect(info).not.to.be(null); |
| 1468 | + expect(info.bytes).to.eql(3381); |
| 1469 | + return cloudinary.v2.api.resources_by_asset_ids([uploadedAssetId]); |
| 1470 | + }) |
| 1471 | + .then((response) => { |
| 1472 | + const { resources } = response; |
| 1473 | + expect(resources[0]).not.to.be(null); |
| 1474 | + expect(resources[0].bytes).to.eql(3381); |
| 1475 | + })); |
| 1476 | + |
| 1477 | + it.skip("should restore different versions of a deleted asset when passed an asset ID", async function () { |
| 1478 | + this.timeout(TIMEOUT.LARGE); |
| 1479 | + |
| 1480 | + // Upload the same file twice (upload->delete->upload->delete) |
| 1481 | + |
| 1482 | + // Upload and delete a file |
| 1483 | + const firstUpload = await uploadImage({ |
| 1484 | + public_id: PUBLIC_ID_BACKUP_1, |
| 1485 | + backup: true |
| 1486 | + }); |
| 1487 | + await wait(1000)(); |
| 1488 | + |
| 1489 | + const firstDelete = await API_V2.delete_resources([ |
| 1490 | + PUBLIC_ID_BACKUP_1 |
| 1491 | + ]); |
| 1492 | + |
| 1493 | + // Upload and delete it again, this time add angle to create a different 'version' |
| 1494 | + const secondUpload = await uploadImage({ |
| 1495 | + public_id: PUBLIC_ID_BACKUP_1, |
| 1496 | + backup: true, |
| 1497 | + angle: "0" |
| 1498 | + }); |
| 1499 | + await wait(1000)(); |
| 1500 | + |
| 1501 | + const secondDelete = await API_V2.delete_resources([ |
| 1502 | + PUBLIC_ID_BACKUP_1 |
| 1503 | + ]); |
| 1504 | + await wait(1000)(); |
| 1505 | + |
| 1506 | + // Sanity, ensure these uploads are different before we continue |
| 1507 | + expect(firstUpload.bytes).not.to.equal(secondUpload.bytes); |
| 1508 | + |
| 1509 | + // Ensure all files were uploaded correctly |
| 1510 | + expect(firstUpload).not.to.be(null); |
| 1511 | + expect(secondUpload).not.to.be(null); |
| 1512 | + |
| 1513 | + // Ensure all files were deleted correctly |
| 1514 | + expect(firstDelete).to.have.property("deleted"); |
| 1515 | + expect(secondDelete).to.have.property("deleted"); |
| 1516 | + |
| 1517 | + // Get the asset ID and versions of the deleted asset |
| 1518 | + const getVersionsResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, { |
| 1519 | + versions: true |
| 1520 | + }); |
| 1521 | + const assetId = getVersionsResp.asset_id; |
| 1522 | + |
| 1523 | + const firstAssetVersion = getVersionsResp.versions[0].version_id; |
| 1524 | + const secondAssetVersion = getVersionsResp.versions[1].version_id; |
| 1525 | + |
| 1526 | + // Restore first version by passing in the asset ID, ensure it's equal to the upload size |
| 1527 | + await wait(1000)(); |
| 1528 | + const firstVerRestore = await API_V2.restore_by_asset_ids([assetId], { |
| 1529 | + versions: [firstAssetVersion] |
| 1530 | + }); |
| 1531 | + |
| 1532 | + expect(firstVerRestore[assetId].bytes).to.eql( |
| 1533 | + firstUpload.bytes |
| 1534 | + ); |
| 1535 | + |
| 1536 | + // Restore second version by passing in the asset ID, ensure it's equal to the upload size |
| 1537 | + await wait(1000)(); |
| 1538 | + const secondVerRestore = await API_V2.restore_by_asset_ids( |
| 1539 | + [assetId], |
| 1540 | + { versions: [secondAssetVersion] } |
| 1541 | + ); |
| 1542 | + expect(secondVerRestore[assetId].bytes).to.eql( |
| 1543 | + secondUpload.bytes |
| 1544 | + ); |
| 1545 | + |
| 1546 | + // Cleanup, |
| 1547 | + const finalDeleteResp = await API_V2.delete_resources([ |
| 1548 | + PUBLIC_ID_BACKUP_1 |
| 1549 | + ]); |
| 1550 | + expect(finalDeleteResp).to.have.property("deleted"); |
| 1551 | + }); |
| 1552 | + |
| 1553 | + it("should restore two different deleted assets when passed asset IDs", async () => { |
| 1554 | + // Upload two different files |
| 1555 | + const firstUpload = await uploadImage({ |
| 1556 | + public_id: PUBLIC_ID_BACKUP_1, |
| 1557 | + backup: true |
| 1558 | + }); |
| 1559 | + const secondUpload = await uploadImage({ |
| 1560 | + public_id: PUBLIC_ID_BACKUP_2, |
| 1561 | + backup: true, |
| 1562 | + angle: "0" |
| 1563 | + }); |
| 1564 | + |
| 1565 | + // delete both resources |
| 1566 | + const deleteAll = await API_V2.delete_resources([ |
| 1567 | + PUBLIC_ID_BACKUP_1, |
| 1568 | + PUBLIC_ID_BACKUP_2 |
| 1569 | + ]); |
| 1570 | + |
| 1571 | + // Expect correct deletion of the assets |
| 1572 | + expect(deleteAll.deleted[PUBLIC_ID_BACKUP_1]).to.be("deleted"); |
| 1573 | + expect(deleteAll.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted"); |
| 1574 | + |
| 1575 | + const getFirstAssetVersion = await API_V2.resource( |
| 1576 | + PUBLIC_ID_BACKUP_1, |
| 1577 | + { versions: true } |
| 1578 | + ); |
| 1579 | + |
| 1580 | + const getSecondAssetVersion = await API_V2.resource( |
| 1581 | + PUBLIC_ID_BACKUP_2, |
| 1582 | + { versions: true } |
| 1583 | + ); |
| 1584 | + |
| 1585 | + const firstAssetId = getFirstAssetVersion.asset_id; |
| 1586 | + const secondAssetId = getSecondAssetVersion.asset_id; |
| 1587 | + |
| 1588 | + const firstAssetVersion = |
| 1589 | + getFirstAssetVersion.versions[0].version_id; |
| 1590 | + const secondAssetVersion = |
| 1591 | + getSecondAssetVersion.versions[0].version_id; |
| 1592 | + |
| 1593 | + const IDS_TO_RESTORE = [firstAssetId, secondAssetId]; |
| 1594 | + const VERSIONS_TO_RESTORE = [firstAssetVersion, secondAssetVersion]; |
| 1595 | + |
| 1596 | + const restore = await API_V2.restore_by_asset_ids(IDS_TO_RESTORE, { |
| 1597 | + versions: VERSIONS_TO_RESTORE |
| 1598 | + }); |
| 1599 | + |
| 1600 | + // Expect correct restorations |
| 1601 | + expect(restore[firstAssetId].bytes).to.equal( |
| 1602 | + firstUpload.bytes |
| 1603 | + ); |
| 1604 | + expect(restore[secondAssetId].bytes).to.equal( |
| 1605 | + secondUpload.bytes |
| 1606 | + ); |
| 1607 | + |
| 1608 | + // Cleanup |
| 1609 | + const finalDelete = await API_V2.delete_resources([ |
| 1610 | + PUBLIC_ID_BACKUP_1, |
| 1611 | + PUBLIC_ID_BACKUP_2 |
| 1612 | + ]); |
| 1613 | + // Expect correct deletion of the assets |
| 1614 | + expect(finalDelete.deleted[PUBLIC_ID_BACKUP_1]).to.be("deleted"); |
| 1615 | + expect(finalDelete.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted"); |
| 1616 | + }); |
| 1617 | + }); |
| 1618 | + |
| 1619 | + |
1434 | 1620 | describe('mapping', function () { |
1435 | 1621 | before(function () { |
1436 | 1622 | this.mapping = `api_test_upload_mapping${Math.floor(Math.random() * 100000)}`; |
|
0 commit comments