debian-mirror-gitlab/spec/lib/safe_zip/extract_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
2.9 KiB
Ruby
Raw Permalink Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2019-02-02 18:00:53 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe SafeZip::Extract do
2019-02-02 18:00:53 +05:30
let(:target_path) { Dir.mktmpdir('safe-zip') }
let(:directories) { %w(public) }
2023-02-26 17:17:37 +05:30
let(:files) { %w(public/index.html) }
2019-02-02 18:00:53 +05:30
let(:object) { described_class.new(archive) }
let(:archive) { Rails.root.join('spec', 'fixtures', 'safe_zip', archive_name) }
after do
FileUtils.remove_entry_secure(target_path)
end
2020-03-13 15:44:24 +05:30
describe '#extract' do
2023-02-26 17:17:37 +05:30
subject { object.extract(directories: directories, files: files, to: target_path) }
2019-02-02 18:00:53 +05:30
2021-01-03 14:25:43 +05:30
shared_examples 'extracts archive' do
2023-02-26 17:17:37 +05:30
context 'when specifying directories' do
subject { object.extract(directories: directories, to: target_path) }
2019-02-02 18:00:53 +05:30
2023-02-26 17:17:37 +05:30
it 'does extract archive' do
subject
expect(File.exist?(File.join(target_path, 'public', 'index.html'))).to eq(true)
expect(File.exist?(File.join(target_path, 'public', 'assets', 'image.png'))).to eq(true)
expect(File.exist?(File.join(target_path, 'source'))).to eq(false)
end
end
context 'when specifying files' do
subject { object.extract(files: files, to: target_path) }
it 'does extract archive' do
subject
expect(File.exist?(File.join(target_path, 'public', 'index.html'))).to eq(true)
expect(File.exist?(File.join(target_path, 'public', 'assets', 'image.png'))).to eq(false)
end
2019-02-02 18:00:53 +05:30
end
end
2021-01-03 14:25:43 +05:30
shared_examples 'fails to extract archive' do
2019-02-02 18:00:53 +05:30
it 'does not extract archive' do
2023-02-26 17:17:37 +05:30
expect { subject }.to raise_error(SafeZip::Extract::Error, including(error_message))
2019-02-02 18:00:53 +05:30
end
end
2019-03-02 22:35:43 +05:30
%w(valid-simple.zip valid-symlinks-first.zip valid-non-writeable.zip).each do |name|
2019-02-02 18:00:53 +05:30
context "when using #{name} archive" do
let(:archive_name) { name }
2021-01-03 14:25:43 +05:30
it_behaves_like 'extracts archive'
2019-02-02 18:00:53 +05:30
end
end
2023-02-26 17:17:37 +05:30
context 'when zip files are invalid' do
using RSpec::Parameterized::TableSyntax
where(:name, :message) do
'invalid-symlink-does-not-exist.zip' | 'does not exist'
'invalid-symlinks-outside.zip' | 'Symlink cannot be created'
'invalid-unexpected-large.zip' | 'larger when inflated'
end
with_them do
2019-02-02 18:00:53 +05:30
let(:archive_name) { name }
2023-02-26 17:17:37 +05:30
let(:error_message) { message }
2019-02-02 18:00:53 +05:30
2021-01-03 14:25:43 +05:30
it_behaves_like 'fails to extract archive'
2019-02-02 18:00:53 +05:30
end
end
context 'when no matching directories are found' do
let(:archive_name) { 'valid-simple.zip' }
let(:directories) { %w(non/existing) }
2023-02-26 17:17:37 +05:30
let(:error_message) { 'No entries extracted' }
subject { object.extract(directories: directories, to: target_path) }
it_behaves_like 'fails to extract archive'
end
context 'when no matching files are found' do
let(:archive_name) { 'valid-simple.zip' }
let(:files) { %w(non/existing) }
let(:error_message) { 'No entries extracted' }
subject { object.extract(files: files, to: target_path) }
2019-02-02 18:00:53 +05:30
2021-01-03 14:25:43 +05:30
it_behaves_like 'fails to extract archive'
2019-02-02 18:00:53 +05:30
end
end
end