File: load_placeholder_references_worker_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (156 lines) | stat: -rw-r--r-- 5,439 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Import::LoadPlaceholderReferencesWorker, feature_category: :importers do
  let(:user) { create(:user) }
  let(:import_source) { 'test_source' }
  let(:uid) { 123 }
  let(:params) { { 'current_user_id' => user.id } }

  describe '#perform' do
    subject(:perform) { described_class.new.perform(import_source, uid, params) }

    it 'executes LoadService' do
      expect_next_instance_of(Import::PlaceholderReferences::LoadService) do |service|
        expect(service).to receive(:execute)
      end

      perform
    end

    it_behaves_like 'an idempotent worker' do
      let(:job_args) { [import_source, uid, params] }
    end

    context 'when importer_user_mapping feature is disabled' do
      before do
        stub_feature_flags(importer_user_mapping: false)
      end

      it 'does not execute LoadService' do
        expect(Import::PlaceholderReferences::LoadService).not_to receive(:new)

        perform
      end

      context 'when importer_user_mapping feature is enabled for the user' do
        before do
          stub_feature_flags(importer_user_mapping: user)
        end

        it 'executes LoadService' do
          expect_next_instance_of(Import::PlaceholderReferences::LoadService) do |service|
            expect(service).to receive(:execute)
          end

          perform
        end
      end
    end
  end

  describe '#sidekiq_retries_exhausted' do
    let_it_be(:project) { create(:project) }

    shared_examples 'failed user contribution mapping' do
      it 'logs the failure and fails the import status record', :aggregate_failures do
        exception = StandardError.new('Some error')

        expect(::Import::Framework::Logger).to receive(:error).with({
          message: 'Failed to load all references to placeholder user contributions',
          error: exception.message,
          import_source: import_source,
          import_uid: uid
        })

        described_class.sidekiq_retries_exhausted_block.call({ 'args' => [import_source, uid] }, exception)

        expect(import_status_record.reload).to be_failed
      end

      # This case should not happen, but in case it does, there should still be a relevant error log anyway
      context 'when an import status object does not exist' do
        let(:import_status_record) { nil }
        let(:uid) { -1 }

        it 'still logs the error without an import object to fail' do
          exception = StandardError.new('Some error')

          expect(::Import::Framework::Logger).to receive(:error).with({
            message: 'Failed to load all references to placeholder user contributions',
            error: exception.message,
            import_source: import_source,
            import_uid: uid
          })

          described_class.sidekiq_retries_exhausted_block.call({ 'args' => [import_source, uid] }, exception)
        end
      end
    end

    context 'when import_source is Direct Transfer' do
      let(:import_status_record) { create(:bulk_import, :started) }
      let(:import_source) { 'gitlab' }
      let(:uid) { import_status_record.id }

      it_behaves_like 'failed user contribution mapping'
    end

    context 'when import_source is GitHub' do
      let(:import_status_record) { create(:import_state, :started, project: project, import_type: 'github') }
      let(:import_source) { 'github' }
      let(:uid) { import_status_record.id }

      it_behaves_like 'failed user contribution mapping'
    end

    context 'when import_source is Bitbucket' do
      let(:import_status_record) { create(:import_state, :started, project: project, import_type: 'bitbucket') }
      let(:import_source) { 'bitbucket' }
      let(:uid) { import_status_record.id }

      it_behaves_like 'failed user contribution mapping'
    end

    context 'when import_source is Bitbucket Server' do
      let(:import_status_record) { create(:import_state, :started, project: project, import_type: 'bitbucket_server') }
      let(:import_source) { 'bitbucket_server' }
      let(:uid) { import_status_record.id }

      it_behaves_like 'failed user contribution mapping'
    end

    context 'when import_source is Gitea' do
      let(:import_status_record) { create(:import_state, :started, project: project, import_type: 'gitea') }
      let(:import_source) { 'gitea' }
      let(:uid) { import_status_record.id }

      it_behaves_like 'failed user contribution mapping'
    end

    context 'when import_source is FogBugz' do
      let(:import_status_record) { create(:import_state, :started, project: project, import_type: 'fogbugz') }
      let(:import_source) { 'fogbugz' }
      let(:uid) { import_status_record.id }

      it_behaves_like 'failed user contribution mapping'
    end

    context 'when import_source is GitLab project export upload' do
      let(:import_status_record) { create(:import_state, :started, project: project, import_type: 'gitlab_project') }
      let(:import_source) { 'gitlab_project' }
      let(:uid) { import_status_record.id }

      it_behaves_like 'failed user contribution mapping'
    end

    context 'when import_source is GitLab group export upload' do
      let(:import_status_record) { create(:group_import_state, :started) }
      let(:import_source) { 'import_group_from_file' }
      let(:uid) { import_status_record.id }

      it_behaves_like 'failed user contribution mapping'
    end
  end
end