file_importer_spec.rb 3.3 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::ImportExport::FileImporter do
4 5
  include ExportFileHelper

6
  let(:shared) { Gitlab::ImportExport::Shared.new(nil) }
7
  let(:storage_path) { "#{Dir.tmpdir}/file_importer_spec" }
8 9
  let(:valid_file) { "#{shared.export_path}/valid.json" }
  let(:symlink_file) { "#{shared.export_path}/invalid.json" }
10
  let(:hidden_symlink_file) { "#{shared.export_path}/.hidden" }
11
  let(:subfolder_symlink_file) { "#{shared.export_path}/subfolder/invalid.json" }
12
  let(:evil_symlink_file) { "#{shared.export_path}/.\nevil" }
13
  let(:custom_mode_symlink_file) { "#{shared.export_path}/symlink.mode" }
14 15 16

  before do
    stub_const('Gitlab::ImportExport::FileImporter::MAX_RETRIES', 0)
17 18 19
    stub_uploads_object_storage(FileUploader)

    allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(storage_path)
20
    allow_any_instance_of(Gitlab::ImportExport::CommandLineUtil).to receive(:untar_zxf).and_return(true)
21
    allow_any_instance_of(Gitlab::ImportExport::Shared).to receive(:relative_archive_path).and_return('test')
22
    allow(SecureRandom).to receive(:hex).and_return('abcd')
23 24 25 26
    setup_files
  end

  after do
27
    FileUtils.rm_rf(storage_path)
28 29
  end

30 31
  context 'normal run' do
    before do
32
      described_class.import(project: build(:project), archive_file: '', shared: shared)
33
    end
34

35 36 37 38 39 40 41 42
    it 'removes symlinks in root folder' do
      expect(File.exist?(symlink_file)).to be false
    end

    it 'removes hidden symlinks in root folder' do
      expect(File.exist?(hidden_symlink_file)).to be false
    end

43 44 45 46
    it 'removes evil symlinks in root folder' do
      expect(File.exist?(evil_symlink_file)).to be false
    end

47 48 49
    it 'removes symlinks in subfolders' do
      expect(File.exist?(subfolder_symlink_file)).to be false
    end
50

51 52 53 54
    it 'removes symlinks without any file permissions' do
      expect(File.exist?(custom_mode_symlink_file)).to be false
    end

55 56 57 58
    it 'does not remove a valid file' do
      expect(File.exist?(valid_file)).to be true
    end

59 60 61 62
    it 'does not change a valid file permissions' do
      expect(file_permissions(valid_file)).not_to eq(0000)
    end

63 64 65
    it 'creates the file in the right subfolder' do
      expect(shared.export_path).to include('test/abcd')
    end
66 67
  end

68 69 70
  context 'error' do
    before do
      allow_any_instance_of(described_class).to receive(:wait_for_archived_file).and_raise(StandardError)
71
      described_class.import(project: build(:project), archive_file: '', shared: shared)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    end

    it 'removes symlinks in root folder' do
      expect(File.exist?(symlink_file)).to be false
    end

    it 'removes hidden symlinks in root folder' do
      expect(File.exist?(hidden_symlink_file)).to be false
    end

    it 'removes symlinks in subfolders' do
      expect(File.exist?(subfolder_symlink_file)).to be false
    end

    it 'does not remove a valid file' do
      expect(File.exist?(valid_file)).to be true
    end
89 90 91 92 93 94 95
  end

  def setup_files
    FileUtils.mkdir_p("#{shared.export_path}/subfolder/")
    FileUtils.touch(valid_file)
    FileUtils.ln_s(valid_file, symlink_file)
    FileUtils.ln_s(valid_file, subfolder_symlink_file)
96 97
    FileUtils.ln_s(valid_file, hidden_symlink_file)
    FileUtils.ln_s(valid_file, evil_symlink_file)
98 99
    FileUtils.ln_s(valid_file, custom_mode_symlink_file)
    FileUtils.chmod_R(0000, custom_mode_symlink_file)
100 101
  end
end