|
| 1 | +require_relative '../spec_helper' |
| 2 | + |
| 3 | +RSpec.describe ImageKitIo::ApiService::Bulk do |
| 4 | + let(:bulk_api_service) { described_class } |
| 5 | + let(:config) { ImageKitIo.config } |
| 6 | + let(:constants) { ImageKitIo.constants } |
| 7 | + let!(:private_key) { config.private_key } |
| 8 | + let!(:public_key) { config.public_key } |
| 9 | + let!(:url_endpoint) { config.url_endpoint } |
| 10 | + |
| 11 | + describe 'TestBatchDelete' do |
| 12 | + it "test_remove_files_fails_on_unauthenticated_request" do |
| 13 | + file_ids = %w[file_id_1 file_id_2] |
| 14 | + request_obj = double |
| 15 | + allow(ImageKitIo::Request) |
| 16 | + .to receive(:new) |
| 17 | + .with(private_key, public_key, url_endpoint) |
| 18 | + .and_return(request_obj) |
| 19 | + |
| 20 | + allow(request_obj) |
| 21 | + .to receive(:create_headers) |
| 22 | + .and_return({}) |
| 23 | + |
| 24 | + allow(request_obj) |
| 25 | + .to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}} |
| 26 | + .and_return({code: 401, body: {}}) |
| 27 | + |
| 28 | + SUT = bulk_api_service.new(request_obj) |
| 29 | + resp = SUT.remove_files(file_ids: file_ids) |
| 30 | + |
| 31 | + expect(@ac[:payload]).to eq({"fileIds": file_ids}.to_json) |
| 32 | + expect(resp[:code]).to eq(401) |
| 33 | + end |
| 34 | + |
| 35 | + it "test_remove_files_fails_on_item_not_found" do |
| 36 | + file_ids = %w[file_id_1 file_id_2] |
| 37 | + request_obj = double |
| 38 | + allow(ImageKitIo::Request) |
| 39 | + .to receive(:new) |
| 40 | + .with(private_key, public_key, url_endpoint) |
| 41 | + .and_return(request_obj) |
| 42 | + |
| 43 | + allow(request_obj) |
| 44 | + .to receive(:create_headers) |
| 45 | + .and_return({}) |
| 46 | + |
| 47 | + allow(request_obj) |
| 48 | + .to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}} |
| 49 | + .and_return({code: 404, body: {message: "The requested file(s) does not exist.", |
| 50 | + help: "For support kindly contact us at [email protected] .", |
| 51 | + missingFileIds: file_ids,},},) |
| 52 | + |
| 53 | + SUT = bulk_api_service.new(request_obj) |
| 54 | + resp = SUT.remove_files(file_ids: file_ids) |
| 55 | + |
| 56 | + expect(@ac[:payload]).to eq({"fileIds": file_ids}.to_json) |
| 57 | + expect(resp[:code]).to eq(404) |
| 58 | + expect(resp[:body][:missingFileIds]).to eq(file_ids) |
| 59 | + end |
| 60 | + |
| 61 | + it "test_remove_files_succeeds" do |
| 62 | + request_obj = double |
| 63 | + file_ids = %w[file_id_1 file_id_2] |
| 64 | + |
| 65 | + allow(ImageKitIo::Request) |
| 66 | + .to receive(:new) |
| 67 | + .with(private_key, public_key, url_endpoint) |
| 68 | + .and_return(request_obj) |
| 69 | + |
| 70 | + allow(request_obj) |
| 71 | + .to receive(:create_headers) |
| 72 | + .and_return({}) |
| 73 | + |
| 74 | + allow(request_obj) |
| 75 | + .to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}} |
| 76 | + .and_return({code: 200, body: {successfullyDeletedFileIds: file_ids}}) |
| 77 | + |
| 78 | + SUT = bulk_api_service.new(request_obj) |
| 79 | + resp = SUT.remove_files(file_ids: file_ids) |
| 80 | + |
| 81 | + expect(@ac[:payload]).to eq({"fileIds": file_ids}.to_json) |
| 82 | + expect(resp[:code]).to eq(200) |
| 83 | + expect(resp[:body][:successfullyDeletedFileIds]).to eq(file_ids) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + context 'batch tags' do |
| 88 | + let!(:req_obj) { double } |
| 89 | + let(:file_ids) { %[file_id_1 file_id_2] } |
| 90 | + |
| 91 | + before do |
| 92 | + allow(ImageKitIo::Request) |
| 93 | + .to receive(:new) |
| 94 | + .with(private_key, public_key, url_endpoint) |
| 95 | + .and_return(req_obj) |
| 96 | + allow(req_obj) |
| 97 | + .to receive(:create_headers) |
| 98 | + .and_return({}) |
| 99 | + allow(req_obj) |
| 100 | + .to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload: payload}} |
| 101 | + .and_return({code: 200, body: { successfullyUpdatedFileIds: file_ids }}) |
| 102 | + @sut = bulk_api_service.new(req_obj) |
| 103 | + end |
| 104 | + |
| 105 | + it 'test_add_tags' do |
| 106 | + resp = @sut.add_tags(file_ids: file_ids, tags: ['custom_tag']) |
| 107 | + expect(@ac[:url]).to eq("https://api.imagekit.io/v1/files/addTags") |
| 108 | + expect(@ac[:payload]).to eq("{\"fileIds\":\"file_id_1 file_id_2\",\"tags\":[\"custom_tag\"]}") |
| 109 | + expect(resp[:code]).to eq(200) |
| 110 | + end |
| 111 | + |
| 112 | + it 'test_remove_tags' do |
| 113 | + resp = @sut.remove_tags(file_ids: file_ids, tags: ['custom_tag_remove']) |
| 114 | + expect(@ac[:url]).to eq("https://api.imagekit.io/v1/files/removeTags") |
| 115 | + expect(@ac[:payload]).to eq("{\"fileIds\":\"file_id_1 file_id_2\",\"tags\":[\"custom_tag_remove\"]}") |
| 116 | + expect(resp[:code]).to eq(200) |
| 117 | + end |
| 118 | + |
| 119 | + it 'test_remove_ai_tags' do |
| 120 | + resp = @sut.remove_ai_tags(file_ids: file_ids, ai_tags: ['custom_ai_tag']) |
| 121 | + expect(@ac[:url]).to eq("https://api.imagekit.io/v1/files/removeAITags") |
| 122 | + expect(@ac[:payload]).to eq("{\"fileIds\":\"file_id_1 file_id_2\",\"AITags\":[\"custom_ai_tag\"]}") |
| 123 | + expect(resp[:code]).to eq(200) |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + context 'job status' do |
| 128 | + let(:req_obj) { double } |
| 129 | + let(:job_id) { '619e038e20b7ef03efc4eeb9' } |
| 130 | + |
| 131 | + before do |
| 132 | + allow(ImageKitIo::Request) |
| 133 | + .to receive(:new) |
| 134 | + .with(private_key, public_key, url_endpoint) |
| 135 | + .and_return(req_obj) |
| 136 | + allow(req_obj) |
| 137 | + .to receive(:create_headers) |
| 138 | + .and_return({}) |
| 139 | + response_body = {"jobId"=> job_id, "type"=>"MOVE_FOLDER", "status"=>"Completed"} |
| 140 | + allow(req_obj) |
| 141 | + .to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload: payload}} |
| 142 | + .and_return({code: 200, body: response_body }) |
| 143 | + @sut = bulk_api_service.new(req_obj) |
| 144 | + end |
| 145 | + |
| 146 | + it 'raises error when parameter id not given' do |
| 147 | + expect { |
| 148 | + @sut.job_status |
| 149 | + }.to raise_error(ArgumentError) |
| 150 | + end |
| 151 | + |
| 152 | + it 'test_job_status' do |
| 153 | + resp = @sut.job_status(job_id: job_id) |
| 154 | + expect(@ac[:url]).to eq("https://api.imagekit.io/v1/bulkJobs/#{job_id}") |
| 155 | + expect(@ac[:method]).to eq('get') |
| 156 | + expect(@ac[:payload]).to eq({ :jobId => job_id}) |
| 157 | + expect(resp[:body]['jobId']).to eq(job_id) |
| 158 | + expect(resp[:body]['status']).to eq('Completed') |
| 159 | + end |
| 160 | + end |
| 161 | +end |
0 commit comments