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

112 lines
3.3 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
2018-05-09 12:01:36 +05:30
let(:project) { create(:project, :repository) }
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) }
let(:user) { create(:user) }
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-05-09 12:01:36 +05:30
end
2015-04-26 12:48:37 +05:30
describe "GET #index" do
let(:project) { create(:project_empty_repo, :public) }
2017-08-17 22:00:37 +05:30
2016-09-13 17:45:13 +05:30
it "redirects empty repo to projects page" do
2019-02-15 15:39:39 +05:30
get(:index, params: { namespace_id: project.namespace.to_param, project_id: 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) }
before do
sign_in(user)
end
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
context 'when a policy restricts rule deletion' 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
allow(ProtectedBranchPolicy).to receive(:new).and_return(policy)
end
it "prevents creation of the protected branch rule" 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
expect(ProtectedBranch.count).to eq 0
end
end
end
describe "PUT #update" do
let(:update_params) { { name: 'new_name' } }
before do
sign_in(user)
end
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
context 'when a policy restricts rule deletion' 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
allow(ProtectedBranchPolicy).to receive(:new).and_return(policy)
end
it "prevents update of the protected branch rule" do
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
before do
sign_in(user)
end
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
context 'when a policy restricts rule deletion' 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
allow(ProtectedBranchPolicy).to receive(:new).and_return(policy)
end
it "prevents deletion of 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
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
2015-04-26 12:48:37 +05:30
end