summaryrefslogtreecommitdiff
path: root/spec/services/groups/import_export/import_service_spec.rb
blob: d41acbcc2deadb86875fa4918a5f28ddecee04fb (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Groups::ImportExport::ImportService do
  describe '#async_execute' do
    let_it_be(:user) { create(:user) }
    let_it_be(:group) { create(:group) }

    before do
      allow(GroupImportWorker).to receive(:with_status).and_return(GroupImportWorker)
    end

    context 'when the job can be successfully scheduled' do
      subject(:import_service) { described_class.new(group: group, user: user) }

      it 'creates group import state' do
        import_service.async_execute

        import_state = group.import_state

        expect(import_state.user).to eq(user)
        expect(import_state.group).to eq(group)
      end

      it 'enqueues an import job' do
        allow(GroupImportWorker).to receive(:with_status).and_return(GroupImportWorker)

        expect(GroupImportWorker).to receive(:perform_async).with(user.id, group.id)

        import_service.async_execute
      end

      it 'marks the group import as in progress' do
        import_service.async_execute

        expect(group.import_state.in_progress?).to eq true
      end

      it 'returns truthy' do
        expect(import_service.async_execute).to be_truthy
      end
    end

    context 'when the job cannot be scheduled' do
      subject(:import_service) { described_class.new(group: group, user: user) }

      before do
        allow(GroupImportWorker).to receive(:perform_async).and_return(nil)
      end

      it 'returns falsey' do
        expect(import_service.async_execute).to be_falsey
      end

      it 'does not mark the group import as created' do
        expect { import_service.async_execute }.not_to change { group.import_state }
      end
    end
  end

  context 'with group_import_ndjson feature flag disabled' do
    let(:user) { create(:user) }
    let(:group) { create(:group) }
    let(:import_logger) { instance_double(Gitlab::Import::Logger) }

    subject(:service) { described_class.new(group: group, user: user) }

    before do
      stub_feature_flags(group_import_ndjson: false)

      group.add_owner(user)

      ImportExportUpload.create!(group: group, import_file: import_file)

      allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
      allow(import_logger).to receive(:error)
      allow(import_logger).to receive(:info)
    end

    context 'with a json file' do
      let(:import_file) { fixture_file_upload('spec/fixtures/legacy_group_export.tar.gz') }

      it 'uses LegacyTreeRestorer to import the file' do
        expect(Gitlab::ImportExport::Group::LegacyTreeRestorer).to receive(:new).and_call_original

        service.execute
      end

      it 'tracks the event' do
        service.execute

        expect_snowplow_event(
          category: 'Groups::ImportExport::ImportService',
          action: 'create',
          label: 'import_group_from_file'
        )
      end
    end

    context 'with a ndjson file' do
      let(:import_file) { fixture_file_upload('spec/fixtures/group_export.tar.gz') }

      it 'fails to import' do
        expect { service.execute }.to raise_error(Gitlab::ImportExport::Error, 'Incorrect JSON format')
      end
    end
  end

  context 'with group_import_ndjson feature flag enabled' do
    before do
      stub_feature_flags(group_import_ndjson: true)
    end

    context 'when importing a ndjson export' do
      let(:user) { create(:user) }
      let(:group) { create(:group) }
      let(:import_file) { fixture_file_upload('spec/fixtures/group_export.tar.gz') }

      let(:import_logger) { instance_double(Gitlab::Import::Logger) }

      subject(:service) { described_class.new(group: group, user: user) }

      before do
        ImportExportUpload.create!(group: group, import_file: import_file)

        allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
        allow(import_logger).to receive(:error)
        allow(import_logger).to receive(:info)
        allow(import_logger).to receive(:warn)
        allow(FileUtils).to receive(:rm_rf).and_call_original
      end

      context 'when user has correct permissions' do
        before do
          group.add_owner(user)
        end

        it 'imports group structure successfully' do
          expect(service.execute).to be_truthy
        end

        it 'tracks the event' do
          service.execute

          expect_snowplow_event(
            category: 'Groups::ImportExport::ImportService',
            action: 'create',
            label: 'import_group_from_file'
          )

          expect_snowplow_event(
            category: 'Groups::ImportExport::ImportService',
            action: 'create',
            label: 'import_access_level',
            user: user,
            extra: { user_role: 'Owner', import_type: 'import_group_from_file' }
          )
        end

        it 'removes import file' do
          service.execute

          expect(group.import_export_upload.import_file.file).to be_nil
        end

        it 'removes tmp files' do
          shared = Gitlab::ImportExport::Shared.new(group)
          allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)

          service.execute

          expect(FileUtils).to have_received(:rm_rf).with(shared.base_path)
          expect(Dir.exist?(shared.base_path)).to eq(false)
        end

        it 'logs the import success' do
          expect(import_logger).to receive(:info).with(
            group_id: group.id,
            group_name: group.name,
            message: 'Group Import/Export: Import succeeded'
          ).once

          service.execute
        end
      end

      context 'when user does not have correct permissions' do
        it 'logs the error and raises an exception' do
          expect(import_logger).to receive(:error).with(
            group_id: group.id,
            group_name: group.name,
            message: a_string_including('Errors occurred')
          )

          expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
        end

        it 'tracks the error' do
          shared = Gitlab::ImportExport::Shared.new(group)
          allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)

          expect(shared).to receive(:error) do |param|
            expect(param.message).to include 'does not have required permissions for'
          end

          expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
        end
      end

      context 'when there are errors with the import file' do
        let(:import_file) { fixture_file_upload('spec/fixtures/symlink_export.tar.gz') }

        it 'logs the error and raises an exception' do
          expect(import_logger).to receive(:error).with(
            group_id: group.id,
            group_name: group.name,
            message: a_string_including('Errors occurred')
          ).once

          expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
        end
      end

      context 'when there are errors with the sub-relations' do
        let(:import_file) { fixture_file_upload('spec/fixtures/group_export_invalid_subrelations.tar.gz') }

        before do
          group.add_owner(user)
        end

        it 'successfully imports the group' do
          expect(service.execute).to be_truthy
        end

        it 'logs the import success' do
          allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)

          expect(import_logger).to receive(:info).with(
            group_id: group.id,
            group_name: group.name,
            message: 'Group Import/Export: Import succeeded'
          )

          service.execute

          expect_snowplow_event(
            category: 'Groups::ImportExport::ImportService',
            action: 'create',
            label: 'import_access_level',
            user: user,
            extra: { user_role: 'Owner', import_type: 'import_group_from_file' }
          )
        end
      end
    end

    context 'when importing a json export' do
      let(:user) { create(:user) }
      let(:group) { create(:group) }
      let(:import_file) { fixture_file_upload('spec/fixtures/legacy_group_export.tar.gz') }

      let(:import_logger) { instance_double(Gitlab::Import::Logger) }

      subject(:service) { described_class.new(group: group, user: user) }

      before do
        ImportExportUpload.create!(group: group, import_file: import_file)

        allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
        allow(import_logger).to receive(:error)
        allow(import_logger).to receive(:warn)
        allow(import_logger).to receive(:info)
        allow(FileUtils).to receive(:rm_rf).and_call_original
      end

      context 'when user has correct permissions' do
        before do
          group.add_owner(user)
        end

        it 'imports group structure successfully' do
          expect(service.execute).to be_truthy
        end

        it 'tracks the event' do
          service.execute

          expect_snowplow_event(
            category: 'Groups::ImportExport::ImportService',
            action: 'create',
            label: 'import_group_from_file'
          )

          expect_snowplow_event(
            category: 'Groups::ImportExport::ImportService',
            action: 'create',
            label: 'import_access_level',
            user: user,
            extra: { user_role: 'Owner', import_type: 'import_group_from_file' }
          )
        end

        it 'removes import file' do
          service.execute

          expect(group.import_export_upload.import_file.file).to be_nil
        end

        it 'removes tmp files' do
          shared = Gitlab::ImportExport::Shared.new(group)
          allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)

          service.execute

          expect(FileUtils).to have_received(:rm_rf).with(shared.base_path)
          expect(Dir.exist?(shared.base_path)).to eq(false)
        end

        it 'logs the import success' do
          expect(import_logger).to receive(:info).with(
            group_id: group.id,
            group_name: group.name,
            message: 'Group Import/Export: Import succeeded'
          ).once

          service.execute
        end
      end

      context 'when user does not have correct permissions' do
        it 'logs the error and raises an exception' do
          expect(import_logger).to receive(:error).with(
            group_id: group.id,
            group_name: group.name,
            message: a_string_including('Errors occurred')
          )

          expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
        end

        it 'tracks the error' do
          shared = Gitlab::ImportExport::Shared.new(group)
          allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)

          expect(shared).to receive(:error) do |param|
            expect(param.message).to include 'does not have required permissions for'
          end

          expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
        end
      end

      context 'when there are errors with the import file' do
        let(:import_file) { fixture_file_upload('spec/fixtures/legacy_symlink_export.tar.gz') }

        it 'logs the error and raises an exception' do
          expect(import_logger).to receive(:error).with(
            group_id: group.id,
            group_name: group.name,
            message: a_string_including('Errors occurred')
          ).once

          expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
        end
      end

      context 'when there are errors with the sub-relations' do
        let(:import_file) { fixture_file_upload('spec/fixtures/legacy_group_export_invalid_subrelations.tar.gz') }

        before do
          group.add_owner(user)
        end

        it 'successfully imports the group' do
          expect(service.execute).to be_truthy
        end

        it 'tracks the event' do
          service.execute

          expect_snowplow_event(
            category: 'Groups::ImportExport::ImportService',
            action: 'create',
            label: 'import_group_from_file'
          )

          expect_snowplow_event(
            category: 'Groups::ImportExport::ImportService',
            action: 'create',
            label: 'import_access_level',
            user: user,
            extra: { user_role: 'Owner', import_type: 'import_group_from_file' }
          )
        end

        it 'logs the import success' do
          allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)

          expect(import_logger).to receive(:info).with(
            group_id: group.id,
            group_name: group.name,
            message: 'Group Import/Export: Import succeeded'
          )

          service.execute
        end
      end
    end
  end
end