summaryrefslogtreecommitdiff
path: root/spec/services/jira_import/start_import_service_spec.rb
blob: e04e3314158a87c052eae46db2c0812d9cb6b9b9 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe JiraImport::StartImportService do
  include JiraServiceHelper

  let_it_be(:user) { create(:user) }
  let_it_be(:project, reload: true) { create(:project) }

  let(:key) { 'KEY' }
  let(:mapping) do
    [
      { jira_account_id: 'abc', gitlab_id: 12 },
      { jira_account_id: 'def', gitlab_id: nil },
      { jira_account_id: nil, gitlab_id: 1 }
    ]
  end

  subject { described_class.new(user, project, key, mapping).execute }

  context 'when an error is returned from the project validation' do
    before do
      allow(Gitlab::JiraImport).to receive(:validate_project_settings!)
        .and_raise(Projects::ImportService::Error, 'Jira import feature is disabled.')
    end

    it_behaves_like 'responds with error', 'Jira import feature is disabled.'
  end

  context 'when project validation is ok' do
    let!(:jira_integration) { create(:jira_integration, project: project, active: true) }

    before do
      stub_jira_integration_test
      allow(Gitlab::JiraImport).to receive(:validate_project_settings!)
    end

    context 'when Jira project key is not provided' do
      let(:key) { '' }

      it_behaves_like 'responds with error', 'Unable to find Jira project to import data from.'
    end

    context 'when correct data provided' do
      let(:fake_key)  { 'some-key' }

      subject { described_class.new(user, project, fake_key, mapping).execute }

      context 'when import is already running' do
        let_it_be(:jira_import_state) { create(:jira_import_state, :started, project: project) }

        it_behaves_like 'responds with error', 'Jira import is already running.'
      end

      context 'when an error is raised while scheduling import' do
        before do
          expect_next_instance_of(JiraImportState) do |jira_impport|
            expect(jira_impport).to receive(:schedule!).and_raise(Projects::ImportService::Error, 'Unexpected failure.')
          end
        end

        it_behaves_like 'responds with error', 'Unexpected failure.'

        it 'saves the error message' do
          subject

          expect(JiraImportState.last.error_message).to eq('Unexpected failure.')
        end
      end

      context 'when everything is ok' do
        context 'with complete mapping' do
          before do
            expect(Gitlab::JiraImport).to receive(:cache_users_mapping).with(project.id, { 'abc' => 12 })
          end

          it 'returns success response' do
            expect(subject).to be_a(ServiceResponse)
            expect(subject).to be_success
          end

          it 'schedules Jira import' do
            subject

            expect(project.latest_jira_import).to be_scheduled
          end

          it 'creates Jira import data', :aggregate_failures do
            jira_import = subject.payload[:import_data]

            expect(jira_import.jira_project_xid).to eq(0)
            expect(jira_import.jira_project_name).to eq(fake_key)
            expect(jira_import.jira_project_key).to eq(fake_key)
            expect(jira_import.user).to eq(user)
          end

          it 'creates Jira import label' do
            expect { subject }.to change { Label.count }.by(1)
          end

          it 'creates Jira label title with correct number' do
            jira_import = subject.payload[:import_data]
            label_title = "jira-import::#{jira_import.jira_project_key}-1"

            expect(jira_import.label.title).to eq(label_title)
          end
        end

        context 'when mapping is nil' do
          let(:mapping) { nil }

          it 'returns success response' do
            expect(Gitlab::JiraImport).not_to receive(:cache_users_mapping)

            expect(subject).to be_a(ServiceResponse)
            expect(subject).to be_success
          end
        end

        context 'when no mapping value is complete' do
          let(:mapping) do
            [
              { jira_account_id: 'def', gitlab_id: nil },
              { jira_account_id: nil, gitlab_id: 1 }
            ]
          end

          it 'returns success response' do
            expect(Gitlab::JiraImport).not_to receive(:cache_users_mapping)

            expect(subject).to be_a(ServiceResponse)
            expect(subject).to be_success
          end
        end
      end

      context 'when multiple Jira imports for same Jira project' do
        let!(:jira_imports) { create_list(:jira_import_state, 3, :finished, project: project, jira_project_key: fake_key)}

        it 'creates Jira label title with correct number' do
          jira_import = subject.payload[:import_data]
          label_title = "jira-import::#{jira_import.jira_project_key}-4"

          expect(jira_import.label.title).to eq(label_title)
        end
      end
    end
  end
end