uploads_manager_spec.rb 2.3 KB
Newer Older
J
James Lopez 已提交
1 2 3 4 5 6
require 'spec_helper'

describe Gitlab::ImportExport::UploadsManager do
  let(:shared) { project.import_export_shared }
  let(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec" }
  let(:project) { create(:project) }
J
James Lopez 已提交
7
  let(:exported_file_path) { "#{shared.export_path}/uploads/#{upload.secret}/#{File.basename(upload.path)}" }
J
James Lopez 已提交
8 9 10 11 12 13 14 15 16 17 18 19

  subject(:manager) { described_class.new(project: project, shared: shared) }

  before do
    allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
    FileUtils.mkdir_p(shared.export_path)
  end

  after do
    FileUtils.rm_rf(shared.export_path)
  end

J
James Lopez 已提交
20
  describe '#save' do
J
James Lopez 已提交
21
    context 'when the project has uploads locally stored' do
J
James Lopez 已提交
22
      let(:upload) { create(:upload, :issuable_upload, :with_file, model: project) }
J
James Lopez 已提交
23 24 25 26 27 28

      before do
        project.uploads << upload
      end

      it 'does not cause errors' do
J
James Lopez 已提交
29
        manager.save
J
James Lopez 已提交
30 31 32 33 34

        expect(shared.errors).to be_empty
      end

      it 'copies the file in the correct location when there is an upload' do
J
James Lopez 已提交
35 36 37
        manager.save

        puts exported_file_path
J
James Lopez 已提交
38

J
James Lopez 已提交
39 40 41 42 43 44 45 46 47 48 49 50
        expect(File).to exist(exported_file_path)
      end
    end

    context 'using object storage' do
      let!(:upload) { create(:upload, :issuable_upload, :object_storage, model: project) }

      before do
        stub_feature_flags(import_export_object_storage: true)
        stub_uploads_object_storage(FileUploader)
      end

J
James Lopez 已提交
51
      it 'saves the file' do
J
James Lopez 已提交
52 53 54 55 56
        fake_uri = double

        expect(fake_uri).to receive(:open).and_return(StringIO.new('File content'))
        expect(URI).to receive(:parse).and_return(fake_uri)

J
James Lopez 已提交
57
        manager.save
J
James Lopez 已提交
58 59

        expect(File.read(exported_file_path)).to eq('File content')
J
James Lopez 已提交
60 61
      end
    end
J
James Lopez 已提交
62 63 64 65 66 67 68 69 70 71 72

    describe '#restore' do
      context 'using object storage' do
        before do
          stub_feature_flags(import_export_object_storage: true)
          stub_uploads_object_storage(FileUploader)

          FileUtils.mkdir_p(File.join(shared.export_path, 'uploads/random'))
          FileUtils.touch(File.join(shared.export_path, 'uploads/random', "dummy.txt"))
        end

J
James Lopez 已提交
73
        it 'restores the file' do
J
James Lopez 已提交
74 75 76 77 78 79 80
          manager.restore

          expect(project.uploads.size).to eq(1)
          expect(project.uploads.first.build_uploader.filename).to eq('dummy.txt')
        end
      end
    end
J
James Lopez 已提交
81 82
  end
end