|
4 | 4 | describe Cloudinary::Api do |
5 | 5 | break puts("Please setup environment for api test to run") if Cloudinary.config.api_secret.blank? |
6 | 6 | include_context "cleanup", TIMESTAMP_TAG |
| 7 | + |
| 8 | + prefix = "api_test_#{SUFFIX}" |
| 9 | + |
7 | 10 | TEST_WIDTH = rand(1000) |
8 | 11 | TEST_TRANSFOMATION = "c_scale,w_#{TEST_WIDTH}" |
9 | | - prefix = "api_test_#{SUFFIX}" |
| 12 | + PUBLIC_ID_BACKUP_1 = "#{prefix}backup_1#{Time.now.to_i}" |
| 13 | + PUBLIC_ID_BACKUP_2 = "#{prefix}backup_2#{Time.now.to_i}" |
| 14 | + |
10 | 15 | test_id_1 = "#{prefix}_1" |
11 | 16 | test_id_2 = "#{prefix}_2" |
12 | 17 | test_id_3 = "#{prefix}_3" |
|
230 | 235 | expect(tags).to be_blank |
231 | 236 | end |
232 | 237 |
|
| 238 | + describe "backup resource" do |
| 239 | + let(:public_id) { "api_test_backup_#{SUFFIX}" } |
| 240 | + |
| 241 | + before(:each) do |
| 242 | + Cloudinary::Uploader.upload(TEST_IMG, :tags => [TEST_TAG, TIMESTAMP_TAG], :public_id => public_id, :backup => true) |
| 243 | + response = @api.resource(public_id) |
| 244 | + expect(response).not_to be_nil |
| 245 | + end |
| 246 | + |
| 247 | + it "should return the asset details together with all of its backed up versions when versions is true" do |
| 248 | + resource = @api.resource(public_id, :versions => true) |
| 249 | + |
| 250 | + expect(resource["versions"]).to be_an_instance_of(Array) |
| 251 | + end |
| 252 | + |
| 253 | + it "should return the asset details together without backed up versions when versions is false" do |
| 254 | + resource = @api.resource(public_id, :versions => false) |
| 255 | + |
| 256 | + expect(resource["versions"]).to be_nil |
| 257 | + end |
| 258 | + end |
| 259 | + |
233 | 260 | describe 'transformations' do |
234 | 261 | it "should allow listing transformations" do |
235 | 262 | transformations = @api.transformations()["transformations"] |
|
507 | 534 | end |
508 | 535 |
|
509 | 536 | describe '.restore' do |
| 537 | + let(:public_id) { "api_test_restore#{SUFFIX}" } |
| 538 | + |
| 539 | + before(:each) do |
| 540 | + Cloudinary::Uploader.upload(TEST_IMG, :tags => [TEST_TAG, TIMESTAMP_TAG], :public_id => public_id, :backup => true) |
| 541 | + sleep(2) |
| 542 | + |
| 543 | + resource = @api.resource(public_id) |
| 544 | + expect(resource).not_to be_nil |
| 545 | + expect(resource["bytes"]).to eq(3381) |
| 546 | + |
| 547 | + @api.delete_resources(public_id) |
| 548 | + |
| 549 | + resource = @api.resource(public_id) |
| 550 | + expect(resource).not_to be_nil |
| 551 | + expect(resource["bytes"]).to eq(0) |
| 552 | + expect(resource["placeholder"]).to eq(true) |
| 553 | + end |
| 554 | + |
510 | 555 | it 'should restore a deleted resource' do |
511 | | - expect(RestClient::Request).to receive(:execute).with(deep_hash_value( [:payload, :public_ids] => "api_test_restore", [:url] => /.*\/restore$/)) |
512 | | - Cloudinary::Api.restore("api_test_restore") |
| 556 | + response = @api.restore([public_id]) |
| 557 | + |
| 558 | + info = response[public_id] |
| 559 | + expect(info).not_to be_nil |
| 560 | + expect(info["bytes"]).to eq(3381) |
| 561 | + |
| 562 | + resource = @api.resource(public_id) |
| 563 | + expect(resource).not_to be_nil |
| 564 | + expect(resource["bytes"]).to eq(3381) |
| 565 | + end |
| 566 | + |
| 567 | + it "should restore different versions of a deleted asset" do |
| 568 | + # Upload the same file twice (upload->delete->upload->delete) |
| 569 | + |
| 570 | + # Upload and delete a file |
| 571 | + first_upload = Cloudinary::Uploader.upload(TEST_IMG, :tags => [TEST_TAG, TIMESTAMP_TAG], :public_id => PUBLIC_ID_BACKUP_1, :backup => true) |
| 572 | + sleep(1) |
| 573 | + |
| 574 | + first_delete = @api.delete_resources([PUBLIC_ID_BACKUP_1]) |
| 575 | + |
| 576 | + # Upload and delete it again, this time add angle to create a different 'version' |
| 577 | + second_upload = Cloudinary::Uploader.upload(TEST_IMG, :tags => [TEST_TAG, TIMESTAMP_TAG], :public_id => PUBLIC_ID_BACKUP_1, :transformation => { :angle => 0 }, :backup => true) |
| 578 | + sleep(1) |
| 579 | + |
| 580 | + second_delete = @api.delete_resources([PUBLIC_ID_BACKUP_1]) |
| 581 | + sleep(1) |
| 582 | + |
| 583 | + # Ensure all files were uploaded correctly |
| 584 | + expect(first_upload).not_to be_nil |
| 585 | + expect(second_upload).not_to be_nil |
| 586 | + |
| 587 | + # Sanity, ensure these uploads are different before we continue |
| 588 | + expect(first_upload["bytes"]).not_to equal(second_upload["bytes"]) |
| 589 | + |
| 590 | + # Ensure all files were deleted correctly |
| 591 | + expect(first_delete).to have_key("deleted") |
| 592 | + expect(second_delete).to have_key("deleted") |
| 593 | + |
| 594 | + # Get the versions of the deleted asset |
| 595 | + get_versions_resp = @api.resource(PUBLIC_ID_BACKUP_1, :versions => true) |
| 596 | + |
| 597 | + first_asset_version = get_versions_resp["versions"][0]["version_id"] |
| 598 | + second_asset_version = get_versions_resp["versions"][1]["version_id"] |
| 599 | + |
| 600 | + # Restore first version, ensure it's equal to the upload size |
| 601 | + sleep(1) |
| 602 | + first_ver_restore = @api.restore([PUBLIC_ID_BACKUP_1], :versions => [first_asset_version]) |
| 603 | + expect(first_ver_restore[PUBLIC_ID_BACKUP_1]["bytes"]).to eq(first_upload["bytes"]) |
| 604 | + |
| 605 | + # Restore second version, ensure it's equal to the upload size |
| 606 | + sleep(1) |
| 607 | + second_ver_restore = @api.restore([PUBLIC_ID_BACKUP_1], { :versions => [second_asset_version] }) |
| 608 | + expect(second_ver_restore[PUBLIC_ID_BACKUP_1]["bytes"]).to eq(second_upload["bytes"]) |
| 609 | + |
| 610 | + # Cleanup |
| 611 | + final_delete_resp = @api.delete_resources([PUBLIC_ID_BACKUP_1]) |
| 612 | + expect(final_delete_resp).to have_key("deleted") |
| 613 | + end |
| 614 | + |
| 615 | + it "should restore two different deleted assets" do |
| 616 | + # Upload two different files |
| 617 | + first_upload = Cloudinary::Uploader.upload(TEST_IMG, :tags => [TEST_TAG, TIMESTAMP_TAG], :public_id => PUBLIC_ID_BACKUP_1, :backup => true) |
| 618 | + second_upload = Cloudinary::Uploader.upload(TEST_IMG, :tags => [TEST_TAG, TIMESTAMP_TAG], :public_id => PUBLIC_ID_BACKUP_2, :transformation => { :angle => 0 }, :backup => true) |
| 619 | + |
| 620 | + # delete both resources |
| 621 | + delete_all = @api.delete_resources([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2]) |
| 622 | + |
| 623 | + # Expect correct deletion of the assets |
| 624 | + expect(delete_all["deleted"][PUBLIC_ID_BACKUP_1]).to eq("deleted") |
| 625 | + expect(delete_all["deleted"][PUBLIC_ID_BACKUP_2]).to eq("deleted") |
| 626 | + |
| 627 | + get_first_asset_version = @api.resource(PUBLIC_ID_BACKUP_1, :versions => true) |
| 628 | + get_second_asset_version = @api.resource(PUBLIC_ID_BACKUP_2, :versions => true) |
| 629 | + |
| 630 | + first_asset_version = get_first_asset_version["versions"][0]["version_id"] |
| 631 | + second_asset_version = get_second_asset_version["versions"][0]["version_id"] |
| 632 | + |
| 633 | + ids_to_restore = [PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2] |
| 634 | + versions_to_restore = [first_asset_version, second_asset_version] |
| 635 | + |
| 636 | + restore = @api.restore(ids_to_restore, :versions => versions_to_restore) |
| 637 | + |
| 638 | + # Expect correct restorations |
| 639 | + expect(restore[PUBLIC_ID_BACKUP_1]["bytes"]).to eq(first_upload["bytes"]) |
| 640 | + expect(restore[PUBLIC_ID_BACKUP_2]["bytes"]).to eq(second_upload["bytes"]) |
| 641 | + |
| 642 | + # Cleanup |
| 643 | + final_delete = @api.delete_resources([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2]) |
| 644 | + |
| 645 | + # Expect correct deletion of the assets |
| 646 | + expect(final_delete["deleted"][PUBLIC_ID_BACKUP_1]).to eq("deleted") |
| 647 | + expect(final_delete["deleted"][PUBLIC_ID_BACKUP_2]).to eq("deleted") |
513 | 648 | end |
514 | 649 | end |
515 | 650 |
|
|
0 commit comments