Skip to content

Commit dba140a

Browse files
committed
fix bugs and add test cases
1 parent 0363e7e commit dba140a

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

lib/imagekitio/api_service/file.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def upload(file: nil, file_name: nil, **options)
4040
else
4141
headers = @req_obj.create_headers
4242
payload = {multipart: true, file: file, fileName: file_name}.merge(options)
43-
4443
url = "#{constants.BASE_URL}#{constants.UPLOAD}"
4544
@req_obj.request("post", url, headers, payload)
4645
end
@@ -155,7 +154,7 @@ def rename(file_path: nil, new_file_name: nil, **options)
155154
raise ArgumentError, 'parameters required'
156155
end
157156
url = "#{constants.BASE_URL}/rename"
158-
payload = { 'filePath': file_path, 'newFileName': new_file_name }.merge(request_formatter(options))
157+
payload = { 'filePath': file_path, 'newFileName': new_file_name }.merge(request_formatter(options)).to_json
159158
@req_obj.request('put', url, @req_obj.create_headers, payload)
160159
end
161160
end

lib/imagekitio/constant.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class << self
2424
def method_missing(symbol, *args)
2525
method_name = symbol.to_s.gsub('=', '')
2626
if self.const_defined? method_name
27-
return self.const_get(method_name) unless args.present?
27+
return self.const_get(method_name) if args.empty?
28+
2829
self.const_set(method_name, args.first)
2930
else
3031
super

test/imagekit/api_service/file_test.rb

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,60 @@
107107

108108
end
109109

110+
it "test_upload_with_valid_expected_success_with_extensions" do
111+
request_obj = double
112+
allow(ImageKitIo::Request)
113+
.to receive(:new)
114+
.with(private_key, public_key, url_endpoint)
115+
.and_return(request_obj)
116+
117+
allow(request_obj)
118+
.to receive(:create_headers)
119+
.and_return({})
120+
@ac={}
121+
allow(request_obj)
122+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
123+
.and_return({code: 200})
124+
SUT = file_api_service.new(request_obj)
125+
upload = SUT.upload(file: "./fake_file.jpg", file_name: "my_file_name", extensions: [
126+
{
127+
name: 'remove-bg',
128+
options: {
129+
add_shadow: true
130+
}
131+
}
132+
])
133+
expect(@ac[:payload]['extensions']).to eq("[{\"name\":\"remove-bg\",\"options\":{\"add_shadow\":true}}]")
134+
expect(upload[:code]).to eq(200)
135+
end
136+
137+
it "test_upload_with_valid_expected_success_with_customMetadata" do
138+
request_obj = double
139+
allow(ImageKitIo::Request)
140+
.to receive(:new)
141+
.with(private_key, public_key, url_endpoint)
142+
.and_return(request_obj)
143+
144+
allow(request_obj)
145+
.to receive(:create_headers)
146+
.and_return({})
147+
@ac={}
148+
allow(request_obj)
149+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
150+
.and_return({code: 200})
151+
SUT = file_api_service.new(request_obj)
152+
upload = SUT.upload(
153+
file: "./fake_file.jpg",
154+
file_name: "my_file_name",
155+
custom_metadata: {
156+
brand: 'Nike',
157+
color: 'red'
158+
}
159+
)
160+
expect(@ac[:payload]['customMetadata']).to eq("{\"brand\":\"Nike\",\"color\":\"red\"}")
161+
expect(upload[:code]).to eq(200)
162+
end
163+
110164
it "test_upload_with_valid_expected_success" do
111165
request_obj = double
112166
allow(ImageKitIo::Request)
@@ -597,6 +651,58 @@
597651
SUT.update_details(file_id: "file_id", **options)
598652
}.to raise_error(ArgumentError)
599653
end
654+
655+
it "test_update_file_details_expected_success_with_extensions" do
656+
request_obj = double
657+
allow(ImageKitIo::Request)
658+
.to receive(:new)
659+
.with(private_key, public_key, url_endpoint)
660+
.and_return(request_obj)
661+
662+
allow(request_obj)
663+
.to receive(:create_headers)
664+
.and_return({})
665+
@ac={}
666+
allow(request_obj)
667+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
668+
.and_return({code: 200})
669+
SUT = file_api_service.new(request_obj)
670+
upload = SUT.update_details(file_id: "file_id_xyz", extensions: [
671+
{
672+
'name': 'google-auto-tagging',
673+
'maxTags': 5,
674+
'minConfidence': 95
675+
}
676+
])
677+
expect(@ac[:payload]).to eq("{\"extensions\":[{\"name\":\"google-auto-tagging\",\"maxTags\":5,\"minConfidence\":95}]}")
678+
expect(upload[:code]).to eq(200)
679+
end
680+
681+
it "test_update_with_valid_expected_success_with_customMetadata" do
682+
request_obj = double
683+
allow(ImageKitIo::Request)
684+
.to receive(:new)
685+
.with(private_key, public_key, url_endpoint)
686+
.and_return(request_obj)
687+
688+
allow(request_obj)
689+
.to receive(:create_headers)
690+
.and_return({})
691+
@ac={}
692+
allow(request_obj)
693+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
694+
.and_return({code: 200})
695+
SUT = file_api_service.new(request_obj)
696+
upload = SUT.update_details(
697+
file_id: "file_id_xyz",
698+
custom_metadata: {
699+
brand: 'Nike',
700+
color: 'red'
701+
}
702+
)
703+
expect(@ac[:payload]).to eq("{\"customMetadata\":{\"brand\":\"Nike\",\"color\":\"red\"}}")
704+
expect(upload[:code]).to eq(200)
705+
end
600706
end
601707

602708
describe 'TestGetRemoteFileURLMetaData' do
@@ -755,12 +861,12 @@
755861

756862
end
757863

758-
it 'test_move' do
864+
it 'test_rename' do
759865
source_file = 'test/dummy.png'
760866
new_name = 'my_image.png'
761867
resp = @sut.rename(file_path: source_file, new_file_name: new_name)
762868
expect(@ac[:url]).to eq("https://api.imagekit.io/v1/files/rename")
763-
expect(@ac[:payload]).to eq({:filePath=>"test/dummy.png", :newFileName=>"my_image.png"})
869+
expect(@ac[:payload]).to eq("{\"filePath\":\"test/dummy.png\",\"newFileName\":\"my_image.png\"}")
764870
expect(@ac[:method]).to eq('put')
765871
expect(resp[:code]).to eq(200)
766872
end

0 commit comments

Comments
 (0)