2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-02 14:01:11 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe SafeZip::Extract do
|
2019-01-02 14:01:11 -05:00
|
|
|
let(:target_path) { Dir.mktmpdir('safe-zip') }
|
|
|
|
let(:directories) { %w(public) }
|
|
|
|
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-02-06 16:08:48 -05:00
|
|
|
describe '#extract' do
|
2019-01-02 14:01:11 -05:00
|
|
|
subject { object.extract(directories: directories, to: target_path) }
|
|
|
|
|
2020-09-28 23:09:57 -04:00
|
|
|
shared_examples 'extracts archive' do
|
2019-01-02 14:01:11 -05:00
|
|
|
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, 'source'))).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-28 23:09:57 -04:00
|
|
|
shared_examples 'fails to extract archive' do
|
2019-01-02 14:01:11 -05:00
|
|
|
it 'does not extract archive' do
|
|
|
|
expect { subject }.to raise_error(SafeZip::Extract::Error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-31 15:31:25 -05:00
|
|
|
%w(valid-simple.zip valid-symlinks-first.zip valid-non-writeable.zip).each do |name|
|
2019-01-02 14:01:11 -05:00
|
|
|
context "when using #{name} archive" do
|
|
|
|
let(:archive_name) { name }
|
|
|
|
|
2020-09-28 23:09:57 -04:00
|
|
|
it_behaves_like 'extracts archive'
|
2019-01-02 14:01:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
%w(invalid-symlink-does-not-exist.zip invalid-symlinks-outside.zip).each do |name|
|
|
|
|
context "when using #{name} archive" do
|
|
|
|
let(:archive_name) { name }
|
|
|
|
|
2020-09-28 23:09:57 -04:00
|
|
|
it_behaves_like 'fails to extract archive'
|
2019-01-02 14:01:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no matching directories are found' do
|
|
|
|
let(:archive_name) { 'valid-simple.zip' }
|
|
|
|
let(:directories) { %w(non/existing) }
|
|
|
|
|
2020-09-28 23:09:57 -04:00
|
|
|
it_behaves_like 'fails to extract archive'
|
2019-01-02 14:01:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|