Skip to content

Commit b7a58c2

Browse files
authored
Merge pull request #16 from violetviolinist/no-transforms-fix
add test case for no transformations URL + improve code coverage
2 parents c9e6d65 + 764eb21 commit b7a58c2

File tree

15 files changed

+320
-40
lines changed

15 files changed

+320
-40
lines changed

.github/workflows/test.yml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ jobs:
88
runs-on: ubuntu-latest
99

1010
steps:
11-
- uses: actions/checkout@v2
12-
- name: Set up Ruby 2.6
13-
uses: actions/setup-ruby@v1
14-
with:
15-
ruby-version: 2.6.x
16-
- name: Build and test with Rake
17-
run: |
18-
gem install bundler
19-
bundle install --jobs 4 --retry 3
20-
bundle exec rake
11+
- uses: actions/checkout@v2
12+
- name: Set up Ruby 2.6
13+
uses: actions/setup-ruby@v1
14+
with:
15+
ruby-version: 2.6.x
16+
- name: Build and test with Rake
17+
run: |
18+
gem install bundler
19+
bundle install --jobs 4 --retry 3
20+
bundle exec rake
21+
# - name: Upload code coverage reports to codecov
22+
# uses: codecov/codecov-action@v1
23+
# with:
24+
# token: 58fd7bff-e88e-4f23-b940-811b12f20dcf # not required for public repos
25+
# # file: ./coverage.xml # optional
26+
# # files: ./coverage1.xml,./coverage2.xml # optional
27+
# # flags: unittests # optional
28+
# # name: codecov-umbrella # optional
29+
# # fail_ci_if_error: true # optional (default = false)

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.bundle/
22
log/*.log
33
pkg/
4+
coverage/
45
test/dummy/db/*.sqlite3
56
test/dummy/db/*.sqlite3-journal
67
test/dummy/db/*.sqlite3-*

Gemfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ gemspec
1717
gem "minitest", "~> 5.0"
1818
# gem "rake", "~> 12.0"
1919
# gem "rest-client", "~>2.1"
20-
gem "rspec"
20+
gem "rspec"
21+
22+
gem 'codecov', require: false, group: 'test'
23+
gem 'webmock', require: false, group: 'test'

lib/imagekit/constants/errors.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,9 @@
6969
'help': "Please pass two pHash values",}
7070
UNEQUAL_STRING_LENGTH = {'message': "Unequal pHash string length",
7171
'help': "For distance calculation, the two pHash strings must have equal length",}
72+
73+
MISSING_PRIVATE_KEY = "ImageKit private key missing"
74+
75+
MISSING_PUBLIC_KEY = "ImageKit public key missing"
76+
77+
MISSING_URL_ENDPOINT = "ImageKit URL Endpoint missing. Default URL Endpoint: https://ik.imagekit.io/<YOUR_IMAGEKIT_ID>/"

lib/imagekit/file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,4 @@ def validate_upload_options(options)
130130
end
131131
request_formatter(options)
132132
end
133-
end
133+
end

lib/imagekit/imagekit.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ class ImageKitClient
1616
attr_reader :file
1717

1818
def initialize(private_key, public_key, url_endpoint, transformation_pos = nil, options = nil)
19+
20+
unless(private_key.is_a?(String) && private_key.to_s.strip.length != 0)
21+
raise ArgumentError, MISSING_PRIVATE_KEY
22+
end
23+
unless(public_key.is_a?(String) && public_key.to_s.strip.length != 0)
24+
raise ArgumentError, MISSING_PUBLIC_KEY
25+
end
26+
unless(url_endpoint.is_a?(String) && url_endpoint.to_s.strip.length != 0)
27+
raise ArgumentError, MISSING_URL_ENDPOINT
28+
end
29+
1930
@private_key = private_key
2031
@public_key = public_key
2132
@url_endpoint = url_endpoint
@@ -92,8 +103,7 @@ def get_remote_file_url_metadata(remote_file_url = "")
92103
def phash_distance(first, second)
93104
# Get hamming distance between two phash(image hash) to check
94105
# similarity between images
95-
96-
unless first && second
106+
if first.to_s.strip == "" || second.to_s.strip == ""
97107
raise ArgumentError, Error::MISSING_PHASH_VALUE
98108
end
99109
hamming_distance(first, second)
@@ -104,5 +114,4 @@ def get_authentication_parameters(token = nil, expire = nil)
104114
get_authenticated_params(token, expire, @ik_req.private_key)
105115
end
106116
end
107-
end
108-
117+
end

lib/imagekit/resource.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ def request(method, url, headers = nil, payload = nil)
3838
headers: headers,
3939
payload: payload).execute
4040

41-
42-
if resp.code == 404
43-
raise RestClient::ExceptionWithResponse
44-
elsif (resp.code >= 200) && (resp.code < 204)
45-
response[:response] = JSON.parse(resp.body.to_s)
41+
if (resp.code >= 200) && (resp.code < 204)
42+
if (resp.headers[:content_type] == "application/json")
43+
response[:response] = JSON.parse(resp.body.to_s)
44+
else
45+
raise =RestClient::ExceptionWithResponse
46+
end
4647
elsif resp.code == 204
4748
response[:response] = {'success': true}
4849
end

lib/imagekit/url.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ def build_url(options)
7070
parts=part.split("=")
7171
if parts.length==2
7272
query_params[parts[0]]=parts[1]
73-
else
74-
query_params[parts[0]]=""
7573
end
7674
end
7775
end
@@ -94,25 +92,30 @@ def build_url(options)
9492
result_url_hash[:path] = result_url_hash[:path].chomp("/")
9593
result_url_hash[:scheme] ||= "https"
9694

95+
query_param_arr = []
96+
query_param_arr.push("ik-sdk-version=ruby-"+Imagekit::Sdk::VERSION)
97+
query_params.each do |key, value|
98+
if value.to_s == ""
99+
query_param_arr.push(key.to_s)
100+
else
101+
query_param_arr.push(key.to_s + "=" + value.to_s)
102+
end
103+
end
104+
105+
query_param_str = query_param_arr.join("&")
106+
result_url_hash[:query] = query_param_str
97107

98108
# Signature String and Timestamp
99109
# We can do this only for URLs that are created using urlEndpoint and path parameter
100110
# because we need to know the endpoint to be able to remove it from the URL to create a signature
101111
# for the remaining. With the src parameter, we would not know the "pattern" in the URL
102-
query_param_arr = []
103-
query_param_arr.push("ik-sdk-version=ruby-"+Imagekit::Sdk::VERSION)
104112
if options[:signed] && !(options[:src])
105113
intermediate_url = result_url_hash.fetch(:scheme, "") + "://" + result_url_hash.fetch(:host, "") + result_url_hash.fetch(:path, "")
106114
if result_url_hash[:query]!=nil && result_url_hash[:query]!=""
107115
intermediate_url += result_url_hash.fetch(:query, "")
108116
end
109117
end
110-
query_params.each do |key, value|
111-
query_param_arr.push(key.to_s + "=" + value.to_s)
112-
end
113118

114-
query_param_str = query_param_arr.join("&")
115-
result_url_hash[:query] = query_param_str
116119
url=hash_to_url(result_url_hash)
117120
if options[:signed]
118121
private_key = options[:private_key]

test/imagekit/calculation_test.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
require "rspec/autorun"
21
require_relative './helper'
2+
require "rspec/autorun"
3+
34
RSpec.describe "CalculationTest" do
45

56
it "is_valid_hex_with_valid_hex" do
@@ -57,4 +58,10 @@
5758
expect(nil).not_to eq(result[:expire])
5859
expect('my_signature').to eq(result[:signature])
5960
end
61+
62+
it "get_authenticated_params_test_with_nil_private_key" do
63+
allow(OpenSSL::HMAC).to receive(:hexdigest).and_return('my_signature')
64+
result=get_authenticated_params('your_token', 1582269249, nil)
65+
expect(result).to eq(nil)
66+
end
6067
end

test/imagekit/file_test.rb

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
require "rspec/autorun"
21
require_relative './helper'
2+
require "rspec/autorun"
33

44
RSpec.describe "FileUploadTest" do
55
it "test_upload_with_valid_expected_success_without_tags_and_remote_url" do
@@ -124,6 +124,27 @@
124124

125125
end
126126

127+
it "test_upload_fails_on_invalid_options" do
128+
request_obj = double
129+
allow(ImageKitRequest)
130+
.to receive(:new)
131+
.with(PRIVATE_KEY, PUBLIC_KEY, URL_ENDPOINT)
132+
.and_return(request_obj)
133+
134+
allow(request_obj)
135+
.to receive(:create_headers)
136+
.and_return({})
137+
138+
allow(request_obj)
139+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
140+
.and_return({code: 401, message: nil})
141+
142+
SUT = ImageKitFile.new(request_obj)
143+
expect{
144+
SUT.upload("fake_file.jpg", "fake_name", { invalid_option: "invalid_option" })
145+
}.to raise_error(ArgumentError)
146+
end
147+
127148
it "test_upload_fails_on_unauthenticated_request" do
128149
request_obj = double
129150
allow(ImageKitRequest)
@@ -504,9 +525,77 @@
504525
expect(resp[:code]).to eq(200)
505526
expect(resp[:body]).to eq(options)
506527
end
528+
529+
it "test_update_file_details_fails_missing_arguments" do
530+
options = {}
531+
request_obj = double
532+
allow(ImageKitRequest)
533+
.to receive(:new)
534+
.with(PRIVATE_KEY, PUBLIC_KEY, URL_ENDPOINT)
535+
.and_return(request_obj)
536+
537+
allow(request_obj)
538+
.to receive(:create_headers)
539+
.and_return({})
540+
541+
allow(request_obj)
542+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
543+
.and_return({code: 200, body: options})
544+
545+
SUT = ImageKitFile.new(request_obj)
546+
expect {
547+
SUT.update_details("file_id", options)
548+
}.to raise_error(ArgumentError)
549+
end
550+
551+
it "test_update_file_details_fails_tags_not_an_array" do
552+
options = {tags: "RANDOM_TEXT", "custom_coordinates": "10,10,100,100"}
553+
request_obj = double
554+
allow(ImageKitRequest)
555+
.to receive(:new)
556+
.with(PRIVATE_KEY, PUBLIC_KEY, URL_ENDPOINT)
557+
.and_return(request_obj)
558+
559+
allow(request_obj)
560+
.to receive(:create_headers)
561+
.and_return({})
562+
563+
allow(request_obj)
564+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
565+
.and_return({code: 200, body: options})
566+
567+
SUT = ImageKitFile.new(request_obj)
568+
expect {
569+
SUT.update_details("file_id", options)
570+
}.to raise_error(ArgumentError)
571+
end
572+
573+
it "test_update_file_details_fails_custom_coordinates_not_a_string" do
574+
options = {"custom_coordinates": %w[random]}
575+
request_obj = double
576+
allow(ImageKitRequest)
577+
.to receive(:new)
578+
.with(PRIVATE_KEY, PUBLIC_KEY, URL_ENDPOINT)
579+
.and_return(request_obj)
580+
581+
allow(request_obj)
582+
.to receive(:create_headers)
583+
.and_return({})
584+
585+
allow(request_obj)
586+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
587+
.and_return({code: 200, body: options})
588+
589+
SUT = ImageKitFile.new(request_obj)
590+
expect {
591+
SUT.update_details("file_id", options)
592+
}.to raise_error(ArgumentError)
593+
end
507594
end
508595

509596

597+
598+
510599
RSpec.describe "TestBatchDelete" do
511600
it "test_batch_delete_fails_on_unauthenticated_request" do
512601
file_ids = %w[file_id_1 file_id_2]
@@ -605,7 +694,27 @@
605694

606695
expect(@ac[:url]).to eq("https://api.imagekit.io/v1/metadata?url=http://example.com/fakefileurl")
607696
expect(resp[:code]).to eq(401)
697+
end
698+
699+
it "test_get_metadata_from_remote_url_fails_on_blank_url" do
700+
request_obj = double
701+
allow(ImageKitRequest)
702+
.to receive(:new)
703+
.with(PRIVATE_KEY, PUBLIC_KEY, URL_ENDPOINT)
704+
.and_return(request_obj)
705+
706+
allow(request_obj)
707+
.to receive(:create_headers)
708+
.and_return({})
608709

710+
allow(request_obj)
711+
.to receive(:request){|method,url,headers,payload| @ac={method: method, url: url, headers: headers, payload:payload}}
712+
.and_return({code: 401, body: {}})
713+
714+
SUT = ImageKitFile.new(request_obj)
715+
expect {
716+
SUT.get_metadata_from_remote_url("")
717+
}.to raise_error(ArgumentError)
609718
end
610719

611720
it "test_get_metadata_from_remote_url_succeeds" do

0 commit comments

Comments
 (0)