gitlab-org--gitlab-foss/spec/uploaders/records_uploads_spec.rb

107 lines
3.2 KiB
Ruby
Raw Normal View History

require 'rails_helper'
describe RecordsUploads do
let!(:uploader) do
class RecordsUploadsExampleUploader < GitlabUploader
2018-01-29 12:57:34 -05:00
include RecordsUploads::Concern
storage :file
2018-01-29 12:57:34 -05:00
def dynamic_segment
'co/fe/ee'
end
end
2018-01-29 12:57:34 -05:00
RecordsUploadsExampleUploader.new(build_stubbed(:user))
end
def upload_fixture(filename)
fixture_file_upload(File.join('spec', 'fixtures', filename))
end
describe 'callbacks' do
2018-01-29 12:57:34 -05:00
let(:upload) { create(:upload) }
before do
uploader.upload = upload
end
it '#record_upload after `store`' do
expect(uploader).to receive(:record_upload).once
uploader.store!(upload_fixture('doc_sample.txt'))
end
2018-01-29 12:57:34 -05:00
it '#destroy_upload after `remove`' do
uploader.store!(upload_fixture('doc_sample.txt'))
2018-01-29 12:57:34 -05:00
expect(uploader).to receive(:destroy_upload).once
uploader.remove!
end
end
describe '#record_upload callback' do
2018-01-29 12:57:34 -05:00
it 'creates an Upload record after store' do
expect { uploader.store!(upload_fixture('rails_sample.jpg')) }.to change { Upload.count }.by(1)
end
2018-01-29 12:57:34 -05:00
it 'creates a new record and assigns size, path, model, and uploader' do
uploader.store!(upload_fixture('rails_sample.jpg'))
2018-01-29 12:57:34 -05:00
upload = uploader.upload
aggregate_failures do
expect(upload).to be_persisted
expect(upload.size).to eq uploader.file.size
expect(upload.path).to eq uploader.upload_path
expect(upload.model_id).to eq uploader.model.id
expect(upload.model_type).to eq uploader.model.class.to_s
expect(upload.uploader).to eq uploader.class.to_s
end
end
2018-01-29 12:57:34 -05:00
it "does not create an Upload record when the file doesn't exist" do
allow(uploader).to receive(:file).and_return(double(exists?: false))
2018-01-29 12:57:34 -05:00
expect { uploader.store!(upload_fixture('rails_sample.jpg')) }.not_to change { Upload.count }
end
it 'does not create an Upload record if model is missing' do
2018-01-29 12:57:34 -05:00
allow_any_instance_of(RecordsUploadsExampleUploader).to receive(:model).and_return(nil)
2018-01-29 12:57:34 -05:00
expect { uploader.store!(upload_fixture('rails_sample.jpg')) }.not_to change { Upload.count }
end
it 'destroys Upload records at the same path before recording' do
existing = Upload.create!(
path: File.join('uploads', 'rails_sample.jpg'),
size: 512.kilobytes,
model: build_stubbed(:user),
uploader: uploader.class.to_s
)
2018-01-29 12:57:34 -05:00
uploader.upload = existing
uploader.store!(upload_fixture('rails_sample.jpg'))
expect { existing.reload }.to raise_error(ActiveRecord::RecordNotFound)
2018-01-29 12:57:34 -05:00
expect(Upload.count).to eq(1)
end
end
describe '#destroy_upload callback' do
it 'destroys Upload records at the same path after removal' do
uploader.store!(upload_fixture('rails_sample.jpg'))
expect { uploader.remove! }.to change { Upload.count }.from(1).to(0)
end
end
describe '#filename' do
it 'gets the filename from the path recorded in the database, not CarrierWave' do
uploader.store!(upload_fixture('rails_sample.jpg'))
expect_any_instance_of(GitlabUploader).not_to receive(:filename)
expect(uploader.filename).to eq('rails_sample.jpg')
end
end
end