diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-12-20 17:22:11 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-12-20 17:22:11 +0300 |
commit | 0c872e02b2c822e3397515ec324051ff540f0cd5 (patch) | |
tree | ce2fb6ce7030e4dad0f4118d21ab6453e5938cdd /spec/services/protected_branches/update_service_spec.rb | |
parent | f7e05a6853b12f02911494c4b3fe53d9540d74fc (diff) |
Add latest changes from gitlab-org/gitlab@15-7-stable-eev15.7.0-rc42
Diffstat (limited to 'spec/services/protected_branches/update_service_spec.rb')
-rw-r--r-- | spec/services/protected_branches/update_service_spec.rb | 74 |
1 files changed, 42 insertions, 32 deletions
diff --git a/spec/services/protected_branches/update_service_spec.rb b/spec/services/protected_branches/update_service_spec.rb index 2ff6c3c489a..c70cc032a6a 100644 --- a/spec/services/protected_branches/update_service_spec.rb +++ b/spec/services/protected_branches/update_service_spec.rb @@ -3,54 +3,64 @@ require 'spec_helper' RSpec.describe ProtectedBranches::UpdateService do - let_it_be_with_reload(:project) { create(:project) } + shared_examples 'execute with entity' do + let(:params) { { name: new_name } } - let!(:protected_branch) { create(:protected_branch, project: project) } - let(:user) { project.first_owner } - let(:params) { { name: new_name } } + subject(:service) { described_class.new(entity, user, params) } - subject(:service) { described_class.new(project, user, params) } + describe '#execute' do + let(:new_name) { 'new protected branch name' } + let(:result) { service.execute(protected_branch) } - describe '#execute' do - let(:new_name) { 'new protected branch name' } - let(:result) { service.execute(protected_branch) } + it 'updates a protected branch' do + expect(result.reload.name).to eq(params[:name]) + end - it 'updates a protected branch' do - expect(result.reload.name).to eq(params[:name]) - end + it 'refreshes the cache' do + expect_next_instance_of(ProtectedBranches::CacheService) do |cache_service| + expect(cache_service).to receive(:refresh) + end - it 'refreshes the cache' do - expect_next_instance_of(ProtectedBranches::CacheService) do |cache_service| - expect(cache_service).to receive(:refresh) + result end - result - end - - context 'when updating name of a protected branch to one that contains HTML tags' do - let(:new_name) { 'foo<b>bar<\b>' } - let(:result) { service.execute(protected_branch) } + context 'when updating name of a protected branch to one that contains HTML tags' do + let(:new_name) { 'foo<b>bar<\b>' } + let(:result) { service.execute(protected_branch) } - it 'updates a protected branch' do - expect(result.reload.name).to eq(new_name) + it 'updates a protected branch' do + expect(result.reload.name).to eq(new_name) + end end - end - context 'without admin_project permissions' do - let(:user) { create(:user) } + context 'when a policy restricts rule update' do + it "prevents update of the protected branch rule" do + disallow(:update_protected_branch, protected_branch) - it "raises error" do - expect { service.execute(protected_branch) }.to raise_error(Gitlab::Access::AccessDeniedError) + expect { service.execute(protected_branch) }.to raise_error(Gitlab::Access::AccessDeniedError) + end end end + end - context 'when a policy restricts rule update' do - it "prevents update of the protected branch rule" do - disallow(:update_protected_branch, protected_branch) + context 'with entity project' do + let_it_be_with_reload(:entity) { create(:project) } + let!(:protected_branch) { create(:protected_branch, project: entity) } + let(:user) { entity.first_owner } - expect { service.execute(protected_branch) }.to raise_error(Gitlab::Access::AccessDeniedError) - end + it_behaves_like 'execute with entity' + end + + context 'with entity group' do + let_it_be_with_reload(:entity) { create(:group) } + let_it_be_with_reload(:user) { create(:user) } + let!(:protected_branch) { create(:protected_branch, group: entity, project: nil) } + + before do + allow(Ability).to receive(:allowed?).with(user, :update_protected_branch, protected_branch).and_return(true) end + + it_behaves_like 'execute with entity' end def disallow(ability, protected_branch) |