2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-12-22 16:03:36 +01:00
|
|
|
describe DownloadManager::ParallelDownloadQueue do
|
2021-12-13 14:35:13 +01:00
|
|
|
let(:test_dir) { Dir.mktmpdir(nil, Dir.tmpdir) }
|
|
|
|
let(:download_to_dir) { test_dir }
|
2021-12-22 16:03:36 +01:00
|
|
|
before do
|
|
|
|
downloadable_manager.on_error = proc { |_, _, _| }
|
|
|
|
end
|
2021-12-13 14:35:13 +01:00
|
|
|
|
2021-12-22 16:03:36 +01:00
|
|
|
after { FileUtils.remove_entry_secure(test_dir) if Dir.exist?(test_dir) }
|
2021-12-13 14:35:13 +01:00
|
|
|
|
2021-12-22 16:03:36 +01:00
|
|
|
let(:downloadable_manager) { DownloadManager::ParallelDownloadQueue.new([attachment], download_to_dir) }
|
2024-04-02 12:54:24 +02:00
|
|
|
let(:http_client) { instance_double(Typhoeus::Hydra) }
|
|
|
|
|
2021-12-13 14:35:13 +01:00
|
|
|
describe '#download_one' do
|
2024-04-02 12:54:24 +02:00
|
|
|
subject { downloadable_manager.download_one(attachment: attachment, path_in_download_dir: destination, http_client:) }
|
2021-12-13 14:35:13 +01:00
|
|
|
|
2021-12-22 16:03:36 +01:00
|
|
|
let(:destination) { 'lol.png' }
|
2021-12-13 14:35:13 +01:00
|
|
|
let(:attachment) do
|
2022-11-18 11:30:21 +01:00
|
|
|
ActiveStorage::FakeAttachment.new(
|
2021-12-13 14:35:13 +01:00
|
|
|
file: StringIO.new('coucou'),
|
|
|
|
filename: "export-dossier.pdf",
|
|
|
|
name: 'pdf_export_for_instructeur',
|
|
|
|
id: 1,
|
|
|
|
created_at: Time.zone.now
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-11-18 11:30:21 +01:00
|
|
|
context 'with a ActiveStorage::FakeAttachment and it works' do
|
2021-12-13 14:35:13 +01:00
|
|
|
it 'write attachment.file to disk' do
|
2021-12-22 16:03:36 +01:00
|
|
|
target = File.join(download_to_dir, destination)
|
2021-12-13 14:35:13 +01:00
|
|
|
expect { subject }.to change { File.exist?(target) }
|
|
|
|
attachment.file.rewind
|
|
|
|
expect(attachment.file.read).to eq(File.read(target))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-18 11:30:21 +01:00
|
|
|
context 'with a ActiveStorage::FakeAttachment and it fails' do
|
2021-12-13 14:35:13 +01:00
|
|
|
it 'write attachment.file to disk' do
|
|
|
|
expect(attachment.file).to receive(:read).and_raise("boom")
|
2021-12-22 16:03:36 +01:00
|
|
|
target = File.join(download_to_dir, destination)
|
2021-12-13 14:35:13 +01:00
|
|
|
expect { subject }.to raise_error(StandardError)
|
|
|
|
expect(File.exist?(target)).to be_falsey
|
2021-12-22 16:03:36 +01:00
|
|
|
# expect(downloadable_manager.errors).to have_key(destination)
|
2021-12-13 14:35:13 +01:00
|
|
|
end
|
|
|
|
end
|
2023-05-04 12:45:58 +02:00
|
|
|
|
|
|
|
context 'with a destination filename too long' do
|
|
|
|
let(:destination) { 'a' * 252 + '.txt' }
|
|
|
|
|
|
|
|
it 'limit the file path to 255 bytes' do
|
|
|
|
target = File.join(download_to_dir, 'a' * 251 + '.txt')
|
|
|
|
expect { subject }.to change { File.exist?(target) }
|
|
|
|
attachment.file.rewind
|
|
|
|
expect(attachment.file.read).to eq(File.read(target))
|
|
|
|
end
|
|
|
|
end
|
2023-05-04 12:55:05 +02:00
|
|
|
|
|
|
|
context 'with filename containing unsafe characters for storage' do
|
|
|
|
let(:destination) { "file:éà\u{202e} K.txt" }
|
|
|
|
|
|
|
|
it 'sanitize the problematic chars' do
|
|
|
|
target = File.join(download_to_dir, 'file-éà- K.txt')
|
|
|
|
expect { subject }.to change { File.exist?(target) }
|
|
|
|
attachment.file.rewind
|
|
|
|
expect(attachment.file.read).to eq(File.read(target))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a destination tree' do
|
|
|
|
let(:destination) { 'subdir/file.txt' }
|
|
|
|
|
|
|
|
it 'preserves the destination tree' do
|
|
|
|
target = File.join(download_to_dir, 'subdir/file.txt')
|
|
|
|
expect { subject }.to change { File.exist?(target) }
|
|
|
|
attachment.file.rewind
|
|
|
|
expect(attachment.file.read).to eq(File.read(target))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-04-02 12:54:24 +02:00
|
|
|
|
|
|
|
context "download strategies" do
|
|
|
|
subject { super(); http_client.run }
|
|
|
|
let(:byte_size) { 1.kilobyte }
|
|
|
|
let(:file_url) { 'http://example.com/test_file' }
|
|
|
|
let(:destination) { 'test_file.txt' }
|
|
|
|
let(:http_client) { Typhoeus::Hydra.new }
|
|
|
|
let(:blob) { instance_double('ActiveStorage::Blob', byte_size:, url: file_url) }
|
|
|
|
let(:attachment) { double('ActiveStorage::Attachment', blob: blob) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(attachment).to receive(:url).and_return(file_url)
|
|
|
|
stub_request(:get, file_url).to_return(body: file_content, status: 200)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for small files using request_in_whole method' do
|
|
|
|
let(:file_content) { 'downloaded content' }
|
|
|
|
it 'downloads the file in whole' do
|
|
|
|
target = Pathname.new(download_to_dir).join(destination)
|
|
|
|
expect { subject }.to change { target.exist? }.from(false).to(true)
|
|
|
|
|
|
|
|
expect(File.read(target)).to eq(file_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for large files using request_in_chunks method' do
|
|
|
|
let(:byte_size) { 20.megabytes } # Adjust byte size for large file scenario
|
|
|
|
let(:file_content) { 'downloaded content' * 1000 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(downloadable_manager).to receive(:request_in_chunks).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'downloads the file in chunks' do
|
|
|
|
target = Pathname.new(download_to_dir).join(destination)
|
|
|
|
expect { subject }.to change { target.exist? }.from(false).to(true)
|
|
|
|
|
|
|
|
expect(File.read(target)).to eq(file_content)
|
|
|
|
expect(downloadable_manager).to have_received(:request_in_chunks) # ensure we're taking the chunks code path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-13 14:35:13 +01:00
|
|
|
end
|
|
|
|
end
|