1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
# frozen_string_literal: true
# rubocop: disable Gitlab/ServiceResponse
require 'spec_helper'
RSpec.describe Projects::DeleteBranchWorker, feature_category: :source_code_management do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let(:branch) { 'master' }
let(:worker) { described_class.new }
let(:service_result) { ServiceResponse.success(message: 'placeholder', http_status: 200) }
before do
allow_next_instance_of(::Branches::DeleteService) do |instance|
allow(instance).to receive(:execute).with(branch).and_return(service_result)
end
end
describe '#perform' do
context 'when the branch does not exist' do
let(:branch) { 'non_existent_branch_name' }
it 'does nothing' do
expect(::Branches::DeleteService).not_to receive(:new)
worker.perform(project.id, user.id, branch)
end
end
context 'with a non-existing project' do
it 'does nothing' do
expect(::Branches::DeleteService).not_to receive(:new)
worker.perform(non_existing_record_id, user.id, branch)
end
end
context 'with a non-existing user' do
it 'does nothing' do
expect(::Branches::DeleteService).not_to receive(:new)
worker.perform(project.id, non_existing_record_id, branch)
end
end
context 'with existing user and project' do
it 'calls service to delete source branch' do
expect_next_instance_of(::Branches::DeleteService) do |instance|
expect(instance).to receive(:execute).with(branch).and_return(service_result)
end
worker.perform(project.id, user.id, branch)
end
context 'when delete service returns an error' do
let(:service_result) { ServiceResponse.error(message: 'placeholder', http_status: status_code) }
context 'when the status code is 400' do
let(:status_code) { 400 }
it 'tracks and raises the exception' do
expect_next_instance_of(::Branches::DeleteService) do |instance|
expect(instance).to receive(:execute).with(branch).and_return(service_result)
end
expect(service_result).to receive(:log_and_raise_exception).and_call_original
expect do
worker.perform(project.id, user.id, branch)
end.to raise_error(Projects::DeleteBranchWorker::GitReferenceLockedError)
end
end
context 'when the status code is not 400' do
let(:status_code) { 405 }
it 'does not track the exception' do
expect_next_instance_of(::Branches::DeleteService) do |instance|
expect(instance).to receive(:execute).with(branch).and_return(service_result)
end
expect(service_result).not_to receive(:log_and_raise_exception)
expect { worker.perform(project.id, user.id, branch) }.not_to raise_error
end
end
end
end
it_behaves_like 'an idempotent worker' do
let(:job_args) { [project.id, user.id, branch] }
end
end
# rubocop: enable Gitlab/ServiceResponse
end
|