2020-05-29 21:08:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe JiraImport::UsersImporter do
|
2020-05-29 21:08:35 +00:00
|
|
|
include JiraServiceHelper
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
2020-11-13 15:09:24 +00:00
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project, reload: true) { create(:project, group: group) }
|
2020-05-29 21:08:35 +00:00
|
|
|
let_it_be(:start_at) { 7 }
|
|
|
|
|
|
|
|
let(:importer) { described_class.new(user, project, start_at) }
|
|
|
|
|
|
|
|
subject { importer.execute }
|
|
|
|
|
|
|
|
describe '#execute' do
|
2020-08-18 15:10:33 +00:00
|
|
|
let(:mapped_users) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
jira_account_id: 'acc1',
|
2020-11-13 15:09:24 +00:00
|
|
|
jira_display_name: 'user-name1',
|
2020-08-18 15:10:33 +00:00
|
|
|
jira_email: 'sample@jira.com',
|
2020-11-13 15:09:24 +00:00
|
|
|
gitlab_id: project_member.id
|
2020-08-18 15:10:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
jira_account_id: 'acc2',
|
2020-11-13 15:09:24 +00:00
|
|
|
jira_display_name: 'user-name2',
|
2020-08-18 15:10:33 +00:00
|
|
|
jira_email: nil,
|
2020-11-13 15:09:24 +00:00
|
|
|
gitlab_id: group_member.id
|
2020-08-18 15:10:33 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-05-29 21:08:35 +00:00
|
|
|
before do
|
|
|
|
stub_jira_service_test
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when Jira import is not configured properly' do
|
2020-06-08 15:08:20 +00:00
|
|
|
it 'returns an error' do
|
|
|
|
expect(subject.errors).to eq(['Jira integration not configured.'])
|
2020-05-29 21:08:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-27 12:10:56 +00:00
|
|
|
RSpec.shared_examples 'maps Jira users to GitLab users' do |users_mapper_service:|
|
2020-08-18 15:10:33 +00:00
|
|
|
context 'when Jira import is configured correctly' do
|
2021-05-27 12:10:56 +00:00
|
|
|
let_it_be(:jira_service) { create(:jira_service, project: project, active: true, url: "http://jira.example.net") }
|
2020-05-29 21:08:35 +00:00
|
|
|
|
2021-05-27 12:10:56 +00:00
|
|
|
context 'when users mapper service raises an error' do
|
2021-03-12 21:09:12 +00:00
|
|
|
let(:error) { Timeout::Error.new }
|
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
it 'returns an error response' do
|
2021-05-27 12:10:56 +00:00
|
|
|
expect_next_instance_of(users_mapper_service) do |instance|
|
|
|
|
expect(instance).to receive(:execute).and_raise(error)
|
|
|
|
end
|
2020-05-29 21:08:35 +00:00
|
|
|
|
2021-05-27 12:10:56 +00:00
|
|
|
expect(Gitlab::ErrorTracking).to receive(:log_exception).with(error, project_id: project.id)
|
2020-08-18 15:10:33 +00:00
|
|
|
expect(subject.error?).to be_truthy
|
|
|
|
expect(subject.message).to include('There was an error when communicating to Jira')
|
2020-05-29 21:08:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-27 12:10:56 +00:00
|
|
|
context 'when users mapper service returns result' do
|
|
|
|
context 'when users mapper service returns an empty array' do
|
2020-11-13 15:09:24 +00:00
|
|
|
it 'returns nil payload' do
|
2021-05-27 12:10:56 +00:00
|
|
|
expect_next_instance_of(users_mapper_service) do |instance|
|
|
|
|
expect(instance).to receive(:execute).and_return([])
|
|
|
|
end
|
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
expect(subject.success?).to be_truthy
|
|
|
|
expect(subject.payload).to be_empty
|
|
|
|
end
|
2020-05-29 21:08:35 +00:00
|
|
|
end
|
|
|
|
|
2021-05-27 12:10:56 +00:00
|
|
|
context 'when Jira client returns any users' do
|
2020-11-13 15:09:24 +00:00
|
|
|
let_it_be(:project_member) { create(:user, email: 'sample@jira.com') }
|
|
|
|
let_it_be(:group_member) { create(:user, name: 'user-name2') }
|
|
|
|
let_it_be(:other_user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(project_member)
|
|
|
|
group.add_developer(group_member)
|
|
|
|
end
|
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
it 'returns the mapped users' do
|
2021-05-27 12:10:56 +00:00
|
|
|
expect_next_instance_of(users_mapper_service) do |instance|
|
|
|
|
expect(instance).to receive(:execute).and_return(mapped_users)
|
|
|
|
end
|
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
expect(subject.success?).to be_truthy
|
|
|
|
expect(subject.payload).to eq(mapped_users)
|
|
|
|
end
|
2020-05-29 21:08:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-18 15:10:33 +00:00
|
|
|
|
|
|
|
context 'when Jira instance is of Server deployment type' do
|
|
|
|
before do
|
2021-05-27 12:10:56 +00:00
|
|
|
allow(project).to receive(:jira_service).and_return(jira_service)
|
|
|
|
|
|
|
|
jira_service.data_fields.deployment_server!
|
2020-08-18 15:10:33 +00:00
|
|
|
end
|
|
|
|
|
2021-05-27 12:10:56 +00:00
|
|
|
it_behaves_like 'maps Jira users to GitLab users', users_mapper_service: JiraImport::ServerUsersMapperService
|
2020-08-18 15:10:33 +00:00
|
|
|
end
|
|
|
|
|
2021-05-27 12:10:56 +00:00
|
|
|
context 'when Jira instance is of Cloud deployment type' do
|
2020-08-18 15:10:33 +00:00
|
|
|
before do
|
2021-05-27 12:10:56 +00:00
|
|
|
allow(project).to receive(:jira_service).and_return(jira_service)
|
|
|
|
|
|
|
|
jira_service.data_fields.deployment_cloud!
|
2020-08-18 15:10:33 +00:00
|
|
|
end
|
|
|
|
|
2021-05-27 12:10:56 +00:00
|
|
|
it_behaves_like 'maps Jira users to GitLab users', users_mapper_service: JiraImport::CloudUsersMapperService
|
2020-08-18 15:10:33 +00:00
|
|
|
end
|
2020-05-29 21:08:35 +00:00
|
|
|
end
|
|
|
|
end
|