Commit ed5048c9 authored by Jarka Košanová's avatar Jarka Košanová

Move jira settings validation to JiraImport

- it is general validation not only project-related
parent 0dffd967
......@@ -902,17 +902,6 @@ class Project < ApplicationRecord
latest_jira_import&.status || 'initial'
end
def validate_jira_import_settings!(user: nil)
raise Projects::ImportService::Error, _('Jira integration not configured.') unless jira_service&.active?
if user
raise Projects::ImportService::Error, _('Cannot import because issues are not available in this project.') unless feature_available?(:issues, user)
raise Projects::ImportService::Error, _('You do not have permissions to run the import.') unless user.can?(:admin_project, self)
end
raise Projects::ImportService::Error, _('Unable to connect to the Jira instance. Please check your Jira integration configuration.') unless jira_service.test(nil)[:success]
end
def human_import_status_name
import_state&.human_status_name || 'none'
end
......
......@@ -62,7 +62,7 @@ module JiraImport
end
def validate
project.validate_jira_import_settings!(user: user)
Gitlab::JiraImport.validate_project_settings!(project, user: user)
return build_error_response(_('Unable to find Jira project to import data from.')) if jira_project_key.blank?
return build_error_response(_('Jira import is already running.')) if import_in_progress?
......
......@@ -13,7 +13,7 @@ module JiraImport
end
def execute
project.validate_jira_import_settings!(user: user)
Gitlab::JiraImport.validate_project_settings!(project, user: user)
return ServiceResponse.success(payload: nil) if users.blank?
......
......@@ -10,6 +10,18 @@ module Gitlab
ITEMS_MAPPER_CACHE_KEY = 'jira-import/items-mapper/%{project_id}/%{collection_type}/%{jira_isssue_id}'
ALREADY_IMPORTED_ITEMS_CACHE_KEY = 'jira-importer/already-imported/%{project}/%{collection_type}'
def self.validate_project_settings!(project, user: nil)
if user
raise Projects::ImportService::Error, _('Cannot import because issues are not available in this project.') unless project.feature_available?(:issues, user)
raise Projects::ImportService::Error, _('You do not have permissions to run the import.') unless user.can?(:admin_project, project)
end
jira_service = project.jira_service
raise Projects::ImportService::Error, _('Jira integration not configured.') unless jira_service&.active?
raise Projects::ImportService::Error, _('Unable to connect to the Jira instance. Please check your Jira integration configuration.') unless jira_service.test(nil)[:success]
end
def self.jira_issue_cache_key(project_id, jira_issue_id)
ITEMS_MAPPER_CACHE_KEY % { project_id: project_id, collection_type: :issues, jira_isssue_id: jira_issue_id }
end
......
......@@ -6,7 +6,7 @@ module Gitlab
attr_reader :project, :client, :formatter, :jira_project_key, :running_import
def initialize(project)
project.validate_jira_import_settings!
Gitlab::JiraImport.validate_project_settings!(project)
@running_import = project.latest_jira_import
@jira_project_key = running_import&.jira_project_key
......
......@@ -14,7 +14,7 @@ describe Gitlab::JiraImport::BaseImporter 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 service exists' do
......
......@@ -5,6 +5,95 @@ require 'spec_helper'
describe Gitlab::JiraImport do
let(:project_id) { 321 }
describe '.validate_project_settings!' do
include JiraServiceHelper
let_it_be(:project, reload: true) { create(:project) }
shared_examples 'raise Jira import error' do |message|
it 'returns error' do
expect { subject }.to raise_error(Projects::ImportService::Error, message)
end
end
shared_examples 'jira configuration base checks' do
context 'when Jira service was not setup' do
it_behaves_like 'raise Jira import error', 'Jira integration not configured.'
end
context 'when Jira service exists' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
context 'when Jira connection is not valid' do
before do
WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/serverInfo')
.to_raise(JIRA::HTTPError.new(double(message: 'Some failure.')))
end
it_behaves_like 'raise Jira import error', 'Unable to connect to the Jira instance. Please check your Jira integration configuration.'
end
end
end
before do
stub_jira_service_test
end
context 'without user param' do
subject { described_class.validate_project_settings!(project) }
it_behaves_like 'jira configuration base checks'
context 'when jira connection is valid' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
it 'does not return any error' do
expect { subject }.not_to raise_error
end
end
end
context 'with user param provided' do
let_it_be(:user) { create(:user) }
subject { described_class.validate_project_settings!(project, user: user) }
context 'when user has permission to run import' do
before do
project.add_maintainer(user)
end
it_behaves_like 'jira configuration base checks'
context 'when jira service is configured' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
context 'when issues feature is disabled' do
let_it_be(:project, reload: true) { create(:project, :issues_disabled) }
it_behaves_like 'raise Jira import error', 'Cannot import because issues are not available in this project.'
end
context 'when everything is ok' do
it 'does not return any error' do
expect { subject }.not_to raise_error
end
end
end
end
context 'when user does not have permissions to run the import' do
before do
create(:jira_service, project: project, active: true)
project.add_developer(user)
end
it_behaves_like 'raise Jira import error', 'You do not have permissions to run the import.'
end
end
end
describe '.jira_issue_cache_key' do
it 'returns cache key for Jira issue imported to given project' do
expect(described_class.jira_issue_cache_key(project_id, 'DEMO-123')).to eq("jira-import/items-mapper/#{project_id}/issues/DEMO-123")
......
......@@ -5999,99 +5999,6 @@ describe Project do
end
end
describe '#validate_jira_import_settings!' do
include JiraServiceHelper
let_it_be(:project, reload: true) { create(:project) }
shared_examples 'raise Jira import error' do |message|
it 'returns error' do
expect { subject }.to raise_error(Projects::ImportService::Error, message)
end
end
shared_examples 'jira configuration base checks' do
context 'when Jira service was not setup' do
it_behaves_like 'raise Jira import error', 'Jira integration not configured.'
end
context 'when Jira service exists' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
context 'when Jira connection is not valid' do
before do
WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/serverInfo')
.to_raise(JIRA::HTTPError.new(double(message: 'Some failure.')))
end
it_behaves_like 'raise Jira import error', 'Unable to connect to the Jira instance. Please check your Jira integration configuration.'
end
end
end
before do
stub_jira_service_test
end
context 'without user param' do
subject { project.validate_jira_import_settings! }
it_behaves_like 'jira configuration base checks'
context 'when jira connection is valid' do
let!(:jira_service) { create(:jira_service, project: project, active: true) }
it 'does not return any error' do
expect { subject }.not_to raise_error
end
end
end
context 'with user param provided' do
let_it_be(:user) { create(:user) }
subject { project.validate_jira_import_settings!(user: user) }
context 'when user has permission to run import' do
before do
project.add_maintainer(user)
end
it_behaves_like 'jira configuration base checks'
end
context 'when user does not have permissions to run the import' do
before do
create(:jira_service, project: project, active: true)
project.add_developer(user)
end
it_behaves_like 'raise Jira import error', 'You do not have permissions to run the import.'
end
context 'when user has permission to run import' do
before do
project.add_maintainer(user)
end
let!(:jira_service) { create(:jira_service, project: project, active: true) }
context 'when issues feature is disabled' do
let_it_be(:project, reload: true) { create(:project, :issues_disabled) }
it_behaves_like 'raise Jira import error', 'Cannot import because issues are not available in this project.'
end
context 'when everything is ok' do
it 'does not return any error' do
expect { subject }.not_to raise_error
end
end
end
end
end
describe '#design_management_enabled?' do
let(:project) { build(:project) }
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment