summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/import_export/lfs_restorer_spec.rb
blob: 2b0bdb909ae9da6390ebcf79054e6b82e11526c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'spec_helper'

describe Gitlab::ImportExport::LfsRestorer do
  include UploadHelpers

  let(:export_path) { "#{Dir.tmpdir}/lfs_object_restorer_spec" }
  let(:project) { create(:project) }
  let(:shared) { project.import_export_shared }
  let(:saver) { Gitlab::ImportExport::LfsSaver.new(project: project, shared: shared) }
  subject(:restorer) { 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

  describe '#restore' do
    context 'when the archive contains lfs files' do
      let(:lfs_object) { create(:lfs_object, :correct_oid, :with_file) }

      # Use the LfsSaver to save data to be restored
      def save_lfs_data
        %w(project wiki).each do |repository_type|
          create(
            :lfs_objects_project,
            project: project,
            repository_type: repository_type,
            lfs_object: lfs_object
          )
        end

        saver.save

        project.lfs_objects.delete_all
      end

      before do
        save_lfs_data
        project.reload
      end

      it 'succeeds' do
        expect(restorer.restore).to eq(true)
        expect(shared.errors).to be_empty
      end

      it 'does not create a new `LfsObject` records, as one already exists' do
        expect { restorer.restore }.not_to change { LfsObject.count }
      end

      it 'creates new `LfsObjectsProject` records in order to link the project to the existing `LfsObject`' do
        expect { restorer.restore }.to change { LfsObjectsProject.count }.by(2)
      end

      it 'restores the correct `LfsObject` records' do
        restorer.restore

        expect(project.lfs_objects).to contain_exactly(lfs_object)
      end

      it 'restores the correct `LfsObjectsProject` records for the project' do
        restorer.restore

        expect(
          project.lfs_objects_projects.pluck(:repository_type)
        ).to contain_exactly('project', 'wiki')
      end

      it 'assigns the file correctly' do
        restorer.restore

        expect(project.lfs_objects.first.file.read).to eq(lfs_object.file.read)
      end

      context 'when there is not an existing `LfsObject`' do
        before do
          lfs_object.destroy
        end

        it 'creates a new lfs object' do
          expect { restorer.restore }.to change { LfsObject.count }.by(1)
        end

        it 'stores the upload' do
          expect_any_instance_of(LfsObjectUploader).to receive(:store!)

          restorer.restore
        end
      end

      context 'when there is no lfs-objects.json file' do
        before do
          json_file = File.join(shared.export_path, ::Gitlab::ImportExport.lfs_objects_filename)

          FileUtils.rm_rf(json_file)
        end

        it 'restores the correct `LfsObject` records' do
          restorer.restore

          expect(project.lfs_objects).to contain_exactly(lfs_object)
        end

        it 'restores a single `LfsObjectsProject` record for the project with "project" for the `repository_type`' do
          restorer.restore

          expect(
            project.lfs_objects_projects.pluck(:repository_type)
          ).to contain_exactly('project')
        end
      end
    end

    context 'without any LFS-objects' do
      it 'succeeds' do
        expect(restorer.restore).to be_truthy
        expect(shared.errors).to be_empty
      end
    end
  end
end