2017-02-15 13:11:44 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe RecordsUploads do
|
2017-05-29 03:54:35 -04:00
|
|
|
let!(:uploader) do
|
2017-02-28 13:34:43 -05:00
|
|
|
class RecordsUploadsExampleUploader < GitlabUploader
|
2017-02-15 13:11:44 -05:00
|
|
|
include RecordsUploads
|
|
|
|
|
|
|
|
storage :file
|
|
|
|
|
|
|
|
def model
|
|
|
|
FactoryGirl.build_stubbed(:user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-28 13:34:43 -05:00
|
|
|
RecordsUploadsExampleUploader.new
|
2017-02-15 13:11:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload_fixture(filename)
|
|
|
|
fixture_file_upload(Rails.root.join('spec', 'fixtures', filename))
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'callbacks' do
|
|
|
|
it 'calls `record_upload` after `store`' do
|
|
|
|
expect(uploader).to receive(:record_upload).once
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('doc_sample.txt'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls `destroy_upload` after `remove`' do
|
|
|
|
expect(uploader).to receive(:destroy_upload).once
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('doc_sample.txt'))
|
|
|
|
|
|
|
|
uploader.remove!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#record_upload callback' do
|
|
|
|
it 'returns early when not using file storage' do
|
|
|
|
allow(uploader).to receive(:file_storage?).and_return(false)
|
|
|
|
expect(Upload).not_to receive(:record)
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns early when the file doesn't exist" do
|
|
|
|
allow(uploader).to receive(:file).and_return(double(exists?: false))
|
|
|
|
expect(Upload).not_to receive(:record)
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an Upload record after store' do
|
|
|
|
expect(Upload).to receive(:record)
|
|
|
|
.with(uploader)
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
end
|
|
|
|
|
2017-05-29 03:54:35 -04:00
|
|
|
it 'does not create an Upload record if model is missing' do
|
|
|
|
expect_any_instance_of(RecordsUploadsExampleUploader).to receive(:model).and_return(nil)
|
|
|
|
expect(Upload).not_to receive(:record).with(uploader)
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
end
|
|
|
|
|
2017-02-15 13:11:44 -05:00
|
|
|
it 'it destroys Upload records at the same path before recording' do
|
|
|
|
existing = Upload.create!(
|
2017-02-28 13:34:43 -05:00
|
|
|
path: File.join('uploads', 'rails_sample.jpg'),
|
2017-02-15 13:11:44 -05:00
|
|
|
size: 512.kilobytes,
|
|
|
|
model: build_stubbed(:user),
|
2017-02-28 13:34:43 -05:00
|
|
|
uploader: uploader.class.to_s
|
2017-02-15 13:11:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
|
|
|
|
expect { existing.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
expect(Upload.count).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy_upload callback' do
|
|
|
|
it 'returns early when not using file storage' do
|
|
|
|
uploader.store!(upload_fixture('rails_sample.jpg'))
|
|
|
|
|
|
|
|
allow(uploader).to receive(:file_storage?).and_return(false)
|
|
|
|
expect(Upload).not_to receive(:remove_path)
|
|
|
|
|
|
|
|
uploader.remove!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns early when file is nil' do
|
|
|
|
expect(Upload).not_to receive(:remove_path)
|
|
|
|
|
|
|
|
uploader.remove!
|
|
|
|
end
|
|
|
|
|
|
|
|
it '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
|
|
|
|
end
|