2020-11-11 12:09:06 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe JiraConnect::SyncProjectWorker, factory_default: :keep do
|
2021-07-09 18:09:51 +00:00
|
|
|
include AfterNextHelpers
|
|
|
|
|
|
|
|
it_behaves_like 'worker with data consistency',
|
|
|
|
described_class,
|
|
|
|
data_consistency: :delayed
|
|
|
|
|
2020-11-11 12:09:06 +00:00
|
|
|
describe '#perform' do
|
2021-02-18 06:09:43 +00:00
|
|
|
let_it_be(:project) { create_default(:project).freeze }
|
2021-04-21 12:09:16 +00:00
|
|
|
|
2020-11-11 12:09:06 +00:00
|
|
|
let!(:mr_with_jira_title) { create(:merge_request, :unique_branches, title: 'TEST-123') }
|
|
|
|
let!(:mr_with_jira_description) { create(:merge_request, :unique_branches, description: 'TEST-323') }
|
|
|
|
let!(:mr_with_other_title) { create(:merge_request, :unique_branches) }
|
|
|
|
let!(:jira_subscription) { create(:jira_connect_subscription, namespace: project.namespace) }
|
|
|
|
|
|
|
|
let(:jira_connect_sync_service) { JiraConnect::SyncService.new(project) }
|
|
|
|
let(:job_args) { [project.id, update_sequence_id] }
|
|
|
|
let(:update_sequence_id) { 1 }
|
2021-07-09 18:09:51 +00:00
|
|
|
let(:request_path) { '/rest/devinfo/0.10/bulk' }
|
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
repositories: [
|
|
|
|
Atlassian::JiraConnect::Serializers::RepositoryEntity.represent(
|
|
|
|
project,
|
|
|
|
merge_requests: [mr_with_jira_description, mr_with_jira_title],
|
|
|
|
update_sequence_id: update_sequence_id
|
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(project_id, update_sequence_id)
|
|
|
|
described_class.new.perform(project_id, update_sequence_id)
|
|
|
|
end
|
2020-11-11 12:09:06 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:post, 'https://sample.atlassian.net/rest/devinfo/0.10/bulk').to_return(status: 200, body: '', headers: {})
|
|
|
|
|
|
|
|
jira_connect_sync_service
|
|
|
|
allow(JiraConnect::SyncService).to receive(:new) { jira_connect_sync_service }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the project is not found' do
|
|
|
|
it 'does not raise an error' do
|
2021-07-09 18:09:51 +00:00
|
|
|
expect { perform('non_existing_record_id', update_sequence_id) }.not_to raise_error
|
2020-11-11 12:09:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'avoids N+1 database queries' do
|
2021-07-09 18:09:51 +00:00
|
|
|
control_count = ActiveRecord::QueryRecorder.new { perform(project.id, update_sequence_id) }.count
|
2020-11-11 12:09:06 +00:00
|
|
|
|
|
|
|
create(:merge_request, :unique_branches, title: 'TEST-123')
|
|
|
|
|
2021-07-09 18:09:51 +00:00
|
|
|
expect { perform(project.id, update_sequence_id) }.not_to exceed_query_limit(control_count)
|
2020-11-11 12:09:06 +00:00
|
|
|
end
|
|
|
|
|
2021-07-09 18:09:51 +00:00
|
|
|
it 'sends the request with custom update_sequence_id' do
|
|
|
|
allow_next(Atlassian::JiraConnect::Client).to receive(:post)
|
|
|
|
.with(request_path, request_body)
|
2020-11-11 12:09:06 +00:00
|
|
|
|
2021-07-09 18:09:51 +00:00
|
|
|
perform(project.id, update_sequence_id)
|
|
|
|
end
|
2020-11-11 12:09:06 +00:00
|
|
|
|
2021-07-09 18:09:51 +00:00
|
|
|
context 'when the number of merge requests to sync is higher than the limit' do
|
|
|
|
let!(:most_recent_merge_request) { create(:merge_request, :unique_branches, description: 'TEST-323', title: 'TEST-123') }
|
2020-11-11 12:09:06 +00:00
|
|
|
|
2021-07-09 18:09:51 +00:00
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::MERGE_REQUEST_LIMIT", 1)
|
|
|
|
end
|
2020-11-11 12:09:06 +00:00
|
|
|
|
2021-07-09 18:09:51 +00:00
|
|
|
it 'syncs only the most recent merge requests within the limit' do
|
|
|
|
expect(jira_connect_sync_service).to receive(:execute)
|
|
|
|
.with(merge_requests: [most_recent_merge_request], update_sequence_id: update_sequence_id)
|
2020-11-11 12:09:06 +00:00
|
|
|
|
2021-07-09 18:09:51 +00:00
|
|
|
perform(project.id, update_sequence_id)
|
2020-11-11 12:09:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|