Skip to content

Commit

Permalink
Sign URL uploads if configured (#139)
Browse files Browse the repository at this point in the history
Fix namespace for service

Add spec for upload_from_url

Fix requiring of SignatureGenerator and location of tests
  • Loading branch information
geeosh authored Dec 11, 2023
1 parent eb0f520 commit 2c6a314
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 11 deletions.
15 changes: 8 additions & 7 deletions lib/uploadcare/client/uploader_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative 'upload_client'
require 'retries'
require 'param/upload/upload_params_generator'
require 'param/upload/signature_generator'

module Uploadcare
module Client
Expand Down Expand Up @@ -106,13 +107,13 @@ def upload_many_body(arr, options = {})

# Prepare upload_from_url initial request body
def upload_from_url_body(url, options = {})
HTTP::FormData::Multipart.new(
options.merge(
'pub_key' => Uploadcare.config.public_key,
'source_url' => url,
'store' => store_value(options[:store])
)
)
opts = {
'pub_key' => Uploadcare.config.public_key,
'source_url' => url,
'store' => store_value(options[:store])
}
opts.merge!(Param::Upload::SignatureGenerator.call) if Uploadcare.config.sign_uploads
HTTP::FormData::Multipart.new(options.merge(opts))
end

def store_value(store)
Expand Down
116 changes: 116 additions & 0 deletions spec/fixtures/vcr_cassettes/upload_upload_from_url_with_signature.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions spec/uploadcare/client/uploader_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
module Uploadcare
module Client
RSpec.describe UploaderClient do
subject { UploaderClient.new }
let!(:file) { ::File.open('spec/fixtures/kitten.jpeg') }
let!(:another_file) { ::File.open('spec/fixtures/another_kitten.jpeg') }
subject { described_class.new }

describe 'upload' do
let(:file) { ::File.open('spec/fixtures/kitten.jpeg') }
let(:another_file) { ::File.open('spec/fixtures/another_kitten.jpeg') }

it 'uploads a file' do
VCR.use_cassette('upload_upload') do
response = subject.upload(file, metadata: { subsystem: 'test' })
Expand Down
31 changes: 30 additions & 1 deletion spec/uploadcare/entity/uploader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,40 @@ module Entity
end

describe 'upload_from_url' do
let(:url) { 'https://placekitten.com/2250/2250' }

before do
allow(HTTP::FormData::Multipart).to receive(:new).and_call_original
end

it 'polls server and returns array of files' do
VCR.use_cassette('upload_upload_from_url') do
url = 'https://placekitten.com/2250/2250'
upload = subject.upload(url)
expect(upload[0]).to be_kind_of(Uploadcare::Entity::File)
expect(HTTP::FormData::Multipart).to have_received(:new).with(
a_hash_including(
'source_url' => url
)
)
end
end

context 'when signed uploads are enabled' do
before do
allow(Uploadcare.config).to receive(:sign_uploads).and_return(true)
end

it 'includes signature' do
VCR.use_cassette('upload_upload_from_url_with_signature') do
upload = subject.upload(url)
expect(upload[0]).to be_kind_of(Uploadcare::Entity::File)
expect(HTTP::FormData::Multipart).to have_received(:new).with(
a_hash_including(
signature: instance_of(String),
expire: instance_of(Integer)
)
)
end
end
end
end
Expand Down

0 comments on commit 2c6a314

Please sign in to comment.