summaryrefslogtreecommitdiff
path: root/spec/workers/bulk_imports/export_request_worker_spec.rb
blob: a7f7aaa7dbaa60b9f64518d14304ffe515877d7a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::ExportRequestWorker do
  let_it_be(:bulk_import) { create(:bulk_import) }
  let_it_be(:config) { create(:bulk_import_configuration, bulk_import: bulk_import) }
  let_it_be(:version_url) { 'https://gitlab.example/api/v4/version' }

  let(:response_double) { double(code: 200, success?: true, parsed_response: {}) }
  let(:job_args) { [entity.id] }

  describe '#perform' do
    before do
      allow(Gitlab::HTTP)
        .to receive(:get)
        .with(version_url, anything)
        .and_return(double(code: 200, success?: true, parsed_response: { 'version' => Gitlab::VERSION }))
      allow(Gitlab::HTTP).to receive(:post).and_return(response_double)
    end

    shared_examples 'requests relations export for api resource' do
      include_examples 'an idempotent worker' do
        it 'requests relations export' do
          expect_next_instance_of(BulkImports::Clients::HTTP) do |client|
            expect(client).to receive(:post).with(expected).twice
          end

          perform_multiple(job_args)
        end

        context 'when network error is raised' do
          it 'logs export failure and marks entity as failed' do
            expect_next_instance_of(BulkImports::Clients::HTTP) do |client|
              expect(client).to receive(:post).and_raise(BulkImports::NetworkError, 'Export error').twice
            end

            expect(Gitlab::Import::Logger).to receive(:error).with(
              hash_including(
                'bulk_import_entity_id' => entity.id,
                'pipeline_class' => 'ExportRequestWorker',
                'exception_class' => 'BulkImports::NetworkError',
                'exception_message' => 'Export error',
                'correlation_id_value' => anything,
                'bulk_import_id' => bulk_import.id,
                'bulk_import_entity_type' => entity.source_type
              )
            ).twice

            perform_multiple(job_args)

            failure = entity.failures.last

            expect(failure.pipeline_class).to eq('ExportRequestWorker')
            expect(failure.exception_message).to eq('Export error')
          end
        end
      end
    end

    context 'when entity is group' do
      let(:entity) { create(:bulk_import_entity, :group_entity, source_full_path: 'foo/bar', bulk_import: bulk_import) }
      let(:expected) { '/groups/foo%2Fbar/export_relations' }

      include_examples 'requests relations export for api resource'
    end

    context 'when entity is project' do
      let(:entity) { create(:bulk_import_entity, :project_entity, source_full_path: 'foo/bar', bulk_import: bulk_import) }
      let(:expected) { '/projects/foo%2Fbar/export_relations' }

      include_examples 'requests relations export for api resource'
    end
  end
end