debian-mirror-gitlab/spec/services/merge_requests/base_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

145 lines
4.9 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-17 16:20:25 +05:30
module MergeRequests
class ExampleService < MergeRequests::BaseService
def execute(merge_request, async: false, allow_duplicate: false)
create_pipeline_for(merge_request, current_user, async: async, allow_duplicate: allow_duplicate)
end
end
end
RSpec.describe MergeRequests::BaseService, feature_category: :code_review_workflow do
2020-11-24 15:15:51 +05:30
include ProjectForksHelper
let_it_be(:project) { create(:project, :repository) }
2021-04-29 21:17:54 +05:30
2023-07-09 08:55:56 +05:30
let(:user) { project.first_owner }
2020-11-24 15:15:51 +05:30
let(:title) { 'Awesome merge_request' }
let(:params) do
{
title: title,
description: 'please fix',
source_branch: 'feature',
target_branch: 'master'
}
end
describe '#execute_hooks' do
2023-07-09 08:55:56 +05:30
subject { MergeRequests::CreateService.new(project: project, current_user: user, params: params).execute }
2020-11-24 15:15:51 +05:30
shared_examples 'enqueues Jira sync worker' do
2021-02-22 17:27:13 +05:30
specify :aggregate_failures do
expect(JiraConnect::SyncMergeRequestWorker).to receive(:perform_async).with(kind_of(Numeric), kind_of(Numeric)).and_call_original
2020-11-24 15:15:51 +05:30
Sidekiq::Testing.fake! do
2023-07-09 08:55:56 +05:30
expect { subject }.to change(JiraConnect::SyncMergeRequestWorker.jobs, :size).by(1)
2020-11-24 15:15:51 +05:30
end
end
end
shared_examples 'does not enqueue Jira sync worker' do
it do
Sidekiq::Testing.fake! do
2023-07-09 08:55:56 +05:30
expect { subject }.not_to change(JiraConnect::SyncMergeRequestWorker.jobs, :size)
2020-11-24 15:15:51 +05:30
end
end
end
context 'with a Jira subscription' do
before do
create(:jira_connect_subscription, namespace: project.namespace)
end
context 'MR contains Jira issue key' do
let(:title) { 'Awesome merge_request with issue JIRA-123' }
2023-07-09 08:55:56 +05:30
it_behaves_like 'does not enqueue Jira sync worker'
context 'for UpdateService' do
subject { MergeRequests::UpdateService.new(project: project, current_user: user, params: params).execute(merge_request) }
let(:merge_request) do
create(:merge_request, :simple, title: 'Old title',
assignee_ids: [user.id],
source_project: project,
author: user)
end
it_behaves_like 'enqueues Jira sync worker'
end
2020-11-24 15:15:51 +05:30
end
context 'MR does not contain Jira issue key' do
it_behaves_like 'does not enqueue Jira sync worker'
end
end
context 'without a Jira subscription' do
it_behaves_like 'does not enqueue Jira sync worker'
end
end
2023-03-17 16:20:25 +05:30
describe `#create_pipeline_for` do
let_it_be(:merge_request) { create(:merge_request) }
2023-07-09 08:55:56 +05:30
subject { MergeRequests::ExampleService.new(project: project, current_user: user, params: params) }
2023-03-17 16:20:25 +05:30
context 'async: false' do
it 'creates a pipeline directly' do
expect(MergeRequests::CreatePipelineService)
.to receive(:new)
2023-07-09 08:55:56 +05:30
.with(hash_including(project: project, current_user: user, params: { allow_duplicate: false }))
2023-03-17 16:20:25 +05:30
.and_call_original
expect(MergeRequests::CreatePipelineWorker).not_to receive(:perform_async)
subject.execute(merge_request, async: false)
end
context 'allow_duplicate: true' do
it 'passes :allow_duplicate as true' do
expect(MergeRequests::CreatePipelineService)
.to receive(:new)
2023-07-09 08:55:56 +05:30
.with(hash_including(project: project, current_user: user, params: { allow_duplicate: true }))
2023-03-17 16:20:25 +05:30
.and_call_original
expect(MergeRequests::CreatePipelineWorker).not_to receive(:perform_async)
subject.execute(merge_request, async: false, allow_duplicate: true)
end
end
end
context 'async: true' do
it 'enques a CreatePipelineWorker' do
expect(MergeRequests::CreatePipelineService).not_to receive(:new)
expect(MergeRequests::CreatePipelineWorker)
.to receive(:perform_async)
2023-07-09 08:55:56 +05:30
.with(project.id, user.id, merge_request.id, { "allow_duplicate" => false })
2023-03-17 16:20:25 +05:30
.and_call_original
Sidekiq::Testing.fake! do
expect { subject.execute(merge_request, async: true) }.to change(MergeRequests::CreatePipelineWorker.jobs, :size).by(1)
end
end
context 'allow_duplicate: true' do
it 'passes :allow_duplicate as true' do
expect(MergeRequests::CreatePipelineService).not_to receive(:new)
expect(MergeRequests::CreatePipelineWorker)
.to receive(:perform_async)
2023-07-09 08:55:56 +05:30
.with(project.id, user.id, merge_request.id, { "allow_duplicate" => true })
2023-03-17 16:20:25 +05:30
.and_call_original
Sidekiq::Testing.fake! do
expect { subject.execute(merge_request, async: true, allow_duplicate: true) }.to change(MergeRequests::CreatePipelineWorker.jobs, :size).by(1)
end
end
end
end
end
2023-05-27 22:25:52 +05:30
describe '#constructor_container_arg' do
it { expect(described_class.constructor_container_arg("some-value")).to eq({ project: "some-value" }) }
end
2020-11-24 15:15:51 +05:30
end