2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe ProtectedBranches::CreateService do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project) }
|
2016-09-29 09:46:39 +05:30
|
|
|
let(:user) { project.owner }
|
|
|
|
let(:params) do
|
|
|
|
{
|
2021-12-07 22:27:20 +05:30
|
|
|
name: name,
|
2018-11-18 11:00:15 +05:30
|
|
|
merge_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
|
|
|
|
push_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }]
|
2016-09-29 09:46:39 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2021-12-07 22:27:20 +05:30
|
|
|
let(:name) { 'master' }
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
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-11-18 11:00:15 +05:30
|
|
|
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-29 09:46:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-12-07 22:27:20 +05:30
|
|
|
context 'when name has escaped HTML' do
|
|
|
|
let(:name) { 'feature->test' }
|
|
|
|
|
|
|
|
it 'creates the new protected branch matching the unescaped version' do
|
|
|
|
expect { service.execute }.to change(ProtectedBranch, :count).by(1)
|
|
|
|
expect(project.protected_branches.last.name).to eq('feature->test')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and name contains HTML tags' do
|
|
|
|
let(:name) { '<b>master</b>' }
|
|
|
|
|
|
|
|
it 'creates the new protected branch with sanitized name' do
|
|
|
|
expect { service.execute }.to change(ProtectedBranch, :count).by(1)
|
|
|
|
expect(project.protected_branches.last.name).to eq('master')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and contains unsafe HTML' do
|
|
|
|
let(:name) { '<script>alert('foo');</script>' }
|
|
|
|
|
|
|
|
it 'does not create the new protected branch' do
|
|
|
|
expect { service.execute }.not_to change(ProtectedBranch, :count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name contains unescaped HTML tags' do
|
|
|
|
let(:name) { '<b>master</b>' }
|
|
|
|
|
|
|
|
it 'creates the new protected branch with sanitized name' do
|
|
|
|
expect { service.execute }.to change(ProtectedBranch, :count).by(1)
|
|
|
|
expect(project.protected_branches.last.name).to eq('master')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
context 'when a policy restricts rule creation' do
|
|
|
|
before do
|
2021-09-04 01:27:46 +05:30
|
|
|
policy = instance_double(ProtectedBranchPolicy, allowed?: false)
|
2018-05-09 12:01:36 +05:30
|
|
|
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-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|