2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-15 04:27:40 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Branches::DeleteService do
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-11-15 04:32:37 -05:00
|
|
|
let(:repository) { project.repository }
|
2016-11-15 04:27:40 -05:00
|
|
|
let(:user) { create(:user) }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2019-12-03 13:06:49 -05:00
|
|
|
subject(:service) { described_class.new(project, user) }
|
2016-11-15 04:27:40 -05:00
|
|
|
|
2019-04-19 11:51:49 -04:00
|
|
|
shared_examples 'a deleted branch' do |branch_name|
|
2020-07-14 08:09:14 -04:00
|
|
|
before do
|
|
|
|
allow(Ci::RefDeleteUnlockArtifactsWorker).to receive(:perform_async)
|
|
|
|
end
|
|
|
|
|
2019-04-19 11:51:49 -04:00
|
|
|
it 'removes the branch' do
|
|
|
|
expect(branch_exists?(branch_name)).to be true
|
|
|
|
|
|
|
|
result = service.execute(branch_name)
|
|
|
|
|
|
|
|
expect(result.status).to eq :success
|
|
|
|
expect(branch_exists?(branch_name)).to be false
|
|
|
|
end
|
2020-07-14 08:09:14 -04:00
|
|
|
|
|
|
|
it 'calls the RefDeleteUnlockArtifactsWorker' do
|
|
|
|
expect(Ci::RefDeleteUnlockArtifactsWorker).to receive(:perform_async).with(project.id, user.id, "refs/heads/#{branch_name}")
|
|
|
|
|
|
|
|
service.execute(branch_name)
|
|
|
|
end
|
2019-04-19 11:51:49 -04:00
|
|
|
end
|
|
|
|
|
2016-11-15 04:27:40 -05:00
|
|
|
describe '#execute' do
|
|
|
|
context 'when user has access to push to repository' do
|
|
|
|
before do
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_developer(user)
|
2016-11-15 04:27:40 -05:00
|
|
|
end
|
|
|
|
|
2019-04-19 11:51:49 -04:00
|
|
|
it_behaves_like 'a deleted branch', 'feature'
|
2020-09-02 11:10:54 -04:00
|
|
|
|
|
|
|
context 'when Gitlab::Git::CommandError is raised' do
|
|
|
|
before do
|
|
|
|
allow(repository).to receive(:rm_branch) do
|
2021-05-04 11:10:36 -04:00
|
|
|
raise Gitlab::Git::CommandError, 'Could not update patch'
|
2020-09-02 11:10:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles and returns error' do
|
|
|
|
result = service.execute('feature')
|
|
|
|
|
|
|
|
expect(result.status).to eq(:error)
|
|
|
|
expect(result.message).to eq('Could not update patch')
|
|
|
|
end
|
|
|
|
end
|
2016-11-15 04:27:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have access to push to repository' do
|
|
|
|
it 'does not remove branch' do
|
2016-11-15 04:32:37 -05:00
|
|
|
expect(branch_exists?('feature')).to be true
|
|
|
|
|
|
|
|
result = service.execute('feature')
|
|
|
|
|
2019-04-19 11:39:12 -04:00
|
|
|
expect(result.status).to eq :error
|
|
|
|
expect(result.message).to eq 'You dont have push access to repo'
|
2016-11-15 04:32:37 -05:00
|
|
|
expect(branch_exists?('feature')).to be true
|
2016-11-15 04:27:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-15 04:32:37 -05:00
|
|
|
|
|
|
|
def branch_exists?(branch_name)
|
|
|
|
repository.ref_exists?("refs/heads/#{branch_name}")
|
|
|
|
end
|
2016-11-15 04:27:40 -05:00
|
|
|
end
|