2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-02 06:16:17 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::ProtectedBranches do
|
2017-08-02 06:16:17 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let!(:project) { create(:project, :repository) }
|
|
|
|
let(:protected_name) { 'feature' }
|
|
|
|
let(:branch_name) { protected_name }
|
|
|
|
let!(:protected_branch) do
|
|
|
|
create(:protected_branch, project: project, name: protected_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects/:id/protected_branches" do
|
2020-02-06 16:08:48 -05:00
|
|
|
let(:params) { {} }
|
2017-08-02 06:16:17 -04:00
|
|
|
let(:route) { "/projects/#{project.id}/protected_branches" }
|
|
|
|
|
|
|
|
shared_examples_for 'protected branches' do
|
|
|
|
it 'returns the protected branches' do
|
2020-02-06 16:08:48 -05:00
|
|
|
get api(route, user), params: params.merge(per_page: 100)
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
|
|
|
|
protected_branch_names = json_response.map { |x| x['name'] }
|
|
|
|
expect(protected_branch_names).to match_array(expected_branch_names)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when authenticated as a maintainer' do
|
2017-08-02 06:16:17 -04:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
context 'when search param is not present' do
|
|
|
|
it_behaves_like 'protected branches' do
|
|
|
|
let(:expected_branch_names) { project.protected_branches.map { |x| x['name'] } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when search param is present' do
|
|
|
|
it_behaves_like 'protected branches' do
|
|
|
|
let(:another_protected_branch) { create(:protected_branch, project: project, name: 'stable') }
|
|
|
|
let(:params) { { search: another_protected_branch.name } }
|
|
|
|
let(:expected_branch_names) { [another_protected_branch.name] }
|
|
|
|
end
|
|
|
|
end
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as a guest' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects/:id/protected_branches/:branch" do
|
|
|
|
let(:route) { "/projects/#{project.id}/protected_branches/#{branch_name}" }
|
|
|
|
|
|
|
|
shared_examples_for 'protected branch' do
|
|
|
|
it 'returns the protected branch' do
|
|
|
|
get api(route, user)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(::Gitlab::Access::MAINTAINER)
|
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(::Gitlab::Access::MAINTAINER)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when protected branch does not exist' do
|
|
|
|
let(:branch_name) { 'unknown' }
|
|
|
|
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route, user) }
|
|
|
|
let(:message) { '404 Not found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when authenticated as a maintainer' do
|
2017-08-02 06:16:17 -04:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'protected branch'
|
|
|
|
|
|
|
|
context 'when protected branch contains a wildcard' do
|
|
|
|
let(:protected_name) { 'feature*' }
|
|
|
|
|
|
|
|
it_behaves_like 'protected branch'
|
|
|
|
end
|
2018-01-19 15:07:44 -05:00
|
|
|
|
|
|
|
context 'when protected branch contains a period' do
|
|
|
|
let(:protected_name) { 'my.feature' }
|
|
|
|
|
|
|
|
it_behaves_like 'protected branch'
|
|
|
|
end
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as a guest' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:id/protected_branches' do
|
|
|
|
let(:branch_name) { 'new_branch' }
|
2017-11-22 12:08:47 -05:00
|
|
|
let(:post_endpoint) { api("/projects/#{project.id}/protected_branches", user) }
|
|
|
|
|
|
|
|
def expect_protection_to_be_successful
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-11-22 12:08:47 -05:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
|
|
|
end
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when authenticated as a maintainer' do
|
2017-08-02 06:16:17 -04:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'protects a single branch' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(Gitlab::Access::MAINTAINER)
|
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(Gitlab::Access::MAINTAINER)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'protects a single branch and developers can push' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name, push_access_level: 30 }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(Gitlab::Access::DEVELOPER)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(Gitlab::Access::MAINTAINER)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'protects a single branch and developers can merge' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name, merge_access_level: 30 }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(Gitlab::Access::MAINTAINER)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(Gitlab::Access::DEVELOPER)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'protects a single branch and developers can push and merge' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name, push_access_level: 30, merge_access_level: 30 }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(Gitlab::Access::DEVELOPER)
|
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(Gitlab::Access::DEVELOPER)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'protects a single branch and no one can push' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name, push_access_level: 0 }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(Gitlab::Access::NO_ACCESS)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(Gitlab::Access::MAINTAINER)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'protects a single branch and no one can merge' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name, merge_access_level: 0 }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(Gitlab::Access::MAINTAINER)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(Gitlab::Access::NO_ACCESS)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'protects a single branch and no one can push or merge' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name, push_access_level: 0, merge_access_level: 0 }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-08-02 06:16:17 -04:00
|
|
|
expect(json_response['name']).to eq(branch_name)
|
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(Gitlab::Access::NO_ACCESS)
|
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(Gitlab::Access::NO_ACCESS)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 409 error if the same branch is protected twice' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: protected_name }
|
2017-11-22 12:08:47 -05:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:conflict)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when branch has a wildcard in its name' do
|
|
|
|
let(:branch_name) { 'feature/*' }
|
|
|
|
|
|
|
|
it "protects multiple branches with a wildcard in the name" do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2017-11-22 12:08:47 -05:00
|
|
|
expect_protection_to_be_successful
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(json_response['push_access_levels'][0]['access_level']).to eq(Gitlab::Access::MAINTAINER)
|
|
|
|
expect(json_response['merge_access_levels'][0]['access_level']).to eq(Gitlab::Access::MAINTAINER)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-24 21:29:13 -04:00
|
|
|
|
|
|
|
context 'when a policy restricts rule deletion' do
|
|
|
|
before do
|
|
|
|
policy = instance_double(ProtectedBranchPolicy, can?: false)
|
|
|
|
expect(ProtectedBranchPolicy).to receive(:new).and_return(policy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "prevents deletion of the protected branch rule" do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name }
|
2018-03-24 21:29:13 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-03-24 21:29:13 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as a guest' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a 403 error if guest" do
|
2018-12-17 17:52:17 -05:00
|
|
|
post post_endpoint, params: { name: branch_name }
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "DELETE /projects/:id/protected_branches/unprotect/:branch" do
|
2018-03-24 21:29:13 -04:00
|
|
|
let(:delete_endpoint) { api("/projects/#{project.id}/protected_branches/#{branch_name}", user) }
|
|
|
|
|
2017-08-02 06:16:17 -04:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "unprotects a single branch" do
|
2018-03-24 21:29:13 -04:00
|
|
|
delete delete_endpoint
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
2017-08-24 12:03:39 -04:00
|
|
|
it_behaves_like '412 response' do
|
2018-03-24 21:29:13 -04:00
|
|
|
let(:request) { delete_endpoint }
|
2017-08-24 12:03:39 -04:00
|
|
|
end
|
|
|
|
|
2017-08-02 06:16:17 -04:00
|
|
|
it "returns 404 if branch does not exist" do
|
|
|
|
delete api("/projects/#{project.id}/protected_branches/barfoo", user)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
|
2018-03-24 21:29:13 -04:00
|
|
|
context 'when a policy restricts rule deletion' do
|
|
|
|
before do
|
|
|
|
policy = instance_double(ProtectedBranchPolicy, can?: false)
|
|
|
|
expect(ProtectedBranchPolicy).to receive(:new).and_return(policy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "prevents deletion of the protected branch rule" do
|
|
|
|
delete delete_endpoint
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-03-24 21:29:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-02 06:16:17 -04:00
|
|
|
context 'when branch has a wildcard in its name' do
|
|
|
|
let(:protected_name) { 'feature*' }
|
2017-08-15 13:44:37 -04:00
|
|
|
|
2017-08-02 06:16:17 -04:00
|
|
|
it "unprotects a wildcard branch" do
|
2018-03-24 21:29:13 -04:00
|
|
|
delete delete_endpoint
|
2017-08-02 06:16:17 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|