2019-10-14 23:06:19 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe PersonalFileUploader do
|
2018-02-02 08:59:43 -05:00
|
|
|
let(:model) { create(:personal_snippet) }
|
|
|
|
let(:uploader) { described_class.new(model) }
|
|
|
|
let(:upload) { create(:upload, :personal_snippet_upload) }
|
2020-03-04 16:07:54 -05:00
|
|
|
let(:secret) { SecureRandom.hex }
|
2017-05-01 09:14:35 -04:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
subject { uploader }
|
2017-05-01 09:14:35 -04:00
|
|
|
|
2019-05-15 12:26:18 -04:00
|
|
|
shared_examples '#base_dir' do
|
2018-02-02 08:59:43 -05:00
|
|
|
before do
|
2020-03-04 16:07:54 -05:00
|
|
|
subject.instance_variable_set(:@secret, secret)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
2018-02-02 08:59:43 -05:00
|
|
|
|
2019-05-15 12:26:18 -04:00
|
|
|
it 'is prefixed with uploads/-/system' do
|
|
|
|
allow(uploader).to receive(:file).and_return(double(extension: 'txt', filename: 'file_name'))
|
2019-01-21 19:03:37 -05:00
|
|
|
|
2019-05-15 12:26:18 -04:00
|
|
|
expect(described_class.base_dir(model)).to eq("uploads/-/system/personal_snippet/#{model.id}")
|
2018-12-09 14:16:09 -05:00
|
|
|
end
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
|
2019-05-15 12:26:18 -04:00
|
|
|
shared_examples '#to_h' do
|
2018-02-02 08:59:43 -05:00
|
|
|
before do
|
2020-03-04 16:07:54 -05:00
|
|
|
subject.instance_variable_set(:@secret, secret)
|
2018-02-02 08:59:43 -05:00
|
|
|
end
|
2017-05-01 09:14:35 -04:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
it 'is correct' do
|
2017-05-01 09:14:35 -04:00
|
|
|
allow(uploader).to receive(:file).and_return(double(extension: 'txt', filename: 'file_name'))
|
2020-03-04 16:07:54 -05:00
|
|
|
expected_url = "/uploads/-/system/personal_snippet/#{model.id}/#{secret}/file_name"
|
2017-05-01 09:14:35 -04:00
|
|
|
|
|
|
|
expect(uploader.to_h).to eq(
|
|
|
|
alt: 'file_name',
|
|
|
|
url: expected_url,
|
|
|
|
markdown: "[file_name](#{expected_url})"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2018-02-02 08:59:43 -05:00
|
|
|
|
2019-05-15 12:26:18 -04:00
|
|
|
describe '#upload_paths' do
|
|
|
|
it 'builds correct paths for both local and remote storage' do
|
|
|
|
paths = uploader.upload_paths('test.jpg')
|
|
|
|
|
|
|
|
expect(paths.first).to match(%r[\h+\/test.jpg])
|
|
|
|
expect(paths.second).to match(%r[^personal_snippet\/\d+\/\h+\/test.jpg])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'object_store is LOCAL' do
|
|
|
|
it_behaves_like 'builds correct paths',
|
|
|
|
store_dir: %r[uploads/-/system/personal_snippet/\d+/\h+],
|
|
|
|
upload_path: %r[\h+/\S+],
|
|
|
|
absolute_path: %r[#{CarrierWave.root}/uploads/-/system/personal_snippet\/\d+\/\h+\/\S+$]
|
|
|
|
|
|
|
|
it_behaves_like '#base_dir'
|
|
|
|
it_behaves_like '#to_h'
|
|
|
|
end
|
|
|
|
|
|
|
|
context "object_store is REMOTE" do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage
|
|
|
|
end
|
|
|
|
|
|
|
|
include_context 'with storage', described_class::Store::REMOTE
|
|
|
|
|
|
|
|
it_behaves_like 'builds correct paths',
|
|
|
|
store_dir: %r[\d+/\h+],
|
|
|
|
upload_path: %r[^personal_snippet\/\d+\/\h+\/<filename>]
|
|
|
|
|
|
|
|
it_behaves_like '#base_dir'
|
|
|
|
it_behaves_like '#to_h'
|
|
|
|
end
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
describe "#migrate!" do
|
|
|
|
before do
|
2018-06-05 17:18:06 -04:00
|
|
|
uploader.store!(fixture_file_upload('spec/fixtures/doc_sample.txt'))
|
2018-02-02 08:59:43 -05:00
|
|
|
stub_uploads_object_storage
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "migrates", to_store: described_class::Store::REMOTE
|
|
|
|
it_behaves_like "migrates", from_store: described_class::Store::REMOTE, to_store: described_class::Store::LOCAL
|
|
|
|
end
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|