summaryrefslogtreecommitdiff
path: root/spec/services/bulk_imports/uploads_export_service_spec.rb
blob: ad6e005485c880c4788503f1c3d733cd1f2f00c8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::UploadsExportService do
  let_it_be(:export_path) { Dir.mktmpdir }
  let_it_be(:project) { create(:project, avatar: fixture_file_upload('spec/fixtures/rails_sample.png', 'image/png')) }

  let!(:upload) { create(:upload, :with_file, :issuable_upload, uploader: FileUploader, model: project) }
  let(:exported_filepath) { File.join(export_path, upload.secret, upload.retrieve_uploader.filename) }

  subject(:service) { described_class.new(project, export_path) }

  after do
    FileUtils.remove_entry(export_path) if Dir.exist?(export_path)
  end

  describe '#execute' do
    it 'exports project uploads and avatar' do
      service.execute

      expect(File).to exist(File.join(export_path, 'avatar', 'rails_sample.png'))
      expect(File).to exist(exported_filepath)
    end

    context 'when upload has underlying file missing' do
      context 'with an upload missing its file' do
        it 'does not cause errors' do
          File.delete(upload.absolute_path)

          expect { service.execute }.not_to raise_error

          expect(File).not_to exist(exported_filepath)
        end
      end

      context 'when upload is in object storage' do
        before do
          stub_uploads_object_storage(FileUploader)
        end

        shared_examples 'export with invalid upload' do
          it 'ignores problematic upload and logs exception' do
            allow(service).to receive(:download_or_copy_upload).and_raise(exception)

            expect(Gitlab::ErrorTracking)
              .to receive(:log_exception)
              .with(
                instance_of(exception), {
                  portable_id: project.id,
                  portable_class: 'Project',
                  upload_id: upload.id
                }
              )

            service.execute

            expect(File).not_to exist(exported_filepath)
          end
        end

        context 'when filename is too long' do
          let(:exception) { Errno::ENAMETOOLONG }

          include_examples 'export with invalid upload'
        end

        context 'when network exception occurs' do
          let(:exception) { Net::OpenTimeout }

          include_examples 'export with invalid upload'
        end
      end
    end
  end
end