summaryrefslogtreecommitdiff
path: root/spec/workers/integrations/create_external_cross_reference_worker_spec.rb
blob: 8e586b909051a9cd270602ebb9fe408ac5e03c31 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Integrations::CreateExternalCrossReferenceWorker do
  include AfterNextHelpers
  using RSpec::Parameterized::TableSyntax

  let_it_be(:project) { create(:project, :with_jira_integration, :repository) }
  let_it_be(:author) { create(:user) }
  let_it_be(:commit) { project.commit }
  let_it_be(:issue) { create(:issue, project: project) }
  let_it_be(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
  let_it_be(:note) { create(:note, project: project) }
  let_it_be(:snippet) { create(:project_snippet, project: project) }

  let(:project_id) { project.id }
  let(:external_issue_id) { 'JIRA-123' }
  let(:mentionable_type) { 'Issue' }
  let(:mentionable_id) { issue.id }
  let(:author_id) { author.id }
  let(:job_args) { [project_id, external_issue_id, mentionable_type, mentionable_id, author_id] }

  def perform
    described_class.new.perform(*job_args)
  end

  before do
    allow(Project).to receive(:find_by_id).and_return(project)
  end

  it_behaves_like 'an idempotent worker' do
    before do
      allow(project.external_issue_tracker).to receive(:create_cross_reference_note)
    end

    it 'can run multiple times with the same arguments' do
      subject

      expect(project.external_issue_tracker).to have_received(:create_cross_reference_note)
        .exactly(worker_exec_times).times
    end
  end

  it 'has the `until_executed` deduplicate strategy' do
    expect(described_class.get_deduplicate_strategy).to eq(:until_executed)
    expect(described_class.get_deduplication_options).to include({ including_scheduled: true })
  end

  # These are the only models where we currently support cross-references,
  # although this should be expanded to all `Mentionable` models.
  # See https://gitlab.com/gitlab-org/gitlab/-/issues/343975
  where(:mentionable_type, :mentionable_id) do
    'Commit'       | lazy { commit.id }
    'Issue'        | lazy { issue.id }
    'MergeRequest' | lazy { merge_request.id }
    'Note'         | lazy { note.id }
    'Snippet'      | lazy { snippet.id }
  end

  with_them do
    it 'creates a cross reference' do
      expect(project.external_issue_tracker).to receive(:create_cross_reference_note).with(
        be_a(ExternalIssue).and(have_attributes(id: external_issue_id, project: project)),
        be_a(mentionable_type.constantize).and(have_attributes(id: mentionable_id)),
        be_a(User).and(have_attributes(id: author_id))
      )

      perform
    end
  end

  describe 'error handling' do
    shared_examples 'does not create a cross reference' do
      it 'does not create a cross reference' do
        expect(project).not_to receive(:external_issue_tracker) if project

        perform
      end
    end

    context 'project_id does not exist' do
      let(:project_id) { non_existing_record_id }
      let(:project) { nil }

      it_behaves_like 'does not create a cross reference'
    end

    context 'author_id does not exist' do
      let(:author_id) { non_existing_record_id }

      it_behaves_like 'does not create a cross reference'
    end

    context 'mentionable_id does not exist' do
      let(:mentionable_id) { non_existing_record_id }

      it_behaves_like 'does not create a cross reference'
    end

    context 'mentionable_type is not a Mentionable' do
      let(:mentionable_type) { 'User' }

      before do
        expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with(kind_of(ArgumentError))
      end

      it_behaves_like 'does not create a cross reference'
    end

    context 'mentionable_type is not a defined constant' do
      let(:mentionable_type) { 'FooBar' }

      before do
        expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with(kind_of(ArgumentError))
      end

      it_behaves_like 'does not create a cross reference'
    end

    context 'mentionable is a Commit and mentionable_id does not exist' do
      let(:mentionable_type) { 'Commit' }
      let(:mentionable_id) { non_existing_record_id }

      it_behaves_like 'does not create a cross reference'
    end
  end
end