2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-18 20:44:34 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe ProtectedBranches::CreateService do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2022-01-20 00:15:02 +00:00
|
|
|
let(:user) { project.first_owner }
|
2016-09-18 20:44:34 +00:00
|
|
|
let(:params) do
|
|
|
|
{
|
2021-12-07 03:12:22 +00:00
|
|
|
name: name,
|
2018-07-11 14:36:08 +00:00
|
|
|
merge_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
|
|
|
|
push_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }]
|
2016-09-18 20:44:34 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2021-12-07 03:12:22 +00:00
|
|
|
let(:name) { 'master' }
|
|
|
|
|
2016-09-18 20:44:34 +00:00
|
|
|
subject(:service) { described_class.new(project, user, params) }
|
|
|
|
|
|
|
|
it 'creates a new protected branch' do
|
|
|
|
expect { service.execute }.to change(ProtectedBranch, :count).by(1)
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(project.protected_branches.last.push_access_levels.map(&:access_level)).to eq([Gitlab::Access::MAINTAINER])
|
|
|
|
expect(project.protected_branches.last.merge_access_levels.map(&:access_level)).to eq([Gitlab::Access::MAINTAINER])
|
2016-09-18 20:44:34 +00:00
|
|
|
end
|
2018-01-03 16:01:46 +00:00
|
|
|
|
2022-02-04 00:13:53 +00:00
|
|
|
context 'when protecting a branch with a name that contains HTML tags' do
|
|
|
|
let(:name) { 'foo<b>bar<\b>' }
|
2021-12-07 03:12:22 +00:00
|
|
|
|
2022-02-04 00:13:53 +00:00
|
|
|
subject(:service) { described_class.new(project, user, params) }
|
2021-12-07 03:12:22 +00:00
|
|
|
|
2022-02-04 00:13:53 +00:00
|
|
|
it 'creates a new protected branch' do
|
|
|
|
expect { service.execute }.to change(ProtectedBranch, :count).by(1)
|
|
|
|
expect(project.protected_branches.last.name).to eq(name)
|
2021-12-07 03:12:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-03 16:01:46 +00:00
|
|
|
context 'when user does not have permission' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new protected branch if we skip authorization step' do
|
|
|
|
expect { service.execute(skip_authorization: true) }.to change(ProtectedBranch, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises Gitlab::Access:AccessDeniedError' do
|
|
|
|
expect { service.execute }.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 00:54:56 +00:00
|
|
|
|
|
|
|
context 'when a policy restricts rule creation' do
|
|
|
|
before do
|
2021-06-16 18:10:35 +00:00
|
|
|
policy = instance_double(ProtectedBranchPolicy, allowed?: false)
|
2018-03-25 00:54:56 +00:00
|
|
|
expect(ProtectedBranchPolicy).to receive(:new).and_return(policy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "prevents creation of the protected branch rule" do
|
|
|
|
expect do
|
|
|
|
service.execute
|
|
|
|
end.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
end
|
|
|
|
end
|
2016-09-18 20:44:34 +00:00
|
|
|
end
|
|
|
|
end
|