debian-mirror-gitlab/spec/controllers/projects/protected_branches_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

132 lines
4.2 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require('spec_helper')
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::ProtectedBranchesController do
2022-08-27 11:52:29 +05:30
let_it_be_with_reload(:project) { create(:project, :repository) }
2022-11-25 23:54:43 +05:30
let_it_be_with_reload(:empty_project) { create(:project, :empty_repo) }
let_it_be(:maintainer) { create(:user, maintainer_projects: [project, empty_project]) }
2022-08-27 11:52:29 +05:30
2018-05-09 12:01:36 +05:30
let(:protected_branch) { create(:protected_branch, project: project) }
let(:project_params) { { namespace_id: project.namespace.to_param, project_id: project } }
let(:base_params) { project_params.merge(id: protected_branch.id) }
2022-08-27 11:52:29 +05:30
let(:user) { maintainer }
2018-05-09 12:01:36 +05:30
before do
2022-08-27 11:52:29 +05:30
sign_in(user)
2018-05-09 12:01:36 +05:30
end
2015-04-26 12:48:37 +05:30
describe "GET #index" do
2022-11-25 23:54:43 +05:30
it 'redirects to repository settings' do
get(:index, params: { namespace_id: empty_project.namespace.to_param, project_id: empty_project })
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
expect(response).to redirect_to(project_settings_repository_path(empty_project))
2015-04-26 12:48:37 +05:30
end
end
2018-05-09 12:01:36 +05:30
describe "POST #create" do
2018-11-18 11:00:15 +05:30
let(:maintainer_access_level) { [{ access_level: Gitlab::Access::MAINTAINER }] }
2018-05-09 12:01:36 +05:30
let(:access_level_params) do
2018-11-18 11:00:15 +05:30
{ merge_access_levels_attributes: maintainer_access_level,
push_access_levels_attributes: maintainer_access_level }
2018-05-09 12:01:36 +05:30
end
2020-10-24 23:57:45 +05:30
2018-05-09 12:01:36 +05:30
let(:create_params) { attributes_for(:protected_branch).merge(access_level_params) }
it 'creates the protected branch rule' do
expect do
2019-02-15 15:39:39 +05:30
post(:create, params: project_params.merge(protected_branch: create_params))
2018-05-09 12:01:36 +05:30
end.to change(ProtectedBranch, :count).by(1)
end
2022-11-25 23:54:43 +05:30
context 'when repository is empty' do
let(:project) { empty_project }
it 'creates the protected branch rule' do
expect do
post(:create, params: project_params.merge(protected_branch: create_params))
end.to change(ProtectedBranch, :count).by(1)
expect(response).to have_gitlab_http_status(:found)
end
end
2022-08-27 11:52:29 +05:30
context 'when a policy restricts rule creation' do
2018-05-09 12:01:36 +05:30
it "prevents creation of the protected branch rule" do
2022-08-27 11:52:29 +05:30
disallow(:create_protected_branch, an_instance_of(ProtectedBranch))
2019-02-15 15:39:39 +05:30
post(:create, params: project_params.merge(protected_branch: create_params))
2018-05-09 12:01:36 +05:30
expect(ProtectedBranch.count).to eq 0
end
end
end
describe "PUT #update" do
let(:update_params) { { name: 'new_name' } }
it 'updates the protected branch rule' do
2019-02-15 15:39:39 +05:30
put(:update, params: base_params.merge(protected_branch: update_params))
2018-05-09 12:01:36 +05:30
expect(protected_branch.reload.name).to eq('new_name')
expect(json_response["name"]).to eq('new_name')
end
2022-11-25 23:54:43 +05:30
context 'when repository is empty' do
let(:project) { empty_project }
it 'updates the protected branch rule' do
put(:update, params: base_params.merge(protected_branch: update_params))
expect(protected_branch.reload.name).to eq('new_name')
expect(json_response["name"]).to eq('new_name')
end
end
2022-08-27 11:52:29 +05:30
context 'when a policy restricts rule update' do
2018-05-09 12:01:36 +05:30
it "prevents update of the protected branch rule" do
2022-08-27 11:52:29 +05:30
disallow(:update_protected_branch, protected_branch)
2018-05-09 12:01:36 +05:30
old_name = protected_branch.name
2019-02-15 15:39:39 +05:30
put(:update, params: base_params.merge(protected_branch: update_params))
2018-05-09 12:01:36 +05:30
expect(protected_branch.reload.name).to eq(old_name)
end
end
end
describe "DELETE #destroy" do
it "deletes the protected branch rule" do
2019-02-15 15:39:39 +05:30
delete(:destroy, params: base_params)
2018-05-09 12:01:36 +05:30
expect { ProtectedBranch.find(protected_branch.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
2022-11-25 23:54:43 +05:30
context 'when repository is empty' do
let(:project) { empty_project }
it 'deletes the protected branch rule' do
delete(:destroy, params: base_params)
expect { ProtectedBranch.find(protected_branch.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
2018-05-09 12:01:36 +05:30
context 'when a policy restricts rule deletion' do
it "prevents deletion of the protected branch rule" do
2022-08-27 11:52:29 +05:30
disallow(:destroy_protected_branch, protected_branch)
2019-02-15 15:39:39 +05:30
delete(:destroy, params: base_params)
2018-05-09 12:01:36 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2018-05-09 12:01:36 +05:30
end
end
end
2022-08-27 11:52:29 +05:30
def disallow(ability, protected_branch)
allow(Ability).to receive(:allowed?).and_call_original
allow(Ability).to receive(:allowed?).with(user, ability, protected_branch).and_return(false)
end
2015-04-26 12:48:37 +05:30
end