Skip to content

Commit

Permalink
Fix describe sytax on Uploadcare::Entity::Uploader to remove rubocop …
Browse files Browse the repository at this point in the history
…warnings
  • Loading branch information
vipulnsward committed Mar 9, 2024
1 parent 931c851 commit 7eba0c2
Showing 1 changed file with 105 additions and 109 deletions.
214 changes: 105 additions & 109 deletions spec/uploadcare/entity/uploader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,131 @@

require 'spec_helper'

module Uploadcare
module Entity
RSpec.describe Uploader do
subject { Uploadcare::Entity::Uploader }
let!(:file) { ::File.open('spec/fixtures/kitten.jpeg') }
let!(:another_file) { ::File.open('spec/fixtures/another_kitten.jpeg') }
let!(:big_file) { ::File.open('spec/fixtures/big.jpeg') }

describe 'upload_many' do
it 'returns a hash of filenames and uids', :aggregate_failures do
VCR.use_cassette('upload_upload_many') do
uploads_list = subject.upload([file, another_file])
expect(uploads_list.length).to eq 2
first_upload = uploads_list.first
expect(first_upload.original_filename).not_to be_empty
expect(first_upload.uuid).not_to be_empty
end
RSpec.describe Uploadcare::Entity::Uploader do
subject { Uploadcare::Entity::Uploader }
let!(:file) { File.open('spec/fixtures/kitten.jpeg') }
let!(:another_file) { File.open('spec/fixtures/another_kitten.jpeg') }
let!(:big_file) { File.open('spec/fixtures/big.jpeg') }

describe 'upload_many' do
it 'returns a hash of filenames and uids', :aggregate_failures do
VCR.use_cassette('upload_upload_many') do
uploads_list = subject.upload([file, another_file])
expect(uploads_list.length).to eq 2
first_upload = uploads_list.first
expect(first_upload.original_filename).not_to be_empty
expect(first_upload.uuid).not_to be_empty
end
end

describe 'upload_one' do
it 'returns a file', :aggregate_failures do
VCR.use_cassette('upload_upload_one') do
upload = subject.upload(file)
expect(upload).to be_kind_of(Uploadcare::Entity::File)
expect(file.path).to end_with(upload.original_filename.to_s)
expect(file.size).to eq(upload.size)
end
end

describe 'upload_one' do
it 'returns a file', :aggregate_failures do
VCR.use_cassette('upload_upload_one') do
upload = subject.upload(file)
expect(upload).to be_kind_of(Uploadcare::Entity::File)
expect(file.path).to end_with(upload.original_filename.to_s)
expect(file.size).to eq(upload.size)
end
end
context 'when the secret key is missing' do
it 'returns a file without details', :aggregate_failures do
Uploadcare.config.secret_key = nil

context 'when the secret key is missing' do
it 'returns a file without details', :aggregate_failures do
Uploadcare.config.secret_key = nil

VCR.use_cassette('upload_upload_one_without_secret_key') do
upload = subject.upload(file)
expect(upload).to be_kind_of(Uploadcare::Entity::File)
expect(file.path).to end_with(upload.original_filename.to_s)
expect(file.size).to eq(upload.size)
end
end
VCR.use_cassette('upload_upload_one_without_secret_key') do
upload = subject.upload(file)
expect(upload).to be_kind_of(Uploadcare::Entity::File)
expect(file.path).to end_with(upload.original_filename.to_s)
expect(file.size).to eq(upload.size)
end
end
end
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
describe 'upload_from_url' do
let(:url) { 'https://placekitten.com/2250/2250' }

it 'polls server and returns array of files' do
VCR.use_cassette('upload_upload_from_url') 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(
'source_url' => url
)
)
end
end
before do
allow(HTTP::FormData::Multipart).to receive(:new).and_call_original
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
it 'polls server and returns array of files' do
VCR.use_cassette('upload_upload_from_url') 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(
'source_url' => url
)
)
end
end

it 'raises error with information if file upload takes time' do
Uploadcare.config.max_request_tries = 1
VCR.use_cassette('upload_upload_from_url') do
url = 'https://placekitten.com/2250/2250'
error_str = 'Upload is taking longer than expected. Try increasing the max_request_tries config if you know your file uploads will take more time.' # rubocop:disable Layout/LineLength
expect { subject.upload(url) }.to raise_error(RetryError, error_str)
end
end
context 'when signed uploads are enabled' do
before do
allow(Uploadcare.config).to receive(:sign_uploads).and_return(true)
end

describe 'multipart_upload' do
let!(:some_var) { nil }

it 'uploads a file', :aggregate_failures do
VCR.use_cassette('upload_multipart_upload') do
# Minimal size for file to be valid for multipart upload is 10 mb
Uploadcare.config.multipart_size_threshold = 10 * 1024 * 1024
expect(some_var).to receive(:to_s).at_least(:once).and_call_original
file = subject.multipart_upload(big_file) { some_var }
expect(file).to be_kind_of(Uploadcare::Entity::File)
expect(file.uuid).not_to be_empty
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

describe 'get_upload_from_url_status' do
it 'gets a status of upload-from-URL' do
VCR.use_cassette('upload_get_upload_from_url_status') do
token = '0313e4e2-f2ca-4564-833b-4f71bc8cba27'
status_info = subject.get_upload_from_url_status(token).success
expect(status_info[:status]).to eq 'success'
end
end
it 'raises error with information if file upload takes time' do
Uploadcare.config.max_request_tries = 1
VCR.use_cassette('upload_upload_from_url') do
url = 'https://placekitten.com/2250/2250'
error_str = 'Upload is taking longer than expected. Try increasing the max_request_tries config if you know your file uploads will take more time.' # rubocop:disable Layout/LineLength
expect { subject.upload(url) }.to raise_error(RetryError, error_str)
end
end
end

describe 'file_info' do
it 'returns file info without the secret key', :aggregate_failures do
uuid = 'a7f9751a-432b-4b05-936c-2f62d51d255d'
describe 'multipart_upload' do
let!(:some_var) { nil }

it 'uploads a file', :aggregate_failures do
VCR.use_cassette('upload_multipart_upload') do
# Minimal size for file to be valid for multipart upload is 10 mb
Uploadcare.config.multipart_size_threshold = 10 * 1024 * 1024
expect(some_var).to receive(:to_s).at_least(:once).and_call_original
file = subject.multipart_upload(big_file) { some_var }
expect(file).to be_kind_of(Uploadcare::Entity::File)
expect(file.uuid).not_to be_empty
end
end
end

VCR.use_cassette('upload_file_info') do
file_info = subject.file_info(uuid).success
expect(file_info[:original_filename]).not_to be_empty
expect(file_info[:size]).to be >= 0
expect(file_info[:uuid]).to eq uuid
end
describe 'get_upload_from_url_status' do
it 'gets a status of upload-from-URL' do
VCR.use_cassette('upload_get_upload_from_url_status') do
token = '0313e4e2-f2ca-4564-833b-4f71bc8cba27'
status_info = subject.get_upload_from_url_status(token).success
expect(status_info[:status]).to eq 'success'
end
end
end
end

describe 'file_info' do
it 'returns file info without the secret key', :aggregate_failures do
uuid = 'a7f9751a-432b-4b05-936c-2f62d51d255d'

VCR.use_cassette('upload_file_info') do
file_info = subject.file_info(uuid).success
expect(file_info[:original_filename]).not_to be_empty
expect(file_info[:size]).to be >= 0
expect(file_info[:uuid]).to eq uuid
end
end
end
end

0 comments on commit 7eba0c2

Please sign in to comment.