2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe JiraConnectSubscriptions::CreateService, feature_category: :integrations do
|
2022-07-23 23:45:48 +05:30
|
|
|
let_it_be(:installation) { create(:jira_connect_installation) }
|
|
|
|
let_it_be(:current_user) { create(:user) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:path) { group.full_path }
|
2021-09-04 01:27:46 +05:30
|
|
|
let(:params) { { namespace_path: path, jira_user: jira_user } }
|
|
|
|
let(:jira_user) { double(:JiraUser, site_admin?: true) }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
subject { described_class.new(installation, current_user, params).execute }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_maintainer(current_user)
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
shared_examples 'a failed execution' do |**status_attributes|
|
2020-11-24 15:15:51 +05:30
|
|
|
it 'does not create a subscription' do
|
|
|
|
expect { subject }.not_to change { installation.subscriptions.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error status' do
|
|
|
|
expect(subject[:status]).to eq(:error)
|
2022-07-23 23:45:48 +05:30
|
|
|
expect(subject).to include(status_attributes)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
context 'remote user does not have access' do
|
|
|
|
let(:jira_user) { double(site_admin?: false) }
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it_behaves_like 'a failed execution',
|
|
|
|
http_status: 403,
|
|
|
|
message: 'The Jira user is not a site administrator. Check the permissions in Jira and try again.'
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'remote user cannot be retrieved' do
|
|
|
|
let(:jira_user) { nil }
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it_behaves_like 'a failed execution',
|
|
|
|
http_status: 403,
|
|
|
|
message: 'Could not fetch user information from Jira. Check the permissions in Jira and try again.'
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'when user does have access' do
|
|
|
|
it 'creates a subscription' do
|
|
|
|
expect { subject }.to change { installation.subscriptions.count }.from(0).to(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success' do
|
|
|
|
expect(subject[:status]).to eq(:success)
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
context 'namespace has projects' do
|
2022-07-23 23:45:48 +05:30
|
|
|
let_it_be(:project_1) { create(:project, group: group) }
|
|
|
|
let_it_be(:project_2) { create(:project, group: group) }
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::MERGE_REQUEST_SYNC_BATCH_SIZE", 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'starts workers to sync projects in batches with delay' do
|
|
|
|
allow(Atlassian::JiraConnect::Client).to receive(:generate_update_sequence_id).and_return(123)
|
|
|
|
|
|
|
|
expect(JiraConnect::SyncProjectWorker).to receive(:bulk_perform_in).with(1.minute, [[project_1.id, 123]])
|
|
|
|
expect(JiraConnect::SyncProjectWorker).to receive(:bulk_perform_in).with(2.minutes, [[project_2.id, 123]])
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when path is invalid' do
|
|
|
|
let(:path) { 'some_invalid_namespace_path' }
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it_behaves_like 'a failed execution',
|
|
|
|
http_status: 401,
|
|
|
|
message: 'Cannot find namespace. Make sure you have sufficient permissions.'
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have access' do
|
2022-07-23 23:45:48 +05:30
|
|
|
let_it_be(:other_group) { create(:group) }
|
|
|
|
|
|
|
|
let(:path) { other_group.full_path }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it_behaves_like 'a failed execution',
|
|
|
|
http_status: 401,
|
|
|
|
message: 'Cannot find namespace. Make sure you have sufficient permissions.'
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|