2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
require 'spec_helper'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe RecordsUploads do
|
2017-09-10 17:25:29 +05:30
|
|
|
let!(:uploader) do
|
2017-08-17 22:00:37 +05:30
|
|
|
class RecordsUploadsExampleUploader < GitlabUploader
|
2018-03-17 18:26:18 +05:30
|
|
|
include RecordsUploads::Concern
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
storage :file
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def dynamic_segment
|
|
|
|
'co/fe/ee'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
RecordsUploadsExampleUploader.new(build_stubbed(:user))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def upload_fixture(filename)
|
2018-11-08 19:23:39 +05:30
|
|
|
fixture_file_upload(File.join('spec', 'fixtures', filename))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'callbacks' do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:upload) { create(:upload) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
uploader.upload = upload
|
|
|
|
end
|
|
|
|
|
|
|
|
it '#record_upload after `store`' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(uploader).to receive(:record_upload).once
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('doc_sample.txt'))
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it '#destroy_upload after `remove`' do
|
2017-08-17 22:00:37 +05:30
|
|
|
uploader.store!(upload_fixture('doc_sample.txt'))
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(uploader).to receive(:destroy_upload).once
|
2017-08-17 22:00:37 +05:30
|
|
|
uploader.remove!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#record_upload callback' do
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'creates an Upload record after store' do
|
|
|
|
expect { uploader.store!(upload_fixture('rails_sample.jpg')) }.to change { Upload.count }.by(1)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'creates a new record and assigns size, path, model, and uploader' do
|
2017-08-17 22:00:37 +05:30
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it "does not create an Upload record when the file doesn't exist" do
|
|
|
|
allow(uploader).to receive(:file).and_return(double(exists?: false))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect { uploader.store!(upload_fixture('rails_sample.jpg')) }.not_to change { Upload.count }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not create an Upload record if model is missing' do
|
2018-03-17 18:26:18 +05:30
|
|
|
allow_any_instance_of(RecordsUploadsExampleUploader).to receive(:model).and_return(nil)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect { uploader.store!(upload_fixture('rails_sample.jpg')) }.not_to change { Upload.count }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'destroys Upload records at the same path before recording' do
|
2017-08-17 22:00:37 +05:30
|
|
|
existing = Upload.create!(
|
|
|
|
path: File.join('uploads', 'rails_sample.jpg'),
|
|
|
|
size: 512.kilobytes,
|
|
|
|
model: build_stubbed(:user),
|
|
|
|
uploader: uploader.class.to_s
|
|
|
|
)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
uploader.upload = existing
|
2017-08-17 22:00:37 +05:30
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
|
|
|
|
expect { existing.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(Upload.count).to eq(1)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
it 'does not affect other uploads with different model but the same path' do
|
|
|
|
project = create(:project)
|
|
|
|
other_project = create(:project)
|
|
|
|
|
|
|
|
uploader = RecordsUploadsExampleUploader.new(other_project)
|
|
|
|
|
|
|
|
upload_for_project = Upload.create!(
|
|
|
|
path: File.join('uploads', 'rails_sample.jpg'),
|
|
|
|
size: 512.kilobytes,
|
|
|
|
model: project,
|
|
|
|
uploader: uploader.class.to_s
|
|
|
|
)
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
|
|
|
|
upload_for_project_fresh = Upload.find(upload_for_project.id)
|
|
|
|
|
|
|
|
expect(upload_for_project).to eq(upload_for_project_fresh)
|
|
|
|
expect(Upload.count).to eq(2)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy_upload callback' do
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'destroys Upload records at the same path after removal' do
|
2017-08-17 22:00:37 +05:30
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
|
|
|
|
expect { uploader.remove! }.to change { Upload.count }.from(1).to(0)
|
|
|
|
end
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|