Skip to content

Commit 0d9dd47

Browse files
committed
Add specs for result and webhook behaviors
1 parent 8194f31 commit 0d9dd47

6 files changed

Lines changed: 88 additions & 0 deletions

File tree

spec/uploadcare/error_handler_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ module Uploadcare
186186
handler.send(:catch_upload_errors, response)
187187
end.not_to raise_error
188188
end
189+
190+
it 'handles invalid JSON body' do
191+
response = { status: 200, body: 'invalid-json' }
192+
193+
expect do
194+
handler.send(:catch_upload_errors, response)
195+
end.not_to raise_error
196+
end
197+
end
198+
199+
describe '#raise_throttle_error' do
200+
it 'raises ThrottleError with retry-after' do
201+
response = { status: 429, headers: { 'Retry-After' => '2.5' } }
202+
203+
expect do
204+
handler.send(:raise_throttle_error, response, 'Too many requests')
205+
end.to raise_error(Uploadcare::Exception::ThrottleError) { |error| expect(error.timeout).to eq(2.5) }
206+
end
207+
208+
it 'defaults to 10 seconds when retry-after is missing' do
209+
response = { status: 429, headers: {} }
210+
211+
expect do
212+
handler.send(:raise_throttle_error, response, 'Too many requests')
213+
end.to raise_error(Uploadcare::Exception::ThrottleError) { |error| expect(error.timeout).to eq(10.0) }
214+
end
189215
end
190216
end
191217
end

spec/uploadcare/param/upload/upload_params_generator_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
expect(params['UPLOADCARE_STORE']).to eq('0')
3434
end
3535

36+
it 'passes through store values' do
37+
config = Uploadcare::Configuration.new(public_key: 'pub')
38+
params = described_class.call(options: { store: 'auto' }, config: config)
39+
40+
expect(params['UPLOADCARE_STORE']).to eq('auto')
41+
end
42+
3643
it 'uses explicit signature params when provided' do
3744
config = Uploadcare::Configuration.new(public_key: 'pub', sign_uploads: true)
3845
params = described_class.call(options: { signature: 'sig', expire: 123 }, config: config)
@@ -41,6 +48,16 @@
4148
expect(params['expire']).to eq(123)
4249
end
4350

51+
it 'supports non-hash signature data' do
52+
config = Uploadcare::Configuration.new(public_key: 'pub', sign_uploads: true)
53+
allow(Uploadcare::Param::Upload::SignatureGenerator).to receive(:call).and_return('signature-string')
54+
55+
params = described_class.call(options: {}, config: config)
56+
57+
expect(params['signature']).to eq('signature-string')
58+
expect(params['expire']).to be_nil
59+
end
60+
4461
it 'converts metadata values to strings' do
4562
config = Uploadcare::Configuration.new(public_key: 'pub')
4663
params = described_class.call(options: { metadata: { count: 12 } }, config: config)

spec/uploadcare/resources/paginated_collection_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ module Uploadcare
145145
end
146146
end
147147

148+
describe '#all' do
149+
it 'collects resources across pages' do
150+
next_collection = described_class.new(params.merge(resources: [double('Resource3')], next_page: nil))
151+
allow(subject).to receive(:next_page).and_return(next_collection, nil)
152+
153+
items = subject.all
154+
155+
expect(items.length).to eq(3)
156+
end
157+
end
158+
148159
describe '#fetch_page' do
149160
let(:page_url) { 'https://api.uploadcare.com/files/?page=2&limit=50' }
150161
let(:page_response) do

spec/uploadcare/resources/webhook_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@
129129
expect(webhook).to be_a(described_class)
130130
expect(webhook.target_url).to eq(target_url)
131131
end
132+
133+
it 'allows deactivating a webhook' do
134+
allow_any_instance_of(Uploadcare::WebhookClient).to receive(:update_webhook)
135+
.with(id: webhook_id, options: hash_including(is_active: false), request_options: {})
136+
.and_return(response_body.merge('is_active' => false))
137+
138+
webhook = described_class.update(id: webhook_id, is_active: false)
139+
140+
expect(webhook.is_active).to eq(false)
141+
end
132142
end
133143
describe '.delete' do
134144
let(:target_url) { 'https://example.com/hooks' }

spec/uploadcare/result_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@
4949

5050
expect { result.value! }.to raise_error(StandardError, 'nope')
5151
end
52+
53+
it 'raises inspected value for non-exception errors' do
54+
error = { error: 'boom' }
55+
result = described_class.failure(error)
56+
57+
expect { result.value! }.to raise_error(RuntimeError, /\{error: "boom"\}/)
58+
end
5259
end

spec/uploadcare/webhook_signature_verifier_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ module Uploadcare
9494
expect(signature_valid?).to be_falsey
9595
end
9696
end
97+
98+
context 'when webhook body is missing' do
99+
let(:params) { super().merge(webhook_body: nil) }
100+
101+
it 'returns false' do
102+
expect(signature_valid?).to be_falsey
103+
end
104+
end
105+
end
106+
107+
describe '.secure_compare?' do
108+
it 'falls back to byte comparison when OpenSSL helper is missing' do
109+
allow(OpenSSL).to receive(:fixed_length_secure_compare).and_raise(NoMethodError)
110+
111+
expect(described_class.secure_compare?('abc', 'abc')).to be(true)
112+
expect(described_class.secure_compare?('abc', 'abd')).to be(false)
113+
end
97114
end
98115
end
99116
end

0 commit comments

Comments
 (0)