diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /spec/services/jira_import | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'spec/services/jira_import')
-rw-r--r-- | spec/services/jira_import/start_import_service_spec.rb | 26 | ||||
-rw-r--r-- | spec/services/jira_import/users_importer_spec.rb | 77 | ||||
-rw-r--r-- | spec/services/jira_import/users_mapper_spec.rb | 43 |
3 files changed, 141 insertions, 5 deletions
diff --git a/spec/services/jira_import/start_import_service_spec.rb b/spec/services/jira_import/start_import_service_spec.rb index 759e4f3363f..9dc8cdb1475 100644 --- a/spec/services/jira_import/start_import_service_spec.rb +++ b/spec/services/jira_import/start_import_service_spec.rb @@ -13,7 +13,7 @@ describe JiraImport::StartImportService do context 'when an error is returned from the project validation' do before do - allow(project).to receive(:validate_jira_import_settings!) + allow(Gitlab::JiraImport).to receive(:validate_project_settings!) .and_raise(Projects::ImportService::Error, 'Jira import feature is disabled.') end @@ -25,7 +25,7 @@ describe JiraImport::StartImportService do before do stub_jira_service_test - allow(project).to receive(:validate_jira_import_settings!) + allow(Gitlab::JiraImport).to receive(:validate_project_settings!) end context 'when Jira project key is not provided' do @@ -45,6 +45,22 @@ describe JiraImport::StartImportService do 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 it 'returns success response' do expect(subject).to be_a(ServiceResponse) @@ -57,7 +73,7 @@ describe JiraImport::StartImportService do expect(project.latest_jira_import).to be_scheduled end - it 'creates Jira import data' do + it 'creates Jira import data', :aggregate_failures do jira_import = subject.payload[:import_data] expect(jira_import.jira_project_xid).to eq(0) @@ -72,8 +88,8 @@ describe JiraImport::StartImportService do 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 @@ -83,8 +99,8 @@ describe JiraImport::StartImportService do 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 diff --git a/spec/services/jira_import/users_importer_spec.rb b/spec/services/jira_import/users_importer_spec.rb new file mode 100644 index 00000000000..28ce5f1b44b --- /dev/null +++ b/spec/services/jira_import/users_importer_spec.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe JiraImport::UsersImporter do + 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 + before do + stub_jira_service_test + project.add_maintainer(user) + end + + context 'when Jira import is not configured properly' do + it 'returns an error' do + expect(subject.errors).to eq(['Jira integration not configured.']) + end + end + + context 'when Jira import is configured correctly' do + let_it_be(:jira_service) { create(:jira_service, project: project, active: true) } + let(:client) { double } + + before do + expect(importer).to receive(:client).and_return(client) + end + + context 'when jira client raises an error' do + it 'returns an error response' do + expect(client).to receive(:get).and_raise(Timeout::Error) + + expect(subject.error?).to be_truthy + expect(subject.message).to include('There was an error when communicating to Jira') + end + end + + context 'when jira client returns result' do + before do + allow(client).to receive(:get).with('/rest/api/2/users?maxResults=50&startAt=7') + .and_return(jira_users) + end + + context 'when jira client returns an empty array' do + let(:jira_users) { [] } + + it 'retturns nil payload' do + expect(subject.success?).to be_truthy + expect(subject.payload).to be_nil + end + end + + context 'when jira client returns an results' do + let(:jira_users) { [{ 'name' => 'user1' }, { 'name' => 'user2' }] } + let(:mapped_users) { [{ jira_display_name: 'user1', gitlab_id: 5 }] } + + before do + expect(JiraImport::UsersMapper).to receive(:new).with(project, jira_users) + .and_return(double(execute: mapped_users)) + end + + it 'returns the mapped users' do + expect(subject.success?).to be_truthy + expect(subject.payload).to eq(mapped_users) + end + end + end + end + end +end diff --git a/spec/services/jira_import/users_mapper_spec.rb b/spec/services/jira_import/users_mapper_spec.rb new file mode 100644 index 00000000000..75dbc41aa2e --- /dev/null +++ b/spec/services/jira_import/users_mapper_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe JiraImport::UsersMapper do + let_it_be(:project) { create(:project) } + + subject { described_class.new(project, jira_users).execute } + + describe '#execute' do + context 'jira_users is nil' do + let(:jira_users) { nil } + + it 'returns an empty array' do + expect(subject).to be_empty + end + end + + context 'when jira_users is present' do + let(:jira_users) do + [ + { 'accountId' => 'abcd', 'displayName' => 'user1' }, + { 'accountId' => 'efg' }, + { 'accountId' => 'hij', 'displayName' => 'user3', 'emailAddress' => 'user3@example.com' } + ] + end + + # TODO: now we only create an array in a proper format + # mapping is tracked in https://gitlab.com/gitlab-org/gitlab/-/issues/219023 + let(:mapped_users) do + [ + { jira_account_id: 'abcd', jira_display_name: 'user1', jira_email: nil, gitlab_id: nil }, + { jira_account_id: 'efg', jira_display_name: nil, jira_email: nil, gitlab_id: nil }, + { jira_account_id: 'hij', jira_display_name: 'user3', jira_email: 'user3@example.com', gitlab_id: nil } + ] + end + + it 'returns users mapped to Gitlab' do + expect(subject).to eq(mapped_users) + end + end + end +end |