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) }
|
|
|
|
let_it_be(:project, reload: true) { create(:project) }
|
|
|
|
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',
|
|
|
|
jira_display_name: 'user1',
|
|
|
|
jira_email: 'sample@jira.com',
|
|
|
|
gitlab_id: nil,
|
|
|
|
gitlab_username: nil,
|
|
|
|
gitlab_name: nil
|
|
|
|
},
|
|
|
|
{
|
|
|
|
jira_account_id: 'acc2',
|
|
|
|
jira_display_name: 'user2',
|
|
|
|
jira_email: nil,
|
|
|
|
gitlab_id: nil,
|
|
|
|
gitlab_username: nil,
|
|
|
|
gitlab_name: nil
|
|
|
|
}
|
|
|
|
]
|
|
|
|
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
|
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
RSpec.shared_examples 'maps jira users to gitlab users' do
|
|
|
|
context 'when Jira import is configured correctly' do
|
|
|
|
let_it_be(:jira_service) { create(:jira_service, project: project, active: true) }
|
|
|
|
let(:client) { double }
|
2020-05-29 21:08:35 +00:00
|
|
|
|
|
|
|
before do
|
2020-08-18 15:10:33 +00:00
|
|
|
expect(importer).to receive(:client).at_least(1).and_return(client)
|
|
|
|
allow(client).to receive_message_chain(:ServerInfo, :all, :deploymentType).and_return(deployment_type)
|
2020-05-29 21:08:35 +00:00
|
|
|
end
|
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
context 'when jira client raises an error' do
|
|
|
|
it 'returns an error response' do
|
|
|
|
expect(client).to receive(:get).and_raise(Timeout::Error)
|
2020-05-29 21:08:35 +00:00
|
|
|
|
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
|
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
context 'when jira client returns result' do
|
|
|
|
context 'when jira client returns an empty array' do
|
|
|
|
let(:jira_users) { [] }
|
2020-05-29 21:08:35 +00:00
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
it 'retturns nil payload' do
|
|
|
|
expect(subject.success?).to be_truthy
|
|
|
|
expect(subject.payload).to be_empty
|
|
|
|
end
|
2020-05-29 21:08:35 +00:00
|
|
|
end
|
|
|
|
|
2020-08-18 15:10:33 +00:00
|
|
|
context 'when jira client returns an results' do
|
|
|
|
it 'returns the mapped users' do
|
|
|
|
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
|
|
|
|
let(:deployment_type) { 'Server' }
|
|
|
|
let(:url) { "/rest/api/2/user/search?username=''&maxResults=50&startAt=#{start_at}" }
|
|
|
|
let(:jira_users) do
|
|
|
|
[
|
|
|
|
{ 'key' => 'acc1', 'name' => 'user1', 'emailAddress' => 'sample@jira.com' },
|
|
|
|
{ 'key' => 'acc2', 'name' => 'user2' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(JiraImport::ServerUsersMapperService) do |instance|
|
|
|
|
allow(instance).to receive(:client).and_return(client)
|
|
|
|
allow(client).to receive(:get).with(url).and_return(jira_users)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'maps jira users to gitlab users'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when Jira instance is of Cloud deploymet type' do
|
|
|
|
let(:deployment_type) { 'Cloud' }
|
|
|
|
let(:url) { "/rest/api/2/users?maxResults=50&startAt=#{start_at}" }
|
|
|
|
let(:jira_users) do
|
|
|
|
[
|
|
|
|
{ 'accountId' => 'acc1', 'displayName' => 'user1', 'emailAddress' => 'sample@jira.com' },
|
|
|
|
{ 'accountId' => 'acc2', 'displayName' => 'user2' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(JiraImport::CloudUsersMapperService) do |instance|
|
|
|
|
allow(instance).to receive(:client).and_return(client)
|
|
|
|
allow(client).to receive(:get).with(url).and_return(jira_users)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'maps jira users to gitlab users'
|
|
|
|
end
|
2020-05-29 21:08:35 +00:00
|
|
|
end
|
|
|
|
end
|