2017-08-17 22:00:37 +05:30
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Upload do
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'assocations' do
|
|
|
|
it { is_expected.to belong_to(:model) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:size) }
|
|
|
|
it { is_expected.to validate_presence_of(:path) }
|
|
|
|
it { is_expected.to validate_presence_of(:model) }
|
|
|
|
it { is_expected.to validate_presence_of(:uploader) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'callbacks' do
|
|
|
|
context 'for a file above the checksum threshold' do
|
|
|
|
it 'schedules checksum calculation' do
|
|
|
|
stub_const('UploadChecksumWorker', spy)
|
|
|
|
|
|
|
|
upload = described_class.create(
|
|
|
|
path: __FILE__,
|
|
|
|
size: described_class::CHECKSUM_THRESHOLD + 1.kilobyte,
|
|
|
|
model: build_stubbed(:user),
|
|
|
|
uploader: double('ExampleUploader')
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(UploadChecksumWorker)
|
|
|
|
.to have_received(:perform_async).with(upload.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a file at or below the checksum threshold' do
|
|
|
|
it 'calculates checksum immediately before save' do
|
|
|
|
upload = described_class.new(
|
|
|
|
path: __FILE__,
|
|
|
|
size: described_class::CHECKSUM_THRESHOLD,
|
|
|
|
model: build_stubbed(:user),
|
|
|
|
uploader: double('ExampleUploader')
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { upload.save }
|
|
|
|
.to change { upload.checksum }.from(nil)
|
|
|
|
.to(a_string_matching(/\A\h{64}\z/))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe 'after_destroy' do
|
|
|
|
context 'uploader is FileUploader-based' do
|
|
|
|
subject { create(:upload, :issuable_upload) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'calls delete_file!' do
|
|
|
|
is_expected.to receive(:delete_file!)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
subject.destroy
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#absolute_path' do
|
|
|
|
it 'returns the path directly when already absolute' do
|
|
|
|
path = '/path/to/namespace/project/secret/file.jpg'
|
|
|
|
upload = described_class.new(path: path)
|
|
|
|
|
|
|
|
expect(upload).not_to receive(:uploader_class)
|
|
|
|
|
|
|
|
expect(upload.absolute_path).to eq path
|
|
|
|
end
|
|
|
|
|
|
|
|
it "delegates to the uploader's absolute_path method" do
|
|
|
|
uploader = spy('FakeUploader')
|
|
|
|
upload = described_class.new(path: 'secret/file.jpg')
|
|
|
|
expect(upload).to receive(:uploader_class).and_return(uploader)
|
|
|
|
|
|
|
|
upload.absolute_path
|
|
|
|
|
|
|
|
expect(uploader).to have_received(:absolute_path).with(upload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#calculate_checksum!' do
|
|
|
|
let(:upload) do
|
|
|
|
described_class.new(path: __FILE__,
|
|
|
|
size: described_class::CHECKSUM_THRESHOLD - 1.megabyte)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets `checksum` to SHA256 sum of the file' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expected = Digest::SHA256.file(__FILE__).hexdigest
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect { upload.calculate_checksum! }
|
2017-08-17 22:00:37 +05:30
|
|
|
.to change { upload.checksum }.from(nil).to(expected)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'sets `checksum` to nil for a non-existant file' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(upload).to receive(:exist?).and_return(false)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
checksum = Digest::SHA256.file(__FILE__).hexdigest
|
|
|
|
upload.checksum = checksum
|
|
|
|
|
|
|
|
expect { upload.calculate_checksum! }
|
|
|
|
.to change { upload.checksum }.from(checksum).to(nil)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#exist?' do
|
|
|
|
it 'returns true when the file exists' do
|
|
|
|
upload = described_class.new(path: __FILE__)
|
|
|
|
|
|
|
|
expect(upload).to exist
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when the file does not exist' do
|
|
|
|
upload = described_class.new(path: "#{__FILE__}-nope")
|
|
|
|
|
|
|
|
expect(upload).not_to exist
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
describe "#uploader_context" do
|
|
|
|
subject { create(:upload, :issuable_upload, secret: 'secret', filename: 'file.txt') }
|
|
|
|
|
|
|
|
it { expect(subject.uploader_context).to match(a_hash_including(secret: 'secret', identifier: 'file.txt')) }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|