2020-04-01 09:07:45 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::JiraImport::LabelsImporter do
|
2020-04-30 06:10:03 +00:00
|
|
|
include JiraServiceHelper
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project, group: group) }
|
2020-04-09 12:09:24 +00:00
|
|
|
let_it_be(:jira_service) { create(:jira_service, project: project) }
|
2020-04-01 09:07:45 +00:00
|
|
|
|
2020-04-30 09:09:39 +00:00
|
|
|
let(:importer) { described_class.new(project) }
|
|
|
|
|
|
|
|
subject { importer.execute }
|
2020-04-01 09:07:45 +00:00
|
|
|
|
|
|
|
before do
|
2020-04-27 09:09:51 +00:00
|
|
|
stub_const('Gitlab::JiraImport::LabelsImporter::MAX_LABELS', 2)
|
2020-04-01 09:07:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute', :clean_gitlab_redis_cache do
|
2020-04-30 06:10:03 +00:00
|
|
|
before do
|
|
|
|
stub_jira_service_test
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when label is missing from jira import' do
|
2020-04-14 18:09:54 +00:00
|
|
|
let_it_be(:no_label_jira_import) { create(:jira_import_state, label: nil, project: project) }
|
2020-04-01 09:07:45 +00:00
|
|
|
|
|
|
|
it 'raises error' do
|
2020-04-21 15:21:10 +00:00
|
|
|
expect { subject }.to raise_error(Projects::ImportService::Error, 'Failed to find import label for Jira import.')
|
2020-04-01 09:07:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-27 09:09:51 +00:00
|
|
|
context 'when jira import label exists' do
|
|
|
|
let_it_be(:label) { create(:label) }
|
2020-04-14 18:09:54 +00:00
|
|
|
let_it_be(:jira_import_with_label) { create(:jira_import_state, label: label, project: project) }
|
2020-04-27 09:09:51 +00:00
|
|
|
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
|
2020-04-30 09:09:39 +00:00
|
|
|
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
|
|
|
|
|
2020-04-27 09:09:51 +00:00
|
|
|
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
|
2020-04-30 09:09:39 +00:00
|
|
|
client = double
|
|
|
|
expect(importer).to receive(:client).and_return(client)
|
|
|
|
allow(client).to receive(:get).and_return(jira_labels)
|
2020-04-27 09:09:51 +00:00
|
|
|
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 } }
|
2020-04-01 09:07:45 +00:00
|
|
|
|
2020-04-27 09:09:51 +00:00
|
|
|
it_behaves_like 'no labels handling'
|
|
|
|
end
|
2020-04-01 09:07:45 +00:00
|
|
|
|
2020-04-27 09:09:51 +00:00
|
|
|
context 'when the isLast argument is missing' do
|
|
|
|
let(:jira_labels) { { "maxResults" => 2, "startAt" => 0, "total" => 3, "values" => %w(bug dev) } }
|
2020-04-01 09:07:45 +00:00
|
|
|
|
2020-04-27 09:09:51 +00:00
|
|
|
it_behaves_like 'no labels handling'
|
|
|
|
end
|
2020-04-01 09:07:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|