summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/jira_import/labels_importer_spec.rb
blob: 67eb541d3761dffc6d32a11695235a0e48f9977a (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::JiraImport::LabelsImporter do
  include JiraServiceHelper

  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project, group: group) }
  let_it_be(:jira_service) { create(:jira_service, project: project) }

  let(:importer) { described_class.new(project) }

  subject { importer.execute }

  before do
    stub_feature_flags(jira_issue_import: true)
    stub_const('Gitlab::JiraImport::LabelsImporter::MAX_LABELS', 2)
  end

  describe '#execute', :clean_gitlab_redis_cache do
    before do
      stub_jira_service_test
    end

    context 'when label is missing from jira import' do
      let_it_be(:no_label_jira_import) { create(:jira_import_state, label: nil, project: project) }

      it 'raises error' do
        expect { subject }.to raise_error(Projects::ImportService::Error, 'Failed to find import label for Jira import.')
      end
    end

    context 'when jira import label exists' do
      let_it_be(:label)                  { create(:label) }
      let_it_be(:jira_import_with_label) { create(:jira_import_state, label: label, project: project) }
      let_it_be(:issue_label)            { create(:label, project: project, title: 'bug') }

      let(:jira_labels_1) { { "maxResults" => 2, "startAt" => 0, "total" => 3, "isLast" => false, "values" => %w(backend bug) } }
      let(:jira_labels_2) { { "maxResults" => 2, "startAt" => 2, "total" => 3, "isLast" => true, "values" => %w(feature) } }

      context 'when labels are returned from jira' do
        before do
          client = double
          expect(importer).to receive(:client).twice.and_return(client)
          allow(client).to receive(:get).twice.and_return(jira_labels_1, jira_labels_2)
        end

        it 'caches import label' do
          expect(Gitlab::Cache::Import::Caching.read(Gitlab::JiraImport.import_label_cache_key(project.id))).to be nil

          subject

          expect(Gitlab::JiraImport.get_import_label_id(project.id).to_i).to eq(label.id)
        end

        it 'calls Gitlab::JiraImport::HandleLabelsService' do
          expect(Gitlab::JiraImport::HandleLabelsService).to receive(:new).with(project, %w(backend bug)).and_return(double(execute: [1, 2]))
          expect(Gitlab::JiraImport::HandleLabelsService).to receive(:new).with(project, %w(feature)).and_return(double(execute: [3]))

          subject
        end
      end

      context 'when there are no labels to be handled' do
        shared_examples 'no labels handling' do
          it 'does not call Gitlab::JiraImport::HandleLabelsService' do
            expect(Gitlab::JiraImport::HandleLabelsService).not_to receive(:new)

            subject
          end
        end

        let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "total" => 3, "values" => [] } }

        before do
          client = double
          expect(importer).to receive(:client).and_return(client)
          allow(client).to receive(:get).and_return(jira_labels)
        end

        context 'when the labels field is empty' do
          let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "isLast" => true, "total" => 3, "values" => [] } }

          it_behaves_like 'no labels handling'
        end

        context 'when the labels field is missing' do
          let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "isLast" => true, "total" => 3 } }

          it_behaves_like 'no labels handling'
        end

        context 'when the isLast argument is missing' do
          let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "total" => 3, "values" => %w(bug dev) } }

          it_behaves_like 'no labels handling'
        end
      end
    end
  end
end