diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
commit | a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4 (patch) | |
tree | fb69158581673816a8cd895f9d352dcb3c678b1e /spec/services/bulk_imports/file_decompression_service_spec.rb | |
parent | d16b2e8639e99961de6ddc93909f3bb5c1445ba1 (diff) |
Add latest changes from gitlab-org/gitlab@14-0-stable-eev14.0.0-rc42
Diffstat (limited to 'spec/services/bulk_imports/file_decompression_service_spec.rb')
-rw-r--r-- | spec/services/bulk_imports/file_decompression_service_spec.rb | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/spec/services/bulk_imports/file_decompression_service_spec.rb b/spec/services/bulk_imports/file_decompression_service_spec.rb new file mode 100644 index 00000000000..4e8f78c8243 --- /dev/null +++ b/spec/services/bulk_imports/file_decompression_service_spec.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BulkImports::FileDecompressionService do + let_it_be(:tmpdir) { Dir.mktmpdir } + let_it_be(:ndjson_filename) { 'labels.ndjson' } + let_it_be(:ndjson_filepath) { File.join(tmpdir, ndjson_filename) } + let_it_be(:gz_filename) { "#{ndjson_filename}.gz" } + let_it_be(:gz_filepath) { "spec/fixtures/bulk_imports/gz/#{gz_filename}" } + + before do + FileUtils.copy_file(gz_filepath, File.join(tmpdir, gz_filename)) + FileUtils.remove_entry(ndjson_filepath) if File.exist?(ndjson_filepath) + end + + after(:all) do + FileUtils.remove_entry(tmpdir) + end + + subject { described_class.new(dir: tmpdir, filename: gz_filename) } + + describe '#execute' do + it 'decompresses specified file' do + subject.execute + + expect(File.exist?(File.join(tmpdir, ndjson_filename))).to eq(true) + expect(File.open(ndjson_filepath, &:readline)).to include('title', 'description') + end + + context 'when validate_import_decompressed_archive_size feature flag is enabled' do + before do + stub_feature_flags(validate_import_decompressed_archive_size: true) + end + + it 'performs decompressed file size validation' do + expect_next_instance_of(Gitlab::ImportExport::DecompressedArchiveSizeValidator) do |validator| + expect(validator).to receive(:valid?).and_return(true) + end + + subject.execute + end + end + + context 'when validate_import_decompressed_archive_size feature flag is disabled' do + before do + stub_feature_flags(validate_import_decompressed_archive_size: false) + end + + it 'does not perform decompressed file size validation' do + expect(Gitlab::ImportExport::DecompressedArchiveSizeValidator).not_to receive(:new) + + subject.execute + end + end + + context 'when dir is not in tmpdir' do + subject { described_class.new(dir: '/etc', filename: 'filename') } + + it 'raises an error' do + expect { subject.execute }.to raise_error(described_class::ServiceError, 'Invalid target directory') + end + end + + context 'when compressed file is a symlink' do + let_it_be(:symlink) { File.join(tmpdir, 'symlink.gz') } + + before do + FileUtils.ln_s(File.join(tmpdir, gz_filename), symlink) + end + + subject { described_class.new(dir: tmpdir, filename: 'symlink.gz') } + + it 'raises an error and removes the file' do + expect { subject.execute }.to raise_error(described_class::ServiceError, 'Invalid file') + + expect(File.exist?(symlink)).to eq(false) + end + end + + context 'when decompressed file is a symlink' do + let_it_be(:symlink) { File.join(tmpdir, 'symlink') } + + before do + FileUtils.ln_s(File.join(tmpdir, ndjson_filename), symlink) + + subject.instance_variable_set(:@decompressed_filepath, symlink) + end + + subject { described_class.new(dir: tmpdir, filename: gz_filename) } + + it 'raises an error and removes the file' do + expect { subject.execute }.to raise_error(described_class::ServiceError, 'Invalid file') + + expect(File.exist?(symlink)).to eq(false) + end + end + end +end |